OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
| 5 part of csslib.parser; |
| 6 |
5 // TODO(terry): Need to be consistent with tokens either they're ASCII tokens | 7 // TODO(terry): Need to be consistent with tokens either they're ASCII tokens |
6 // e.g., ASTERISK or they're CSS e.g., PSEUDO, COMBINATOR_*. | 8 // e.g., ASTERISK or they're CSS e.g., PSEUDO, COMBINATOR_*. |
7 class TokenKind { | 9 class TokenKind { |
8 // Common shared tokens used in TokenizerBase. | 10 // Common shared tokens used in TokenizerBase. |
9 static const int UNUSED = 0; // Unused place holder... | 11 static const int UNUSED = 0; // Unused place holder... |
10 static const int END_OF_FILE = 1; // TODO(terry): Must match base | 12 static const int END_OF_FILE = 1; // EOF |
11 static const int LPAREN = 2; // ( | 13 static const int LPAREN = 2; // ( |
12 static const int RPAREN = 3; // ) | 14 static const int RPAREN = 3; // ) |
13 static const int LBRACK = 4; // [ | 15 static const int LBRACK = 4; // [ |
14 static const int RBRACK = 5; // ] | 16 static const int RBRACK = 5; // ] |
15 static const int LBRACE = 6; // { | 17 static const int LBRACE = 6; // { |
16 static const int RBRACE = 7; // } | 18 static const int RBRACE = 7; // } |
17 static const int DOT = 8; // . | 19 static const int DOT = 8; // . |
18 static const int SEMICOLON = 9; // ; | 20 static const int SEMICOLON = 9; // ; |
19 | 21 |
20 // Unique tokens for CSS. | 22 // Unique tokens for CSS. |
(...skipping 15 matching lines...) Expand all Loading... |
36 static const int SINGLE_QUOTE = 25; // ' | 38 static const int SINGLE_QUOTE = 25; // ' |
37 static const int DOUBLE_QUOTE = 26; // " | 39 static const int DOUBLE_QUOTE = 26; // " |
38 static const int SLASH = 27; // / | 40 static const int SLASH = 27; // / |
39 static const int EQUALS = 28; // = | 41 static const int EQUALS = 28; // = |
40 static const int OR = 29; // | | 42 static const int OR = 29; // | |
41 static const int CARET = 30; // ^ | 43 static const int CARET = 30; // ^ |
42 static const int DOLLAR = 31; // $ | 44 static const int DOLLAR = 31; // $ |
43 static const int LESS = 32; // < | 45 static const int LESS = 32; // < |
44 static const int BANG = 33; // ! | 46 static const int BANG = 33; // ! |
45 static const int MINUS = 34; // - | 47 static const int MINUS = 34; // - |
| 48 static const int BACKSLASH = 35; // \ |
| 49 static const int AMPERSAND = 36; // & |
46 | 50 |
47 // WARNING: END_TOKENS must be 1 greater than the last token above (last | 51 // WARNING: Tokens from this point and above must have the corresponding ASCII |
48 // character in our list). Also add to kindToString function and the | 52 // character in the TokenChar list at the bottom of this file. The |
49 // constructor for TokenKind. | 53 // order of the above tokens should be the same order as TokenChar. |
50 | |
51 static const int END_TOKENS = 35; // Marker for last token in list | |
52 | 54 |
53 /** [TokenKind] representing integer tokens. */ | 55 /** [TokenKind] representing integer tokens. */ |
54 static const int INTEGER = 60; // TODO(terry): must match base | 56 static const int INTEGER = 60; |
55 | 57 |
56 /** [TokenKind] representing hex integer tokens. */ | 58 /** [TokenKind] representing hex integer tokens. */ |
57 // static const int HEX_INTEGER = 61; // TODO(terry): must match bas
e | 59 static const int HEX_INTEGER = 61; |
58 | 60 |
59 /** [TokenKind] representing double tokens. */ | 61 /** [TokenKind] representing double tokens. */ |
60 static const int DOUBLE = 62; // TODO(terry): must match base | 62 static const int DOUBLE = 62; |
61 | 63 |
62 /** [TokenKind] representing whitespace tokens. */ | 64 /** [TokenKind] representing whitespace tokens. */ |
63 static const int WHITESPACE = 63; // TODO(terry): must match base | 65 static const int WHITESPACE = 63; |
64 | 66 |
65 /** [TokenKind] representing comment tokens. */ | 67 /** [TokenKind] representing comment tokens. */ |
66 static const int COMMENT = 64; // TODO(terry): must match base | 68 static const int COMMENT = 64; |
67 | 69 |
68 /** [TokenKind] representing error tokens. */ | 70 /** [TokenKind] representing error tokens. */ |
69 static const int ERROR = 65; // TODO(terry): must match base | 71 static const int ERROR = 65; |
70 | 72 |
71 /** [TokenKind] representing incomplete string tokens. */ | 73 /** [TokenKind] representing incomplete string tokens. */ |
72 static const int INCOMPLETE_STRING = 66; // TODO(terry): must match base | 74 static const int INCOMPLETE_STRING = 66; |
73 | 75 |
74 /** [TokenKind] representing incomplete comment tokens. */ | 76 /** [TokenKind] representing incomplete comment tokens. */ |
75 static const int INCOMPLETE_COMMENT = 67; // TODO(terry): must match base | 77 static const int INCOMPLETE_COMMENT = 67; |
| 78 |
| 79 static const int VAR_DEFINITION = 400; // var-NNN-NNN |
| 80 static const int VAR_USAGE = 401; // var(NNN-NNN [,default]) |
76 | 81 |
77 // Synthesized Tokens (no character associated with TOKEN). | 82 // Synthesized Tokens (no character associated with TOKEN). |
78 // TODO(terry): Possible common names used by both Dart and CSS tokenizers. | |
79 static const int STRING = 500; | 83 static const int STRING = 500; |
80 static const int STRING_PART = 501; | 84 static const int STRING_PART = 501; |
81 static const int NUMBER = 502; | 85 static const int NUMBER = 502; |
82 static const int HEX_NUMBER = 503; | 86 static const int HEX_NUMBER = 503; |
83 static const int HTML_COMMENT = 504; // <!-- | 87 static const int HTML_COMMENT = 504; // <!-- |
84 static const int IMPORTANT = 505; // !important | 88 static const int IMPORTANT = 505; // !important |
| 89 static const int CDATA_START = 506; // <![CDATA[ |
| 90 static const int CDATA_END = 507; // ]]> |
| 91 // U+uNumber[-U+uNumber] |
| 92 // uNumber = 0..10FFFF | ?[?]* |
| 93 static const int UNICODE_RANGE = 508; |
| 94 static const int HEX_RANGE = 509; // ? in the hex range |
85 static const int IDENTIFIER = 511; | 95 static const int IDENTIFIER = 511; |
86 | 96 |
87 // Uniquely synthesized tokens for CSS. | 97 // Uniquely synthesized tokens for CSS. |
88 static const int SELECTOR_EXPRESSION = 512; | 98 static const int SELECTOR_EXPRESSION = 512; |
89 static const int COMBINATOR_NONE = 513; | 99 static const int COMBINATOR_NONE = 513; |
90 static const int COMBINATOR_DESCENDANT = 514; // Space combinator | 100 static const int COMBINATOR_DESCENDANT = 514; // Space combinator |
91 static const int COMBINATOR_PLUS = 515; // + combinator | 101 static const int COMBINATOR_PLUS = 515; // + combinator |
92 static const int COMBINATOR_GREATER = 516; // > combinator | 102 static const int COMBINATOR_GREATER = 516; // > combinator |
93 static const int COMBINATOR_TILDE = 517; // ~ combinator | 103 static const int COMBINATOR_TILDE = 517; // ~ combinator |
94 | 104 |
(...skipping 12 matching lines...) Expand all Loading... |
107 static const int UNIT_EX = 601; | 117 static const int UNIT_EX = 601; |
108 static const int UNIT_LENGTH_PX = 602; | 118 static const int UNIT_LENGTH_PX = 602; |
109 static const int UNIT_LENGTH_CM = 603; | 119 static const int UNIT_LENGTH_CM = 603; |
110 static const int UNIT_LENGTH_MM = 604; | 120 static const int UNIT_LENGTH_MM = 604; |
111 static const int UNIT_LENGTH_IN = 605; | 121 static const int UNIT_LENGTH_IN = 605; |
112 static const int UNIT_LENGTH_PT = 606; | 122 static const int UNIT_LENGTH_PT = 606; |
113 static const int UNIT_LENGTH_PC = 607; | 123 static const int UNIT_LENGTH_PC = 607; |
114 static const int UNIT_ANGLE_DEG = 608; | 124 static const int UNIT_ANGLE_DEG = 608; |
115 static const int UNIT_ANGLE_RAD = 609; | 125 static const int UNIT_ANGLE_RAD = 609; |
116 static const int UNIT_ANGLE_GRAD = 610; | 126 static const int UNIT_ANGLE_GRAD = 610; |
117 static const int UNIT_TIME_MS = 611; | 127 static const int UNIT_ANGLE_TURN = 611; |
118 static const int UNIT_TIME_S = 612; | 128 static const int UNIT_TIME_MS = 612; |
119 static const int UNIT_FREQ_HZ = 613; | 129 static const int UNIT_TIME_S = 613; |
120 static const int UNIT_FREQ_KHZ = 614; | 130 static const int UNIT_FREQ_HZ = 614; |
121 static const int UNIT_PERCENT = 615; | 131 static const int UNIT_FREQ_KHZ = 615; |
122 static const int UNIT_FRACTION = 616; | 132 static const int UNIT_PERCENT = 616; |
| 133 static const int UNIT_FRACTION = 617; |
| 134 static const int UNIT_RESOLUTION_DPI = 618; |
| 135 static const int UNIT_RESOLUTION_DPCM = 619; |
| 136 static const int UNIT_RESOLUTION_DPPX = 620; |
| 137 static const int UNIT_CH = 621; // Measure of "0" U+0030 glyph. |
| 138 static const int UNIT_REM = 622; // computed value ‘font-size’ on root elem. |
| 139 static const int UNIT_VIEWPORT_VW = 623; |
| 140 static const int UNIT_VIEWPORT_VH = 624; |
| 141 static const int UNIT_VIEWPORT_VMIN = 625; |
| 142 static const int UNIT_VIEWPORT_VMAX = 626; |
123 | 143 |
124 // Directives (@nnnn) | 144 // Directives (@nnnn) |
125 static const int DIRECTIVE_NONE = 650; | 145 static const int DIRECTIVE_NONE = 650; |
126 static const int DIRECTIVE_IMPORT = 651; | 146 static const int DIRECTIVE_IMPORT = 651; |
127 static const int DIRECTIVE_MEDIA = 652; | 147 static const int DIRECTIVE_MEDIA = 652; |
128 static const int DIRECTIVE_PAGE = 653; | 148 static const int DIRECTIVE_PAGE = 653; |
129 static const int DIRECTIVE_INCLUDE = 654; | 149 static const int DIRECTIVE_CHARSET = 654; |
130 static const int DIRECTIVE_STYLET = 655; | 150 static const int DIRECTIVE_STYLET = 655; |
131 static const int DIRECTIVE_KEYFRAMES = 656; | 151 static const int DIRECTIVE_KEYFRAMES = 656; |
132 static const int DIRECTIVE_FONTFACE = 657; | 152 static const int DIRECTIVE_WEB_KIT_KEYFRAMES = 657; |
| 153 static const int DIRECTIVE_MOZ_KEYFRAMES = 658; |
| 154 static const int DIRECTIVE_MS_KEYFRAMES = 659; |
| 155 static const int DIRECTIVE_O_KEYFRAMES = 660; |
| 156 static const int DIRECTIVE_FONTFACE = 661; |
| 157 static const int DIRECTIVE_NAMESPACE = 662; |
| 158 static const int DIRECTIVE_HOST = 663; |
| 159 |
| 160 // Media query operators |
| 161 static const int MEDIA_OP_ONLY = 665; // Unary. |
| 162 static const int MEDIA_OP_NOT = 666; // Unary. |
| 163 static const int MEDIA_OP_AND = 667; // Binary. |
| 164 |
| 165 // Directives inside of a @page (margin sym). |
| 166 static const int MARGIN_DIRECTIVE_TOPLEFTCORNER = 670; |
| 167 static const int MARGIN_DIRECTIVE_TOPLEFT = 671; |
| 168 static const int MARGIN_DIRECTIVE_TOPCENTER = 672; |
| 169 static const int MARGIN_DIRECTIVE_TOPRIGHT = 673; |
| 170 static const int MARGIN_DIRECTIVE_TOPRIGHTCORNER = 674; |
| 171 static const int MARGIN_DIRECTIVE_BOTTOMLEFTCORNER = 675; |
| 172 static const int MARGIN_DIRECTIVE_BOTTOMLEFT = 676; |
| 173 static const int MARGIN_DIRECTIVE_BOTTOMCENTER = 677; |
| 174 static const int MARGIN_DIRECTIVE_BOTTOMRIGHT = 678; |
| 175 static const int MARGIN_DIRECTIVE_BOTTOMRIGHTCORNER = 679; |
| 176 static const int MARGIN_DIRECTIVE_LEFTTOP = 680; |
| 177 static const int MARGIN_DIRECTIVE_LEFTMIDDLE = 681; |
| 178 static const int MARGIN_DIRECTIVE_LEFTBOTTOM = 682; |
| 179 static const int MARGIN_DIRECTIVE_RIGHTTOP = 683; |
| 180 static const int MARGIN_DIRECTIVE_RIGHTMIDDLE = 684; |
| 181 static const int MARGIN_DIRECTIVE_RIGHTBOTTOM = 685; |
133 | 182 |
134 // Simple selector type. | 183 // Simple selector type. |
135 static const int CLASS_NAME = 700; // .class | 184 static const int CLASS_NAME = 700; // .class |
136 static const int ELEMENT_NAME = 701; // tagName | 185 static const int ELEMENT_NAME = 701; // tagName |
137 static const int HASH_NAME = 702; // #elementId | 186 static const int HASH_NAME = 702; // #elementId |
138 static const int ATTRIBUTE_NAME = 703; // [attrib] | 187 static const int ATTRIBUTE_NAME = 703; // [attrib] |
139 static const int PSEUDO_ELEMENT_NAME = 704; // ::pseudoElement | 188 static const int PSEUDO_ELEMENT_NAME = 704; // ::pseudoElement |
140 static const int PSEUDO_CLASS_NAME = 705; // :pseudoClass | 189 static const int PSEUDO_CLASS_NAME = 705; // :pseudoClass |
141 static const int NEGATION = 706; // NOT | 190 static const int NEGATION = 706; // NOT |
142 | 191 |
143 static const List<Map<int, String>> _DIRECTIVES = const [ | 192 static const List<Map<int, String>> _DIRECTIVES = const [ |
144 const {'type': TokenKind.DIRECTIVE_IMPORT, 'value' : 'import'}, | 193 const {'type': TokenKind.DIRECTIVE_IMPORT, 'value' : 'import'}, |
145 const {'type': TokenKind.DIRECTIVE_MEDIA, 'value' : 'media'}, | 194 const {'type': TokenKind.DIRECTIVE_MEDIA, 'value' : 'media'}, |
146 const {'type': TokenKind.DIRECTIVE_PAGE, 'value' : 'page'}, | 195 const {'type': TokenKind.DIRECTIVE_PAGE, 'value' : 'page'}, |
147 const {'type': TokenKind.DIRECTIVE_INCLUDE, 'value' : 'include'}, | 196 const {'type': TokenKind.DIRECTIVE_CHARSET, 'value' : 'charset'}, |
148 const {'type': TokenKind.DIRECTIVE_STYLET, 'value' : 'stylet'}, | 197 const {'type': TokenKind.DIRECTIVE_STYLET, 'value' : 'stylet'}, |
149 const {'type': TokenKind.DIRECTIVE_KEYFRAMES, 'value' : '-webkit-keyframes'}
, | 198 const {'type': TokenKind.DIRECTIVE_KEYFRAMES, 'value' : 'keyframes'}, |
| 199 const {'type': TokenKind.DIRECTIVE_WEB_KIT_KEYFRAMES, |
| 200 'value' : '-webkit-keyframes'}, |
| 201 const {'type': TokenKind.DIRECTIVE_MOZ_KEYFRAMES, |
| 202 'value' : '-moz-keyframes'}, |
| 203 const {'type': TokenKind.DIRECTIVE_MS_KEYFRAMES, 'value' : '-ms-keyframes'}, |
| 204 const {'type': TokenKind.DIRECTIVE_O_KEYFRAMES, 'value' : '-o-keyframes'}, |
150 const {'type': TokenKind.DIRECTIVE_FONTFACE, 'value' : 'font-face'}, | 205 const {'type': TokenKind.DIRECTIVE_FONTFACE, 'value' : 'font-face'}, |
| 206 const {'type': TokenKind.DIRECTIVE_NAMESPACE, 'value' : 'namespace'}, |
| 207 const {'type': TokenKind.DIRECTIVE_HOST, 'value' : 'host'}, |
151 ]; | 208 ]; |
152 | 209 |
153 static const List<Map<int, String>> _UNITS = const [ | 210 static const List<Map<int, String>> MEDIA_OPERATORS = const [ |
| 211 const {'type': TokenKind.MEDIA_OP_ONLY, 'value' : 'only'}, |
| 212 const {'type': TokenKind.MEDIA_OP_NOT, 'value' : 'not'}, |
| 213 const {'type': TokenKind.MEDIA_OP_AND, 'value' : 'and'}, |
| 214 ]; |
| 215 |
| 216 static const List<Map<int, String>> MARGIN_DIRECTIVES = const [ |
| 217 const {'type': TokenKind.MARGIN_DIRECTIVE_TOPLEFTCORNER, |
| 218 'value' : 'top-left-corner'}, |
| 219 const {'type': TokenKind.MARGIN_DIRECTIVE_TOPLEFT, |
| 220 'value' : 'top-left'}, |
| 221 const {'type': TokenKind.MARGIN_DIRECTIVE_TOPCENTER, |
| 222 'value' : 'top-center'}, |
| 223 const {'type': TokenKind.MARGIN_DIRECTIVE_TOPRIGHT, |
| 224 'value' : 'top-right'}, |
| 225 const {'type': TokenKind.MARGIN_DIRECTIVE_TOPRIGHTCORNER, |
| 226 'value' : 'top-right-corner'}, |
| 227 const {'type': TokenKind.MARGIN_DIRECTIVE_BOTTOMLEFTCORNER, |
| 228 'value' : 'bottom-left-corner'}, |
| 229 const {'type': TokenKind.MARGIN_DIRECTIVE_BOTTOMLEFT, |
| 230 'value' : 'bottom-left'}, |
| 231 const {'type': TokenKind.MARGIN_DIRECTIVE_BOTTOMCENTER, |
| 232 'value' : 'bottom-center'}, |
| 233 const {'type': TokenKind.MARGIN_DIRECTIVE_BOTTOMRIGHT, |
| 234 'value' : 'bottom-right'}, |
| 235 const {'type': TokenKind.MARGIN_DIRECTIVE_BOTTOMRIGHTCORNER, |
| 236 'value' : 'bottom-right-corner'}, |
| 237 const {'type': TokenKind.MARGIN_DIRECTIVE_LEFTTOP, |
| 238 'value' : 'left-top'}, |
| 239 const {'type': TokenKind.MARGIN_DIRECTIVE_LEFTMIDDLE, |
| 240 'value' : 'left-middle'}, |
| 241 const {'type': TokenKind.MARGIN_DIRECTIVE_LEFTBOTTOM, |
| 242 'value' : 'right-bottom'}, |
| 243 const {'type': TokenKind.MARGIN_DIRECTIVE_RIGHTTOP, |
| 244 'value' : 'right-top'}, |
| 245 const {'type': TokenKind.MARGIN_DIRECTIVE_RIGHTMIDDLE, |
| 246 'value' : 'right-middle'}, |
| 247 const {'type': TokenKind.MARGIN_DIRECTIVE_RIGHTBOTTOM, |
| 248 'value' : 'right-bottom'}, |
| 249 ]; |
| 250 |
| 251 static const List<Map> _UNITS = const [ |
154 const {'unit': TokenKind.UNIT_EM, 'value' : 'em'}, | 252 const {'unit': TokenKind.UNIT_EM, 'value' : 'em'}, |
155 const {'unit': TokenKind.UNIT_EX, 'value' : 'ex'}, | 253 const {'unit': TokenKind.UNIT_EX, 'value' : 'ex'}, |
156 const {'unit': TokenKind.UNIT_LENGTH_PX, 'value' : 'px'}, | 254 const {'unit': TokenKind.UNIT_LENGTH_PX, 'value' : 'px'}, |
157 const {'unit': TokenKind.UNIT_LENGTH_CM, 'value' : 'cm'}, | 255 const {'unit': TokenKind.UNIT_LENGTH_CM, 'value' : 'cm'}, |
158 const {'unit': TokenKind.UNIT_LENGTH_MM, 'value' : 'mm'}, | 256 const {'unit': TokenKind.UNIT_LENGTH_MM, 'value' : 'mm'}, |
159 const {'unit': TokenKind.UNIT_LENGTH_IN, 'value' : 'in'}, | 257 const {'unit': TokenKind.UNIT_LENGTH_IN, 'value' : 'in'}, |
160 const {'unit': TokenKind.UNIT_LENGTH_PT, 'value' : 'pt'}, | 258 const {'unit': TokenKind.UNIT_LENGTH_PT, 'value' : 'pt'}, |
161 const {'unit': TokenKind.UNIT_LENGTH_PC, 'value' : 'pc'}, | 259 const {'unit': TokenKind.UNIT_LENGTH_PC, 'value' : 'pc'}, |
162 const {'unit': TokenKind.UNIT_ANGLE_DEG, 'value' : 'deg'}, | 260 const {'unit': TokenKind.UNIT_ANGLE_DEG, 'value' : 'deg'}, |
163 const {'unit': TokenKind.UNIT_ANGLE_RAD, 'value' : 'rad'}, | 261 const {'unit': TokenKind.UNIT_ANGLE_RAD, 'value' : 'rad'}, |
164 const {'unit': TokenKind.UNIT_ANGLE_GRAD, 'value' : 'grad'}, | 262 const {'unit': TokenKind.UNIT_ANGLE_GRAD, 'value' : 'grad'}, |
| 263 const {'unit': TokenKind.UNIT_ANGLE_TURN, 'value' : 'turn'}, |
165 const {'unit': TokenKind.UNIT_TIME_MS, 'value' : 'ms'}, | 264 const {'unit': TokenKind.UNIT_TIME_MS, 'value' : 'ms'}, |
166 const {'unit': TokenKind.UNIT_TIME_S, 'value' : 's'}, | 265 const {'unit': TokenKind.UNIT_TIME_S, 'value' : 's'}, |
167 const {'unit': TokenKind.UNIT_FREQ_HZ, 'value' : 'hz'}, | 266 const {'unit': TokenKind.UNIT_FREQ_HZ, 'value' : 'hz'}, |
168 const {'unit': TokenKind.UNIT_FREQ_KHZ, 'value' : 'khz'}, | 267 const {'unit': TokenKind.UNIT_FREQ_KHZ, 'value' : 'khz'}, |
169 const {'unit': TokenKind.UNIT_FRACTION, 'value' : 'fr'}, | 268 const {'unit': TokenKind.UNIT_FRACTION, 'value' : 'fr'}, |
170 ]; | 269 const {'unit': TokenKind.UNIT_RESOLUTION_DPI, 'value' : 'dpi'}, |
| 270 const {'unit': TokenKind.UNIT_RESOLUTION_DPCM, 'value' : 'dpcm'}, |
| 271 const {'unit': TokenKind.UNIT_RESOLUTION_DPPX, 'value' : 'dppx'}, |
| 272 const {'unit': TokenKind.UNIT_CH, 'value' : 'ch'}, |
| 273 const {'unit': TokenKind.UNIT_REM, 'value' : 'rem'}, |
| 274 const {'unit': TokenKind.UNIT_VIEWPORT_VW, 'value' : 'vw'}, |
| 275 const {'unit': TokenKind.UNIT_VIEWPORT_VH, 'value' : 'vh'}, |
| 276 const {'unit': TokenKind.UNIT_VIEWPORT_VMIN, 'value' : 'vmin'}, |
| 277 const {'unit': TokenKind.UNIT_VIEWPORT_VMAX, 'value' : 'vmax'}, |
| 278 ]; |
171 | 279 |
172 // Some more constants: | 280 // Some more constants: |
173 static const int ASCII_UPPER_A = 65; // ASCII value for uppercase A | 281 static const int ASCII_UPPER_A = 65; // ASCII value for uppercase A |
174 static const int ASCII_UPPER_Z = 90; // ASCII value for uppercase Z | 282 static const int ASCII_UPPER_Z = 90; // ASCII value for uppercase Z |
175 | 283 |
176 // Extended color keywords: | 284 // Extended color keywords: |
177 static const List<Map<String, int>> _EXTENDED_COLOR_NAMES = const [ | 285 static const List<Map> _EXTENDED_COLOR_NAMES = const [ |
178 const {'name' : 'aliceblue', 'value' : 0xF08FF}, | 286 const {'name' : 'aliceblue', 'value' : 0xF08FF}, |
179 const {'name' : 'antiquewhite', 'value' : 0xFAEBD7}, | 287 const {'name' : 'antiquewhite', 'value' : 0xFAEBD7}, |
180 const {'name' : 'aqua', 'value' : 0x00FFFF}, | 288 const {'name' : 'aqua', 'value' : 0x00FFFF}, |
181 const {'name' : 'aquamarine', 'value' : 0x7FFFD4}, | 289 const {'name' : 'aquamarine', 'value' : 0x7FFFD4}, |
182 const {'name' : 'azure', 'value' : 0xF0FFFF}, | 290 const {'name' : 'azure', 'value' : 0xF0FFFF}, |
183 const {'name' : 'beige', 'value' : 0xF5F5DC}, | 291 const {'name' : 'beige', 'value' : 0xF5F5DC}, |
184 const {'name' : 'bisque', 'value' : 0xFFE4C4}, | 292 const {'name' : 'bisque', 'value' : 0xFFE4C4}, |
185 const {'name' : 'black', 'value' : 0x000000}, | 293 const {'name' : 'black', 'value' : 0x000000}, |
186 const {'name' : 'blanchedalmond', 'value' : 0xFFEBCD}, | 294 const {'name' : 'blanchedalmond', 'value' : 0xFFEBCD}, |
187 const {'name' : 'blue', 'value' : 0x0000FF}, | 295 const {'name' : 'blue', 'value' : 0x0000FF}, |
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
363 const {'name' : 'scale3d', 'info' : const {'params' : 3, 'expr' : false}}, | 471 const {'name' : 'scale3d', 'info' : const {'params' : 3, 'expr' : false}}, |
364 const {'name' : 'scaleZ', 'info' : const {'params' : 1, 'expr' : false}}, | 472 const {'name' : 'scaleZ', 'info' : const {'params' : 1, 'expr' : false}}, |
365 const {'name' : 'rotate3d', 'info' : const {'params' : 3, 'expr' : false}}, | 473 const {'name' : 'rotate3d', 'info' : const {'params' : 3, 'expr' : false}}, |
366 const {'name' : 'rotateX', 'info' : const {'params' : 1, 'expr' : false}}, | 474 const {'name' : 'rotateX', 'info' : const {'params' : 1, 'expr' : false}}, |
367 const {'name' : 'rotateY', 'info' : const {'params' : 1, 'expr' : false}}, | 475 const {'name' : 'rotateY', 'info' : const {'params' : 1, 'expr' : false}}, |
368 const {'name' : 'rotateZ', 'info' : const {'params' : 1, 'expr' : false}}, | 476 const {'name' : 'rotateZ', 'info' : const {'params' : 1, 'expr' : false}}, |
369 const {'name' : 'perspective', | 477 const {'name' : 'perspective', |
370 'info' : const {'params' : 1, 'expr' : false}}, | 478 'info' : const {'params' : 1, 'expr' : false}}, |
371 ]; | 479 ]; |
372 | 480 |
373 List<int> tokens; | 481 /** |
| 482 * Check if name is a pre-defined CSS name. Used by error handler to report |
| 483 * if name is unknown or used improperly. |
| 484 */ |
| 485 static bool isPredefinedName(String name) { |
| 486 var nameLen = name.length; |
| 487 // TODO(terry): Add more pre-defined names (hidden, bolder, inherit, etc.). |
| 488 if (matchUnits(name, 0, nameLen) == -1 || |
| 489 matchDirectives(name, 0, nameLen) == -1 || |
| 490 matchMarginDirectives(name, 0, nameLen) == -1 || |
| 491 matchColorName(name) == null) { |
| 492 return false; |
| 493 } |
374 | 494 |
375 /* | 495 return true; |
376 * Return the token that matches the unit ident found. | 496 } |
377 */ | 497 |
| 498 /** Return the token that matches the unit ident found. */ |
378 static int matchList(var identList, String tokenField, String text, | 499 static int matchList(var identList, String tokenField, String text, |
379 int offset, int length) { | 500 int offset, int length) { |
380 for (final entry in identList) { | 501 for (final entry in identList) { |
381 String ident = entry['value']; | 502 String ident = entry['value']; |
| 503 |
382 if (length == ident.length) { | 504 if (length == ident.length) { |
383 int idx = offset; | 505 int idx = offset; |
384 bool match = true; | 506 bool match = true; |
385 for (int identIdx = 0; identIdx < ident.length; identIdx++) { | 507 for (int i = 0; i < ident.length; i++) { |
386 int identChar = ident.codeUnitAt(identIdx); | 508 int identChar = ident.codeUnitAt(i); |
387 int char = text.codeUnitAt(idx++); | 509 int char = text.codeUnitAt(idx++); |
388 // Compare lowercase to lowercase then check if char is uppercase. | 510 // Compare lowercase to lowercase then check if char is uppercase. |
389 match = match && (char == identChar || | 511 match = match && (char == identChar || |
390 ((char >= ASCII_UPPER_A && char <= ASCII_UPPER_Z) && | 512 ((char >= ASCII_UPPER_A && char <= ASCII_UPPER_Z) && |
391 (char + 32) == identChar)); | 513 (char + 32) == identChar)); |
392 if (!match) { | 514 if (!match) { |
393 break; | 515 break; |
394 } | 516 } |
395 } | 517 } |
396 | 518 |
397 if (match) { | 519 if (match) { |
398 // Completely matched; return the token for this unit. | 520 // Completely matched; return the token for this unit. |
399 return entry[tokenField]; | 521 return entry[tokenField]; |
400 } | 522 } |
401 } | 523 } |
402 } | 524 } |
403 | 525 |
404 return -1; // Not a unit token. | 526 return -1; // Not a unit token. |
405 } | 527 } |
406 | 528 |
407 /* | 529 /** Return the token that matches the unit ident found. */ |
408 * Return the token that matches the unit ident found. | |
409 */ | |
410 static int matchUnits(String text, int offset, int length) { | 530 static int matchUnits(String text, int offset, int length) { |
411 return matchList(_UNITS, 'unit', text, offset, length); | 531 return matchList(_UNITS, 'unit', text, offset, length); |
412 } | 532 } |
413 | 533 |
414 /* | 534 /** Return the token that matches the directive name found. */ |
415 * Return the token that matches the directive ident found. | |
416 */ | |
417 static int matchDirectives(String text, int offset, int length) { | 535 static int matchDirectives(String text, int offset, int length) { |
418 return matchList(_DIRECTIVES, 'type', text, offset, length); | 536 return matchList(_DIRECTIVES, 'type', text, offset, length); |
419 } | 537 } |
420 | 538 |
421 /* | 539 /** Return the token that matches the margin directive name found. */ |
422 * Return the unit token as its pretty name. | 540 static int matchMarginDirectives(String text, int offset, int length) { |
423 */ | 541 return matchList(MARGIN_DIRECTIVES, 'type', text, offset, length); |
| 542 } |
| 543 |
| 544 /** Return the token that matches the media operator found. */ |
| 545 static int matchMediaOperator(String text, int offset, int length) { |
| 546 return matchList(MEDIA_OPERATORS, 'type', text, offset, length); |
| 547 } |
| 548 |
| 549 static String idToValue(var identList, int tokenId) { |
| 550 for (var entry in identList) { |
| 551 if (tokenId == entry['type']) { |
| 552 return entry['value']; |
| 553 } |
| 554 } |
| 555 |
| 556 return null; |
| 557 } |
| 558 |
| 559 |
| 560 /** Return the unit token as its pretty name. */ |
424 static String unitToString(int unitTokenToFind) { | 561 static String unitToString(int unitTokenToFind) { |
425 if (unitTokenToFind == TokenKind.PERCENT) { | 562 if (unitTokenToFind == TokenKind.PERCENT) { |
426 return '%'; | 563 return '%'; |
427 } else { | 564 } else { |
428 for (final entry in _UNITS) { | 565 for (final entry in _UNITS) { |
429 int unit = entry['unit']; | 566 int unit = entry['unit']; |
430 if (unit == unitTokenToFind) { | 567 if (unit == unitTokenToFind) { |
431 return entry['value']; | 568 return entry['value']; |
432 } | 569 } |
433 } | 570 } |
434 } | 571 } |
435 | 572 |
436 return '<BAD UNIT>'; // Not a unit token. | 573 return '<BAD UNIT>'; // Not a unit token. |
437 } | 574 } |
438 | 575 |
439 /* | 576 /** |
440 * Match color name, case insensitive match and return the associated RGB | 577 * Match color name, case insensitive match and return the associated color |
441 * value as decimal number. | 578 * entry from _EXTENDED_COLOR_NAMES list, return [null] if not found. |
442 */ | 579 */ |
443 static int matchColorName(String text) { | 580 static Map matchColorName(String text) { |
444 int length = text.length; | 581 var name = text.toLowerCase(); |
| 582 return _EXTENDED_COLOR_NAMES. |
| 583 firstWhere((e) => e['name'] == name, orElse: () => null); |
| 584 } |
| 585 |
| 586 /** Return RGB value as [int] from a color entry in _EXTENDED_COLOR_NAMES. */ |
| 587 static int colorValue(Map entry) { |
| 588 assert(entry != null); |
| 589 return entry['value']; |
| 590 } |
| 591 |
| 592 static String hexToColorName(hexValue) { |
445 for (final entry in _EXTENDED_COLOR_NAMES) { | 593 for (final entry in _EXTENDED_COLOR_NAMES) { |
446 String ident = entry['name']; | 594 if (entry['value'] == hexValue) { |
447 if (length == ident.length) { | 595 return entry['name']; |
448 int idx = 0; | |
449 bool match = true; | |
450 for (int identIdx = 0; identIdx < ident.length; identIdx++) { | |
451 int identChar = ident.codeUnitAt(identIdx); | |
452 int char = text.codeUnitAt(idx++); | |
453 // Compare lowercase to lowercase then check if char is uppercase. | |
454 match = match && (char == identChar || | |
455 ((char >= ASCII_UPPER_A && char <= ASCII_UPPER_Z) && | |
456 (char + 32) == identChar)); | |
457 if (!match) { | |
458 break; | |
459 } | |
460 } | |
461 | |
462 if (match) { | |
463 // Completely matched; return the token for this unit. | |
464 return entry['value']; | |
465 } | |
466 } | 596 } |
467 } | 597 } |
468 | 598 |
469 // No match. | 599 return null; |
470 throw new NoColorMatchException(text); | |
471 } | 600 } |
472 | 601 |
473 static String decimalToHex(int num, [int minDigits = 1]) { | 602 static String decimalToHex(int number, [int minDigits = 1]) { |
474 final String _HEX_DIGITS = '0123456789abcdef'; | 603 final String _HEX_DIGITS = '0123456789abcdef'; |
475 | 604 |
476 List<String> result = new List<String>(); | 605 List<String> result = new List<String>(); |
477 | 606 |
478 int dividend = num >> 4; | 607 int dividend = number >> 4; |
479 int remain = num % 16; | 608 int remain = number % 16; |
480 result.add(_HEX_DIGITS[remain]); | 609 result.add(_HEX_DIGITS[remain]); |
481 while (dividend != 0) { | 610 while (dividend != 0) { |
482 remain = dividend % 16; | 611 remain = dividend % 16; |
483 dividend >>= 4; | 612 dividend >>= 4; |
484 result.add(_HEX_DIGITS[remain]); | 613 result.add(_HEX_DIGITS[remain]); |
485 } | 614 } |
486 | 615 |
487 StringBuffer invertResult = new StringBuffer(); | 616 StringBuffer invertResult = new StringBuffer(); |
488 int paddings = minDigits - result.length; | 617 int paddings = minDigits - result.length; |
489 while (paddings-- > 0) { | 618 while (paddings-- > 0) { |
490 invertResult.write('0'); | 619 invertResult.write('0'); |
491 } | 620 } |
492 for (int idx = result.length - 1; idx >= 0; idx--) { | 621 for (int i = result.length - 1; i >= 0; i--) { |
493 invertResult.write(result[idx]); | 622 invertResult.write(result[i]); |
494 } | 623 } |
495 | 624 |
496 return invertResult.toString(); | 625 return invertResult.toString(); |
497 } | 626 } |
498 | 627 |
499 static String kindToString(int kind) { | 628 static String kindToString(int kind) { |
500 switch(kind) { | 629 switch(kind) { |
501 case TokenKind.UNUSED: return "ERROR"; | 630 case TokenKind.UNUSED: return "ERROR"; |
502 case TokenKind.END_OF_FILE: return "end of file"; | 631 case TokenKind.END_OF_FILE: return "end of file"; |
503 case TokenKind.LPAREN: return "("; | 632 case TokenKind.LPAREN: return "("; |
(...skipping 22 matching lines...) Expand all Loading... |
526 case TokenKind.SINGLE_QUOTE: return "'"; | 655 case TokenKind.SINGLE_QUOTE: return "'"; |
527 case TokenKind.DOUBLE_QUOTE: return "\""; | 656 case TokenKind.DOUBLE_QUOTE: return "\""; |
528 case TokenKind.SLASH: return "/"; | 657 case TokenKind.SLASH: return "/"; |
529 case TokenKind.EQUALS: return '='; | 658 case TokenKind.EQUALS: return '='; |
530 case TokenKind.OR: return '|'; | 659 case TokenKind.OR: return '|'; |
531 case TokenKind.CARET: return '^'; | 660 case TokenKind.CARET: return '^'; |
532 case TokenKind.DOLLAR: return '\$'; | 661 case TokenKind.DOLLAR: return '\$'; |
533 case TokenKind.LESS: return '<'; | 662 case TokenKind.LESS: return '<'; |
534 case TokenKind.BANG: return '!'; | 663 case TokenKind.BANG: return '!'; |
535 case TokenKind.MINUS: return '-'; | 664 case TokenKind.MINUS: return '-'; |
536 | 665 case TokenKind.BACKSLASH: return '\\'; |
537 default: | 666 default: |
538 throw "Unknown TOKEN"; | 667 throw "Unknown TOKEN"; |
539 } | 668 } |
540 } | 669 } |
541 | 670 |
542 TokenKind() { | 671 static bool isKindIdentifier(int kind) { |
543 tokens = []; | 672 switch(kind) { |
544 | 673 // Synthesized tokens. |
545 // All tokens must be in TokenKind order. | 674 case TokenKind.DIRECTIVE_IMPORT: |
546 tokens.add(-1); // TokenKind.UNUSED | 675 case TokenKind.DIRECTIVE_MEDIA: |
547 tokens.add(0); // TokenKind.END_OF_FILE match base | 676 case TokenKind.DIRECTIVE_PAGE: |
548 tokens.add(TokenKind.kindToString(TokenKind.LPAREN).codeUnitAt(0)); | 677 case TokenKind.DIRECTIVE_CHARSET: |
549 tokens.add(TokenKind.kindToString(TokenKind.RPAREN).codeUnitAt(0)); | 678 case TokenKind.DIRECTIVE_STYLET: |
550 tokens.add(TokenKind.kindToString(TokenKind.LBRACK).codeUnitAt(0)); | 679 case TokenKind.DIRECTIVE_KEYFRAMES: |
551 tokens.add(TokenKind.kindToString(TokenKind.RBRACK).codeUnitAt(0)); | 680 case TokenKind.DIRECTIVE_WEB_KIT_KEYFRAMES: |
552 tokens.add(TokenKind.kindToString(TokenKind.LBRACE).codeUnitAt(0)); | 681 case TokenKind.DIRECTIVE_MOZ_KEYFRAMES: |
553 tokens.add(TokenKind.kindToString(TokenKind.RBRACE).codeUnitAt(0)); | 682 case TokenKind.DIRECTIVE_MS_KEYFRAMES: |
554 tokens.add(TokenKind.kindToString(TokenKind.DOT).codeUnitAt(0)); | 683 case TokenKind.DIRECTIVE_O_KEYFRAMES: |
555 tokens.add(TokenKind.kindToString(TokenKind.SEMICOLON).codeUnitAt(0)); | 684 case TokenKind.DIRECTIVE_FONTFACE: |
556 tokens.add(TokenKind.kindToString(TokenKind.AT).codeUnitAt(0)); | 685 case TokenKind.DIRECTIVE_NAMESPACE: |
557 tokens.add(TokenKind.kindToString(TokenKind.HASH).codeUnitAt(0)); | 686 case TokenKind.DIRECTIVE_HOST: |
558 tokens.add(TokenKind.kindToString(TokenKind.PLUS).codeUnitAt(0)); | 687 case TokenKind.UNIT_EM: |
559 tokens.add(TokenKind.kindToString(TokenKind.GREATER).codeUnitAt(0)); | 688 case TokenKind.UNIT_EX: |
560 tokens.add(TokenKind.kindToString(TokenKind.TILDE).codeUnitAt(0)); | 689 case TokenKind.UNIT_LENGTH_PX: |
561 tokens.add(TokenKind.kindToString(TokenKind.ASTERISK).codeUnitAt(0)); | 690 case TokenKind.UNIT_LENGTH_CM: |
562 tokens.add(TokenKind.kindToString(TokenKind.NAMESPACE).codeUnitAt(0)); | 691 case TokenKind.UNIT_LENGTH_MM: |
563 tokens.add(TokenKind.kindToString(TokenKind.COLON).codeUnitAt(0)); | 692 case TokenKind.UNIT_LENGTH_IN: |
564 tokens.add(TokenKind.kindToString(TokenKind.PRIVATE_NAME).codeUnitAt(0)); | 693 case TokenKind.UNIT_LENGTH_PT: |
565 tokens.add(TokenKind.kindToString(TokenKind.COMMA).codeUnitAt(0)); | 694 case TokenKind.UNIT_LENGTH_PC: |
566 tokens.add(TokenKind.kindToString(TokenKind.SPACE).codeUnitAt(0)); | 695 case TokenKind.UNIT_ANGLE_DEG: |
567 tokens.add(TokenKind.kindToString(TokenKind.TAB).codeUnitAt(0)); | 696 case TokenKind.UNIT_ANGLE_RAD: |
568 tokens.add(TokenKind.kindToString(TokenKind.NEWLINE).codeUnitAt(0)); | 697 case TokenKind.UNIT_ANGLE_GRAD: |
569 tokens.add(TokenKind.kindToString(TokenKind.RETURN).codeUnitAt(0)); | 698 case TokenKind.UNIT_TIME_MS: |
570 tokens.add(TokenKind.kindToString(TokenKind.PERCENT).codeUnitAt(0)); | 699 case TokenKind.UNIT_TIME_S: |
571 tokens.add(TokenKind.kindToString(TokenKind.SINGLE_QUOTE).codeUnitAt(0)); | 700 case TokenKind.UNIT_FREQ_HZ: |
572 tokens.add(TokenKind.kindToString(TokenKind.DOUBLE_QUOTE).codeUnitAt(0)); | 701 case TokenKind.UNIT_FREQ_KHZ: |
573 tokens.add(TokenKind.kindToString(TokenKind.SLASH).codeUnitAt(0)); | 702 case TokenKind.UNIT_FRACTION: |
574 tokens.add(TokenKind.kindToString(TokenKind.EQUALS).codeUnitAt(0)); | 703 return true; |
575 tokens.add(TokenKind.kindToString(TokenKind.OR).codeUnitAt(0)); | 704 default: |
576 tokens.add(TokenKind.kindToString(TokenKind.CARET).codeUnitAt(0)); | 705 return false; |
577 tokens.add(TokenKind.kindToString(TokenKind.DOLLAR).codeUnitAt(0)); | 706 } |
578 tokens.add(TokenKind.kindToString(TokenKind.LESS).codeUnitAt(0)); | |
579 tokens.add(TokenKind.kindToString(TokenKind.BANG).codeUnitAt(0)); | |
580 tokens.add(TokenKind.kindToString(TokenKind.MINUS).codeUnitAt(0)); | |
581 | |
582 assert(tokens.length == TokenKind.END_TOKENS); | |
583 } | 707 } |
584 | 708 |
585 static bool isIdentifier(int kind) { | 709 static bool isIdentifier(int kind) { |
586 return kind == IDENTIFIER ; | 710 return kind == IDENTIFIER ; |
587 } | 711 } |
588 | |
589 } | 712 } |
590 | 713 |
591 class NoColorMatchException implements Exception { | 714 // Note: these names should match TokenKind names |
592 String _colorName; | 715 class TokenChar { |
593 NoColorMatchException(this._colorName); | 716 static const int UNUSED = -1; |
594 | 717 static const int END_OF_FILE = 0; |
595 String get name => _colorName; | 718 static const int LPAREN = 0x28; // "(".codeUnitAt(0) |
| 719 static const int RPAREN = 0x29; // ")".codeUnitAt(0) |
| 720 static const int LBRACK = 0x5b; // "[".codeUnitAt(0) |
| 721 static const int RBRACK = 0x5d; // "]".codeUnitAt(0) |
| 722 static const int LBRACE = 0x7b; // "{".codeUnitAt(0) |
| 723 static const int RBRACE = 0x7d; // "}".codeUnitAt(0) |
| 724 static const int DOT = 0x2e; // ".".codeUnitAt(0) |
| 725 static const int SEMICOLON = 0x3b; // ";".codeUnitAt(0) |
| 726 static const int AT = 0x40; // "@".codeUnitAt(0) |
| 727 static const int HASH = 0x23; // "#".codeUnitAt(0) |
| 728 static const int PLUS = 0x2b; // "+".codeUnitAt(0) |
| 729 static const int GREATER = 0x3e; // ">".codeUnitAt(0) |
| 730 static const int TILDE = 0x7e; // "~".codeUnitAt(0) |
| 731 static const int ASTERISK = 0x2a; // "*".codeUnitAt(0) |
| 732 static const int NAMESPACE = 0x7c; // "|".codeUnitAt(0) |
| 733 static const int COLON = 0x3a; // ":".codeUnitAt(0) |
| 734 static const int PRIVATE_NAME = 0x5f; // "_".codeUnitAt(0) |
| 735 static const int COMMA = 0x2c; // ",".codeUnitAt(0) |
| 736 static const int SPACE = 0x20; // " ".codeUnitAt(0) |
| 737 static const int TAB = 0x9; // "\t".codeUnitAt(0) |
| 738 static const int NEWLINE = 0xa; // "\n".codeUnitAt(0) |
| 739 static const int RETURN = 0xd; // "\r".codeUnitAt(0) |
| 740 static const int BACKSPACE = 0x8; // "/b".codeUnitAt(0) |
| 741 static const int FF = 0xc; // "/f".codeUnitAt(0) |
| 742 static const int VT = 0xb; // "/v".codeUnitAt(0) |
| 743 static const int PERCENT = 0x25; // "%".codeUnitAt(0) |
| 744 static const int SINGLE_QUOTE = 0x27; // "'".codeUnitAt(0) |
| 745 static const int DOUBLE_QUOTE = 0x22; // '"'.codeUnitAt(0) |
| 746 static const int SLASH = 0x2f; // "/".codeUnitAt(0) |
| 747 static const int EQUALS = 0x3d; // "=".codeUnitAt(0) |
| 748 static const int OR = 0x7c; // "|".codeUnitAt(0) |
| 749 static const int CARET = 0x5e; // "^".codeUnitAt(0) |
| 750 static const int DOLLAR = 0x24; // "\$".codeUnitAt(0) |
| 751 static const int LESS = 0x3c; // "<".codeUnitAt(0) |
| 752 static const int BANG = 0x21; // "!".codeUnitAt(0) |
| 753 static const int MINUS = 0x2d; // "-".codeUnitAt(0) |
| 754 static const int BACKSLASH = 0x5c; // "\".codeUnitAt(0) |
| 755 static const int AMPERSAND = 0x26; // "&".codeUnitAt(0) |
596 } | 756 } |
OLD | NEW |