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