Home | History | Annotate | Download | only in front-end
      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 // re2c -isc WebCore/inspector/front-end/SourceCSSTokenizer.re2js \
     34 // | sed 's|^yy\([^:]*\)*\:|case \1:|' \
     35 // | sed 's|[*]cursor[+][+]|this._charAt(cursor++)|' \
     36 // | sed 's|[[*][+][+]cursor|this._charAt(++cursor)|' \
     37 // | sed 's|[*]cursor|this._charAt(cursor)|' \
     38 // | sed 's|yych = \*\([^;]*\)|yych = this._charAt\1|' \
     39 // | sed 's|goto case \([^;]*\)|{ gotoCase = \1; continue; }|' \
     40 // | sed 's|unsigned\ int|var|' \
     41 // | sed 's|var\ yych|case 1: var yych|'
     42 
     43 WebInspector.SourceCSSTokenizer = function()
     44 {
     45     WebInspector.SourceTokenizer.call(this);
     46 
     47     this._propertyKeywords = WebInspector.cssNameCompletions.keySet();
     48 
     49     this._valueKeywords = [
     50         "above", "absolute", "activeborder", "activecaption", "afar", "after-white-space", "ahead", "alias", "all", "all-scroll",
     51         "alternate", "always","amharic", "amharic-abegede", "antialiased", "appworkspace", "aqua", "arabic-indic", "armenian", "asterisks",
     52         "auto", "avoid", "background", "backwards", "baseline", "below", "bidi-override", "binary", "bengali", "black", "blink",
     53         "block", "block-axis", "blue", "bold", "bolder", "border", "border-box", "both", "bottom", "break-all", "break-word", "button",
     54         "button-bevel", "buttonface", "buttonhighlight", "buttonshadow", "buttontext", "cambodian", "capitalize", "caps-lock-indicator",
     55         "caption", "captiontext", "caret", "cell", "center", "checkbox", "circle", "cjk-earthly-branch", "cjk-heavenly-stem", "cjk-ideographic",
     56         "clear", "clip", "close-quote", "col-resize", "collapse", "compact", "condensed", "contain", "content", "content-box", "context-menu",
     57         "continuous", "copy", "cover", "crop", "cross", "crosshair", "currentcolor", "cursive", "dashed", "decimal", "decimal-leading-zero", "default",
     58         "default-button", "destination-atop", "destination-in", "destination-out", "destination-over", "devanagari", "disc", "discard", "document",
     59         "dot-dash", "dot-dot-dash", "dotted", "double", "down", "e-resize", "ease", "ease-in", "ease-in-out", "ease-out", "element",
     60         "ellipsis", "embed", "end", "ethiopic", "ethiopic-abegede", "ethiopic-abegede-am-et", "ethiopic-abegede-gez",
     61         "ethiopic-abegede-ti-er", "ethiopic-abegede-ti-et", "ethiopic-halehame-aa-er", "ethiopic-halehame-aa-et",
     62         "ethiopic-halehame-am-et", "ethiopic-halehame-gez", "ethiopic-halehame-om-et", "ethiopic-halehame-sid-et",
     63         "ethiopic-halehame-so-et", "ethiopic-halehame-ti-er", "ethiopic-halehame-ti-et", "ethiopic-halehame-tig", "ew-resize", "expanded",
     64         "extra-condensed", "extra-expanded", "fantasy", "fast", "fill", "fixed", "flat", "footnotes", "forwards", "from", "fuchsia", "geometricPrecision",
     65         "georgian", "gray", "graytext", "green", "grey", "groove", "gujarati", "gurmukhi", "hand", "hangul", "hangul-consonant", "hebrew", "help",
     66         "hidden", "hide", "higher", "highlight", "highlighttext", "hiragana", "hiragana-iroha", "horizontal", "hsl", "hsla", "icon", "ignore",
     67         "inactiveborder", "inactivecaption", "inactivecaptiontext", "infinite", "infobackground", "infotext", "inherit", "initial", "inline",
     68         "inline-axis", "inline-block", "inline-table", "inset", "inside", "intrinsic", "invert", "italic", "justify", "kannada", "katakana",
     69         "katakana-iroha", "khmer", "landscape", "lao", "large", "larger", "left", "level", "lighter", "lime", "line-through", "linear", "lines",
     70         "list-button", "list-item", "listbox", "listitem", "local", "logical", "loud", "lower", "lower-alpha", "lower-armenian", "lower-greek",
     71         "lower-hexadecimal", "lower-latin", "lower-norwegian", "lower-roman", "lowercase", "ltr", "malayalam", "maroon", "match", "media-controls-background",
     72         "media-current-time-display", "media-fullscreen-button", "media-mute-button", "media-play-button", "media-return-to-realtime-button",
     73         "media-rewind-button", "media-seek-back-button", "media-seek-forward-button", "media-slider", "media-sliderthumb", "media-time-remaining-display",
     74         "media-volume-slider", "media-volume-slider-container", "media-volume-sliderthumb", "medium", "menu", "menulist", "menulist-button",
     75         "menulist-text", "menulist-textfield", "menutext", "message-box", "middle", "min-intrinsic", "mix", "mongolian", "monospace", "move", "multiple",
     76         "myanmar", "n-resize", "narrower", "navy", "ne-resize", "nesw-resize", "no-close-quote", "no-drop", "no-open-quote", "no-repeat", "none",
     77         "normal", "not-allowed", "nowrap", "ns-resize", "nw-resize", "nwse-resize", "oblique", "octal", "olive", "open-quote", "optimizeLegibility",
     78         "optimizeSpeed", "orange", "oriya", "oromo", "outset", "outside", "overlay", "overline", "padding", "padding-box", "painted", "paused",
     79         "persian", "plus-darker", "plus-lighter", "pointer", "portrait", "pre", "pre-line", "pre-wrap", "preserve-3d", "progress", "purple",
     80         "push-button", "radio", "read-only", "read-write", "read-write-plaintext-only", "red", "relative", "repeat", "repeat-x",
     81         "repeat-y", "reset", "reverse", "rgb", "rgba", "ridge", "right", "round", "row-resize", "rtl", "run-in", "running", "s-resize", "sans-serif",
     82         "scroll", "scrollbar", "se-resize", "searchfield", "searchfield-cancel-button", "searchfield-decoration", "searchfield-results-button",
     83         "searchfield-results-decoration", "semi-condensed", "semi-expanded", "separate", "serif", "show", "sidama", "silver", "single",
     84         "skip-white-space", "slide", "slider-horizontal", "slider-vertical", "sliderthumb-horizontal", "sliderthumb-vertical", "slow",
     85         "small", "small-caps", "small-caption", "smaller", "solid", "somali", "source-atop", "source-in", "source-out", "source-over",
     86         "space", "square", "square-button", "start", "static", "status-bar", "stretch", "stroke", "sub", "subpixel-antialiased", "super",
     87         "sw-resize", "table", "table-caption", "table-cell", "table-column", "table-column-group", "table-footer-group", "table-header-group",
     88         "table-row", "table-row-group", "teal", "telugu", "text", "text-bottom", "text-top", "textarea", "textfield", "thai", "thick", "thin",
     89         "threeddarkshadow", "threedface", "threedhighlight", "threedlightshadow", "threedshadow", "tibetan", "tigre", "tigrinya-er", "tigrinya-er-abegede",
     90         "tigrinya-et", "tigrinya-et-abegede", "to", "top", "transparent", "ultra-condensed", "ultra-expanded", "underline", "up", "upper-alpha", "upper-armenian",
     91         "upper-greek", "upper-hexadecimal", "upper-latin", "upper-norwegian", "upper-roman", "uppercase", "urdu", "url", "vertical", "vertical-text", "visible",
     92         "visibleFill", "visiblePainted", "visibleStroke", "visual", "w-resize", "wait", "wave", "white", "wider", "window", "windowframe", "windowtext",
     93         "x-large", "x-small", "xor", "xx-large", "xx-small", "yellow", "-wap-marquee", "-webkit-activelink", "-webkit-auto", "-webkit-baseline-middle",
     94         "-webkit-body", "-webkit-box", "-webkit-center", "-webkit-control", "-webkit-focus-ring-color", "-webkit-grab", "-webkit-grabbing",
     95         "-webkit-gradient", "-webkit-inline-box", "-webkit-left", "-webkit-link", "-webkit-marquee", "-webkit-mini-control", "-webkit-nowrap", "-webkit-right",
     96         "-webkit-small-control", "-webkit-text", "-webkit-xxx-large", "-webkit-zoom-in", "-webkit-zoom-out",
     97     ].keySet();
     98 
     99     this._mediaTypes = ["all", "aural", "braille", "embossed", "handheld", "import", "print", "projection", "screen", "tty", "tv"].keySet();
    100 
    101     this._lexConditions = {
    102         INITIAL: 0,
    103         COMMENT: 1,
    104         DSTRING: 2,
    105         SSTRING: 3
    106     };
    107 
    108     this._parseConditions = {
    109         INITIAL: 0,
    110         PROPERTY: 1,
    111         PROPERTY_VALUE: 2,
    112         AT_RULE: 3
    113     };
    114 
    115     this.case_INITIAL = 1000;
    116     this.case_COMMENT = 1002;
    117     this.case_DSTRING = 1003;
    118     this.case_SSTRING = 1004;
    119 
    120     this.initialCondition = { lexCondition: this._lexConditions.INITIAL, parseCondition: this._parseConditions.INITIAL }
    121 }
    122 
    123 WebInspector.SourceCSSTokenizer.prototype = {
    124     _stringToken: function(cursor, stringEnds)
    125     {
    126         if (this._isPropertyValue())
    127             this.tokenType = "css-string";
    128         else
    129             this.tokenType = null;
    130         return cursor;
    131     },
    132 
    133     _isPropertyValue: function()
    134     {
    135         return this._condition.parseCondition === this._parseConditions.PROPERTY_VALUE || this._condition.parseCondition === this._parseConditions.AT_RULE;
    136     },
    137 
    138     nextToken: function(cursor)
    139     {
    140         var cursorOnEnter = cursor;
    141         var gotoCase = 1;
    142         while (1) {
    143             switch (gotoCase)
    144             // Following comment is replaced with generated state machine.
    145             /*!re2c
    146                 re2c:define:YYCTYPE  = "var";
    147                 re2c:define:YYCURSOR = cursor;
    148                 re2c:define:YYGETCONDITION = "this.getLexCondition";
    149                 re2c:define:YYSETCONDITION = "this.setLexCondition";
    150                 re2c:condprefix = "case this.case_";
    151                 re2c:condenumprefix = "this._lexConditions.";
    152                 re2c:yyfill:enable = 0;
    153                 re2c:labelprefix = "case ";
    154                 re2c:indent:top = 2;
    155                 re2c:indent:string = "    ";
    156 
    157                 CommentContent = ([^*\r\n] | ("*"+[^/*]))*;
    158                 Comment = "/*" CommentContent "*"+ "/";
    159                 CommentStart = "/*" CommentContent [\r\n];
    160                 CommentEnd = CommentContent "*"+ "/";
    161 
    162                 OpenCurlyBracket = "{";
    163                 CloseCurlyBracket = "}";
    164 
    165                 Colon = ":";
    166                 Semicolon = ";";
    167 
    168                 NumericLiteral = "-"? ([0-9]+ | [0-9]* "." [0-9]+)  ("em" | "rem" | "__qem" | "ex" | "px" | "cm" |
    169                     "mm" | "in" | "pt" | "pc" | "deg" | "rad" | "grad" | "turn" | "ms" | "s" | "Hz" | "kHz" | "%")?;
    170 
    171                 Identifier = [@!_\-$0-9a-zA-Z\[\]='"/]+;
    172 
    173                 DoubleStringContent = ([^\r\n\"\\] | "\\" ['"\\bfnrtv])*;
    174                 SingleStringContent = ([^\r\n\'\\] | "\\" ['"\\bfnrtv])*;
    175                 StringLiteral = "\"" DoubleStringContent "\"" | "'" SingleStringContent "'";
    176                 DoubleStringStart = "\"" DoubleStringContent "\\" [\r\n];
    177                 DoubleStringEnd = DoubleStringContent "\"";
    178                 SingleStringStart = "'" SingleStringContent "\\" [\r\n];
    179                 SingleStringEnd = SingleStringContent "'";
    180 
    181                 <INITIAL> Comment { this.tokenType = "css-comment"; return cursor; }
    182                 <INITIAL> CommentStart => COMMENT { this.tokenType = "css-comment"; return cursor; }
    183                 <COMMENT> CommentContent => COMMENT { this.tokenType = "css-comment"; return cursor; }
    184                 <COMMENT> CommentEnd => INITIAL { this.tokenType = "css-comment"; return cursor; }
    185 
    186                 <INITIAL> StringLiteral { return this._stringToken(cursor, true); }
    187                 <INITIAL> DoubleStringStart => DSTRING { return this._stringToken(cursor); }
    188                 <DSTRING> DoubleStringContent => DSTRING { return this._stringToken(cursor); }
    189                 <DSTRING> DoubleStringEnd => INITIAL { return this._stringToken(cursor, true); }
    190                 <INITIAL> SingleStringStart => SSTRING { return this._stringToken(cursor); }
    191                 <SSTRING> SingleStringContent => SSTRING { return this._stringToken(cursor); }
    192                 <SSTRING> SingleStringEnd => INITIAL { return this._stringToken(cursor, true); }
    193 
    194                 <INITIAL> OpenCurlyBracket
    195                 {
    196                     this.tokenType = null;
    197                     if (this._condition.parseCondition === this._parseConditions.AT_RULE)
    198                         this._condition.parseCondition = this._parseConditions.INITIAL;
    199                     else
    200                         this._condition.parseCondition = this._parseConditions.PROPERTY;
    201                     return cursor;
    202                 }
    203 
    204                 <INITIAL> CloseCurlyBracket
    205                 {
    206                     this.tokenType = null;
    207                     this._condition.parseCondition = this._parseConditions.INITIAL;
    208                     return cursor;
    209                 }
    210 
    211                 <INITIAL> Colon
    212                 {
    213                     this.tokenType = null;
    214                     if (this._condition.parseCondition === this._parseConditions.PROPERTY)
    215                         this._condition.parseCondition = this._parseConditions.PROPERTY_VALUE;
    216                     return cursor;
    217                 }
    218 
    219                 <INITIAL> Semicolon
    220                 {
    221                     this.tokenType = null;
    222                     if (this._condition.parseCondition === this._parseConditions.AT_RULE)
    223                         this._condition.parseCondition = this._parseConditions.INITIAL;
    224                     else
    225                         this._condition.parseCondition = this._parseConditions.PROPERTY;
    226                     return cursor;
    227                 }
    228 
    229                 <INITIAL> NumericLiteral
    230                 {
    231                     if (this._isPropertyValue())
    232                         this.tokenType = "css-number";
    233                     else
    234                         this.tokenType = null;
    235                     return cursor;
    236                 }
    237 
    238                 <INITIAL> Identifier
    239                 {
    240                     var token = this._line.substring(cursorOnEnter, cursor);
    241                     if (this._condition.parseCondition === this._parseConditions.INITIAL) {
    242                         if (token === "@import" || token === "@media") {
    243                             this.tokenType = "css-at-rule";
    244                             this._condition.parseCondition = this._parseConditions.AT_RULE;
    245                         } else if (token.indexOf("@") === 0)
    246                             this.tokenType = "css-at-rule";
    247                         else
    248                             this.tokenType = "css-selector";
    249                     }
    250                     else if (this._condition.parseCondition === this._parseConditions.AT_RULE && token in this._mediaTypes)
    251                         this.tokenType = "css-keyword";
    252                     else if (this._condition.parseCondition === this._parseConditions.PROPERTY && token in this._propertyKeywords)
    253                         this.tokenType = "css-property";
    254                     else if (this._isPropertyValue() && token in this._valueKeywords)
    255                         this.tokenType = "css-keyword";
    256                     else if (token === "!important")
    257                         this.tokenType = "css-important";
    258                     else
    259                         this.tokenType = null;
    260                     return cursor;
    261                 }
    262                 <*> [^] { this.tokenType = null; return cursor; }
    263             */
    264         }
    265     }
    266 }
    267 
    268 WebInspector.SourceCSSTokenizer.prototype.__proto__ = WebInspector.SourceTokenizer.prototype;
    269