MediaWiki  1.23.2
csslex.py
Go to the documentation of this file.
1 #!/usr/bin/python
2 #
3 # Copyright 2007 Google Inc. All Rights Reserved.
4 
5 """CSS Lexical Grammar rules.
6 
7 CSS lexical grammar from http://www.w3.org/TR/CSS21/grammar.html
8 """
9 
10 __author__ = ['elsigh@google.com (Lindsey Simon)',
11  'msamuel@google.com (Mike Samuel)']
12 
13 # public symbols
14 __all__ = [ "NEWLINE", "HEX", "NON_ASCII", "UNICODE", "ESCAPE", "NMSTART", "NMCHAR", "STRING1", "STRING2", "IDENT", "NAME", "HASH", "NUM", "STRING", "URL", "SPACE", "WHITESPACE", "COMMENT", "QUANTITY", "PUNC" ]
15 
16 # The comments below are mostly copied verbatim from the grammar.
17 
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))'
23 
24 # nl \n|\r\n|\r|\f ; a newline
25 NEWLINE = r'\n|\r\n|\r|\f'
26 
27 # h [0-9a-f] ; a hexadecimal digit
28 HEX = r'[0-9a-f]'
29 
30 # nonascii [\200-\377]
31 NON_ASCII = r'[\200-\377]'
32 
33 # unicode \\{h}{1,6}(\r\n|[ \t\r\n\f])?
34 UNICODE = r'(?:(?:\\' + HEX + r'{1,6})(?:\r\n|[ \t\r\n\f])?)'
35 
36 # escape {unicode}|\\[^\r\n\f0-9a-f]
37 ESCAPE = r'(?:' + UNICODE + r'|\\[^\r\n\f0-9a-f])'
38 
39 # nmstart [_a-z]|{nonascii}|{escape}
40 NMSTART = r'(?:[_a-z]|' + NON_ASCII + r'|' + ESCAPE + r')'
41 
42 # nmchar [_a-z0-9-]|{nonascii}|{escape}
43 NMCHAR = r'(?:[_a-z0-9-]|' + NON_ASCII + r'|' + ESCAPE + r')'
44 
45 # ident -?{nmstart}{nmchar}*
46 IDENT = r'-?' + NMSTART + NMCHAR + '*'
47 
48 # name {nmchar}+
49 NAME = NMCHAR + r'+'
50 
51 # hash
52 HASH = r'#' + NAME
53 
54 # string1 \"([^\n\r\f\\"]|\\{nl}|{escape})*\" ; "string"
55 STRING1 = r'"(?:[^\"\\]|\\.)*"'
56 
57 # string2 \'([^\n\r\f\\']|\\{nl}|{escape})*\' ; 'string'
58 STRING2 = r"'(?:[^\'\\]|\\.)*'"
59 
60 # string {string1}|{string2}
61 STRING = '(?:' + STRING1 + r'|' + STRING2 + ')'
62 
63 # num [0-9]+|[0-9]*"."[0-9]+
64 NUM = r'(?:[0-9]*\.[0-9]+|[0-9]+)'
65 
66 # s [ \t\r\n\f]
67 SPACE = r'[ \t\r\n\f]'
68 
69 # w {s}*
70 WHITESPACE = '(?:' + SPACE + r'*)'
71 
72 # url special chars
73 URL_SPECIAL_CHARS = r'[!#$%&*-~]'
74 
75 # url chars ({url_special_chars}|{nonascii}|{escape})*
76 URL_CHARS = r'(?:%s|%s|%s)*' % (URL_SPECIAL_CHARS, NON_ASCII, ESCAPE)
77 
78 # url
79 URL = r'url\(%s(%s|%s)%s\)' % (WHITESPACE, STRING, URL_CHARS, WHITESPACE)
80 
81 # comments
82 # see http://www.w3.org/TR/CSS21/grammar.html
83 COMMENT = r'/\*[^*]*\*+([^/*][^*]*\*+)*/'
84 
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;}
97 # {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|%)'
102 
103 # {num}{UNIT|IDENT} {return NUMBER;}
104 QUANTITY = '%s(?:%s%s|%s)?' % (NUM, WHITESPACE, UNIT, IDENT)
105 
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'<!--|-->|~=|\|=|[\{\+>,:;]'