9e7b3ff2247b62f8d4ddfc8a2aeed2ba11013afb
[WebKit-https.git] / Source / WebCore / inspector / front-end / SourceCSSTokenizer.re2js
1 /*
2  * Copyright (C) 2009 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 // Generate js file as follows:
32
33 /*
34 re2c -isc Source/WebCore/inspector/front-end/SourceCSSTokenizer.re2js \
35   | sed 's|^yy\([^:]*\)*\:|case \1:|' \
36   | sed 's|[*]cursor[+][+]|this._charAt(cursor++)|' \
37   | sed 's|[[*][+][+]cursor|this._charAt(++cursor)|' \
38   | sed 's|[*]cursor|this._charAt(cursor)|' \
39   | sed 's|yych = \*\([^;]*\)|yych = this._charAt\1|' \
40   | sed 's|goto case \([^;]*\)|{ gotoCase = \1; continue; }|' \
41   | sed 's|yych <= \(0x[0-9a-fA-F]*\)|yych \<\= String.fromCharCode(\1)|' \
42   | sed 's|unsigned\ int|var|' \
43   | sed 's|var\ yych|case 1: var yych|' > Source/WebCore/inspector/front-end/SourceCSSTokenizer.js
44 */
45
46 /**
47  * @constructor
48  * @extends {WebInspector.SourceTokenizer}
49  */
50 WebInspector.SourceCSSTokenizer = function()
51 {
52     WebInspector.SourceTokenizer.call(this);
53
54     this._propertyKeywords = WebInspector.CSSCompletions.cssPropertiesMetainfo.keySet();
55     this._colorKeywords = WebInspector.CSSKeywordCompletions.colors();
56
57     this._valueKeywords = [
58         "above", "absolute", "activeborder", "activecaption", "afar", "after-white-space", "ahead", "alias", "all", "all-scroll",
59         "alternate", "always", "amharic", "amharic-abegede", "antialiased", "appworkspace", "arabic-indic", "armenian", "asterisks",
60         "auto", "avoid", "background", "backwards", "baseline", "below", "bidi-override", "binary", "bengali", "blink",
61         "block", "block-axis", "bold", "bolder", "border", "border-box", "both", "bottom", "break-all", "break-word", "button",
62         "button-bevel", "buttonface", "buttonhighlight", "buttonshadow", "buttontext", "cambodian", "capitalize", "caps-lock-indicator",
63         "caption", "captiontext", "caret", "cell", "center", "checkbox", "circle", "cjk-earthly-branch", "cjk-heavenly-stem", "cjk-ideographic",
64         "clear", "clip", "close-quote", "col-resize", "collapse", "compact", "condensed", "contain", "content", "content-box", "context-menu",
65         "continuous", "copy", "cover", "crop", "cross", "crosshair", "currentcolor", "cursive", "dashed", "decimal", "decimal-leading-zero", "default",
66         "default-button", "destination-atop", "destination-in", "destination-out", "destination-over", "devanagari", "disc", "discard", "document",
67         "dot-dash", "dot-dot-dash", "dotted", "double", "down", "e-resize", "ease", "ease-in", "ease-in-out", "ease-out", "element",
68         "ellipsis", "embed", "end", "ethiopic", "ethiopic-abegede", "ethiopic-abegede-am-et", "ethiopic-abegede-gez",
69         "ethiopic-abegede-ti-er", "ethiopic-abegede-ti-et", "ethiopic-halehame-aa-er", "ethiopic-halehame-aa-et",
70         "ethiopic-halehame-am-et", "ethiopic-halehame-gez", "ethiopic-halehame-om-et", "ethiopic-halehame-sid-et",
71         "ethiopic-halehame-so-et", "ethiopic-halehame-ti-er", "ethiopic-halehame-ti-et", "ethiopic-halehame-tig", "ew-resize", "expanded",
72         "extra-condensed", "extra-expanded", "fantasy", "fast", "fill", "fixed", "flat", "footnotes", "forwards", "from", "geometricPrecision",
73         "georgian", "graytext", "groove", "gujarati", "gurmukhi", "hand", "hangul", "hangul-consonant", "hebrew", "help",
74         "hidden", "hide", "higher", "highlight", "highlighttext", "hiragana", "hiragana-iroha", "horizontal", "hsl", "hsla", "icon", "ignore",
75         "inactiveborder", "inactivecaption", "inactivecaptiontext", "infinite", "infobackground", "infotext", "inherit", "initial", "inline",
76         "inline-axis", "inline-block", "inline-table", "inset", "inside", "intrinsic", "invert", "italic", "justify", "kannada", "katakana",
77         "katakana-iroha", "khmer", "landscape", "lao", "large", "larger", "left", "level", "lighter", "line-through", "linear", "lines",
78         "list-item", "listbox", "listitem", "local", "logical", "loud", "lower", "lower-alpha", "lower-armenian", "lower-greek",
79         "lower-hexadecimal", "lower-latin", "lower-norwegian", "lower-roman", "lowercase", "ltr", "malayalam", "match", "media-controls-background",
80         "media-current-time-display", "media-fullscreen-button", "media-mute-button", "media-play-button", "media-return-to-realtime-button",
81         "media-rewind-button", "media-seek-back-button", "media-seek-forward-button", "media-slider", "media-sliderthumb", "media-time-remaining-display",
82         "media-volume-slider", "media-volume-slider-container", "media-volume-sliderthumb", "medium", "menu", "menulist", "menulist-button",
83         "menulist-text", "menulist-textfield", "menutext", "message-box", "middle", "min-intrinsic", "mix", "mongolian", "monospace", "move", "multiple",
84         "myanmar", "n-resize", "narrower", "navy", "ne-resize", "nesw-resize", "no-close-quote", "no-drop", "no-open-quote", "no-repeat", "none",
85         "normal", "not-allowed", "nowrap", "ns-resize", "nw-resize", "nwse-resize", "oblique", "octal", "open-quote", "optimizeLegibility",
86         "optimizeSpeed", "oriya", "oromo", "outset", "outside", "overlay", "overline", "padding", "padding-box", "painted", "paused",
87         "persian", "plus-darker", "plus-lighter", "pointer", "portrait", "pre", "pre-line", "pre-wrap", "preserve-3d", "progress",
88         "push-button", "radio", "read-only", "read-write", "read-write-plaintext-only", "relative", "repeat", "repeat-x",
89         "repeat-y", "reset", "reverse", "rgb", "rgba", "ridge", "right", "round", "row-resize", "rtl", "run-in", "running", "s-resize", "sans-serif",
90         "scroll", "scrollbar", "se-resize", "searchfield", "searchfield-cancel-button", "searchfield-decoration", "searchfield-results-button",
91         "searchfield-results-decoration", "semi-condensed", "semi-expanded", "separate", "serif", "show", "sidama", "single",
92         "skip-white-space", "slide", "slider-horizontal", "slider-vertical", "sliderthumb-horizontal", "sliderthumb-vertical", "slow",
93         "small", "small-caps", "small-caption", "smaller", "solid", "somali", "source-atop", "source-in", "source-out", "source-over",
94         "space", "square", "square-button", "start", "static", "status-bar", "stretch", "stroke", "sub", "subpixel-antialiased", "super",
95         "sw-resize", "table", "table-caption", "table-cell", "table-column", "table-column-group", "table-footer-group", "table-header-group",
96         "table-row", "table-row-group", "telugu", "text", "text-bottom", "text-top", "textarea", "textfield", "thai", "thick", "thin",
97         "threeddarkshadow", "threedface", "threedhighlight", "threedlightshadow", "threedshadow", "tibetan", "tigre", "tigrinya-er", "tigrinya-er-abegede",
98         "tigrinya-et", "tigrinya-et-abegede", "to", "top", "transparent", "ultra-condensed", "ultra-expanded", "underline", "up", "upper-alpha", "upper-armenian",
99         "upper-greek", "upper-hexadecimal", "upper-latin", "upper-norwegian", "upper-roman", "uppercase", "urdu", "url", "vertical", "vertical-text", "visible",
100         "visibleFill", "visiblePainted", "visibleStroke", "visual", "w-resize", "wait", "wave", "white", "wider", "window", "windowframe", "windowtext",
101         "x-large", "x-small", "xor", "xx-large", "xx-small", "yellow", "-wap-marquee", "-webkit-activelink", "-webkit-auto", "-webkit-baseline-middle",
102         "-webkit-body", "-webkit-box", "-webkit-center", "-webkit-control", "-webkit-focus-ring-color", "-webkit-grab", "-webkit-grabbing",
103         "-webkit-gradient", "-webkit-inline-box", "-webkit-left", "-webkit-link", "-webkit-marquee", "-webkit-mini-control", "-webkit-nowrap", "-webkit-pictograph",
104         "-webkit-right", "-webkit-small-control", "-webkit-text", "-webkit-xxx-large", "-webkit-zoom-in", "-webkit-zoom-out",
105     ].keySet();
106
107     this._mediaTypes = ["all", "aural", "braille", "embossed", "handheld", "import", "print", "projection", "screen", "tty", "tv"].keySet();
108
109     this._lexConditions = {
110         INITIAL: 0,
111         COMMENT: 1,
112         DSTRING: 2,
113         SSTRING: 3
114     };
115
116     this._parseConditions = {
117         INITIAL: 0,
118         PROPERTY: 1,
119         PROPERTY_VALUE: 2,
120         AT_RULE: 3,
121         AT_MEDIA_RULE: 4
122     };
123
124     this.case_INITIAL = 1000;
125     this.case_COMMENT = 1002;
126     this.case_DSTRING = 1003;
127     this.case_SSTRING = 1004;
128
129     this.condition = this.createInitialCondition();
130 }
131
132 WebInspector.SourceCSSTokenizer.prototype = {
133     createInitialCondition: function()
134     {
135         return { lexCondition: this._lexConditions.INITIAL, parseCondition: this._parseConditions.INITIAL };
136     },
137
138     /**
139      * @param {boolean=} stringEnds
140      */
141     _stringToken: function(cursor, stringEnds)
142     {
143         if (this._isPropertyValue())
144             this.tokenType = "css-string";
145         else
146             this.tokenType = null;
147         return cursor;
148     },
149
150     _isPropertyValue: function()
151     {
152         return this._condition.parseCondition === this._parseConditions.PROPERTY_VALUE || this._condition.parseCondition === this._parseConditions.AT_RULE;
153     },
154
155     nextToken: function(cursor)
156     {
157         var cursorOnEnter = cursor;
158         var gotoCase = 1;
159         var YYMARKER;
160         while (1) {
161             switch (gotoCase)
162             // Following comment is replaced with generated state machine.
163             /*!re2c
164                 re2c:define:YYCTYPE  = "var";
165                 re2c:define:YYCURSOR = cursor;
166                 re2c:define:YYGETCONDITION = "this.getLexCondition";
167                 re2c:define:YYSETCONDITION = "this.setLexCondition";
168                 re2c:condprefix = "case this.case_";
169                 re2c:condenumprefix = "this._lexConditions.";
170                 re2c:yyfill:enable = 0;
171                 re2c:labelprefix = "case ";
172                 re2c:indent:top = 2;
173                 re2c:indent:string = "    ";
174
175                 CommentContent = ([^*\r\n] | ("*"+[^/*]))*;
176                 Comment = "/*" CommentContent "*"+ "/";
177                 CommentStart = "/*" CommentContent [\r\n];
178                 CommentEnd = CommentContent "*"+ "/";
179
180                 OpenCurlyBracket = "{";
181                 CloseCurlyBracket = "}";
182
183                 Colon = ":";
184                 Semicolon = ";";
185
186                 NumericLiteral = "-"? ([0-9]+ | [0-9]* "." [0-9]+)  ("em" | "rem" | "__qem" | "ex" | "px" | "cm" |
187                     "mm" | "in" | "pt" | "pc" | "deg" | "rad" | "grad" | "turn" | "ms" | "s" | "Hz" | "kHz" | "%")?;
188
189                 Color = "#" [0-9A-Fa-f]+;
190                 Identifier = [@!_\-$0-9a-zA-Z\[\]='"/]+;
191
192                 DoubleStringContent = ([^\r\n\"\\] | "\\" ['"\\bfnrtv])*;
193                 SingleStringContent = ([^\r\n\'\\] | "\\" ['"\\bfnrtv])*;
194                 StringLiteral = "\"" DoubleStringContent "\"" | "'" SingleStringContent "'";
195                 DoubleStringStart = "\"" DoubleStringContent "\\" [\r\n];
196                 DoubleStringEnd = DoubleStringContent "\"";
197                 SingleStringStart = "'" SingleStringContent "\\" [\r\n];
198                 SingleStringEnd = SingleStringContent "'";
199
200                 <INITIAL> Comment { this.tokenType = "css-comment"; return cursor; }
201                 <INITIAL> CommentStart => COMMENT { this.tokenType = "css-comment"; return cursor; }
202                 <COMMENT> CommentContent => COMMENT { this.tokenType = "css-comment"; return cursor; }
203                 <COMMENT> CommentEnd => INITIAL { this.tokenType = "css-comment"; return cursor; }
204
205                 <INITIAL> StringLiteral { return this._stringToken(cursor, true); }
206                 <INITIAL> DoubleStringStart => DSTRING { return this._stringToken(cursor); }
207                 <DSTRING> DoubleStringContent => DSTRING { return this._stringToken(cursor); }
208                 <DSTRING> DoubleStringEnd => INITIAL { return this._stringToken(cursor, true); }
209                 <INITIAL> SingleStringStart => SSTRING { return this._stringToken(cursor); }
210                 <SSTRING> SingleStringContent => SSTRING { return this._stringToken(cursor); }
211                 <SSTRING> SingleStringEnd => INITIAL { return this._stringToken(cursor, true); }
212
213                 <INITIAL> OpenCurlyBracket
214                 {
215                     this.tokenType = "block-start";
216                     if (this._condition.parseCondition === this._parseConditions.AT_MEDIA_RULE)
217                         this._condition.parseCondition = this._parseConditions.INITIAL;
218                     else
219                         this._condition.parseCondition = this._parseConditions.PROPERTY;
220                     return cursor;
221                 }
222
223                 <INITIAL> CloseCurlyBracket
224                 {
225                     this.tokenType = "block-end";
226                     this._condition.parseCondition = this._parseConditions.INITIAL;
227                     return cursor;
228                 }
229
230                 <INITIAL> Colon
231                 {
232                     this.tokenType = null;
233                     if (this._condition.parseCondition === this._parseConditions.PROPERTY)
234                         this._condition.parseCondition = this._parseConditions.PROPERTY_VALUE;
235                     return cursor;
236                 }
237
238                 <INITIAL> Semicolon
239                 {
240                     this.tokenType = null;
241                     if (this._condition.parseCondition === this._parseConditions.AT_RULE)
242                         this._condition.parseCondition = this._parseConditions.INITIAL;
243                     else
244                         this._condition.parseCondition = this._parseConditions.PROPERTY;
245                     return cursor;
246                 }
247
248                 <INITIAL> NumericLiteral
249                 {
250                     if (this._isPropertyValue())
251                         this.tokenType = "css-number";
252                     else
253                         this.tokenType = null;
254                     return cursor;
255                 }
256
257                 <INITIAL> Color
258                 {
259                     if (this._isPropertyValue())
260                         this.tokenType = "css-color";
261                     else
262                         this.tokenType = null;
263                     return cursor;
264                 }
265
266                 <INITIAL> Identifier
267                 {
268                     var token = this._line.substring(cursorOnEnter, cursor);
269                     if (this._condition.parseCondition === this._parseConditions.INITIAL) {
270                         if (token === "@media") {
271                             this.tokenType = "css-at-rule";
272                             this._condition.parseCondition = this._parseConditions.AT_MEDIA_RULE;
273                         } else if (token.startsWith("@")) {
274                             this.tokenType = "css-at-rule";
275                             this._condition.parseCondition = this._parseConditions.AT_RULE;
276                         } else
277                             this.tokenType = "css-selector";
278                     } else if ((this._condition.parseCondition === this._parseConditions.AT_MEDIA_RULE || this._condition.parseCondition === this._parseConditions.AT_RULE) && token in this._mediaTypes)
279                         this.tokenType = "css-keyword";
280                     else if (this._condition.parseCondition === this._parseConditions.PROPERTY && token in this._propertyKeywords)
281                         this.tokenType = "css-property";
282                     else if (this._isPropertyValue()) {
283                         if (token in this._valueKeywords)
284                             this.tokenType = "css-keyword";
285                         else if (token in this._colorKeywords) {
286                             // FIXME: this does not convert tokens toLowerCase() for the sake of speed.
287                             this.tokenType = "css-color";
288                         } else if (token === "!important")
289                             this.tokenType = "css-important";
290                     } else
291                         this.tokenType = null;
292                     return cursor;
293                 }
294                 <*> [^] { this.tokenType = null; return cursor; }
295             */
296         }
297     }
298 }
299
300 WebInspector.SourceCSSTokenizer.prototype.__proto__ = WebInspector.SourceTokenizer.prototype;