3 # Copyright 2007 Google Inc. All Rights Reserved.
5 """CSS Lexical Grammar rules.
7 CSS lexical grammar from http://www.w3.org/TR/CSS21/grammar.html
10 __author__
= ['elsigh@google.com (Lindsey Simon)',
11 'msamuel@google.com (Mike Samuel)']
14 __all__
= [ "NEWLINE", "HEX", "NON_ASCII", "UNICODE", "ESCAPE", "NMSTART", "NMCHAR", "STRING1", "STRING2", "IDENT", "NAME", "HASH", "NUM", "STRING", "URL", "SPACE", "WHITESPACE", "COMMENT", "QUANTITY", "PUNC" ]
16 # The comments below are mostly copied verbatim from the grammar.
18 # "@import" {return IMPORT_SYM;}
19 # "@page" {return PAGE_SYM;}
20 # "@media" {return MEDIA_SYM;}
21 # "@charset" {return CHARSET_SYM;}
22 KEYWORD
= r
'(?:\@(?:import|page|media|charset))'
24 # nl \n|\r\n|\r|\f ; a newline
25 NEWLINE
= r
'\n|\r\n|\r|\f'
27 # h [0-9a-f] ; a hexadecimal digit
30 # nonascii [\200-\377]
31 NON_ASCII
= r
'[\200-\377]'
33 # unicode \\{h}{1,6}(\r\n|[ \t\r\n\f])?
34 UNICODE
= r
'(?:(?:\\' + HEX
+ r
'{1,6})(?:\r\n|[ \t\r\n\f])?)'
36 # escape {unicode}|\\[^\r\n\f0-9a-f]
37 ESCAPE
= r
'(?:' + UNICODE
+ r
'|\\[^\r\n\f0-9a-f])'
39 # nmstart [_a-z]|{nonascii}|{escape}
40 NMSTART
= r
'(?:[_a-z]|' + NON_ASCII
+ r
'|' + ESCAPE
+ r
')'
42 # nmchar [_a-z0-9-]|{nonascii}|{escape}
43 NMCHAR
= r
'(?:[_a-z0-9-]|' + NON_ASCII
+ r
'|' + ESCAPE
+ r
')'
45 # ident -?{nmstart}{nmchar}*
46 IDENT
= r
'-?' + NMSTART
+ NMCHAR
+ '*'
54 # string1 \"([^\n\r\f\\"]|\\{nl}|{escape})*\" ; "string"
55 STRING1
= r
'"(?:[^\"\\]|\\.)*"'
57 # string2 \'([^\n\r\f\\']|\\{nl}|{escape})*\' ; 'string'
58 STRING2
= r
"'(?:[^\'\\]|\\.)*'"
60 # string {string1}|{string2}
61 STRING
= '(?:' + STRING1
+ r
'|' + STRING2
+ ')'
63 # num [0-9]+|[0-9]*"."[0-9]+
64 NUM
= r
'(?:[0-9]*\.[0-9]+|[0-9]+)'
67 SPACE
= r
'[ \t\r\n\f]'
70 WHITESPACE
= '(?:' + SPACE
+ r
'*)'
73 URL_SPECIAL_CHARS
= r
'[!#$%&*-~]'
75 # url chars ({url_special_chars}|{nonascii}|{escape})*
76 URL_CHARS
= r
'(?:%s|%s|%s)*' % (URL_SPECIAL_CHARS
, NON_ASCII
, ESCAPE
)
79 URL
= r
'url\(%s(%s|%s)%s\)' % (WHITESPACE
, STRING
, URL_CHARS
, WHITESPACE
)
82 # see http://www.w3.org/TR/CSS21/grammar.html
83 COMMENT
= r
'/\*[^*]*\*+([^/*][^*]*\*+)*/'
85 # {E}{M} {return EMS;}
86 # {E}{X} {return EXS;}
87 # {P}{X} {return LENGTH;}
88 # {C}{M} {return LENGTH;}
89 # {M}{M} {return LENGTH;}
90 # {I}{N} {return LENGTH;}
91 # {P}{T} {return LENGTH;}
92 # {P}{C} {return LENGTH;}
93 # {D}{E}{G} {return ANGLE;}
94 # {R}{A}{D} {return ANGLE;}
95 # {G}{R}{A}{D} {return ANGLE;}
96 # {M}{S} {return TIME;}
98 # {H}{Z} {return FREQ;}
99 # {K}{H}{Z} {return FREQ;}
100 # % {return PERCENTAGE;}
101 UNIT
= r
'(?:em|ex|px|cm|mm|in|pt|pc|deg|rad|grad|ms|s|hz|khz|%)'
103 # {num}{UNIT|IDENT} {return NUMBER;}
104 QUANTITY
= '%s(?:%s%s|%s)?' % (NUM
, WHITESPACE
, UNIT
, IDENT
)
106 # "<!--" {return CDO;}
107 # "-->" {return CDC;}
108 # "~=" {return INCLUDES;}
109 # "|=" {return DASHMATCH;}
110 # {w}"{" {return LBRACE;}
111 # {w}"+" {return PLUS;}
112 # {w}">" {return GREATER;}
113 # {w}"," {return COMMA;}
114 PUNC
= r
'<!--|-->|~=|\|=|[\{\+>,:;]'