util.ml 8.8 KB

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