SourceCSSTokenizer.re2js 19 KB

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