parser.mly 8.8 KB

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