pfeldman@chromium.org | 57f7c77 | 2010-01-08 11:50:25 +0000 | [diff] [blame] | 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 | |
pfeldman@chromium.org | 1db2ebc | 2012-07-09 17:30:38 +0000 | [diff] [blame] | 31 | /* |
| 32 | * Generate js file as follows: |
| 33 | re2c -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.org | 57f7c77 | 2010-01-08 11:50:25 +0000 | [diff] [blame] | 44 | |
pfeldman@chromium.org | 50463be | 2011-10-04 16:11:59 +0000 | [diff] [blame] | 45 | /** |
| 46 | * @constructor |
| 47 | * @extends {WebInspector.SourceTokenizer} |
| 48 | */ |
pfeldman@chromium.org | d1814d0 | 2010-01-26 10:18:50 +0000 | [diff] [blame] | 49 | WebInspector.SourceJavaScriptTokenizer = function() |
pfeldman@chromium.org | 57f7c77 | 2010-01-08 11:50:25 +0000 | [diff] [blame] | 50 | { |
pfeldman@chromium.org | b6cee79 | 2010-01-29 11:04:09 +0000 | [diff] [blame] | 51 | WebInspector.SourceTokenizer.call(this); |
pfeldman@chromium.org | d1814d0 | 2010-01-26 10:18:50 +0000 | [diff] [blame] | 52 | |
pfeldman@chromium.org | d1814d0 | 2010-01-26 10:18:50 +0000 | [diff] [blame] | 53 | this._lexConditions = { |
pfeldman@chromium.org | 57f7c77 | 2010-01-08 11:50:25 +0000 | [diff] [blame] | 54 | 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.org | b5fbb0b | 2011-05-13 14:56:24 +0000 | [diff] [blame] | 69 | this.condition = this.createInitialCondition(); |
pfeldman@chromium.org | 57f7c77 | 2010-01-08 11:50:25 +0000 | [diff] [blame] | 70 | } |
| 71 | |
pfeldman@chromium.org | 8cdb3de | 2012-02-21 11:30:30 +0000 | [diff] [blame] | 72 | WebInspector.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.org | d1814d0 | 2010-01-26 10:18:50 +0000 | [diff] [blame] | 79 | WebInspector.SourceJavaScriptTokenizer.prototype = { |
podivilov@chromium.org | b5fbb0b | 2011-05-13 14:56:24 +0000 | [diff] [blame] | 80 | createInitialCondition: function() |
| 81 | { |
| 82 | return { lexCondition: this._lexConditions.NODIV }; |
| 83 | }, |
| 84 | |
pfeldman@chromium.org | 57f7c77 | 2010-01-08 11:50:25 +0000 | [diff] [blame] | 85 | nextToken: function(cursor) |
| 86 | { |
| 87 | var cursorOnEnter = cursor; |
| 88 | var gotoCase = 1; |
pfeldman@chromium.org | db47929 | 2011-10-07 15:36:19 +0000 | [diff] [blame] | 89 | var YYMARKER; |
pfeldman@chromium.org | 57f7c77 | 2010-01-08 11:50:25 +0000 | [diff] [blame] | 90 | 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.org | d1814d0 | 2010-01-26 10:18:50 +0000 | [diff] [blame] | 96 | re2c:define:YYGETCONDITION = "this.getLexCondition"; |
| 97 | re2c:define:YYSETCONDITION = "this.setLexCondition"; |
pfeldman@chromium.org | 57f7c77 | 2010-01-08 11:50:25 +0000 | [diff] [blame] | 98 | re2c:condprefix = "case this.case_"; |
pfeldman@chromium.org | d1814d0 | 2010-01-26 10:18:50 +0000 | [diff] [blame] | 99 | re2c:condenumprefix = "this._lexConditions."; |
pfeldman@chromium.org | 57f7c77 | 2010-01-08 11:50:25 +0000 | [diff] [blame] | 100 | 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.org | d1814d0 | 2010-01-26 10:18:50 +0000 | [diff] [blame] | 139 | SingleStringStart = "'" SingleStringContent "\\" [\r\n]; |
| 140 | SingleStringEnd = SingleStringContent "'"; |
pfeldman@chromium.org | 57f7c77 | 2010-01-08 11:50:25 +0000 | [diff] [blame] | 141 | |
| 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.org | b6cee79 | 2010-01-29 11:04:09 +0000 | [diff] [blame] | 151 | <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.org | 57f7c77 | 2010-01-08 11:50:25 +0000 | [diff] [blame] | 156 | |
pfeldman@chromium.org | b6cee79 | 2010-01-29 11:04:09 +0000 | [diff] [blame] | 157 | <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.org | 57f7c77 | 2010-01-08 11:50:25 +0000 | [diff] [blame] | 164 | |
pfeldman@chromium.org | b6cee79 | 2010-01-29 11:04:09 +0000 | [diff] [blame] | 165 | <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.org | 57f7c77 | 2010-01-08 11:50:25 +0000 | [diff] [blame] | 169 | |
pfeldman@chromium.org | b6cee79 | 2010-01-29 11:04:09 +0000 | [diff] [blame] | 170 | <DIV,NODIV> NumericLiteral => DIV { this.tokenType = "javascript-number"; return cursor; } |
pfeldman@chromium.org | 57f7c77 | 2010-01-08 11:50:25 +0000 | [diff] [blame] | 171 | <DIV,NODIV> Identifier => DIV |
| 172 | { |
| 173 | var token = this._line.substring(cursorOnEnter, cursor); |
pfeldman@chromium.org | 8cdb3de | 2012-02-21 11:30:30 +0000 | [diff] [blame] | 174 | if (WebInspector.SourceJavaScriptTokenizer.Keywords[token] === true && token !== "__proto__") |
pfeldman@chromium.org | b6cee79 | 2010-01-29 11:04:09 +0000 | [diff] [blame] | 175 | this.tokenType = "javascript-keyword"; |
pfeldman@chromium.org | 57f7c77 | 2010-01-08 11:50:25 +0000 | [diff] [blame] | 176 | else |
pfeldman@chromium.org | b6cee79 | 2010-01-29 11:04:09 +0000 | [diff] [blame] | 177 | this.tokenType = "javascript-ident"; |
pfeldman@chromium.org | 57f7c77 | 2010-01-08 11:50:25 +0000 | [diff] [blame] | 178 | return cursor; |
| 179 | } |
| 180 | <DIV,NODIV> RightParen => DIV { this.tokenType = null; return cursor; } |
pfeldman@chromium.org | 1db2ebc | 2012-07-09 17:30:38 +0000 | [diff] [blame] | 181 | <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.org | 57f7c77 | 2010-01-08 11:50:25 +0000 | [diff] [blame] | 191 | <DIV> Division => NODIV { this.tokenType = null; return cursor; } |
| 192 | <*> [^] { this.tokenType = null; return cursor; } |
| 193 | */ |
| 194 | } |
| 195 | } |
| 196 | } |
pfeldman@chromium.org | d1814d0 | 2010-01-26 10:18:50 +0000 | [diff] [blame] | 197 | |
pfeldman@chromium.org | b6cee79 | 2010-01-29 11:04:09 +0000 | [diff] [blame] | 198 | WebInspector.SourceJavaScriptTokenizer.prototype.__proto__ = WebInspector.SourceTokenizer.prototype; |