css1.peg 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. # CSS1 parser for a PEG parser generator.
  2. # and a user command parser
  3. # Doesn't support import statements.
  4. STYLESHEET ← (CDO / CDC)* (RULESET/DECLARATIONS/COMMENT/ATRULE)* (CDO / CDC)*
  5. RULESET ← SELECTORS "{" DECLARATIONS? "}"
  6. SELECTORS ← SELECTOR ("," SELECTOR)*
  7. SELECTOR ← ( NAME ID? CLASS? PSEUDO_CLASS? /
  8. ID CLASS? PSEUDO_CLASS? /
  9. CLASS PSEUDO_CLASS? /
  10. PSEUDO_CLASS )+ PSEUDO_ELEMENT* / PSEUDO_ELEMENT
  11. ATRULE ← ATID STRING ";" / ATID "{" DECLARATIONS "}"
  12. NAME ← <IDENT>
  13. DECLARATIONS ← DECLARATION (';' DECLARATION)* ';'?
  14. DECLARATION ← NAME ":" EXPR PRIO? / COMMENT
  15. OPERATOR ← '/' / ','
  16. # UNARY_OPERATOR ← '-' / '+'
  17. PRIO ← IMPORTANT
  18. EXPR ← TERM (OPERATOR TERM)*
  19. TERM ← STRING / PERCENTAGE / LENGTH / HEXCOLOR / URL / RGB / NUM / IDENT
  20. ~COMMENT ← "/*" (!("*/").)* "*/"
  21. ATID ← < "@" [a-zA-Z][-_a-zA-Z0-9]+ >
  22. IMPORTANT ← < "!" WS "important" >
  23. IDENT ← < [-_a-zA-Z][-_a-zA-Z0-9]* >
  24. PERCENTAGE ← < NUM > "%"
  25. NUM ← < [+-]*[0-9]+ / [0-9]* "." [0-9]+ >
  26. LENGTH ← NUM UNIT
  27. UNIT ← <"pt" / "mm" / "pt" / "in" / "px" / "em" / "ex" >
  28. PSEUDO_ELEMENT ← <":first-letter" / ":first-line" >
  29. PSEUDO_CLASS ← <":hover" / ":link" / ":mark" / ":active" / ":visited" >
  30. HEXCOLOR ← "#" < ([0-9a-fA-F])+ >
  31. ID ← "#" < IDENT >
  32. CLASS ← "." < IDENT >
  33. # Raw string macro.
  34. RAWSM(O,C) ← O < (!(C).)* > C
  35. # Escaped string macro.
  36. ESCSM(O,C) ← O < (!(C)STRCHAR)* > C
  37. # String escapes
  38. STRESC1 ← "\\" [nrtfv\'\\"\[\]\\]
  39. STRESC2 ← "\\" [0-3] [0-7] [0-7]
  40. STRESC3 ← "\\" [0-7] [0-7]*
  41. STRESC4 ← "\\x" [0-9a-fA-F] [0-9a-fA-F]?
  42. STRESC5 ← "\\u" [0-9a-fA-F]+
  43. STRNOESC ← (!('\\\\').)
  44. STRCHAR ← STRESC1 / STRESC2 / STRESC3 / STRESC4 / STRESC5 / STRNOESC
  45. # Strings
  46. STRING ← ESCSM("\"", "\"") / ESCSM("'", "'")
  47. URL ← "url(" STRING ")"
  48. RGB ← < "rgb("[ \t]*[0-9]+[ \t]* "," [ \t]*[0-9]+[ \t]* "," [ \t]*[0-9]+[ \t]* ")" >
  49. ~WS ← [ \t\n\r]*
  50. CDO ← "<!--"
  51. CDC ← "-->"
  52. %whitespace ← [ \t\n\r]*
  53. # %word ← [a-zA-Z][-+/*_a-zA-Z0-9]*