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