| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2011 Google Inc. All rights reserved. | 2 * Copyright (C) 2011 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
| 6 * met: | 6 * met: |
| 7 * | 7 * |
| 8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 */ | 29 */ |
| 30 | 30 |
| 31 function FormattedContentBuilder(content, mapping, originalOffset, formattedOffs
et, indentString) | 31 /** |
| 32 { | 32 * @constructor |
| 33 this._originalContent = content; | 33 * @param {FormatterWorker.JavaScriptTokenizer} tokenizer |
| 34 this._originalOffset = originalOffset; | 34 * @param {FormatterWorker.JavaScriptFormattedContentBuilder} builder |
| 35 this._lastOriginalPosition = 0; | 35 */ |
| 36 | 36 FormatterWorker.JavaScriptFormatter = function(tokenizer, builder) |
| 37 this._formattedContent = []; | |
| 38 this._formattedContentLength = 0; | |
| 39 this._formattedOffset = formattedOffset; | |
| 40 this._lastFormattedPosition = 0; | |
| 41 | |
| 42 this._mapping = mapping; | |
| 43 | |
| 44 this._lineNumber = 0; | |
| 45 this._nestingLevel = 0; | |
| 46 this._indentString = indentString; | |
| 47 this._cachedIndents = {}; | |
| 48 } | |
| 49 | |
| 50 FormattedContentBuilder.prototype = { | |
| 51 addToken: function(token) | |
| 52 { | |
| 53 for (var i = 0; i < token.comments_before.length; ++i) | |
| 54 this._addComment(token.comments_before[i]); | |
| 55 | |
| 56 while (this._lineNumber < token.line) { | |
| 57 this._addText("\n"); | |
| 58 this._addIndent(); | |
| 59 this._needNewLine = false; | |
| 60 this._lineNumber += 1; | |
| 61 } | |
| 62 | |
| 63 if (this._needNewLine) { | |
| 64 this._addText("\n"); | |
| 65 this._addIndent(); | |
| 66 this._needNewLine = false; | |
| 67 } | |
| 68 | |
| 69 this._addMappingIfNeeded(token.pos); | |
| 70 this._addText(this._originalContent.substring(token.pos, token.endPos)); | |
| 71 this._lineNumber = token.endLine; | |
| 72 }, | |
| 73 | |
| 74 addSpace: function() | |
| 75 { | |
| 76 this._addText(" "); | |
| 77 }, | |
| 78 | |
| 79 addNewLine: function() | |
| 80 { | |
| 81 this._needNewLine = true; | |
| 82 }, | |
| 83 | |
| 84 increaseNestingLevel: function() | |
| 85 { | |
| 86 this._nestingLevel += 1; | |
| 87 }, | |
| 88 | |
| 89 decreaseNestingLevel: function() | |
| 90 { | |
| 91 this._nestingLevel -= 1; | |
| 92 }, | |
| 93 | |
| 94 content: function() | |
| 95 { | |
| 96 return this._formattedContent.join(""); | |
| 97 }, | |
| 98 | |
| 99 mapping: function() | |
| 100 { | |
| 101 return { original: this._originalPositions, formatted: this._formattedPo
sitions }; | |
| 102 }, | |
| 103 | |
| 104 _addIndent: function() | |
| 105 { | |
| 106 if (this._cachedIndents[this._nestingLevel]) { | |
| 107 this._addText(this._cachedIndents[this._nestingLevel]); | |
| 108 return; | |
| 109 } | |
| 110 | |
| 111 var fullIndent = ""; | |
| 112 for (var i = 0; i < this._nestingLevel; ++i) | |
| 113 fullIndent += this._indentString; | |
| 114 this._addText(fullIndent); | |
| 115 | |
| 116 // Cache a maximum of 20 nesting level indents. | |
| 117 if (this._nestingLevel <= 20) | |
| 118 this._cachedIndents[this._nestingLevel] = fullIndent; | |
| 119 }, | |
| 120 | |
| 121 _addComment: function(comment) | |
| 122 { | |
| 123 if (this._lineNumber < comment.line) { | |
| 124 for (var j = this._lineNumber; j < comment.line; ++j) | |
| 125 this._addText("\n"); | |
| 126 this._lineNumber = comment.line; | |
| 127 this._needNewLine = false; | |
| 128 this._addIndent(); | |
| 129 } else | |
| 130 this.addSpace(); | |
| 131 | |
| 132 this._addMappingIfNeeded(comment.pos); | |
| 133 if (comment.type === "comment1") | |
| 134 this._addText("//"); | |
| 135 else | |
| 136 this._addText("/*"); | |
| 137 | |
| 138 this._addText(comment.value); | |
| 139 | |
| 140 if (comment.type !== "comment1") { | |
| 141 this._addText("*/"); | |
| 142 var position; | |
| 143 while ((position = comment.value.indexOf("\n", position + 1)) !== -1
) | |
| 144 this._lineNumber += 1; | |
| 145 } | |
| 146 }, | |
| 147 | |
| 148 _addText: function(text) | |
| 149 { | |
| 150 this._formattedContent.push(text); | |
| 151 this._formattedContentLength += text.length; | |
| 152 }, | |
| 153 | |
| 154 _addMappingIfNeeded: function(originalPosition) | |
| 155 { | |
| 156 if (originalPosition - this._lastOriginalPosition === this._formattedCon
tentLength - this._lastFormattedPosition) | |
| 157 return; | |
| 158 this._mapping.original.push(this._originalOffset + originalPosition); | |
| 159 this._lastOriginalPosition = originalPosition; | |
| 160 this._mapping.formatted.push(this._formattedOffset + this._formattedCont
entLength); | |
| 161 this._lastFormattedPosition = this._formattedContentLength; | |
| 162 } | |
| 163 } | |
| 164 | |
| 165 var tokens = [ | |
| 166 ["EOS"], | |
| 167 ["LPAREN", "("], ["RPAREN", ")"], ["LBRACK", "["], ["RBRACK", "]"], ["LBRACE
", "{"], ["RBRACE", "}"], ["COLON", ":"], ["SEMICOLON", ";"], ["PERIOD", "."], [
"CONDITIONAL", "?"], | |
| 168 ["INC", "++"], ["DEC", "--"], | |
| 169 ["ASSIGN", "="], ["ASSIGN_BIT_OR", "|="], ["ASSIGN_BIT_XOR", "^="], ["ASSIGN
_BIT_AND", "&="], ["ASSIGN_SHL", "<<="], ["ASSIGN_SAR", ">>="], ["ASSIGN_SHR", "
>>>="], | |
| 170 ["ASSIGN_ADD", "+="], ["ASSIGN_SUB", "-="], ["ASSIGN_MUL", "*="], ["ASSIGN_D
IV", "/="], ["ASSIGN_MOD", "%="], | |
| 171 ["COMMA", ","], ["OR", "||"], ["AND", "&&"], ["BIT_OR", "|"], ["BIT_XOR", "^
"], ["BIT_AND", "&"], ["SHL", "<<"], ["SAR", ">>"], ["SHR", ">>>"], | |
| 172 ["ADD", "+"], ["SUB", "-"], ["MUL", "*"], ["DIV", "/"], ["MOD", "%"], | |
| 173 ["EQ", "=="], ["NE", "!="], ["EQ_STRICT", "==="], ["NE_STRICT", "!=="], ["LT
", "<"], ["GT", ">"], ["LTE", "<="], ["GTE", ">="], | |
| 174 ["INSTANCEOF", "instanceof"], ["IN", "in"], ["NOT", "!"], ["BIT_NOT", "~"],
["DELETE", "delete"], ["TYPEOF", "typeof"], ["VOID", "void"], | |
| 175 ["BREAK", "break"], ["CASE", "case"], ["CATCH", "catch"], ["CONTINUE", "cont
inue"], ["DEBUGGER", "debugger"], ["DEFAULT", "default"], ["DO", "do"], ["ELSE",
"else"], ["FINALLY", "finally"], | |
| 176 ["FOR", "for"], ["FUNCTION", "function"], ["IF", "if"], ["NEW", "new"], ["RE
TURN", "return"], ["SWITCH", "switch"], ["THIS", "this"], ["THROW", "throw"], ["
TRY", "try"], ["VAR", "var"], | |
| 177 ["WHILE", "while"], ["WITH", "with"], ["NULL_LITERAL", "null"], ["TRUE_LITER
AL", "true"], ["FALSE_LITERAL", "false"], ["NUMBER"], ["STRING"], ["IDENTIFIER"]
, ["CONST", "const"] | |
| 178 ]; | |
| 179 | |
| 180 var Tokens = {}; | |
| 181 for (var i = 0; i < tokens.length; ++i) | |
| 182 Tokens[tokens[i][0]] = i; | |
| 183 | |
| 184 var TokensByValue = {}; | |
| 185 for (var i = 0; i < tokens.length; ++i) { | |
| 186 if (tokens[i][1]) | |
| 187 TokensByValue[tokens[i][1]] = i; | |
| 188 } | |
| 189 | |
| 190 var TokensByType = { | |
| 191 "eof": Tokens.EOS, | |
| 192 "name": Tokens.IDENTIFIER, | |
| 193 "num": Tokens.NUMBER, | |
| 194 "regexp": Tokens.DIV, | |
| 195 "string": Tokens.STRING | |
| 196 }; | |
| 197 | |
| 198 function Tokenizer(content) | |
| 199 { | |
| 200 this._readNextToken = parse.tokenizer(content); | |
| 201 this._state = this._readNextToken.context(); | |
| 202 } | |
| 203 | |
| 204 Tokenizer.prototype = { | |
| 205 content: function() | |
| 206 { | |
| 207 return this._state.text; | |
| 208 }, | |
| 209 | |
| 210 next: function(forceRegexp) | |
| 211 { | |
| 212 var uglifyToken = this._readNextToken(forceRegexp); | |
| 213 uglifyToken.endPos = this._state.pos; | |
| 214 uglifyToken.endLine = this._state.line; | |
| 215 uglifyToken.token = this._convertUglifyToken(uglifyToken); | |
| 216 return uglifyToken; | |
| 217 }, | |
| 218 | |
| 219 _convertUglifyToken: function(uglifyToken) | |
| 220 { | |
| 221 var token = TokensByType[uglifyToken.type]; | |
| 222 if (typeof token === "number") | |
| 223 return token; | |
| 224 token = TokensByValue[uglifyToken.value]; | |
| 225 if (typeof token === "number") | |
| 226 return token; | |
| 227 throw "Unknown token type " + uglifyToken.type; | |
| 228 } | |
| 229 } | |
| 230 | |
| 231 function JavaScriptFormatter(tokenizer, builder) | |
| 232 { | 37 { |
| 233 this._tokenizer = tokenizer; | 38 this._tokenizer = tokenizer; |
| 234 this._builder = builder; | 39 this._builder = builder; |
| 235 this._token = null; | 40 this._token = null; |
| 236 this._nextToken = this._tokenizer.next(); | 41 this._nextToken = this._tokenizer.next(); |
| 237 } | 42 } |
| 238 | 43 |
| 239 JavaScriptFormatter.prototype = { | 44 FormatterWorker.JavaScriptFormatter.prototype = { |
| 240 format: function() | 45 format: function() |
| 241 { | 46 { |
| 242 this._parseSourceElements(Tokens.EOS); | 47 this._parseSourceElements(FormatterWorker.JavaScriptTokens.EOS); |
| 243 this._consume(Tokens.EOS); | 48 this._consume(FormatterWorker.JavaScriptTokens.EOS); |
| 244 }, | 49 }, |
| 245 | 50 |
| 51 /** |
| 52 * @return {string} |
| 53 */ |
| 246 _peek: function() | 54 _peek: function() |
| 247 { | 55 { |
| 248 return this._nextToken.token; | 56 return this._nextToken.token; |
| 249 }, | 57 }, |
| 250 | 58 |
| 59 /** |
| 60 * @return {string} |
| 61 */ |
| 251 _next: function() | 62 _next: function() |
| 252 { | 63 { |
| 253 if (this._token && this._token.token === Tokens.EOS) | 64 if (this._token && this._token.token === FormatterWorker.JavaScriptToken
s.EOS) |
| 254 throw "Unexpected EOS token"; | 65 throw "Unexpected EOS token"; |
| 255 | 66 |
| 256 this._builder.addToken(this._nextToken); | 67 this._builder.addToken(this._nextToken); |
| 257 this._token = this._nextToken; | 68 this._token = this._nextToken; |
| 258 this._nextToken = this._tokenizer.next(this._forceRegexp); | 69 this._nextToken = this._tokenizer.next(this._forceRegexp); |
| 259 this._forceRegexp = false; | 70 this._forceRegexp = false; |
| 260 return this._token.token; | 71 return this._token.token; |
| 261 }, | 72 }, |
| 262 | 73 |
| 74 /** |
| 75 * @param {string} token |
| 76 */ |
| 263 _consume: function(token) | 77 _consume: function(token) |
| 264 { | 78 { |
| 265 var next = this._next(); | 79 var next = this._next(); |
| 266 if (next !== token) | 80 if (next !== token) |
| 267 throw "Unexpected token in consume: expected " + token + ", actual "
+ next; | 81 throw "Unexpected token in consume: expected " + token + ", actual "
+ next; |
| 268 }, | 82 }, |
| 269 | 83 |
| 84 /** |
| 85 * @param {string} token |
| 86 */ |
| 270 _expect: function(token) | 87 _expect: function(token) |
| 271 { | 88 { |
| 272 var next = this._next(); | 89 var next = this._next(); |
| 273 if (next !== token) | 90 if (next !== token) |
| 274 throw "Unexpected token: expected " + token + ", actual " + next; | 91 throw "Unexpected token: expected " + token + ", actual " + next; |
| 275 }, | 92 }, |
| 276 | 93 |
| 277 _expectSemicolon: function() | 94 _expectSemicolon: function() |
| 278 { | 95 { |
| 279 if (this._peek() === Tokens.SEMICOLON) | 96 if (this._peek() === FormatterWorker.JavaScriptTokens.SEMICOLON) |
| 280 this._consume(Tokens.SEMICOLON); | 97 this._consume(FormatterWorker.JavaScriptTokens.SEMICOLON); |
| 281 }, | 98 }, |
| 282 | 99 |
| 100 /** |
| 101 * @return {boolean} |
| 102 */ |
| 283 _hasLineTerminatorBeforeNext: function() | 103 _hasLineTerminatorBeforeNext: function() |
| 284 { | 104 { |
| 285 return this._nextToken.nlb; | 105 return this._nextToken.nlb; |
| 286 }, | 106 }, |
| 287 | 107 |
| 108 /** |
| 109 * @param {string} endToken |
| 110 */ |
| 288 _parseSourceElements: function(endToken) | 111 _parseSourceElements: function(endToken) |
| 289 { | 112 { |
| 290 while (this._peek() !== endToken) { | 113 while (this._peek() !== endToken) { |
| 291 this._parseStatement(); | 114 this._parseStatement(); |
| 292 this._builder.addNewLine(); | 115 this._builder.addNewLine(); |
| 293 } | 116 } |
| 294 }, | 117 }, |
| 295 | 118 |
| 296 _parseStatementOrBlock: function() | 119 _parseStatementOrBlock: function() |
| 297 { | 120 { |
| 298 if (this._peek() === Tokens.LBRACE) { | 121 if (this._peek() === FormatterWorker.JavaScriptTokens.LBRACE) { |
| 299 this._builder.addSpace(); | 122 this._builder.addSpace(); |
| 300 this._parseBlock(); | 123 this._parseBlock(); |
| 301 return true; | 124 return true; |
| 302 } | 125 } |
| 303 | 126 |
| 304 this._builder.addNewLine(); | 127 this._builder.addNewLine(); |
| 305 this._builder.increaseNestingLevel(); | 128 this._builder.increaseNestingLevel(); |
| 306 this._parseStatement(); | 129 this._parseStatement(); |
| 307 this._builder.decreaseNestingLevel(); | 130 this._builder.decreaseNestingLevel(); |
| 308 }, | 131 }, |
| 309 | 132 |
| 310 _parseStatement: function() | 133 _parseStatement: function() |
| 311 { | 134 { |
| 312 switch (this._peek()) { | 135 switch (this._peek()) { |
| 313 case Tokens.LBRACE: | 136 case FormatterWorker.JavaScriptTokens.LBRACE: |
| 314 return this._parseBlock(); | 137 return this._parseBlock(); |
| 315 case Tokens.CONST: | 138 case FormatterWorker.JavaScriptTokens.CONST: |
| 316 case Tokens.VAR: | 139 case FormatterWorker.JavaScriptTokens.VAR: |
| 317 return this._parseVariableStatement(); | 140 return this._parseVariableStatement(); |
| 318 case Tokens.SEMICOLON: | 141 case FormatterWorker.JavaScriptTokens.SEMICOLON: |
| 319 return this._next(); | 142 return this._next(); |
| 320 case Tokens.IF: | 143 case FormatterWorker.JavaScriptTokens.IF: |
| 321 return this._parseIfStatement(); | 144 return this._parseIfStatement(); |
| 322 case Tokens.DO: | 145 case FormatterWorker.JavaScriptTokens.DO: |
| 323 return this._parseDoWhileStatement(); | 146 return this._parseDoWhileStatement(); |
| 324 case Tokens.WHILE: | 147 case FormatterWorker.JavaScriptTokens.WHILE: |
| 325 return this._parseWhileStatement(); | 148 return this._parseWhileStatement(); |
| 326 case Tokens.FOR: | 149 case FormatterWorker.JavaScriptTokens.FOR: |
| 327 return this._parseForStatement(); | 150 return this._parseForStatement(); |
| 328 case Tokens.CONTINUE: | 151 case FormatterWorker.JavaScriptTokens.CONTINUE: |
| 329 return this._parseContinueStatement(); | 152 return this._parseContinueStatement(); |
| 330 case Tokens.BREAK: | 153 case FormatterWorker.JavaScriptTokens.BREAK: |
| 331 return this._parseBreakStatement(); | 154 return this._parseBreakStatement(); |
| 332 case Tokens.RETURN: | 155 case FormatterWorker.JavaScriptTokens.RETURN: |
| 333 return this._parseReturnStatement(); | 156 return this._parseReturnStatement(); |
| 334 case Tokens.WITH: | 157 case FormatterWorker.JavaScriptTokens.WITH: |
| 335 return this._parseWithStatement(); | 158 return this._parseWithStatement(); |
| 336 case Tokens.SWITCH: | 159 case FormatterWorker.JavaScriptTokens.SWITCH: |
| 337 return this._parseSwitchStatement(); | 160 return this._parseSwitchStatement(); |
| 338 case Tokens.THROW: | 161 case FormatterWorker.JavaScriptTokens.THROW: |
| 339 return this._parseThrowStatement(); | 162 return this._parseThrowStatement(); |
| 340 case Tokens.TRY: | 163 case FormatterWorker.JavaScriptTokens.TRY: |
| 341 return this._parseTryStatement(); | 164 return this._parseTryStatement(); |
| 342 case Tokens.FUNCTION: | 165 case FormatterWorker.JavaScriptTokens.FUNCTION: |
| 343 return this._parseFunctionDeclaration(); | 166 return this._parseFunctionDeclaration(); |
| 344 case Tokens.DEBUGGER: | 167 case FormatterWorker.JavaScriptTokens.DEBUGGER: |
| 345 return this._parseDebuggerStatement(); | 168 return this._parseDebuggerStatement(); |
| 346 default: | 169 default: |
| 347 return this._parseExpressionOrLabelledStatement(); | 170 return this._parseExpressionOrLabelledStatement(); |
| 348 } | 171 } |
| 349 }, | 172 }, |
| 350 | 173 |
| 351 _parseFunctionDeclaration: function() | 174 _parseFunctionDeclaration: function() |
| 352 { | 175 { |
| 353 this._expect(Tokens.FUNCTION); | 176 this._expect(FormatterWorker.JavaScriptTokens.FUNCTION); |
| 354 this._builder.addSpace(); | 177 this._builder.addSpace(); |
| 355 this._expect(Tokens.IDENTIFIER); | 178 this._expect(FormatterWorker.JavaScriptTokens.IDENTIFIER); |
| 356 this._parseFunctionLiteral() | 179 this._parseFunctionLiteral() |
| 357 }, | 180 }, |
| 358 | 181 |
| 359 _parseBlock: function() | 182 _parseBlock: function() |
| 360 { | 183 { |
| 361 this._expect(Tokens.LBRACE); | 184 this._expect(FormatterWorker.JavaScriptTokens.LBRACE); |
| 362 this._builder.addNewLine(); | 185 this._builder.addNewLine(); |
| 363 this._builder.increaseNestingLevel(); | 186 this._builder.increaseNestingLevel(); |
| 364 while (this._peek() !== Tokens.RBRACE) { | 187 while (this._peek() !== FormatterWorker.JavaScriptTokens.RBRACE) { |
| 365 this._parseStatement(); | 188 this._parseStatement(); |
| 366 this._builder.addNewLine(); | 189 this._builder.addNewLine(); |
| 367 } | 190 } |
| 368 this._builder.decreaseNestingLevel(); | 191 this._builder.decreaseNestingLevel(); |
| 369 this._expect(Tokens.RBRACE); | 192 this._expect(FormatterWorker.JavaScriptTokens.RBRACE); |
| 370 }, | 193 }, |
| 371 | 194 |
| 372 _parseVariableStatement: function() | 195 _parseVariableStatement: function() |
| 373 { | 196 { |
| 374 this._parseVariableDeclarations(); | 197 this._parseVariableDeclarations(); |
| 375 this._expectSemicolon(); | 198 this._expectSemicolon(); |
| 376 }, | 199 }, |
| 377 | 200 |
| 378 _parseVariableDeclarations: function() | 201 _parseVariableDeclarations: function() |
| 379 { | 202 { |
| 380 if (this._peek() === Tokens.VAR) | 203 if (this._peek() === FormatterWorker.JavaScriptTokens.VAR) |
| 381 this._consume(Tokens.VAR); | 204 this._consume(FormatterWorker.JavaScriptTokens.VAR); |
| 382 else | 205 else |
| 383 this._consume(Tokens.CONST) | 206 this._consume(FormatterWorker.JavaScriptTokens.CONST) |
| 384 this._builder.addSpace(); | 207 this._builder.addSpace(); |
| 385 | 208 |
| 386 var isFirstVariable = true; | 209 var isFirstVariable = true; |
| 387 do { | 210 do { |
| 388 if (!isFirstVariable) { | 211 if (!isFirstVariable) { |
| 389 this._consume(Tokens.COMMA); | 212 this._consume(FormatterWorker.JavaScriptTokens.COMMA); |
| 390 this._builder.addSpace(); | 213 this._builder.addSpace(); |
| 391 } | 214 } |
| 392 isFirstVariable = false; | 215 isFirstVariable = false; |
| 393 this._expect(Tokens.IDENTIFIER); | 216 this._expect(FormatterWorker.JavaScriptTokens.IDENTIFIER); |
| 394 if (this._peek() === Tokens.ASSIGN) { | 217 if (this._peek() === FormatterWorker.JavaScriptTokens.ASSIGN) { |
| 395 this._builder.addSpace(); | 218 this._builder.addSpace(); |
| 396 this._consume(Tokens.ASSIGN); | 219 this._consume(FormatterWorker.JavaScriptTokens.ASSIGN); |
| 397 this._builder.addSpace(); | 220 this._builder.addSpace(); |
| 398 this._parseAssignmentExpression(); | 221 this._parseAssignmentExpression(); |
| 399 } | 222 } |
| 400 } while (this._peek() === Tokens.COMMA); | 223 } while (this._peek() === FormatterWorker.JavaScriptTokens.COMMA); |
| 401 }, | 224 }, |
| 402 | 225 |
| 403 _parseExpressionOrLabelledStatement: function() | 226 _parseExpressionOrLabelledStatement: function() |
| 404 { | 227 { |
| 405 this._parseExpression(); | 228 this._parseExpression(); |
| 406 if (this._peek() === Tokens.COLON) { | 229 if (this._peek() === FormatterWorker.JavaScriptTokens.COLON) { |
| 407 this._expect(Tokens.COLON); | 230 this._expect(FormatterWorker.JavaScriptTokens.COLON); |
| 408 this._builder.addSpace(); | 231 this._builder.addSpace(); |
| 409 this._parseStatement(); | 232 this._parseStatement(); |
| 410 } | 233 } |
| 411 this._expectSemicolon(); | 234 this._expectSemicolon(); |
| 412 }, | 235 }, |
| 413 | 236 |
| 414 _parseIfStatement: function() | 237 _parseIfStatement: function() |
| 415 { | 238 { |
| 416 this._expect(Tokens.IF); | 239 this._expect(FormatterWorker.JavaScriptTokens.IF); |
| 417 this._builder.addSpace(); | 240 this._builder.addSpace(); |
| 418 this._expect(Tokens.LPAREN); | 241 this._expect(FormatterWorker.JavaScriptTokens.LPAREN); |
| 419 this._parseExpression(); | 242 this._parseExpression(); |
| 420 this._expect(Tokens.RPAREN); | 243 this._expect(FormatterWorker.JavaScriptTokens.RPAREN); |
| 421 | 244 |
| 422 var isBlock = this._parseStatementOrBlock(); | 245 var isBlock = this._parseStatementOrBlock(); |
| 423 if (this._peek() === Tokens.ELSE) { | 246 if (this._peek() === FormatterWorker.JavaScriptTokens.ELSE) { |
| 424 if (isBlock) | 247 if (isBlock) |
| 425 this._builder.addSpace(); | 248 this._builder.addSpace(); |
| 426 else | 249 else |
| 427 this._builder.addNewLine(); | 250 this._builder.addNewLine(); |
| 428 this._next(); | 251 this._next(); |
| 429 | 252 |
| 430 if (this._peek() === Tokens.IF) { | 253 if (this._peek() === FormatterWorker.JavaScriptTokens.IF) { |
| 431 this._builder.addSpace(); | 254 this._builder.addSpace(); |
| 432 this._parseStatement(); | 255 this._parseStatement(); |
| 433 } else | 256 } else |
| 434 this._parseStatementOrBlock(); | 257 this._parseStatementOrBlock(); |
| 435 } | 258 } |
| 436 }, | 259 }, |
| 437 | 260 |
| 438 _parseContinueStatement: function() | 261 _parseContinueStatement: function() |
| 439 { | 262 { |
| 440 this._expect(Tokens.CONTINUE); | 263 this._expect(FormatterWorker.JavaScriptTokens.CONTINUE); |
| 441 var token = this._peek(); | 264 var token = this._peek(); |
| 442 if (!this._hasLineTerminatorBeforeNext() && token !== Tokens.SEMICOLON &
& token !== Tokens.RBRACE && token !== Tokens.EOS) { | 265 if (!this._hasLineTerminatorBeforeNext() && token !== FormatterWorker.Ja
vaScriptTokens.SEMICOLON && token !== FormatterWorker.JavaScriptTokens.RBRACE &&
token !== FormatterWorker.JavaScriptTokens.EOS) { |
| 443 this._builder.addSpace(); | 266 this._builder.addSpace(); |
| 444 this._expect(Tokens.IDENTIFIER); | 267 this._expect(FormatterWorker.JavaScriptTokens.IDENTIFIER); |
| 445 } | 268 } |
| 446 this._expectSemicolon(); | 269 this._expectSemicolon(); |
| 447 }, | 270 }, |
| 448 | 271 |
| 449 _parseBreakStatement: function() | 272 _parseBreakStatement: function() |
| 450 { | 273 { |
| 451 this._expect(Tokens.BREAK); | 274 this._expect(FormatterWorker.JavaScriptTokens.BREAK); |
| 452 var token = this._peek(); | 275 var token = this._peek(); |
| 453 if (!this._hasLineTerminatorBeforeNext() && token !== Tokens.SEMICOLON &
& token !== Tokens.RBRACE && token !== Tokens.EOS) { | 276 if (!this._hasLineTerminatorBeforeNext() && token !== FormatterWorker.Ja
vaScriptTokens.SEMICOLON && token !== FormatterWorker.JavaScriptTokens.RBRACE &&
token !== FormatterWorker.JavaScriptTokens.EOS) { |
| 454 this._builder.addSpace(); | 277 this._builder.addSpace(); |
| 455 this._expect(Tokens.IDENTIFIER); | 278 this._expect(FormatterWorker.JavaScriptTokens.IDENTIFIER); |
| 456 } | 279 } |
| 457 this._expectSemicolon(); | 280 this._expectSemicolon(); |
| 458 }, | 281 }, |
| 459 | 282 |
| 460 _parseReturnStatement: function() | 283 _parseReturnStatement: function() |
| 461 { | 284 { |
| 462 this._expect(Tokens.RETURN); | 285 this._expect(FormatterWorker.JavaScriptTokens.RETURN); |
| 463 var token = this._peek(); | 286 var token = this._peek(); |
| 464 if (!this._hasLineTerminatorBeforeNext() && token !== Tokens.SEMICOLON &
& token !== Tokens.RBRACE && token !== Tokens.EOS) { | 287 if (!this._hasLineTerminatorBeforeNext() && token !== FormatterWorker.Ja
vaScriptTokens.SEMICOLON && token !== FormatterWorker.JavaScriptTokens.RBRACE &&
token !== FormatterWorker.JavaScriptTokens.EOS) { |
| 465 this._builder.addSpace(); | 288 this._builder.addSpace(); |
| 466 this._parseExpression(); | 289 this._parseExpression(); |
| 467 } | 290 } |
| 468 this._expectSemicolon(); | 291 this._expectSemicolon(); |
| 469 }, | 292 }, |
| 470 | 293 |
| 471 _parseWithStatement: function() | 294 _parseWithStatement: function() |
| 472 { | 295 { |
| 473 this._expect(Tokens.WITH); | 296 this._expect(FormatterWorker.JavaScriptTokens.WITH); |
| 474 this._builder.addSpace(); | 297 this._builder.addSpace(); |
| 475 this._expect(Tokens.LPAREN); | 298 this._expect(FormatterWorker.JavaScriptTokens.LPAREN); |
| 476 this._parseExpression(); | 299 this._parseExpression(); |
| 477 this._expect(Tokens.RPAREN); | 300 this._expect(FormatterWorker.JavaScriptTokens.RPAREN); |
| 478 this._parseStatementOrBlock(); | 301 this._parseStatementOrBlock(); |
| 479 }, | 302 }, |
| 480 | 303 |
| 481 _parseCaseClause: function() | 304 _parseCaseClause: function() |
| 482 { | 305 { |
| 483 if (this._peek() === Tokens.CASE) { | 306 if (this._peek() === FormatterWorker.JavaScriptTokens.CASE) { |
| 484 this._expect(Tokens.CASE); | 307 this._expect(FormatterWorker.JavaScriptTokens.CASE); |
| 485 this._builder.addSpace(); | 308 this._builder.addSpace(); |
| 486 this._parseExpression(); | 309 this._parseExpression(); |
| 487 } else | 310 } else |
| 488 this._expect(Tokens.DEFAULT); | 311 this._expect(FormatterWorker.JavaScriptTokens.DEFAULT); |
| 489 this._expect(Tokens.COLON); | 312 this._expect(FormatterWorker.JavaScriptTokens.COLON); |
| 490 this._builder.addNewLine(); | 313 this._builder.addNewLine(); |
| 491 | 314 |
| 492 this._builder.increaseNestingLevel(); | 315 this._builder.increaseNestingLevel(); |
| 493 while (this._peek() !== Tokens.CASE && this._peek() !== Tokens.DEFAULT &
& this._peek() !== Tokens.RBRACE) { | 316 while (this._peek() !== FormatterWorker.JavaScriptTokens.CASE && this._p
eek() !== FormatterWorker.JavaScriptTokens.DEFAULT && this._peek() !== Formatter
Worker.JavaScriptTokens.RBRACE) { |
| 494 this._parseStatement(); | 317 this._parseStatement(); |
| 495 this._builder.addNewLine(); | 318 this._builder.addNewLine(); |
| 496 } | 319 } |
| 497 this._builder.decreaseNestingLevel(); | 320 this._builder.decreaseNestingLevel(); |
| 498 }, | 321 }, |
| 499 | 322 |
| 500 _parseSwitchStatement: function() | 323 _parseSwitchStatement: function() |
| 501 { | 324 { |
| 502 this._expect(Tokens.SWITCH); | 325 this._expect(FormatterWorker.JavaScriptTokens.SWITCH); |
| 503 this._builder.addSpace(); | 326 this._builder.addSpace(); |
| 504 this._expect(Tokens.LPAREN); | 327 this._expect(FormatterWorker.JavaScriptTokens.LPAREN); |
| 505 this._parseExpression(); | 328 this._parseExpression(); |
| 506 this._expect(Tokens.RPAREN); | 329 this._expect(FormatterWorker.JavaScriptTokens.RPAREN); |
| 507 this._builder.addSpace(); | 330 this._builder.addSpace(); |
| 508 | 331 |
| 509 this._expect(Tokens.LBRACE); | 332 this._expect(FormatterWorker.JavaScriptTokens.LBRACE); |
| 510 this._builder.addNewLine(); | 333 this._builder.addNewLine(); |
| 511 this._builder.increaseNestingLevel(); | 334 this._builder.increaseNestingLevel(); |
| 512 while (this._peek() !== Tokens.RBRACE) | 335 while (this._peek() !== FormatterWorker.JavaScriptTokens.RBRACE) |
| 513 this._parseCaseClause(); | 336 this._parseCaseClause(); |
| 514 this._builder.decreaseNestingLevel(); | 337 this._builder.decreaseNestingLevel(); |
| 515 this._expect(Tokens.RBRACE); | 338 this._expect(FormatterWorker.JavaScriptTokens.RBRACE); |
| 516 }, | 339 }, |
| 517 | 340 |
| 518 _parseThrowStatement: function() | 341 _parseThrowStatement: function() |
| 519 { | 342 { |
| 520 this._expect(Tokens.THROW); | 343 this._expect(FormatterWorker.JavaScriptTokens.THROW); |
| 521 this._builder.addSpace(); | 344 this._builder.addSpace(); |
| 522 this._parseExpression(); | 345 this._parseExpression(); |
| 523 this._expectSemicolon(); | 346 this._expectSemicolon(); |
| 524 }, | 347 }, |
| 525 | 348 |
| 526 _parseTryStatement: function() | 349 _parseTryStatement: function() |
| 527 { | 350 { |
| 528 this._expect(Tokens.TRY); | 351 this._expect(FormatterWorker.JavaScriptTokens.TRY); |
| 529 this._builder.addSpace(); | 352 this._builder.addSpace(); |
| 530 this._parseBlock(); | 353 this._parseBlock(); |
| 531 | 354 |
| 532 var token = this._peek(); | 355 var token = this._peek(); |
| 533 if (token === Tokens.CATCH) { | 356 if (token === FormatterWorker.JavaScriptTokens.CATCH) { |
| 534 this._builder.addSpace(); | 357 this._builder.addSpace(); |
| 535 this._consume(Tokens.CATCH); | 358 this._consume(FormatterWorker.JavaScriptTokens.CATCH); |
| 536 this._builder.addSpace(); | 359 this._builder.addSpace(); |
| 537 this._expect(Tokens.LPAREN); | 360 this._expect(FormatterWorker.JavaScriptTokens.LPAREN); |
| 538 this._expect(Tokens.IDENTIFIER); | 361 this._expect(FormatterWorker.JavaScriptTokens.IDENTIFIER); |
| 539 this._expect(Tokens.RPAREN); | 362 this._expect(FormatterWorker.JavaScriptTokens.RPAREN); |
| 540 this._builder.addSpace(); | 363 this._builder.addSpace(); |
| 541 this._parseBlock(); | 364 this._parseBlock(); |
| 542 token = this._peek(); | 365 token = this._peek(); |
| 543 } | 366 } |
| 544 | 367 |
| 545 if (token === Tokens.FINALLY) { | 368 if (token === FormatterWorker.JavaScriptTokens.FINALLY) { |
| 546 this._consume(Tokens.FINALLY); | 369 this._consume(FormatterWorker.JavaScriptTokens.FINALLY); |
| 547 this._builder.addSpace(); | 370 this._builder.addSpace(); |
| 548 this._parseBlock(); | 371 this._parseBlock(); |
| 549 } | 372 } |
| 550 }, | 373 }, |
| 551 | 374 |
| 552 _parseDoWhileStatement: function() | 375 _parseDoWhileStatement: function() |
| 553 { | 376 { |
| 554 this._expect(Tokens.DO); | 377 this._expect(FormatterWorker.JavaScriptTokens.DO); |
| 555 var isBlock = this._parseStatementOrBlock(); | 378 var isBlock = this._parseStatementOrBlock(); |
| 556 if (isBlock) | 379 if (isBlock) |
| 557 this._builder.addSpace(); | 380 this._builder.addSpace(); |
| 558 else | 381 else |
| 559 this._builder.addNewLine(); | 382 this._builder.addNewLine(); |
| 560 this._expect(Tokens.WHILE); | 383 this._expect(FormatterWorker.JavaScriptTokens.WHILE); |
| 561 this._builder.addSpace(); | 384 this._builder.addSpace(); |
| 562 this._expect(Tokens.LPAREN); | 385 this._expect(FormatterWorker.JavaScriptTokens.LPAREN); |
| 563 this._parseExpression(); | 386 this._parseExpression(); |
| 564 this._expect(Tokens.RPAREN); | 387 this._expect(FormatterWorker.JavaScriptTokens.RPAREN); |
| 565 this._expectSemicolon(); | 388 this._expectSemicolon(); |
| 566 }, | 389 }, |
| 567 | 390 |
| 568 _parseWhileStatement: function() | 391 _parseWhileStatement: function() |
| 569 { | 392 { |
| 570 this._expect(Tokens.WHILE); | 393 this._expect(FormatterWorker.JavaScriptTokens.WHILE); |
| 571 this._builder.addSpace(); | 394 this._builder.addSpace(); |
| 572 this._expect(Tokens.LPAREN); | 395 this._expect(FormatterWorker.JavaScriptTokens.LPAREN); |
| 573 this._parseExpression(); | 396 this._parseExpression(); |
| 574 this._expect(Tokens.RPAREN); | 397 this._expect(FormatterWorker.JavaScriptTokens.RPAREN); |
| 575 this._parseStatementOrBlock(); | 398 this._parseStatementOrBlock(); |
| 576 }, | 399 }, |
| 577 | 400 |
| 578 _parseForStatement: function() | 401 _parseForStatement: function() |
| 579 { | 402 { |
| 580 this._expect(Tokens.FOR); | 403 this._expect(FormatterWorker.JavaScriptTokens.FOR); |
| 581 this._builder.addSpace(); | 404 this._builder.addSpace(); |
| 582 this._expect(Tokens.LPAREN); | 405 this._expect(FormatterWorker.JavaScriptTokens.LPAREN); |
| 583 if (this._peek() !== Tokens.SEMICOLON) { | 406 if (this._peek() !== FormatterWorker.JavaScriptTokens.SEMICOLON) { |
| 584 if (this._peek() === Tokens.VAR || this._peek() === Tokens.CONST) { | 407 if (this._peek() === FormatterWorker.JavaScriptTokens.VAR || this._p
eek() === FormatterWorker.JavaScriptTokens.CONST) { |
| 585 this._parseVariableDeclarations(); | 408 this._parseVariableDeclarations(); |
| 586 if (this._peek() === Tokens.IN) { | 409 if (this._peek() === FormatterWorker.JavaScriptTokens.IN) { |
| 587 this._builder.addSpace(); | 410 this._builder.addSpace(); |
| 588 this._consume(Tokens.IN); | 411 this._consume(FormatterWorker.JavaScriptTokens.IN); |
| 589 this._builder.addSpace(); | 412 this._builder.addSpace(); |
| 590 this._parseExpression(); | 413 this._parseExpression(); |
| 591 } | 414 } |
| 592 } else | 415 } else |
| 593 this._parseExpression(); | 416 this._parseExpression(); |
| 594 } | 417 } |
| 595 | 418 |
| 596 if (this._peek() !== Tokens.RPAREN) { | 419 if (this._peek() !== FormatterWorker.JavaScriptTokens.RPAREN) { |
| 597 this._expect(Tokens.SEMICOLON); | 420 this._expect(FormatterWorker.JavaScriptTokens.SEMICOLON); |
| 598 this._builder.addSpace(); | 421 this._builder.addSpace(); |
| 599 if (this._peek() !== Tokens.SEMICOLON) | 422 if (this._peek() !== FormatterWorker.JavaScriptTokens.SEMICOLON) |
| 600 this._parseExpression(); | 423 this._parseExpression(); |
| 601 this._expect(Tokens.SEMICOLON); | 424 this._expect(FormatterWorker.JavaScriptTokens.SEMICOLON); |
| 602 this._builder.addSpace(); | 425 this._builder.addSpace(); |
| 603 if (this._peek() !== Tokens.RPAREN) | 426 if (this._peek() !== FormatterWorker.JavaScriptTokens.RPAREN) |
| 604 this._parseExpression(); | 427 this._parseExpression(); |
| 605 } | 428 } |
| 606 this._expect(Tokens.RPAREN); | 429 this._expect(FormatterWorker.JavaScriptTokens.RPAREN); |
| 607 | 430 |
| 608 this._parseStatementOrBlock(); | 431 this._parseStatementOrBlock(); |
| 609 }, | 432 }, |
| 610 | 433 |
| 611 _parseExpression: function() | 434 _parseExpression: function() |
| 612 { | 435 { |
| 613 this._parseAssignmentExpression(); | 436 this._parseAssignmentExpression(); |
| 614 while (this._peek() === Tokens.COMMA) { | 437 while (this._peek() === FormatterWorker.JavaScriptTokens.COMMA) { |
| 615 this._expect(Tokens.COMMA); | 438 this._expect(FormatterWorker.JavaScriptTokens.COMMA); |
| 616 this._builder.addSpace(); | 439 this._builder.addSpace(); |
| 617 this._parseAssignmentExpression(); | 440 this._parseAssignmentExpression(); |
| 618 } | 441 } |
| 619 }, | 442 }, |
| 620 | 443 |
| 621 _parseAssignmentExpression: function() | 444 _parseAssignmentExpression: function() |
| 622 { | 445 { |
| 623 this._parseConditionalExpression(); | 446 this._parseConditionalExpression(); |
| 624 var token = this._peek(); | 447 var token = this._peek(); |
| 625 if (Tokens.ASSIGN <= token && token <= Tokens.ASSIGN_MOD) { | 448 if (FormatterWorker.JavaScriptTokens.ASSIGN <= token && token <= Formatt
erWorker.JavaScriptTokens.ASSIGN_MOD) { |
| 626 this._builder.addSpace(); | 449 this._builder.addSpace(); |
| 627 this._next(); | 450 this._next(); |
| 628 this._builder.addSpace(); | 451 this._builder.addSpace(); |
| 629 this._parseAssignmentExpression(); | 452 this._parseAssignmentExpression(); |
| 630 } | 453 } |
| 631 }, | 454 }, |
| 632 | 455 |
| 633 _parseConditionalExpression: function() | 456 _parseConditionalExpression: function() |
| 634 { | 457 { |
| 635 this._parseBinaryExpression(); | 458 this._parseBinaryExpression(); |
| 636 if (this._peek() === Tokens.CONDITIONAL) { | 459 if (this._peek() === FormatterWorker.JavaScriptTokens.CONDITIONAL) { |
| 637 this._builder.addSpace(); | 460 this._builder.addSpace(); |
| 638 this._consume(Tokens.CONDITIONAL); | 461 this._consume(FormatterWorker.JavaScriptTokens.CONDITIONAL); |
| 639 this._builder.addSpace(); | 462 this._builder.addSpace(); |
| 640 this._parseAssignmentExpression(); | 463 this._parseAssignmentExpression(); |
| 641 this._builder.addSpace(); | 464 this._builder.addSpace(); |
| 642 this._expect(Tokens.COLON); | 465 this._expect(FormatterWorker.JavaScriptTokens.COLON); |
| 643 this._builder.addSpace(); | 466 this._builder.addSpace(); |
| 644 this._parseAssignmentExpression(); | 467 this._parseAssignmentExpression(); |
| 645 } | 468 } |
| 646 }, | 469 }, |
| 647 | 470 |
| 648 _parseBinaryExpression: function() | 471 _parseBinaryExpression: function() |
| 649 { | 472 { |
| 650 this._parseUnaryExpression(); | 473 this._parseUnaryExpression(); |
| 651 var token = this._peek(); | 474 var token = this._peek(); |
| 652 while (Tokens.OR <= token && token <= Tokens.IN) { | 475 while (FormatterWorker.JavaScriptTokens.OR <= token && token <= Formatte
rWorker.JavaScriptTokens.IN) { |
| 653 this._builder.addSpace(); | 476 this._builder.addSpace(); |
| 654 this._next(); | 477 this._next(); |
| 655 this._builder.addSpace(); | 478 this._builder.addSpace(); |
| 656 this._parseBinaryExpression(); | 479 this._parseBinaryExpression(); |
| 657 token = this._peek(); | 480 token = this._peek(); |
| 658 } | 481 } |
| 659 }, | 482 }, |
| 660 | 483 |
| 661 _parseUnaryExpression: function() | 484 _parseUnaryExpression: function() |
| 662 { | 485 { |
| 663 var token = this._peek(); | 486 var token = this._peek(); |
| 664 if ((Tokens.NOT <= token && token <= Tokens.VOID) || token === Tokens.AD
D || token === Tokens.SUB || token === Tokens.INC || token === Tokens.DEC) { | 487 if ((FormatterWorker.JavaScriptTokens.NOT <= token && token <= Formatter
Worker.JavaScriptTokens.VOID) || token === FormatterWorker.JavaScriptTokens.ADD
|| token === FormatterWorker.JavaScriptTokens.SUB || token === FormatterWorker.
JavaScriptTokens.INC || token === FormatterWorker.JavaScriptTokens.DEC) { |
| 665 this._next(); | 488 this._next(); |
| 666 if (token === Tokens.DELETE || token === Tokens.TYPEOF || token ===
Tokens.VOID) | 489 if (token === FormatterWorker.JavaScriptTokens.DELETE || token === F
ormatterWorker.JavaScriptTokens.TYPEOF || token === FormatterWorker.JavaScriptTo
kens.VOID) |
| 667 this._builder.addSpace(); | 490 this._builder.addSpace(); |
| 668 this._parseUnaryExpression(); | 491 this._parseUnaryExpression(); |
| 669 } else | 492 } else |
| 670 return this._parsePostfixExpression(); | 493 return this._parsePostfixExpression(); |
| 671 }, | 494 }, |
| 672 | 495 |
| 673 _parsePostfixExpression: function() | 496 _parsePostfixExpression: function() |
| 674 { | 497 { |
| 675 this._parseLeftHandSideExpression(); | 498 this._parseLeftHandSideExpression(); |
| 676 var token = this._peek(); | 499 var token = this._peek(); |
| 677 if (!this._hasLineTerminatorBeforeNext() && (token === Tokens.INC || tok
en === Tokens.DEC)) | 500 if (!this._hasLineTerminatorBeforeNext() && (token === FormatterWorker.J
avaScriptTokens.INC || token === FormatterWorker.JavaScriptTokens.DEC)) |
| 678 this._next(); | 501 this._next(); |
| 679 }, | 502 }, |
| 680 | 503 |
| 681 _parseLeftHandSideExpression: function() | 504 _parseLeftHandSideExpression: function() |
| 682 { | 505 { |
| 683 if (this._peek() === Tokens.NEW) | 506 if (this._peek() === FormatterWorker.JavaScriptTokens.NEW) |
| 684 this._parseNewExpression(); | 507 this._parseNewExpression(); |
| 685 else | 508 else |
| 686 this._parseMemberExpression(); | 509 this._parseMemberExpression(); |
| 687 | 510 |
| 688 while (true) { | 511 while (true) { |
| 689 switch (this._peek()) { | 512 switch (this._peek()) { |
| 690 case Tokens.LBRACK: | 513 case FormatterWorker.JavaScriptTokens.LBRACK: |
| 691 this._consume(Tokens.LBRACK); | 514 this._consume(FormatterWorker.JavaScriptTokens.LBRACK); |
| 692 this._parseExpression(); | 515 this._parseExpression(); |
| 693 this._expect(Tokens.RBRACK); | 516 this._expect(FormatterWorker.JavaScriptTokens.RBRACK); |
| 694 break; | 517 break; |
| 695 | 518 |
| 696 case Tokens.LPAREN: | 519 case FormatterWorker.JavaScriptTokens.LPAREN: |
| 697 this._parseArguments(); | 520 this._parseArguments(); |
| 698 break; | 521 break; |
| 699 | 522 |
| 700 case Tokens.PERIOD: | 523 case FormatterWorker.JavaScriptTokens.PERIOD: |
| 701 this._consume(Tokens.PERIOD); | 524 this._consume(FormatterWorker.JavaScriptTokens.PERIOD); |
| 702 this._expect(Tokens.IDENTIFIER); | 525 this._expect(FormatterWorker.JavaScriptTokens.IDENTIFIER); |
| 703 break; | 526 break; |
| 704 | 527 |
| 705 default: | 528 default: |
| 706 return; | 529 return; |
| 707 } | 530 } |
| 708 } | 531 } |
| 709 }, | 532 }, |
| 710 | 533 |
| 711 _parseNewExpression: function() | 534 _parseNewExpression: function() |
| 712 { | 535 { |
| 713 this._expect(Tokens.NEW); | 536 this._expect(FormatterWorker.JavaScriptTokens.NEW); |
| 714 this._builder.addSpace(); | 537 this._builder.addSpace(); |
| 715 if (this._peek() === Tokens.NEW) | 538 if (this._peek() === FormatterWorker.JavaScriptTokens.NEW) |
| 716 this._parseNewExpression(); | 539 this._parseNewExpression(); |
| 717 else | 540 else |
| 718 this._parseMemberExpression(); | 541 this._parseMemberExpression(); |
| 719 }, | 542 }, |
| 720 | 543 |
| 721 _parseMemberExpression: function() | 544 _parseMemberExpression: function() |
| 722 { | 545 { |
| 723 if (this._peek() === Tokens.FUNCTION) { | 546 if (this._peek() === FormatterWorker.JavaScriptTokens.FUNCTION) { |
| 724 this._expect(Tokens.FUNCTION); | 547 this._expect(FormatterWorker.JavaScriptTokens.FUNCTION); |
| 725 if (this._peek() === Tokens.IDENTIFIER) { | 548 if (this._peek() === FormatterWorker.JavaScriptTokens.IDENTIFIER) { |
| 726 this._builder.addSpace(); | 549 this._builder.addSpace(); |
| 727 this._expect(Tokens.IDENTIFIER); | 550 this._expect(FormatterWorker.JavaScriptTokens.IDENTIFIER); |
| 728 } | 551 } |
| 729 this._parseFunctionLiteral(); | 552 this._parseFunctionLiteral(); |
| 730 } else | 553 } else |
| 731 this._parsePrimaryExpression(); | 554 this._parsePrimaryExpression(); |
| 732 | 555 |
| 733 while (true) { | 556 while (true) { |
| 734 switch (this._peek()) { | 557 switch (this._peek()) { |
| 735 case Tokens.LBRACK: | 558 case FormatterWorker.JavaScriptTokens.LBRACK: |
| 736 this._consume(Tokens.LBRACK); | 559 this._consume(FormatterWorker.JavaScriptTokens.LBRACK); |
| 737 this._parseExpression(); | 560 this._parseExpression(); |
| 738 this._expect(Tokens.RBRACK); | 561 this._expect(FormatterWorker.JavaScriptTokens.RBRACK); |
| 739 break; | 562 break; |
| 740 | 563 |
| 741 case Tokens.PERIOD: | 564 case FormatterWorker.JavaScriptTokens.PERIOD: |
| 742 this._consume(Tokens.PERIOD); | 565 this._consume(FormatterWorker.JavaScriptTokens.PERIOD); |
| 743 this._expect(Tokens.IDENTIFIER); | 566 this._expect(FormatterWorker.JavaScriptTokens.IDENTIFIER); |
| 744 break; | 567 break; |
| 745 | 568 |
| 746 case Tokens.LPAREN: | 569 case FormatterWorker.JavaScriptTokens.LPAREN: |
| 747 this._parseArguments(); | 570 this._parseArguments(); |
| 748 break; | 571 break; |
| 749 | 572 |
| 750 default: | 573 default: |
| 751 return; | 574 return; |
| 752 } | 575 } |
| 753 } | 576 } |
| 754 }, | 577 }, |
| 755 | 578 |
| 756 _parseDebuggerStatement: function() | 579 _parseDebuggerStatement: function() |
| 757 { | 580 { |
| 758 this._expect(Tokens.DEBUGGER); | 581 this._expect(FormatterWorker.JavaScriptTokens.DEBUGGER); |
| 759 this._expectSemicolon(); | 582 this._expectSemicolon(); |
| 760 }, | 583 }, |
| 761 | 584 |
| 762 _parsePrimaryExpression: function() | 585 _parsePrimaryExpression: function() |
| 763 { | 586 { |
| 764 switch (this._peek()) { | 587 switch (this._peek()) { |
| 765 case Tokens.THIS: | 588 case FormatterWorker.JavaScriptTokens.THIS: |
| 766 return this._consume(Tokens.THIS); | 589 return this._consume(FormatterWorker.JavaScriptTokens.THIS); |
| 767 case Tokens.NULL_LITERAL: | 590 case FormatterWorker.JavaScriptTokens.NULL_LITERAL: |
| 768 return this._consume(Tokens.NULL_LITERAL); | 591 return this._consume(FormatterWorker.JavaScriptTokens.NULL_LITERAL); |
| 769 case Tokens.TRUE_LITERAL: | 592 case FormatterWorker.JavaScriptTokens.TRUE_LITERAL: |
| 770 return this._consume(Tokens.TRUE_LITERAL); | 593 return this._consume(FormatterWorker.JavaScriptTokens.TRUE_LITERAL); |
| 771 case Tokens.FALSE_LITERAL: | 594 case FormatterWorker.JavaScriptTokens.FALSE_LITERAL: |
| 772 return this._consume(Tokens.FALSE_LITERAL); | 595 return this._consume(FormatterWorker.JavaScriptTokens.FALSE_LITERAL)
; |
| 773 case Tokens.IDENTIFIER: | 596 case FormatterWorker.JavaScriptTokens.IDENTIFIER: |
| 774 return this._consume(Tokens.IDENTIFIER); | 597 return this._consume(FormatterWorker.JavaScriptTokens.IDENTIFIER); |
| 775 case Tokens.NUMBER: | 598 case FormatterWorker.JavaScriptTokens.NUMBER: |
| 776 return this._consume(Tokens.NUMBER); | 599 return this._consume(FormatterWorker.JavaScriptTokens.NUMBER); |
| 777 case Tokens.STRING: | 600 case FormatterWorker.JavaScriptTokens.STRING: |
| 778 return this._consume(Tokens.STRING); | 601 return this._consume(FormatterWorker.JavaScriptTokens.STRING); |
| 779 case Tokens.ASSIGN_DIV: | 602 case FormatterWorker.JavaScriptTokens.ASSIGN_DIV: |
| 780 return this._parseRegExpLiteral(); | 603 return this._parseRegExpLiteral(); |
| 781 case Tokens.DIV: | 604 case FormatterWorker.JavaScriptTokens.DIV: |
| 782 return this._parseRegExpLiteral(); | 605 return this._parseRegExpLiteral(); |
| 783 case Tokens.LBRACK: | 606 case FormatterWorker.JavaScriptTokens.LBRACK: |
| 784 return this._parseArrayLiteral(); | 607 return this._parseArrayLiteral(); |
| 785 case Tokens.LBRACE: | 608 case FormatterWorker.JavaScriptTokens.LBRACE: |
| 786 return this._parseObjectLiteral(); | 609 return this._parseObjectLiteral(); |
| 787 case Tokens.LPAREN: | 610 case FormatterWorker.JavaScriptTokens.LPAREN: |
| 788 this._consume(Tokens.LPAREN); | 611 this._consume(FormatterWorker.JavaScriptTokens.LPAREN); |
| 789 this._parseExpression(); | 612 this._parseExpression(); |
| 790 this._expect(Tokens.RPAREN); | 613 this._expect(FormatterWorker.JavaScriptTokens.RPAREN); |
| 791 return; | 614 return; |
| 792 default: | 615 default: |
| 793 return this._next(); | 616 return this._next(); |
| 794 } | 617 } |
| 795 }, | 618 }, |
| 796 | 619 |
| 797 _parseArrayLiteral: function() | 620 _parseArrayLiteral: function() |
| 798 { | 621 { |
| 799 this._expect(Tokens.LBRACK); | 622 this._expect(FormatterWorker.JavaScriptTokens.LBRACK); |
| 800 this._builder.increaseNestingLevel(); | 623 this._builder.increaseNestingLevel(); |
| 801 while (this._peek() !== Tokens.RBRACK) { | 624 while (this._peek() !== FormatterWorker.JavaScriptTokens.RBRACK) { |
| 802 if (this._peek() !== Tokens.COMMA) | 625 if (this._peek() !== FormatterWorker.JavaScriptTokens.COMMA) |
| 803 this._parseAssignmentExpression(); | 626 this._parseAssignmentExpression(); |
| 804 if (this._peek() !== Tokens.RBRACK) { | 627 if (this._peek() !== FormatterWorker.JavaScriptTokens.RBRACK) { |
| 805 this._expect(Tokens.COMMA); | 628 this._expect(FormatterWorker.JavaScriptTokens.COMMA); |
| 806 this._builder.addSpace(); | 629 this._builder.addSpace(); |
| 807 } | 630 } |
| 808 } | 631 } |
| 809 this._builder.decreaseNestingLevel(); | 632 this._builder.decreaseNestingLevel(); |
| 810 this._expect(Tokens.RBRACK); | 633 this._expect(FormatterWorker.JavaScriptTokens.RBRACK); |
| 811 }, | 634 }, |
| 812 | 635 |
| 813 _parseObjectLiteralGetSet: function() | 636 _parseObjectLiteralGetSet: function() |
| 814 { | 637 { |
| 815 var token = this._peek(); | 638 var token = this._peek(); |
| 816 if (token === Tokens.IDENTIFIER || token === Tokens.NUMBER || token ===
Tokens.STRING || | 639 if (token === FormatterWorker.JavaScriptTokens.IDENTIFIER || token === F
ormatterWorker.JavaScriptTokens.NUMBER || token === FormatterWorker.JavaScriptTo
kens.STRING || |
| 817 Tokens.DELETE <= token && token <= Tokens.FALSE_LITERAL || | 640 FormatterWorker.JavaScriptTokens.DELETE <= token && token <= Formatt
erWorker.JavaScriptTokens.FALSE_LITERAL || |
| 818 token === Tokens.INSTANCEOF || token === Tokens.IN || token === Toke
ns.CONST) { | 641 token === FormatterWorker.JavaScriptTokens.INSTANCEOF || token === F
ormatterWorker.JavaScriptTokens.IN || token === FormatterWorker.JavaScriptTokens
.CONST) { |
| 819 this._next(); | 642 this._next(); |
| 820 this._parseFunctionLiteral(); | 643 this._parseFunctionLiteral(); |
| 821 } | 644 } |
| 822 }, | 645 }, |
| 823 | 646 |
| 824 _parseObjectLiteral: function() | 647 _parseObjectLiteral: function() |
| 825 { | 648 { |
| 826 this._expect(Tokens.LBRACE); | 649 this._expect(FormatterWorker.JavaScriptTokens.LBRACE); |
| 827 this._builder.increaseNestingLevel(); | 650 this._builder.increaseNestingLevel(); |
| 828 while (this._peek() !== Tokens.RBRACE) { | 651 while (this._peek() !== FormatterWorker.JavaScriptTokens.RBRACE) { |
| 829 var token = this._peek(); | 652 var token = this._peek(); |
| 830 switch (token) { | 653 switch (token) { |
| 831 case Tokens.IDENTIFIER: | 654 case FormatterWorker.JavaScriptTokens.IDENTIFIER: |
| 832 this._consume(Tokens.IDENTIFIER); | 655 this._consume(FormatterWorker.JavaScriptTokens.IDENTIFIER); |
| 833 var name = this._token.value; | 656 var name = this._token.value; |
| 834 if ((name === "get" || name === "set") && this._peek() !== Token
s.COLON) { | 657 if ((name === "get" || name === "set") && this._peek() !== Forma
tterWorker.JavaScriptTokens.COLON) { |
| 835 this._builder.addSpace(); | 658 this._builder.addSpace(); |
| 836 this._parseObjectLiteralGetSet(); | 659 this._parseObjectLiteralGetSet(); |
| 837 if (this._peek() !== Tokens.RBRACE) { | 660 if (this._peek() !== FormatterWorker.JavaScriptTokens.RBRACE
) { |
| 838 this._expect(Tokens.COMMA); | 661 this._expect(FormatterWorker.JavaScriptTokens.COMMA); |
| 839 } | 662 } |
| 840 continue; | 663 continue; |
| 841 } | 664 } |
| 842 break; | 665 break; |
| 843 | 666 |
| 844 case Tokens.STRING: | 667 case FormatterWorker.JavaScriptTokens.STRING: |
| 845 this._consume(Tokens.STRING); | 668 this._consume(FormatterWorker.JavaScriptTokens.STRING); |
| 846 break; | 669 break; |
| 847 | 670 |
| 848 case Tokens.NUMBER: | 671 case FormatterWorker.JavaScriptTokens.NUMBER: |
| 849 this._consume(Tokens.NUMBER); | 672 this._consume(FormatterWorker.JavaScriptTokens.NUMBER); |
| 850 break; | 673 break; |
| 851 | 674 |
| 852 default: | 675 default: |
| 853 this._next(); | 676 this._next(); |
| 854 } | 677 } |
| 855 | 678 |
| 856 this._expect(Tokens.COLON); | 679 this._expect(FormatterWorker.JavaScriptTokens.COLON); |
| 857 this._builder.addSpace(); | 680 this._builder.addSpace(); |
| 858 this._parseAssignmentExpression(); | 681 this._parseAssignmentExpression(); |
| 859 if (this._peek() !== Tokens.RBRACE) { | 682 if (this._peek() !== FormatterWorker.JavaScriptTokens.RBRACE) { |
| 860 this._expect(Tokens.COMMA); | 683 this._expect(FormatterWorker.JavaScriptTokens.COMMA); |
| 861 } | 684 } |
| 862 } | 685 } |
| 863 this._builder.decreaseNestingLevel(); | 686 this._builder.decreaseNestingLevel(); |
| 864 | 687 |
| 865 this._expect(Tokens.RBRACE); | 688 this._expect(FormatterWorker.JavaScriptTokens.RBRACE); |
| 866 }, | 689 }, |
| 867 | 690 |
| 868 _parseRegExpLiteral: function() | 691 _parseRegExpLiteral: function() |
| 869 { | 692 { |
| 870 if (this._nextToken.type === "regexp") | 693 if (this._nextToken.type === "regexp") |
| 871 this._next(); | 694 this._next(); |
| 872 else { | 695 else { |
| 873 this._forceRegexp = true; | 696 this._forceRegexp = true; |
| 874 this._next(); | 697 this._next(); |
| 875 } | 698 } |
| 876 }, | 699 }, |
| 877 | 700 |
| 878 _parseArguments: function() | 701 _parseArguments: function() |
| 879 { | 702 { |
| 880 this._expect(Tokens.LPAREN); | 703 this._expect(FormatterWorker.JavaScriptTokens.LPAREN); |
| 881 var done = (this._peek() === Tokens.RPAREN); | 704 var done = (this._peek() === FormatterWorker.JavaScriptTokens.RPAREN); |
| 882 while (!done) { | 705 while (!done) { |
| 883 this._parseAssignmentExpression(); | 706 this._parseAssignmentExpression(); |
| 884 done = (this._peek() === Tokens.RPAREN); | 707 done = (this._peek() === FormatterWorker.JavaScriptTokens.RPAREN); |
| 885 if (!done) { | 708 if (!done) { |
| 886 this._expect(Tokens.COMMA); | 709 this._expect(FormatterWorker.JavaScriptTokens.COMMA); |
| 887 this._builder.addSpace(); | 710 this._builder.addSpace(); |
| 888 } | 711 } |
| 889 } | 712 } |
| 890 this._expect(Tokens.RPAREN); | 713 this._expect(FormatterWorker.JavaScriptTokens.RPAREN); |
| 891 }, | 714 }, |
| 892 | 715 |
| 893 _parseFunctionLiteral: function() | 716 _parseFunctionLiteral: function() |
| 894 { | 717 { |
| 895 this._expect(Tokens.LPAREN); | 718 this._expect(FormatterWorker.JavaScriptTokens.LPAREN); |
| 896 var done = (this._peek() === Tokens.RPAREN); | 719 var done = (this._peek() === FormatterWorker.JavaScriptTokens.RPAREN); |
| 897 while (!done) { | 720 while (!done) { |
| 898 this._expect(Tokens.IDENTIFIER); | 721 this._expect(FormatterWorker.JavaScriptTokens.IDENTIFIER); |
| 899 done = (this._peek() === Tokens.RPAREN); | 722 done = (this._peek() === FormatterWorker.JavaScriptTokens.RPAREN); |
| 900 if (!done) { | 723 if (!done) { |
| 901 this._expect(Tokens.COMMA); | 724 this._expect(FormatterWorker.JavaScriptTokens.COMMA); |
| 902 this._builder.addSpace(); | 725 this._builder.addSpace(); |
| 903 } | 726 } |
| 904 } | 727 } |
| 905 this._expect(Tokens.RPAREN); | 728 this._expect(FormatterWorker.JavaScriptTokens.RPAREN); |
| 906 this._builder.addSpace(); | 729 this._builder.addSpace(); |
| 907 | 730 |
| 908 this._expect(Tokens.LBRACE); | 731 this._expect(FormatterWorker.JavaScriptTokens.LBRACE); |
| 909 this._builder.addNewLine(); | 732 this._builder.addNewLine(); |
| 910 this._builder.increaseNestingLevel(); | 733 this._builder.increaseNestingLevel(); |
| 911 this._parseSourceElements(Tokens.RBRACE); | 734 this._parseSourceElements(FormatterWorker.JavaScriptTokens.RBRACE); |
| 912 this._builder.decreaseNestingLevel(); | 735 this._builder.decreaseNestingLevel(); |
| 913 this._expect(Tokens.RBRACE); | 736 this._expect(FormatterWorker.JavaScriptTokens.RBRACE); |
| 914 } | 737 } |
| 915 } | 738 } |
| 739 |
| 740 /** |
| 741 * @constructor |
| 742 * @param {string} content |
| 743 * @param {{original: Array.<number>, formatted: Array.<number>}} mapping |
| 744 * @param {number} originalOffset |
| 745 * @param {number} formattedOffset |
| 746 * @param {string} indentString |
| 747 */ |
| 748 FormatterWorker.JavaScriptFormattedContentBuilder = function(content, mapping, o
riginalOffset, formattedOffset, indentString) |
| 749 { |
| 750 this._originalContent = content; |
| 751 this._originalOffset = originalOffset; |
| 752 this._lastOriginalPosition = 0; |
| 753 |
| 754 this._formattedContent = []; |
| 755 this._formattedContentLength = 0; |
| 756 this._formattedOffset = formattedOffset; |
| 757 this._lastFormattedPosition = 0; |
| 758 |
| 759 this._mapping = mapping; |
| 760 |
| 761 this._lineNumber = 0; |
| 762 this._nestingLevel = 0; |
| 763 this._indentString = indentString; |
| 764 this._cachedIndents = {}; |
| 765 } |
| 766 |
| 767 FormatterWorker.JavaScriptFormattedContentBuilder.prototype = { |
| 768 /** |
| 769 * @param {{comments_before: Array.<string>, line: number, pos: number, endL
ine: number, nlb: boolean}} token |
| 770 */ |
| 771 addToken: function(token) |
| 772 { |
| 773 for (var i = 0; i < token.comments_before.length; ++i) |
| 774 this._addComment(token.comments_before[i]); |
| 775 |
| 776 while (this._lineNumber < token.line) { |
| 777 this._addText("\n"); |
| 778 this._addIndent(); |
| 779 this._needNewLine = false; |
| 780 this._lineNumber += 1; |
| 781 } |
| 782 |
| 783 if (this._needNewLine) { |
| 784 this._addText("\n"); |
| 785 this._addIndent(); |
| 786 this._needNewLine = false; |
| 787 } |
| 788 |
| 789 this._addMappingIfNeeded(token.pos); |
| 790 this._addText(this._originalContent.substring(token.pos, token.endPos)); |
| 791 this._lineNumber = token.endLine; |
| 792 }, |
| 793 |
| 794 addSpace: function() |
| 795 { |
| 796 this._addText(" "); |
| 797 }, |
| 798 |
| 799 addNewLine: function() |
| 800 { |
| 801 this._needNewLine = true; |
| 802 }, |
| 803 |
| 804 increaseNestingLevel: function() |
| 805 { |
| 806 this._nestingLevel += 1; |
| 807 }, |
| 808 |
| 809 decreaseNestingLevel: function() |
| 810 { |
| 811 this._nestingLevel -= 1; |
| 812 }, |
| 813 |
| 814 /** |
| 815 * @return {string} |
| 816 */ |
| 817 content: function() |
| 818 { |
| 819 return this._formattedContent.join(""); |
| 820 }, |
| 821 |
| 822 _addIndent: function() |
| 823 { |
| 824 if (this._cachedIndents[this._nestingLevel]) { |
| 825 this._addText(this._cachedIndents[this._nestingLevel]); |
| 826 return; |
| 827 } |
| 828 |
| 829 var fullIndent = ""; |
| 830 for (var i = 0; i < this._nestingLevel; ++i) |
| 831 fullIndent += this._indentString; |
| 832 this._addText(fullIndent); |
| 833 |
| 834 // Cache a maximum of 20 nesting level indents. |
| 835 if (this._nestingLevel <= 20) |
| 836 this._cachedIndents[this._nestingLevel] = fullIndent; |
| 837 }, |
| 838 |
| 839 _addComment: function(comment) |
| 840 { |
| 841 if (this._lineNumber < comment.line) { |
| 842 for (var j = this._lineNumber; j < comment.line; ++j) |
| 843 this._addText("\n"); |
| 844 this._lineNumber = comment.line; |
| 845 this._needNewLine = false; |
| 846 this._addIndent(); |
| 847 } else |
| 848 this.addSpace(); |
| 849 |
| 850 this._addMappingIfNeeded(comment.pos); |
| 851 if (comment.type === "comment1") |
| 852 this._addText("//"); |
| 853 else |
| 854 this._addText("/*"); |
| 855 |
| 856 this._addText(comment.value); |
| 857 |
| 858 if (comment.type !== "comment1") { |
| 859 this._addText("*/"); |
| 860 var position; |
| 861 while ((position = comment.value.indexOf("\n", position + 1)) !== -1
) |
| 862 this._lineNumber += 1; |
| 863 } |
| 864 }, |
| 865 |
| 866 /** |
| 867 * @param {string} text |
| 868 */ |
| 869 _addText: function(text) |
| 870 { |
| 871 this._formattedContent.push(text); |
| 872 this._formattedContentLength += text.length; |
| 873 }, |
| 874 |
| 875 /** |
| 876 * @param {number} originalPosition |
| 877 */ |
| 878 _addMappingIfNeeded: function(originalPosition) |
| 879 { |
| 880 if (originalPosition - this._lastOriginalPosition === this._formattedCon
tentLength - this._lastFormattedPosition) |
| 881 return; |
| 882 this._mapping.original.push(this._originalOffset + originalPosition); |
| 883 this._lastOriginalPosition = originalPosition; |
| 884 this._mapping.formatted.push(this._formattedOffset + this._formattedCont
entLength); |
| 885 this._lastFormattedPosition = this._formattedContentLength; |
| 886 } |
| 887 } |
| 888 |
| 889 FormatterWorker.JavaScriptTokens = {}; |
| 890 FormatterWorker.JavaScriptTokensByValue = {}; |
| 891 |
| 892 FormatterWorker.JavaScriptTokens.EOS = 0; |
| 893 FormatterWorker.JavaScriptTokens.LPAREN = FormatterWorker.JavaScriptTokensByValu
e["("] = 1; |
| 894 FormatterWorker.JavaScriptTokens.RPAREN = FormatterWorker.JavaScriptTokensByValu
e[")"] = 2; |
| 895 FormatterWorker.JavaScriptTokens.LBRACK = FormatterWorker.JavaScriptTokensByValu
e["["] = 3; |
| 896 FormatterWorker.JavaScriptTokens.RBRACK = FormatterWorker.JavaScriptTokensByValu
e["]"] = 4; |
| 897 FormatterWorker.JavaScriptTokens.LBRACE = FormatterWorker.JavaScriptTokensByValu
e["{"] = 5; |
| 898 FormatterWorker.JavaScriptTokens.RBRACE = FormatterWorker.JavaScriptTokensByValu
e["}"] = 6; |
| 899 FormatterWorker.JavaScriptTokens.COLON = FormatterWorker.JavaScriptTokensByValue
[":"] = 7; |
| 900 FormatterWorker.JavaScriptTokens.SEMICOLON = FormatterWorker.JavaScriptTokensByV
alue[";"] = 8; |
| 901 FormatterWorker.JavaScriptTokens.PERIOD = FormatterWorker.JavaScriptTokensByValu
e["."] = 9; |
| 902 FormatterWorker.JavaScriptTokens.CONDITIONAL = FormatterWorker.JavaScriptTokensB
yValue["?"] = 10; |
| 903 FormatterWorker.JavaScriptTokens.INC = FormatterWorker.JavaScriptTokensByValue["
++"] = 11; |
| 904 FormatterWorker.JavaScriptTokens.DEC = FormatterWorker.JavaScriptTokensByValue["
--"] = 12; |
| 905 FormatterWorker.JavaScriptTokens.ASSIGN = FormatterWorker.JavaScriptTokensByValu
e["="] = 13; |
| 906 FormatterWorker.JavaScriptTokens.ASSIGN_BIT_OR = FormatterWorker.JavaScriptToken
sByValue["|="] = 14; |
| 907 FormatterWorker.JavaScriptTokens.ASSIGN_BIT_XOR = FormatterWorker.JavaScriptToke
nsByValue["^="] = 15; |
| 908 FormatterWorker.JavaScriptTokens.ASSIGN_BIT_AND = FormatterWorker.JavaScriptToke
nsByValue["&="] = 16; |
| 909 FormatterWorker.JavaScriptTokens.ASSIGN_SHL = FormatterWorker.JavaScriptTokensBy
Value["<<="] = 17; |
| 910 FormatterWorker.JavaScriptTokens.ASSIGN_SAR = FormatterWorker.JavaScriptTokensBy
Value[">>="] = 18; |
| 911 FormatterWorker.JavaScriptTokens.ASSIGN_SHR = FormatterWorker.JavaScriptTokensBy
Value[">>>="] = 19; |
| 912 FormatterWorker.JavaScriptTokens.ASSIGN_ADD = FormatterWorker.JavaScriptTokensBy
Value["+="] = 20; |
| 913 FormatterWorker.JavaScriptTokens.ASSIGN_SUB = FormatterWorker.JavaScriptTokensBy
Value["-="] = 21; |
| 914 FormatterWorker.JavaScriptTokens.ASSIGN_MUL = FormatterWorker.JavaScriptTokensBy
Value["*="] = 22; |
| 915 FormatterWorker.JavaScriptTokens.ASSIGN_DIV = FormatterWorker.JavaScriptTokensBy
Value["/="] = 23; |
| 916 FormatterWorker.JavaScriptTokens.ASSIGN_MOD = FormatterWorker.JavaScriptTokensBy
Value["%="] = 24; |
| 917 FormatterWorker.JavaScriptTokens.COMMA = FormatterWorker.JavaScriptTokensByValue
[","] = 25; |
| 918 FormatterWorker.JavaScriptTokens.OR = FormatterWorker.JavaScriptTokensByValue["|
|"] = 26; |
| 919 FormatterWorker.JavaScriptTokens.AND = FormatterWorker.JavaScriptTokensByValue["
&&"] = 27; |
| 920 FormatterWorker.JavaScriptTokens.BIT_OR = FormatterWorker.JavaScriptTokensByValu
e["|"] = 28; |
| 921 FormatterWorker.JavaScriptTokens.BIT_XOR = FormatterWorker.JavaScriptTokensByVal
ue["^"] = 29; |
| 922 FormatterWorker.JavaScriptTokens.BIT_AND = FormatterWorker.JavaScriptTokensByVal
ue["&"] = 30; |
| 923 FormatterWorker.JavaScriptTokens.SHL = FormatterWorker.JavaScriptTokensByValue["
<<"] = 31; |
| 924 FormatterWorker.JavaScriptTokens.SAR = FormatterWorker.JavaScriptTokensByValue["
>>"] = 32; |
| 925 FormatterWorker.JavaScriptTokens.SHR = FormatterWorker.JavaScriptTokensByValue["
>>>"] = 33; |
| 926 FormatterWorker.JavaScriptTokens.ADD = FormatterWorker.JavaScriptTokensByValue["
+"] = 34; |
| 927 FormatterWorker.JavaScriptTokens.SUB = FormatterWorker.JavaScriptTokensByValue["
-"] = 35; |
| 928 FormatterWorker.JavaScriptTokens.MUL = FormatterWorker.JavaScriptTokensByValue["
*"] = 36; |
| 929 FormatterWorker.JavaScriptTokens.DIV = FormatterWorker.JavaScriptTokensByValue["
/"] = 37; |
| 930 FormatterWorker.JavaScriptTokens.MOD = FormatterWorker.JavaScriptTokensByValue["
%"] = 38; |
| 931 FormatterWorker.JavaScriptTokens.EQ = FormatterWorker.JavaScriptTokensByValue["=
="] = 39; |
| 932 FormatterWorker.JavaScriptTokens.NE = FormatterWorker.JavaScriptTokensByValue["!
="] = 40; |
| 933 FormatterWorker.JavaScriptTokens.EQ_STRICT = FormatterWorker.JavaScriptTokensByV
alue["==="] = 41; |
| 934 FormatterWorker.JavaScriptTokens.NE_STRICT = FormatterWorker.JavaScriptTokensByV
alue["!=="] = 42; |
| 935 FormatterWorker.JavaScriptTokens.LT = FormatterWorker.JavaScriptTokensByValue["<
"] = 43; |
| 936 FormatterWorker.JavaScriptTokens.GT = FormatterWorker.JavaScriptTokensByValue[">
"] = 44; |
| 937 FormatterWorker.JavaScriptTokens.LTE = FormatterWorker.JavaScriptTokensByValue["
<="] = 45; |
| 938 FormatterWorker.JavaScriptTokens.GTE = FormatterWorker.JavaScriptTokensByValue["
>="] = 46; |
| 939 FormatterWorker.JavaScriptTokens.INSTANCEOF = FormatterWorker.JavaScriptTokensBy
Value["instanceof"] = 47; |
| 940 FormatterWorker.JavaScriptTokens.IN = FormatterWorker.JavaScriptTokensByValue["i
n"] = 48; |
| 941 FormatterWorker.JavaScriptTokens.NOT = FormatterWorker.JavaScriptTokensByValue["
!"] = 49; |
| 942 FormatterWorker.JavaScriptTokens.BIT_NOT = FormatterWorker.JavaScriptTokensByVal
ue["~"] = 50; |
| 943 FormatterWorker.JavaScriptTokens.DELETE = FormatterWorker.JavaScriptTokensByValu
e["delete"] = 51; |
| 944 FormatterWorker.JavaScriptTokens.TYPEOF = FormatterWorker.JavaScriptTokensByValu
e["typeof"] = 52; |
| 945 FormatterWorker.JavaScriptTokens.VOID = FormatterWorker.JavaScriptTokensByValue[
"void"] = 53; |
| 946 FormatterWorker.JavaScriptTokens.BREAK = FormatterWorker.JavaScriptTokensByValue
["break"] = 54; |
| 947 FormatterWorker.JavaScriptTokens.CASE = FormatterWorker.JavaScriptTokensByValue[
"case"] = 55; |
| 948 FormatterWorker.JavaScriptTokens.CATCH = FormatterWorker.JavaScriptTokensByValue
["catch"] = 56; |
| 949 FormatterWorker.JavaScriptTokens.CONTINUE = FormatterWorker.JavaScriptTokensByVa
lue["continue"] = 57; |
| 950 FormatterWorker.JavaScriptTokens.DEBUGGER = FormatterWorker.JavaScriptTokensByVa
lue["debugger"] = 58; |
| 951 FormatterWorker.JavaScriptTokens.DEFAULT = FormatterWorker.JavaScriptTokensByVal
ue["default"] = 59; |
| 952 FormatterWorker.JavaScriptTokens.DO = FormatterWorker.JavaScriptTokensByValue["d
o"] = 60; |
| 953 FormatterWorker.JavaScriptTokens.ELSE = FormatterWorker.JavaScriptTokensByValue[
"else"] = 61; |
| 954 FormatterWorker.JavaScriptTokens.FINALLY = FormatterWorker.JavaScriptTokensByVal
ue["finally"] = 62; |
| 955 FormatterWorker.JavaScriptTokens.FOR = FormatterWorker.JavaScriptTokensByValue["
for"] = 63; |
| 956 FormatterWorker.JavaScriptTokens.FUNCTION = FormatterWorker.JavaScriptTokensByVa
lue["function"] = 64; |
| 957 FormatterWorker.JavaScriptTokens.IF = FormatterWorker.JavaScriptTokensByValue["i
f"] = 65; |
| 958 FormatterWorker.JavaScriptTokens.NEW = FormatterWorker.JavaScriptTokensByValue["
new"] = 66; |
| 959 FormatterWorker.JavaScriptTokens.RETURN = FormatterWorker.JavaScriptTokensByValu
e["return"] = 67; |
| 960 FormatterWorker.JavaScriptTokens.SWITCH = FormatterWorker.JavaScriptTokensByValu
e["switch"] = 68; |
| 961 FormatterWorker.JavaScriptTokens.THIS = FormatterWorker.JavaScriptTokensByValue[
"this"] = 69; |
| 962 FormatterWorker.JavaScriptTokens.THROW = FormatterWorker.JavaScriptTokensByValue
["throw"] = 70; |
| 963 FormatterWorker.JavaScriptTokens.TRY = FormatterWorker.JavaScriptTokensByValue["
try"] = 71; |
| 964 FormatterWorker.JavaScriptTokens.VAR = FormatterWorker.JavaScriptTokensByValue["
var"] = 72; |
| 965 FormatterWorker.JavaScriptTokens.WHILE = FormatterWorker.JavaScriptTokensByValue
["while"] = 73; |
| 966 FormatterWorker.JavaScriptTokens.WITH = FormatterWorker.JavaScriptTokensByValue[
"with"] = 74; |
| 967 FormatterWorker.JavaScriptTokens.NULL_LITERAL = FormatterWorker.JavaScriptTokens
ByValue["null"] = 75; |
| 968 FormatterWorker.JavaScriptTokens.TRUE_LITERAL = FormatterWorker.JavaScriptTokens
ByValue["true"] = 76; |
| 969 FormatterWorker.JavaScriptTokens.FALSE_LITERAL = FormatterWorker.JavaScriptToken
sByValue["false"] = 77; |
| 970 FormatterWorker.JavaScriptTokens.NUMBER = 78; |
| 971 FormatterWorker.JavaScriptTokens.STRING = 79; |
| 972 FormatterWorker.JavaScriptTokens.IDENTIFIER = 80; |
| 973 FormatterWorker.JavaScriptTokens.CONST = FormatterWorker.JavaScriptTokensByValue
["const"] = 81; |
| 974 |
| 975 FormatterWorker.JavaScriptTokensByType = { |
| 976 "eof": FormatterWorker.JavaScriptTokens.EOS, |
| 977 "name": FormatterWorker.JavaScriptTokens.IDENTIFIER, |
| 978 "num": FormatterWorker.JavaScriptTokens.NUMBER, |
| 979 "regexp": FormatterWorker.JavaScriptTokens.DIV, |
| 980 "string": FormatterWorker.JavaScriptTokens.STRING |
| 981 }; |
| 982 |
| 983 /** |
| 984 * @constructor |
| 985 * @param {string} content |
| 986 */ |
| 987 FormatterWorker.JavaScriptTokenizer = function(content) |
| 988 { |
| 989 this._readNextToken = parse.tokenizer(content); |
| 990 this._state = this._readNextToken.context(); |
| 991 } |
| 992 |
| 993 FormatterWorker.JavaScriptTokenizer.prototype = { |
| 994 /** |
| 995 * @return {string} |
| 996 */ |
| 997 content: function() |
| 998 { |
| 999 return this._state.text; |
| 1000 }, |
| 1001 |
| 1002 /** |
| 1003 * @param {boolean=} forceRegexp |
| 1004 */ |
| 1005 next: function(forceRegexp) |
| 1006 { |
| 1007 var uglifyToken = this._readNextToken(forceRegexp); |
| 1008 uglifyToken.endPos = this._state.pos; |
| 1009 uglifyToken.endLine = this._state.line; |
| 1010 uglifyToken.token = this._convertUglifyToken(uglifyToken); |
| 1011 return uglifyToken; |
| 1012 }, |
| 1013 |
| 1014 _convertUglifyToken: function(uglifyToken) |
| 1015 { |
| 1016 var token = FormatterWorker.JavaScriptTokensByType[uglifyToken.type]; |
| 1017 if (typeof token === "number") |
| 1018 return token; |
| 1019 token = FormatterWorker.JavaScriptTokensByValue[uglifyToken.value]; |
| 1020 if (typeof token === "number") |
| 1021 return token; |
| 1022 throw "Unknown token type " + uglifyToken.type; |
| 1023 } |
| 1024 } |
| OLD | NEW |