util.ml 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. open Printf
  2. open Types
  3. (** List utilities *)
  4. let is_none = function None -> true | Some _ -> false
  5. let is_some = function None -> false | Some _ -> true
  6. let some_val = function None -> failwith "no value" | Some v -> v
  7. let rec filter_none = function
  8. | [] -> []
  9. | None :: tl -> filter_none tl
  10. | Some hd :: tl -> hd :: filter_none tl
  11. (** Reading input from file/stdin *)
  12. let input_all ic =
  13. let n = in_channel_length ic in
  14. let buf = Bytes.create n in
  15. really_input ic buf 0 n;
  16. close_in ic;
  17. Bytes.unsafe_to_string buf
  18. let input_buffered ic chunksize =
  19. let rec read_all buf bufsize pos =
  20. match input ic buf pos (bufsize - pos) with
  21. | 0 -> close_in ic; Bytes.unsafe_to_string buf
  22. | nread when nread = bufsize - pos ->
  23. let bufsize = bufsize + chunksize in
  24. let pos = pos + nread in
  25. read_all (Bytes.cat buf (Bytes.create chunksize)) bufsize pos
  26. | nread ->
  27. read_all buf bufsize (pos + nread)
  28. in
  29. read_all (Bytes.create chunksize) chunksize 0
  30. (** Error printing *)
  31. let noloc = ("", 0, 0, 0, 0)
  32. let tabwidth = 4
  33. let count_tabs str upto =
  34. let rec count n = function
  35. | 0 -> n
  36. | i -> count (if String.get str (i - 1) = '\t' then n + 1 else n) (i - 1)
  37. in
  38. count 0 upto
  39. let rec repeat s n = if n < 1 then "" else s ^ (repeat s (n - 1))
  40. let retab str = Str.global_replace (Str.regexp "\t") (repeat " " tabwidth) str
  41. let indent n = repeat (repeat " " (tabwidth - 1)) n
  42. let prerr_loc (fname, ystart, yend, xstart, xend) =
  43. let file = open_in fname in
  44. (* skip lines until the first matched line *)
  45. for i = 1 to ystart - 1 do ignore (input_line file) done;
  46. (* for each line in `loc`, print the source line with an underline *)
  47. for l = ystart to yend do
  48. let line = input_line file in
  49. let linewidth = String.length line in
  50. let left = if l = ystart then xstart else 1 in
  51. let right = if l = yend then xend else linewidth in
  52. if linewidth > 0 then begin
  53. prerr_endline (retab line);
  54. prerr_string (indent (count_tabs line right));
  55. for i = 1 to left - 1 do prerr_char ' ' done;
  56. for i = left to right do prerr_char '^' done;
  57. prerr_endline "";
  58. end
  59. done
  60. let prerr_loc_msg loc msg =
  61. let (fname, ystart, yend, xstart, xend) = loc in
  62. if loc != noloc then begin
  63. let line_s = if yend != ystart
  64. then sprintf "lines %d-%d" ystart yend
  65. else sprintf "line %d" ystart
  66. in
  67. let char_s = if xend != xstart || yend != ystart
  68. then sprintf "characters %d-%d" xstart xend
  69. else sprintf "character %d" xstart
  70. in
  71. eprintf "File \"%s\", %s, %s:\n" fname line_s char_s;
  72. end;
  73. eprintf "%s\n" msg;
  74. if loc != noloc then
  75. try prerr_loc loc
  76. with Sys_error _ -> ()
  77. (** AST traversal *)
  78. #define TRAV_ALL(id, constructor) \
  79. trav_all_##id l = \
  80. let rec filter_clear = function \
  81. | [] -> [] \
  82. | Clear :: tl -> filter_clear tl \
  83. | constructor hd :: tl -> hd :: filter_clear tl \
  84. | _ -> failwith ("expected " ^ #constructor ^ " or Clear") \
  85. in \
  86. filter_clear (List.map trav_##id l)
  87. #define EXPECT(id, constructor) \
  88. expect_##id value = \
  89. match trav_##id value with \
  90. | constructor decl -> decl \
  91. | _ -> failwith ("expected " ^ #constructor)
  92. let transform_stylesheet f stylesheet =
  93. let f x =
  94. match f x with
  95. | Expr (Concat [expr] | Nary (_, [expr])) -> Expr expr
  96. | value -> value
  97. in
  98. let rec trav_expr = function
  99. | Concat terms -> f (Expr (Concat (trav_all_expr terms)))
  100. | Function (name, arg) -> f (Expr (Function (name, expect_expr arg)))
  101. | Unary (op, opnd) -> f (Expr (Unary (op, expect_expr opnd)))
  102. | Nary (op, opnds) -> f (Expr (Nary (op, trav_all_expr opnds)))
  103. | value -> f (Expr value)
  104. and EXPECT(expr, Expr)
  105. and TRAV_ALL(expr, Expr) in
  106. let trav_declaration (name, value, important) =
  107. f (Declaration (name, expect_expr value, important))
  108. in
  109. let TRAV_ALL(declaration, Declaration) in
  110. let rec trav_selector = function
  111. | (No_element | All_elements | Element _) as elem ->
  112. f (Selector elem)
  113. | Id (base, id) ->
  114. f (Selector (Id (expect_selector base, id)))
  115. | Class (base, cls) ->
  116. f (Selector (Class (expect_selector base, cls)))
  117. | Attribute (base, attr, value) ->
  118. f (Selector (Attribute (expect_selector base, attr, value)))
  119. | Pseudo_class (base, cls, None) ->
  120. f (Selector (Pseudo_class (expect_selector base, cls, None)))
  121. | Pseudo_class (base, fn, Some args) ->
  122. let args = trav_all_pseudo_class_arg args in
  123. f (Selector (Pseudo_class (expect_selector base, fn, Some args)))
  124. | Pseudo_element (base, elem) ->
  125. f (Selector (Pseudo_element (expect_selector base, elem)))
  126. | Combinator (left, com, right) ->
  127. let left = expect_selector left in
  128. let right = expect_selector right in
  129. f (Selector (Combinator (left, com, right)))
  130. and EXPECT(selector, Selector)
  131. and TRAV_ALL(selector, Selector)
  132. and trav_pseudo_class_arg = function
  133. | Nested_selector s ->
  134. f (Pseudo_class_arg (Nested_selector (expect_selector s)))
  135. | Nth _ as elem ->
  136. f (Pseudo_class_arg elem)
  137. and TRAV_ALL(pseudo_class_arg, Pseudo_class_arg) in
  138. let trav_media_expr = function
  139. | (_, None) as value ->
  140. f (Media_expr value)
  141. | (name, Some value) ->
  142. let value =
  143. match trav_expr value with
  144. | Expr value -> Some value
  145. | Clear -> None
  146. | _ -> failwith "expected Expr or Clear"
  147. in
  148. f (Media_expr (name, value))
  149. in
  150. let TRAV_ALL(media_expr, Media_expr) in
  151. let trav_media_query (prefix, mtype, queries) =
  152. f (Media_query (prefix, mtype, trav_all_media_expr queries))
  153. in
  154. let TRAV_ALL(media_query, Media_query) in
  155. let trav_descriptor_declaration (name, value) =
  156. f (Descriptor_declaration (name, expect_expr value))
  157. in
  158. let TRAV_ALL(descriptor_declaration, Descriptor_declaration) in
  159. let trav_keyframe_ruleset (selector, decls) =
  160. f (Keyframe_ruleset (expect_expr selector, trav_all_declaration decls))
  161. in
  162. let TRAV_ALL(keyframe_ruleset, Keyframe_ruleset) in
  163. let trav_supports_declaration (name, value) =
  164. f (Supports_declaration (name, expect_expr value))
  165. in
  166. let EXPECT(supports_declaration, Supports_declaration) in
  167. let rec trav_condition = function
  168. | Not c -> f (Condition (Not (expect_condition c)))
  169. | And l -> f (Condition (And (trav_all_condition l)))
  170. | Or l -> f (Condition (Or (trav_all_condition l)))
  171. | Decl d -> f (Condition (Decl (expect_supports_declaration d)))
  172. and EXPECT(condition, Condition)
  173. and TRAV_ALL(condition, Condition) in
  174. let rec trav_statement = function
  175. | Ruleset (selectors, decls) ->
  176. let selectors = trav_all_selector selectors in
  177. let decls = trav_all_declaration decls in
  178. f (Statement (Ruleset (selectors, decls)))
  179. | Media (queries, rulesets) ->
  180. let queries = trav_all_media_query queries in
  181. let rulesets = trav_all_statement rulesets in
  182. f (Statement (Media (queries, rulesets)))
  183. | Import (target, queries) ->
  184. let target = expect_expr target in
  185. let queries = trav_all_media_query queries in
  186. f (Statement (Import (target, queries)))
  187. | Page (pseudo, decls) ->
  188. let decls = trav_all_declaration decls in
  189. f (Statement (Page (pseudo, decls)))
  190. | Font_face decls ->
  191. let decls = trav_all_descriptor_declaration decls in
  192. f (Statement (Font_face decls))
  193. | Namespace (prefix, uri) ->
  194. let uri = expect_expr uri in
  195. f (Statement (Namespace (prefix, uri)))
  196. | Keyframes (prefix, id, rules) ->
  197. let rules = trav_all_keyframe_ruleset rules in
  198. f (Statement (Keyframes (prefix, id, rules)))
  199. | Supports (condition, statements) ->
  200. let condition = expect_condition condition in
  201. let statements = trav_all_statement statements in
  202. f (Statement (Supports (condition, statements)))
  203. | s ->
  204. f (Statement s)
  205. and TRAV_ALL(statement, Statement) in
  206. trav_all_statement stylesheet
  207. (** Expression identification *)
  208. let is_color = Color_names.is_color
  209. (** Sorting declarations *)
  210. let sort_stylesheet =
  211. let pattern = Str.regexp "^\\([^-]+\\)-\\(.*\\)$" in
  212. transform_stylesheet begin function
  213. | Statement (Ruleset (selectors, decls)) ->
  214. let split x =
  215. if Str.string_match pattern x 0
  216. then Some (Str.matched_group 1 x, Str.matched_group 2 x)
  217. else None
  218. in
  219. let rec cmp a b =
  220. match split a, split b with
  221. | Some (base_a, sub_a), Some (base_b, sub_b) when base_a = base_b ->
  222. cmp sub_a sub_b
  223. | Some (base_a, _), Some (base_b, _) ->
  224. String.compare base_a base_b
  225. | Some (base_a, _), None when base_a = b -> 1
  226. | Some (base_a, _), None -> String.compare base_a b
  227. | None, Some (base_b, _) when a = base_b -> -1
  228. | None, Some (base_b, _) -> String.compare a base_b
  229. | None, None -> String.compare a b
  230. in
  231. let cmp_decls (a, _, _) (b, _, _) = cmp a b in
  232. Statement (Ruleset (selectors, List.stable_sort cmp_decls decls))
  233. | v -> v
  234. end
  235. (** Misc *)
  236. let is_int n = float_of_int (int_of_float n) = n