34 |
| null args = go factor |
| null args = go factor |
35 |
| argument "factor" args = go factor |
| argument "factor" args = go factor |
36 |
| argument "explode" args = go (concatMap explode) |
| argument "explode" args = go (concatMap explode) |
37 |
|
| argument "identity" args = go id |
38 |
|
| argument "lex" args = show (alexScanTokens input) ++ "\n" |
39 |
|
| argument "tree" args = show stylesheet ++ "\n" |
40 |
|
| argument "statistics" args = stats stylesheet ++ "\n" |
41 |
| otherwise = usage |
| otherwise = usage |
42 |
where go f = (unlines . show_stylesheet . process f . |
where go f = (unlines . show_stylesheet . process f) stylesheet |
43 |
parser . alexScanTokens) input |
stylesheet = (parser . alexScanTokens) input |
44 |
|
|
45 |
-- Check an argument list for an argument in short or long form. |
-- Check an argument list for an argument in short or long form. |
46 |
argument :: String -> [String] -> Bool |
argument :: String -> [String] -> Bool |
60 |
"standard output.", |
"standard output.", |
61 |
"", |
"", |
62 |
"Output mode:", |
"Output mode:", |
63 |
" -f, --factor factor out common declarations (default)", |
" -f, --factor factor out common declarations (default)", |
64 |
" -e, --explode produce rulesets with one selector and declaration each", |
" -e, --explode produce rulesets with one selector and declaration each", |
65 |
|
" -i, --identity just parse and output unmodified", |
66 |
|
" -l, --lex tokenise and display token stream", |
67 |
|
" -t, --tree parse and display parse tree", |
68 |
|
" -s, --statistics count rulesets, selectors, and declarations", |
69 |
"", |
"", |
70 |
" -h, --help display this help and exit"] |
" -h, --help display this help and exit"] |
71 |
|
|
72 |
-- Process a Stylesheet. |
-- Process a Stylesheet. |
73 |
process :: ([Statement] -> [Statement]) -> Stylesheet -> Stylesheet |
process :: ([Statement] -> [Statement]) -> Stylesheet -> Stylesheet |
144 |
implode_sel :: [Statement] -> Statement |
implode_sel :: [Statement] -> Statement |
145 |
implode_sel ((Ruleset sels decls):stmts) = |
implode_sel ((Ruleset sels decls):stmts) = |
146 |
Ruleset sels (concat (decls:(map declarations stmts))) |
Ruleset sels (concat (decls:(map declarations stmts))) |
|
where declarations :: Statement -> [Declaration] |
|
|
declarations (Ruleset s d) = d |
|
147 |
|
|
148 |
-- Implode a list of Ruleset Statements with equal lists of Declarations to a |
-- Implode a list of Ruleset Statements with equal lists of Declarations to a |
149 |
-- single equivalent Ruleset Statement. |
-- single equivalent Ruleset Statement. |
155 |
implode_decl :: [Statement] -> Statement |
implode_decl :: [Statement] -> Statement |
156 |
implode_decl ((Ruleset sels decls):stmts) = |
implode_decl ((Ruleset sels decls):stmts) = |
157 |
Ruleset (concat (sels:(map selectors stmts))) decls |
Ruleset (concat (sels:(map selectors stmts))) decls |
158 |
where selectors :: Statement -> [Selector] |
|
159 |
selectors (Ruleset s d) = s |
-- Count rulesets, selectors, and declarations in a Stylesheet. |
160 |
|
stats :: Stylesheet -> String |
161 |
|
stats (Stylesheet charset imports stmts) |
162 |
|
| n == 0 = "0 rulesets" |
163 |
|
| otherwise = "rulesets " ++ show n ++ |
164 |
|
", selectors min " ++ show s0 ++ |
165 |
|
" max " ++ show s1 ++ |
166 |
|
" mean " ++ take 5 ( |
167 |
|
show (fromIntegral sn / fromIntegral n)) ++ |
168 |
|
", declarations min " ++ show d0 ++ |
169 |
|
" max " ++ show d1 ++ |
170 |
|
" mean " ++ take 5 ( |
171 |
|
show (fromIntegral dn / fromIntegral n)) |
172 |
|
where [n, s0, s1, sn, d0, d1, dn] = stats_stmts stmts |
173 |
|
|
174 |
|
-- Count rulesets, selectors, and declarations in a list of Statements. |
175 |
|
stats_stmts :: [Statement] -> [Int] |
176 |
|
stats_stmts stmts = stats_rulesets (filter is_ruleset stmts ++ |
177 |
|
(concatMap get_stmts (filter is_media stmts))) |
178 |
|
where get_stmts (Media media stmts) = stmts |
179 |
|
|
180 |
|
-- Count rulesets, selectors, and declarations in a list of Ruleset Statements. |
181 |
|
stats_rulesets :: [Statement] -> [Int] |
182 |
|
stats_rulesets stmts = [length stmts, |
183 |
|
minimum sel_lengths, maximum sel_lengths, sum sel_lengths, |
184 |
|
minimum decl_lengths, maximum decl_lengths, sum decl_lengths] |
185 |
|
where sel_lengths = map (length . selectors) stmts |
186 |
|
decl_lengths = map (length . declarations) stmts |
187 |
|
|
188 |
|
-- Extract the list of Selectors from a Ruleset Statement. |
189 |
|
selectors :: Statement -> [Selector] |
190 |
|
selectors (Ruleset s d) = s |
191 |
|
|
192 |
|
-- Extract the list of Declarations from a Ruleset Statement. |
193 |
|
declarations :: Statement -> [Declaration] |
194 |
|
declarations (Ruleset s d) = d |