/* Tool to sort attribute sets. Primarily useful for keeping all-packages.nix tidy. To compile: $ strc -i ../../maintainers/scripts/sort-attrs.str -la stratego-lib Typical invocation: $ sglr -m -p ~/Dev/nix/src/libexpr/nix.tbl -i all-packages.nix \ | implode-asfix --lex \ | ../../maintainers/scripts/sort-attrs \ | asfix-yield */ module sort-attrs imports libstratego-lib libstratego-sglr strategies no-wsp = !appl(prod([], cf(opt(layout())), no-attrs()), []) rules list-sep(s): [] -> [] list-sep(s): [x | xs] -> [[x | before] | [split | after]] where xs => (before, split, after) list-sep(s): [x | xs] -> [[x | xs]] where xs sort-attrs: appl(p@prod(_, _, attrs([term(cons("Attrs"))])), [ lit("{") , ws1 , appl(p2@list(cf(iter-star(sort("Bind")))), attrs) , ws2 , lit("}") ] ) -> appl(p, [lit("{"), , appl(p2, attrs'), ws2, lit("}")]) where "found it"; [ws1 | attrs] => withWSP; withWSP => groups; groups; [x | xs]\ )> groups => attrs'; "did it" attach-wsp: [a, b | cs] -> [(a, b) | cs] attach-wsp: [] -> [] starts-section: x@(appl(prod([cf(layout())], cf(opt(layout())), no-attrs()), cs), attr) -> x where cs compare-attrs: x@ ( (_, appl(p1@prod(_, _, attrs([term(cons("Bind"))])), [id1 | xs1])) , (_, appl(p2@prod(_, _, attrs([term(cons("Bind"))])), [id2 | xs2])) ) -> x where (id1, id2) strategies main = io-wrap( topdown(try(sort-attrs)) )