parser.mly 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. %{
  2. (* CSS grammar based on:
  3. * - http://www.w3.org/TR/CSS2/grammar.html
  4. * - http://www.w3.org/TR/css3-mediaqueries/
  5. *)
  6. open Lexing
  7. open Types
  8. let ( |> ) a b = b a
  9. (* TODO: move this to utils *)
  10. let rec filter_none = function
  11. | [] -> []
  12. | None :: tl -> filter_none tl
  13. | Some hd :: tl -> hd :: filter_none tl
  14. type term_t = Term of expr | Operator of string
  15. let rec transform_value f = function
  16. | Concat terms -> Concat (List.map (transform_value f) terms)
  17. | Function (name, arg) -> Function (name, transform_value f arg)
  18. | Unary (op, term) -> Unary (op, transform_value f term)
  19. | Nary (op, terms) -> Nary (op, List.map (transform_value f) terms)
  20. | value -> f value
  21. let concat_terms terms =
  22. let rec transform_ops = function
  23. | [] -> []
  24. | Term left :: Operator op :: Term right :: tl ->
  25. transform_ops (Term (Nary (op, [left; right])) :: tl)
  26. | Term hd :: tl -> hd :: transform_ops tl
  27. | Operator op :: _ -> raise (SyntaxError ("unexpected operator \"" ^ op ^ "\""))
  28. in
  29. let rec flatten_nary = function
  30. | [] -> []
  31. | Nary (op, Nary (op2, left) :: right) :: tl when op2 = op ->
  32. Nary (op, flatten_nary left @ flatten_nary right) :: flatten_nary tl
  33. | hd :: tl -> hd :: flatten_nary tl
  34. in
  35. match terms |> transform_ops |> flatten_nary with
  36. | [hd] -> hd
  37. | l -> Concat l
  38. %}
  39. (* Tokens *)
  40. %token S CDO CDC IMPORT_SYM PAGE_SYM MEDIA_SYM CHARSET_SYM FONT_FACE_SYM
  41. %token NAMESPACE_SYM KEYFRAMES_SYM IMPORTANT_SYM
  42. %token <float> PERCENTAGE NUMBER
  43. %token <float * string> UNIT_VALUE
  44. %token <string> COMBINATOR RELATION STRING IDENT HASH URI FUNCTION
  45. %token LPAREN RPAREN LBRACE RBRACE LBRACK RBRACK SEMICOL COLON COMMA DOT PLUS
  46. %token MINUS SLASH STAR ONLY AND NOT FROM TO EOF
  47. (* Start symbol *)
  48. %type <Types.stylesheet> stylesheet
  49. %start stylesheet
  50. %%
  51. (* list with arbitrary whitespace between elements and separators *)
  52. %inline wslist(sep, x): S? l=separated_list(sep, terminated(x, S?)) { l }
  53. %inline wspreceded(prefix, x): p=preceded(pair(prefix, S?), x) { p }
  54. cd: CDO S? | CDC S? {}
  55. stylesheet:
  56. | charset = charset? S? cd*
  57. imports = terminated(import, cd*)*
  58. namespaces = terminated(namespace, cd*)*
  59. statements = terminated(statement, cd*)*
  60. EOF
  61. { let charset = match charset with None -> [] | Some c -> [c] in
  62. charset @ imports @ namespaces @ statements }
  63. statement:
  64. | s=ruleset | s=media | s=page | s=font_face | s=keyframes
  65. { s }
  66. charset:
  67. | CHARSET_SYM name=STRING S? SEMICOL
  68. { Charset name }
  69. import:
  70. | IMPORT_SYM S? tgt=string_or_uri media=media_query_list SEMICOL S?
  71. { Import (tgt, media) }
  72. %inline string_or_uri:
  73. | str=STRING { Strlit str }
  74. | uri=URI { Uri uri }
  75. namespace:
  76. | NAMESPACE_SYM S? prefix=terminated(namespace_prefix, S?)? ns=string_or_uri S? SEMICOL S?
  77. { Namespace (prefix, ns) }
  78. %inline namespace_prefix:
  79. | prefix=IDENT
  80. { prefix }
  81. media:
  82. | MEDIA_SYM queries=media_query_list LBRACE S? rulesets=ruleset* RBRACE S?
  83. { Media (queries, rulesets) }
  84. media_query_list:
  85. | S?
  86. { [] }
  87. | S? hd=media_query tl=wspreceded(COMMA, media_query)*
  88. { hd :: tl }
  89. media_query:
  90. | prefix=only_or_not? typ=media_type S? feat=wspreceded(AND, media_expr)*
  91. { (prefix, Some typ, feat) }
  92. | hd=media_expr tl=wspreceded(AND, media_expr)*
  93. { (None, None, (hd :: tl)) }
  94. %inline only_or_not:
  95. | ONLY S? { "only" }
  96. | NOT S? { "not" }
  97. %inline media_type:
  98. | id=IDENT { id }
  99. media_expr:
  100. | LPAREN S? feature=media_feature S? value=wspreceded(COLON, expr)? RPAREN S?
  101. { (feature, value) }
  102. %inline media_feature:
  103. | id=IDENT { id }
  104. page:
  105. | PAGE_SYM S? pseudo=pseudo_page? decls=decls_block
  106. { Page (pseudo, decls) }
  107. pseudo_page:
  108. | COLON pseudo=IDENT S?
  109. { pseudo }
  110. font_face:
  111. | FONT_FACE_SYM S? LBRACE S? hd=descriptor_declaration?
  112. tl=wspreceded(SEMICOL, descriptor_declaration?)* RBRACE S?
  113. { Font_face (filter_none (hd :: tl)) }
  114. descriptor_declaration:
  115. | name=property COLON S? value=expr
  116. { (name, value) }
  117. keyframes:
  118. | KEYFRAMES_SYM S? id=IDENT S? LBRACE S? rules=keyframe_ruleset* RBRACE S?
  119. { Keyframes (id, rules) }
  120. keyframe_ruleset:
  121. | selector=keyframe_selector S? decls=decls_block
  122. { (selector, decls) }
  123. keyframe_selector:
  124. | FROM { Ident "from" }
  125. | TO { Ident "to" }
  126. | n=PERCENTAGE { Number (n, Some "%") }
  127. %inline decls_block:
  128. | LBRACE S? hd=declaration? tl=wspreceded(SEMICOL, declaration?)* RBRACE S?
  129. { filter_none (hd :: tl) }
  130. ruleset:
  131. | selectors_hd = selector
  132. selectors_tl = wspreceded(COMMA, selector)*
  133. decls = decls_block
  134. { Ruleset (selectors_hd :: selectors_tl, decls) }
  135. selector:
  136. | simple=simple_selector S?
  137. { Simple simple }
  138. | left=simple_selector S right=selector
  139. { Combinator (Simple left, " ", right) }
  140. | left=simple_selector S? com=combinator right=selector
  141. { Combinator (Simple left, com, right) }
  142. %inline combinator:
  143. | PLUS S? { "+" }
  144. | c=COMBINATOR S? { c }
  145. simple_selector:
  146. | elem=element_name addons=element_addon*
  147. { elem ^ String.concat "" addons }
  148. | addons=element_addon+
  149. { String.concat "" addons }
  150. %inline element_addon:
  151. | a=HASH | a=cls | a=attrib | a=pseudo
  152. { a }
  153. element_name:
  154. | tag=IDENT { tag }
  155. | STAR { "*" }
  156. cls:
  157. | DOT name=IDENT
  158. { "." ^ name }
  159. attrib:
  160. | LBRACK S? left=IDENT S? right=pair(RELATION, rel_value)? RBRACK
  161. { let right = match right with None -> "" | Some (op, term) -> op ^ term in
  162. "[" ^ left ^ right ^ "]" }
  163. %inline rel_value:
  164. | S? id=IDENT S? { id }
  165. | S? s=STRING S? { "\"" ^ s ^ "\"" }
  166. pseudo:
  167. | COLON id=IDENT
  168. { ":" ^ id }
  169. | COLON f=FUNCTION S? arg=terminated(IDENT, S?)? RPAREN
  170. { let arg = match arg with None -> "" | Some id -> id in
  171. ":" ^ f ^ "(" ^ arg ^ ")" }
  172. declaration:
  173. | name=property S? COLON S? value=expr important=boption(pair(IMPORTANT_SYM, S?))
  174. { (String.lowercase name, value, important) }
  175. %inline property: name=IDENT { name }
  176. expr:
  177. | l=exprl { concat_terms l }
  178. %inline exprl:
  179. | hd=term tl=opterm* { Term hd :: List.concat tl }
  180. %inline opterm:
  181. | t=term { [Term t] }
  182. | op=operator t=term { [Operator op; Term t] }
  183. %inline operator:
  184. | SLASH S? { "/" }
  185. | COMMA S? { "," }
  186. term:
  187. | op=unary_operator v=numval S? { Unary (op, v) }
  188. | v=numval S? { v }
  189. | str=STRING S? { Strlit str }
  190. | id=IDENT S? { Ident id }
  191. | uri=URI S? { Uri uri }
  192. | fn=FUNCTION arg=expr RPAREN S? { Function (fn, arg) }
  193. | hex=HASH S?
  194. { let h = "[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]" in
  195. if Str.string_match (Str.regexp ("^" ^ h ^ "\\(" ^ h ^ "\\)?$")) hex 0
  196. then Hexcolor (String.lowercase hex)
  197. else raise (SyntaxError ("invalid color #" ^ hex)) }
  198. unary_operator:
  199. | MINUS { "-" }
  200. | PLUS { "+" }
  201. %inline numval:
  202. | n=NUMBER { Number (n, None) }
  203. | v=UNIT_VALUE { let n, u = v in Number (n, Some u) }
  204. | n=PERCENTAGE { Number (n, Some "%") }