blob: 34a6786341c57588b1934a2affa5438ce20c9c0d [file] [log] [blame]
pfeldman@chromium.org57f7c772010-01-08 11:50:25 +00001/*
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
pfeldman@chromium.org1db2ebc2012-07-09 17:30:38 +000031/*
32 * Generate js file as follows:
33re2c -isc Source/WebCore/inspector/front-end/SourceJavaScriptTokenizer.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|yych <= \(0x[0-9a-fA-F]*\)|yych \<\= String.fromCharCode(\1)|' \
41 | sed 's|unsigned\ int|var|' \
42 | sed 's|var\ yych|case 1: var yych|' > Source/WebCore/inspector/front-end/SourceJavaScriptTokenizer.js
43 */
pfeldman@chromium.org57f7c772010-01-08 11:50:25 +000044
pfeldman@chromium.org50463be2011-10-04 16:11:59 +000045/**
46 * @constructor
47 * @extends {WebInspector.SourceTokenizer}
48 */
pfeldman@chromium.orgd1814d02010-01-26 10:18:50 +000049WebInspector.SourceJavaScriptTokenizer = function()
pfeldman@chromium.org57f7c772010-01-08 11:50:25 +000050{
pfeldman@chromium.orgb6cee792010-01-29 11:04:09 +000051 WebInspector.SourceTokenizer.call(this);
pfeldman@chromium.orgd1814d02010-01-26 10:18:50 +000052
pfeldman@chromium.orgd1814d02010-01-26 10:18:50 +000053 this._lexConditions = {
pfeldman@chromium.org57f7c772010-01-08 11:50:25 +000054 DIV: 0,
55 NODIV: 1,
56 COMMENT: 2,
57 DSTRING: 3,
58 SSTRING: 4,
59 REGEX: 5
60 };
61
62 this.case_DIV = 1000;
63 this.case_NODIV = 1001;
64 this.case_COMMENT = 1002;
65 this.case_DSTRING = 1003;
66 this.case_SSTRING = 1004;
67 this.case_REGEX = 1005;
68
podivilov@chromium.orgb5fbb0b2011-05-13 14:56:24 +000069 this.condition = this.createInitialCondition();
pfeldman@chromium.org57f7c772010-01-08 11:50:25 +000070}
71
pfeldman@chromium.org8cdb3de2012-02-21 11:30:30 +000072WebInspector.SourceJavaScriptTokenizer.Keywords = [
73 "null", "true", "false", "break", "case", "catch", "const", "default", "finally", "for",
74 "instanceof", "new", "var", "continue", "function", "return", "void", "delete", "if",
75 "this", "do", "while", "else", "in", "switch", "throw", "try", "typeof", "debugger",
76 "class", "enum", "export", "extends", "import", "super", "get", "set", "with"
77 ].keySet();
78
pfeldman@chromium.orgd1814d02010-01-26 10:18:50 +000079WebInspector.SourceJavaScriptTokenizer.prototype = {
podivilov@chromium.orgb5fbb0b2011-05-13 14:56:24 +000080 createInitialCondition: function()
81 {
82 return { lexCondition: this._lexConditions.NODIV };
83 },
84
pfeldman@chromium.org57f7c772010-01-08 11:50:25 +000085 nextToken: function(cursor)
86 {
87 var cursorOnEnter = cursor;
88 var gotoCase = 1;
pfeldman@chromium.orgdb479292011-10-07 15:36:19 +000089 var YYMARKER;
pfeldman@chromium.org57f7c772010-01-08 11:50:25 +000090 while (1) {
91 switch (gotoCase)
92 // Following comment is replaced with generated state machine.
93 /*!re2c
94 re2c:define:YYCTYPE = "var";
95 re2c:define:YYCURSOR = cursor;
pfeldman@chromium.orgd1814d02010-01-26 10:18:50 +000096 re2c:define:YYGETCONDITION = "this.getLexCondition";
97 re2c:define:YYSETCONDITION = "this.setLexCondition";
pfeldman@chromium.org57f7c772010-01-08 11:50:25 +000098 re2c:condprefix = "case this.case_";
pfeldman@chromium.orgd1814d02010-01-26 10:18:50 +000099 re2c:condenumprefix = "this._lexConditions.";
pfeldman@chromium.org57f7c772010-01-08 11:50:25 +0000100 re2c:yyfill:enable = 0;
101 re2c:labelprefix = "case ";
102 re2c:indent:top = 2;
103 re2c:indent:string = " ";
104
105 LineComment = "//" [^\r\n]*;
106 CommentContent = ([^*\r\n] | ("*"+[^/*]))*;
107 Comment = "/*" CommentContent "*"+ "/";
108 CommentStart = "/*" CommentContent [\r\n];
109 CommentEnd = CommentContent "*"+ "/";
110
111 DecimalDigit = [0-9];
112 NonZeroDigit = [1-9];
113 OctalDigit = [0-7];
114 HexDigit = [0-9a-fA-F];
115 SignedInteger = ("+"|"-")? DecimalDigit+;
116 ExponentPart = ("e" | "E") SignedInteger;
117 DecimalIntegerLiteral = "0" | NonZeroDigit DecimalDigit*;
118 DecimalLiteral = DecimalIntegerLiteral "." DecimalDigit* ExponentPart? | "." DecimalDigit+ ExponentPart? | DecimalIntegerLiteral ExponentPart?;
119 HexIntegerLiteral = "0" ("x"|"X") HexDigit+;
120 OctalIntegerLiteral = "0" OctalDigit+;
121 NumericLiteral = DecimalLiteral | HexIntegerLiteral | OctalIntegerLiteral;
122
123 Punctuation = [\!\%\&\(\*\+\,\-\.\:\;\<\=\>\?\[\]\^\{\|\}\~] | "!=" | "!==" | "%=" | "&&" | "&=" | "*=" | "++" | "+=" | "--" | "-=" | "<<" | "<<=" | "<=" | "==" | "===" | ">=" | ">>" | ">>=" | ">>>" | ">>>=" | "^=" | "|=" | "||";
124 Division = "/" | "/=";
125 RightParen = ")";
126
127 Letter = [a-zA-Z\x80-\xFF];
128 UnicodeEscapeSequence = "\\u" HexDigit HexDigit HexDigit HexDigit;
129
130 IdentifierStart = Letter | "_" | "$" | UnicodeEscapeSequence;
131 IdentifierPart = IdentifierStart | DecimalDigit;
132 Identifier = IdentifierStart IdentifierPart *;
133
134 DoubleStringContent = ([^\r\n\"\\] | UnicodeEscapeSequence | "\\" ['"\\bfnrtv])*;
135 SingleStringContent = ([^\r\n\'\\] | UnicodeEscapeSequence | "\\" ['"\\bfnrtv])*;
136 StringLiteral = "\"" DoubleStringContent "\"" | "'" SingleStringContent "'";
137 DoubleStringStart = "\"" DoubleStringContent "\\" [\r\n];
138 DoubleStringEnd = DoubleStringContent "\"";
pfeldman@chromium.orgd1814d02010-01-26 10:18:50 +0000139 SingleStringStart = "'" SingleStringContent "\\" [\r\n];
140 SingleStringEnd = SingleStringContent "'";
pfeldman@chromium.org57f7c772010-01-08 11:50:25 +0000141
142 BackslashSequence = "\\" [^\r\n];
143 RegexSet = "[" ([^\r\n*\\/] | BackslashSequence)* "]";
144 RegexFirstChar = [^\r\n*\\/\[\]] | BackslashSequence | RegexSet;
145 RegexChar = [^\r\n\\/\[\]] | BackslashSequence | RegexSet;
146 RegexContent = RegexChar*;
147 Regex = "/" RegexFirstChar RegexContent "/" [igm]*;
148 RegexStart = "/" RegexFirstChar RegexContent "\\";
149 RegexEnd = RegexContent "/" [igm]*;
150
pfeldman@chromium.orgb6cee792010-01-29 11:04:09 +0000151 <DIV,NODIV> LineComment { this.tokenType = "javascript-comment"; return cursor; }
152 <DIV,NODIV> Comment { this.tokenType = "javascript-comment"; return cursor; }
153 <DIV,NODIV> CommentStart => COMMENT { this.tokenType = "javascript-comment"; return cursor; }
154 <COMMENT> CommentContent => COMMENT { this.tokenType = "javascript-comment"; return cursor; }
155 <COMMENT> CommentEnd => NODIV { this.tokenType = "javascript-comment"; return cursor; }
pfeldman@chromium.org57f7c772010-01-08 11:50:25 +0000156
pfeldman@chromium.orgb6cee792010-01-29 11:04:09 +0000157 <DIV,NODIV> StringLiteral { this.tokenType = "javascript-string"; return cursor; }
158 <DIV,NODIV> DoubleStringStart => DSTRING { this.tokenType = "javascript-string"; return cursor; }
159 <DSTRING> DoubleStringContent => DSTRING { this.tokenType = "javascript-string"; return cursor; }
160 <DSTRING> DoubleStringEnd => NODIV { this.tokenType = "javascript-string"; return cursor; }
161 <DIV,NODIV> SingleStringStart => SSTRING { this.tokenType = "javascript-string"; return cursor; }
162 <SSTRING> SingleStringContent => SSTRING { this.tokenType = "javascript-string"; return cursor; }
163 <SSTRING> SingleStringEnd => NODIV { this.tokenType = "javascript-string"; return cursor; }
pfeldman@chromium.org57f7c772010-01-08 11:50:25 +0000164
pfeldman@chromium.orgb6cee792010-01-29 11:04:09 +0000165 <NODIV> Regex { this.tokenType = "javascript-regexp"; return cursor; }
166 <NODIV> RegexStart => REGEX { this.tokenType = "javascript-regexp"; return cursor; }
167 <REGEX> RegexContent => REGEX { this.tokenType = "javascript-regexp"; return cursor; }
168 <REGEX> RegexEnd => NODIV { this.tokenType = "javascript-regexp"; return cursor; }
pfeldman@chromium.org57f7c772010-01-08 11:50:25 +0000169
pfeldman@chromium.orgb6cee792010-01-29 11:04:09 +0000170 <DIV,NODIV> NumericLiteral => DIV { this.tokenType = "javascript-number"; return cursor; }
pfeldman@chromium.org57f7c772010-01-08 11:50:25 +0000171 <DIV,NODIV> Identifier => DIV
172 {
173 var token = this._line.substring(cursorOnEnter, cursor);
pfeldman@chromium.org8cdb3de2012-02-21 11:30:30 +0000174 if (WebInspector.SourceJavaScriptTokenizer.Keywords[token] === true && token !== "__proto__")
pfeldman@chromium.orgb6cee792010-01-29 11:04:09 +0000175 this.tokenType = "javascript-keyword";
pfeldman@chromium.org57f7c772010-01-08 11:50:25 +0000176 else
pfeldman@chromium.orgb6cee792010-01-29 11:04:09 +0000177 this.tokenType = "javascript-ident";
pfeldman@chromium.org57f7c772010-01-08 11:50:25 +0000178 return cursor;
179 }
180 <DIV,NODIV> RightParen => DIV { this.tokenType = null; return cursor; }
pfeldman@chromium.org1db2ebc2012-07-09 17:30:38 +0000181 <DIV,NODIV> Punctuation => NODIV
182 {
183 var token = this._line.charAt(cursorOnEnter);
184 if (token === "{")
185 this.tokenType = "block-start";
186 else if (token === "}")
187 this.tokenType = "block-end";
188 else this.tokenType = null;
189 return cursor;
190 }
pfeldman@chromium.org57f7c772010-01-08 11:50:25 +0000191 <DIV> Division => NODIV { this.tokenType = null; return cursor; }
192 <*> [^] { this.tokenType = null; return cursor; }
193 */
194 }
195 }
196}
pfeldman@chromium.orgd1814d02010-01-26 10:18:50 +0000197
pfeldman@chromium.orgb6cee792010-01-29 11:04:09 +0000198WebInspector.SourceJavaScriptTokenizer.prototype.__proto__ = WebInspector.SourceTokenizer.prototype;