Posted to tcl by colin at Mon Mar 11 22:29:23 GMT 2013view raw

  1. PEG css (stylesheet)
  2.  
  3. leaf: CharSpecial <- "\\" [nrt'"\[\]\\] ;
  4. leaf: CharUnicode <- "\\" HexDigit (HexDigit (HexDigit HexDigit?)?)? ;
  5. leaf: CharUnescaped <- !"\\" . ;
  6. leaf: Ident <- ('_' / <alpha>) ('_' / <alnum>)* ;
  7. Char <- CharSpecial / CharUnicode / CharUnescaped;
  8.  
  9. void: EOL <- "\n\r" / "\n" / "\r" ;
  10. void: APOSTROPH <- "'";
  11. void: DAPOSTROPH <- '"';
  12.  
  13. void: COMMENT <- "/*" ([^*] [/] / "*" [^/] / nl)* "*/" ;
  14. void: WS <- (" " / "\t" / EOL / COMMENT)*;
  15.  
  16. void: HexDigit <- [0-9a-fA-F] ;
  17.  
  18. Literal <- APOSTROPH (!APOSTROPH Char)* APOSTROPH / DAPOSTROPH (!DAPOSTROPH Char)* DAPOSTROPH ;
  19.  
  20. leaf: CDO <- "<!--";
  21. leaf: CDC <- "-->";
  22. leaf: INCLUDES <- "~=";
  23. leaf: DASHMATCH <- "|=";
  24.  
  25. leaf: HASH <- "#" Ident;
  26.  
  27. Uri <- "url" WS Literal WS;
  28.  
  29. Function <- Ident WS "(";
  30.  
  31. leaf: LBRACKET <- "[";
  32. leaf: RBRACKET <- "]";
  33. void: LBRACE <- "{";
  34. void: RBRACE <- "}";
  35. void: LPAREN <- "(";
  36. void: RPAREN <- ")";
  37. leaf: STAR <- "*";
  38. void: SEMI <- [;+];
  39.  
  40. # stylesheet
  41. # : [ CHARSET_SYM STRING ';' ]?
  42. # [S|CDO|CDC]* [ import [ CDO S* | CDC S* ]* ]*
  43. # [ [ ruleset | media | page ] [ CDO S* | CDC S* ]* ]*
  44. # ;
  45. leaf: CHARSET <- "@" [Cc][Hh][Aa][Rr][Ss][Ee][Tt];
  46. stylesheet <- WS CHARSET Literal SEMI stylesheet /
  47. WS (ruleset / import / media / page) stylesheet;
  48.  
  49. # import
  50. # : IMPORT_SYM S*
  51. # [STRING|URI] S* media_list? ';' S*
  52. # ;
  53. leaf: IMPORT <- [Ii][Mm][Pp][Oo][Rr][Tt];
  54. import <- IMPORT WS Literal WS media_list WS SEMI
  55. / IMPORT WS Literal WS SEMI
  56. / IMPORT WS Uri WS media_list WS SEMI
  57. / IMPORT WS Uri WS SEMI;
  58.  
  59. # media
  60. # : MEDIA_SYM WS media_list '{' WS ruleset* '}' WS
  61. # ;
  62. leaf: MEDIA <- [Mm][Ee][Dd][Ii][Aa];
  63. media <- MEDIA WS media_list LBRACE WS ruleset_seq RBRACE;
  64.  
  65. ruleset_seq <- ruleset ruleset_seq / ruleset;
  66.  
  67. # media_list
  68. # : medium [ COMMA WS medium]*
  69. # ;
  70. media_list <- Ident WS COMMA WS media_list / Ident;
  71.  
  72. # page
  73. # : PAGE_SYM WS pseudo_page?
  74. # '{' WS declaration? [ ';' WS declaration? ]* '}' WS
  75. # ;
  76. leaf: PAGE <- [Pp][Aa][Gg][Ee];
  77. page <- PAGE WS pseudo_page WS LBRACE WS declaration_seq RBRACE
  78. / PAGE WS LBRACE WS declaration_seq RBRACE;
  79.  
  80. # pseudo_page
  81. # : ':' IDENT WS
  82. # ;
  83. pseudo_page <- ":" Ident;
  84.  
  85. # operator
  86. # : '/' WS | ',' WS
  87. # ;
  88. operator <- [/,=];
  89.  
  90. # combinator
  91. # : '+' WS
  92. # | '>' WS
  93. # ;
  94. combinator <- [+>];
  95.  
  96. # unary_operator
  97. # : '-' | '+'
  98. # ;
  99. unary_operator <- [-+];
  100.  
  101. # property
  102. # : IDENT WS
  103. # ;
  104. property <- Ident;
  105.  
  106. # ruleset
  107. # : selector [ ',' WS selector ]*
  108. # '{' WS declaration? [ ';' WS declaration? ]* '}' WS
  109. # ;
  110. ruleset <- selector_seq WS LBRACE WS declaration_seq RBRACE;
  111.  
  112. selector_seq <- selector WS COMMA WS selector_seq / selector;
  113.  
  114. declaration_seq <- declaration SEMI WS declaration_seq / declaration;
  115.  
  116.  
  117. # selector
  118. # : simple_selector [ combinator selector | S+ [ combinator? selector ]? ]?
  119. # ;
  120. selector <- simple_selector / simple_selector WS combinator WS selector / simple_selector S WS combinator WS selector / simple_selector S WS selector;
  121.  
  122. # simple_selector
  123. # : element_name [ HASH | class | attrib | pseudo ]*
  124. # | [ HASH | class | attrib | pseudo ]+
  125. # ;
  126. simple_selector <- element_name selector_sub / element_name / selector_sub;
  127.  
  128. selector_sub <- HASH selector_sub
  129. / class selector_sub
  130. / attrib selector_sub
  131. / pseudo selector_sub
  132. / HASH
  133. / class
  134. / attrib
  135. / pseudo;
  136.  
  137. # class
  138. # : '.' IDENT
  139. # ;
  140. class <- DOT Ident;
  141.  
  142. # element_name
  143. # : IDENT | '*'
  144. # ;
  145. element_name <- Ident / STAR;
  146.  
  147. # attrib
  148. # : '[' WS IDENT WS [ [ '=' | INCLUDES | DASHMATCH ] WS
  149. # [ IDENT | Literal ] WS ]? ']'
  150. # ;
  151. attrib <- LBRACKET WS IDENT WS RBRACKET /
  152. LBRACKET WS IDENT WS "=" WS IDENT WS RBRACKET /
  153. LBRACKET WS IDENT WS "=" WS Literal WS RBRACKET /
  154. LBRACKET WS IDENT WS INCLUDES WS IDENT WS RBRACKET /
  155. LBRACKET WS IDENT WS INCLUDES WS Literal WS RBRACKET /
  156. LBRACKET WS IDENT WS DASHMATCH WS IDENT WS RBRACKET /
  157. LBRACKET WS IDENT WS DASHMATCH WS Literal WS RBRACKET;
  158.  
  159. # pseudo
  160. # : ':' [ IDENT | FUNCTION WS [IDENT WS]? ')' ]
  161. # ;
  162. pseudo <- ":" IDENT / ":" FUNCTION WS RPAREN / ":" FUNCTION WS IDENT WS RPAREN;
  163.  
  164. # declaration
  165. # : property ':' WS expr prio?
  166. # ;
  167. leaf: IMPORTANT <- "!" WS [Ii][Mm][Pp][Oo][Rr][Tt][Aa][Nn][Tt];
  168.  
  169. declaration <- property WS ":" WS expr / property WS ":" WS expr WS IMPORTANT;
  170.  
  171. # expr
  172. # : term [ operator? term ]*
  173. # ;
  174. expr <- term / term WS expr WS / term WS operator WS expr;
  175.  
  176. # term
  177. # : unary_operator?
  178. # [ NUMBER WS | PERCENTAGE WS | LENGTH WS | EMS WS | EXS WS | ANGLE WS |
  179. # TIME WS | FREQ WS ]
  180. # | Literal WS | IDENT WS | URI WS | hexcolor | function
  181. # ;
  182. term <- unary_operator term_numeric / term_numeric / Literal / Ident / Uri / hexcolor / function;
  183. Digits <- [0-9]+;
  184. Length <- Digits ([Pp][Xx] / [Cc][Mm] / [Mm][Mm] / [Ii][Nn] / [Pp][Tt] / [Pp][Cc]);
  185. Angle <- Digits ([Dd][Ee][Gg] / [Rr][Aa][Dd] / [Gg][Rr][Aa][Dd]);
  186. Time <- Digits ([Mm][Ss] / [Ss]);
  187. Freq <- Digits ([Hh][Zz] / [Kk][Hh][Zz]);
  188. Dimension <- "<digit>"+ Ident;
  189. Percentage <- "<digit>" "%";
  190. Sign <- [-+];
  191. Number <- Sign? "<ddigit>"+;
  192. EMS <- Digits [Ee][Mm];
  193. EXS <- Digits [Ee][Xx];
  194.  
  195. term_numeric <- Number / Percentage / Length / EMS / EXS / Angle / Time / Freq;
  196.  
  197. # function
  198. # : FUNCTION WS expr ')' WS
  199. # ;
  200. function <- Function WS expr RPAREN WS;
  201.  
  202. # /*
  203. # * There is a constraint on the color that it must
  204. # * have either 3 or 6 hex-digits (i.e., [0-9a-fA-F])
  205. # * after the "#"; e.g., "#000" is OK, but "#abcd" is not.
  206. # */
  207. # hexcolor
  208. # : HASH WS
  209. # ;
  210. hexcolor <- Hash;
  211. END;