parser.mly 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. %{
  2. (* CSS grammar based on:
  3. * - http://www.w3.org/TR/CSS2/grammar.html
  4. * - http://www.w3.org/TR/css3-mediaqueries/
  5. * - http://www.w3.org/TR/css3-fonts/
  6. * - http://www.w3.org/TR/css3-namespace/
  7. * - http://www.w3.org/TR/css3-animations/
  8. * - http://www.w3.org/TR/css3-conditional/
  9. *)
  10. open Lexing
  11. open Types
  12. open Util
  13. type term = Term of expr | Operator of string
  14. let concat_terms terms =
  15. let rec transform_ops = function
  16. | [] -> []
  17. | Term left :: Operator op :: Term right :: tl ->
  18. transform_ops (Term (Nary (op, [left; right])) :: tl)
  19. | Term hd :: tl -> hd :: transform_ops tl
  20. | Operator op :: _ -> raise (Syntax_error ("unexpected operator \"" ^ op ^ "\""))
  21. in
  22. let rec flatten_nary = function
  23. | [] -> []
  24. | Nary (op, Nary (op2, left) :: right) :: tl when op2 = op ->
  25. Nary (op, flatten_nary left @ flatten_nary right) :: flatten_nary tl
  26. | hd :: tl -> hd :: flatten_nary tl
  27. in
  28. match terms |> transform_ops |> flatten_nary with
  29. | [hd] -> hd
  30. | l -> Concat l
  31. (* TODO: move this to a normalization stage, because the syntax should be
  32. * preserved during parsing (e.g. for -echo command) *)
  33. let unary_number = function
  34. | Unary ("-", Number (n, u)) -> Number (-.n, u)
  35. | Unary ("+", (Number _ as n)) -> n
  36. | value -> value
  37. %}
  38. (* Tokens *)
  39. %token S CDO CDC IMPORT_SYM PAGE_SYM MEDIA_SYM CHARSET_SYM FONT_FACE_SYM
  40. %token NAMESPACE_SYM KEYFRAMES_SYM SUPPORTS_SYM IMPORTANT_SYM
  41. %token <float> PERCENTAGE NUMBER
  42. %token <float * string> UNIT_VALUE
  43. %token <string> COMBINATOR RELATION STRING IDENT HASH URI FUNCTION
  44. %token LPAREN RPAREN LBRACE RBRACE LBRACK RBRACK SEMICOL COLON COMMA DOT PLUS
  45. %token MINUS SLASH STAR ONLY AND (*OR*) NOT FROM TO EOF
  46. %token WS_AND WS_OR
  47. (* Start symbol *)
  48. %type <Types.stylesheet> stylesheet
  49. %start stylesheet
  50. %%
  51. (* list with arbitrary whitespace between elements and separators *)
  52. %inline ig2(a, b): a b {}
  53. %inline ig3(a, b, c): a b c {}
  54. %inline wslist(sep, x): S* l=separated_list(sep, terminated(x, S*)) { l }
  55. %inline wspreceded(prefix, x): p=preceded(ig2(prefix, S*), x) { p }
  56. %inline all_and: AND | WS_AND {}
  57. cd: CDO S* | CDC S* {}
  58. stylesheet:
  59. | charset = charset? S* cd*
  60. imports = terminated(import, cd*)*
  61. namespaces = terminated(namespace, cd*)*
  62. statements = terminated(nested_statement, cd*)*
  63. EOF
  64. { let charset = match charset with None -> [] | Some c -> [c] in
  65. charset @ imports @ namespaces @ statements }
  66. nested_statement:
  67. | s=ruleset | s=media | s=page | s=font_face_rule | s=keyframes_rule
  68. | s=supports_rule
  69. { s }
  70. group_rule_body:
  71. | LBRACE S* statements=nested_statement* RBRACE S*
  72. { statements }
  73. charset:
  74. | CHARSET_SYM name=STRING S* SEMICOL
  75. { Charset name }
  76. import:
  77. | IMPORT_SYM S* tgt=string_or_uri media=media_query_list SEMICOL S*
  78. { Import (tgt, media) }
  79. %inline string_or_uri:
  80. | str=STRING { Strlit str }
  81. | uri=URI { Uri uri }
  82. namespace:
  83. | NAMESPACE_SYM S* prefix=terminated(namespace_prefix, S*)? ns=string_or_uri S* SEMICOL S*
  84. { Namespace (prefix, ns) }
  85. %inline namespace_prefix:
  86. | prefix=IDENT
  87. { prefix }
  88. media:
  89. | MEDIA_SYM queries=media_query_list rulesets=group_rule_body
  90. { Media (queries, rulesets) }
  91. media_query_list:
  92. | S*
  93. { [] }
  94. | S* hd=media_query tl=wspreceded(COMMA, media_query)*
  95. { hd :: tl }
  96. media_query:
  97. | prefix=only_or_not? typ=media_type S* feat=wspreceded(all_and, media_expr)*
  98. { (prefix, Some typ, feat) }
  99. | hd=media_expr tl=wspreceded(all_and, media_expr)*
  100. { (None, None, (hd :: tl)) }
  101. %inline only_or_not:
  102. | ONLY S* { "only" }
  103. | NOT S* { "not" }
  104. %inline media_type:
  105. | id=IDENT { id }
  106. media_expr:
  107. | LPAREN S* feature=media_feature S* value=wspreceded(COLON, expr)? RPAREN S*
  108. { (feature, value) }
  109. %inline media_feature:
  110. | id=IDENT { id }
  111. page:
  112. | PAGE_SYM S* pseudo=pseudo_page? decls=decls_block
  113. { Page (pseudo, decls) }
  114. pseudo_page:
  115. | COLON pseudo=IDENT S*
  116. { pseudo }
  117. font_face_rule:
  118. | FONT_FACE_SYM S* LBRACE S* hd=descriptor_declaration?
  119. tl=wspreceded(SEMICOL, descriptor_declaration?)* RBRACE S*
  120. { Font_face (filter_none (hd :: tl)) }
  121. descriptor_declaration:
  122. | name=property COLON S* value=expr
  123. { (name, value) }
  124. keyframes_rule:
  125. | KEYFRAMES_SYM S* id=IDENT S* LBRACE S* rules=keyframe_ruleset* RBRACE S*
  126. { Keyframes (id, rules) }
  127. keyframe_ruleset:
  128. | selector=keyframe_selector S* decls=decls_block
  129. { (selector, decls) }
  130. keyframe_selector:
  131. | FROM { Ident "from" }
  132. | TO { Ident "to" }
  133. | n=PERCENTAGE { Number (n, Some "%") }
  134. supports_rule:
  135. | SUPPORTS_SYM S* cond=supports_condition S* body=group_rule_body
  136. { Supports (cond, body) }
  137. supports_condition:
  138. | c=supports_negation
  139. | c=supports_conjunction
  140. | c=supports_disjunction
  141. | c=supports_condition_in_parens
  142. { c }
  143. supports_condition_in_parens:
  144. | LPAREN S* c=supports_condition S* RPAREN
  145. | c=supports_declaration_condition
  146. (*XXX: | c=general_enclosed*)
  147. { c }
  148. supports_negation:
  149. | NOT S+ c=supports_condition_in_parens
  150. { Not c }
  151. supports_conjunction:
  152. | hd=supports_condition_in_parens tl=preceded(WS_AND, supports_condition_in_parens)+
  153. { And (hd :: tl) }
  154. supports_disjunction:
  155. | hd=supports_condition_in_parens tl=preceded(WS_OR, supports_condition_in_parens)+
  156. { Or (hd :: tl) }
  157. supports_declaration_condition:
  158. | LPAREN S* decl=supports_declaration RPAREN
  159. { Decl decl }
  160. supports_declaration:
  161. | name=property S* COLON S* value=expr
  162. { (name, value) }
  163. (*XXX:
  164. general_enclosed:
  165. | ( FUNCTION | LPAREN ) ( any | unused )* RPAREN
  166. { Enclosed expr }
  167. any:
  168. [ IDENT | NUMBER | PERCENTAGE | DIMENSION | STRING
  169. | DELIM | URI | HASH | UNICODE-RANGE | INCLUDES
  170. | DASHMATCH | ':' | FUNCTION S* [any|unused]* ')'
  171. | '(' S* [any|unused]* ')' | '[' S* [any|unused]* ']'
  172. ]
  173. S*;
  174. unused : block | ATKEYWORD S* | ';' S* | CDO S* | CDC S*;
  175. *)
  176. %inline decls_block:
  177. | LBRACE S* hd=declaration? tl=wspreceded(SEMICOL, declaration?)* RBRACE S*
  178. { filter_none (hd :: tl) }
  179. ruleset:
  180. | selectors_hd = selector
  181. selectors_tl = wspreceded(COMMA, selector)*
  182. decls = decls_block
  183. { Ruleset (selectors_hd :: selectors_tl, decls) }
  184. selector:
  185. | simple=simple_selector S*
  186. { Simple simple }
  187. | left=simple_selector S+ right=selector
  188. { Combinator (Simple left, " ", right) }
  189. | left=simple_selector S* com=combinator right=selector
  190. { Combinator (Simple left, com, right) }
  191. %inline combinator:
  192. | PLUS S* { "+" }
  193. | c=COMBINATOR S* { c }
  194. simple_selector:
  195. | elem=element_name addons=element_addon*
  196. { elem ^ String.concat "" addons }
  197. | addons=element_addon+
  198. { String.concat "" addons }
  199. %inline element_addon: a=HASH | a=cls | a=attrib | a=pseudo { a }
  200. element_name:
  201. | tag=IDENT { String.lowercase tag }
  202. | STAR { "*" }
  203. cls:
  204. | DOT name=IDENT
  205. { "." ^ name }
  206. attrib:
  207. | LBRACK S* left=IDENT S* right=pair(RELATION, rel_value)? RBRACK
  208. { let right = match right with None -> "" | Some (op, term) -> op ^ term in
  209. "[" ^ String.lowercase left ^ right ^ "]" }
  210. %inline rel_value:
  211. | S* id=IDENT S* { id }
  212. | S* s=STRING S* { "\"" ^ s ^ "\"" }
  213. pseudo:
  214. | COLON id=IDENT
  215. { ":" ^ (String.lowercase id) }
  216. | COLON f=FUNCTION args=wslist(COMMA, simple_selector) RPAREN
  217. { ":" ^ f ^ "(" ^ String.concat "," args ^ ")" }
  218. declaration:
  219. | name=property S* COLON S* value=expr important=boption(ig2(IMPORTANT_SYM, S*))
  220. { (String.lowercase name, value, important) }
  221. %inline property:
  222. | name=IDENT { name }
  223. | STAR name=IDENT { "*" ^ name } (* IE7 property name hack *)
  224. expr:
  225. | l=exprl { concat_terms l }
  226. %inline exprl:
  227. | hd=term tl=opterm* { Term hd :: List.concat tl }
  228. %inline opterm:
  229. | t=term { [Term t] }
  230. | op=operator t=term { [Operator op; Term t] }
  231. %inline operator:
  232. | SLASH S* { "/" }
  233. | COMMA S* { "," }
  234. term:
  235. | op=unary_operator v=numval S* { unary_number (Unary (op, v)) }
  236. | v=numval S* { v }
  237. | str=STRING S* { Strlit str }
  238. | id=IDENT S* { Ident (String.lowercase id) }
  239. | uri=URI S* { Uri uri }
  240. | fn=FUNCTION arg=expr RPAREN S* { Function (String.lowercase fn, arg) }
  241. | hex=HASH S*
  242. { let h = "[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]" in
  243. if Str.string_match (Str.regexp ("^" ^ h ^ "\\(" ^ h ^ "\\)?$")) hex 0
  244. then Hexcolor (String.lowercase hex)
  245. else raise (Syntax_error ("invalid color #" ^ hex)) }
  246. unary_operator:
  247. | MINUS { "-" }
  248. | PLUS { "+" }
  249. %inline numval:
  250. | n=NUMBER { Number (n, None) }
  251. | v=UNIT_VALUE { let n, u = v in Number (n, Some (String.lowercase u)) }
  252. | n=PERCENTAGE { Number (n, Some "%") }