| 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 23 matching lines...) Expand all Loading... |
| 34 * @param {!FormatterWorker.JavaScriptFormattedContentBuilder} builder | 34 * @param {!FormatterWorker.JavaScriptFormattedContentBuilder} builder |
| 35 */ | 35 */ |
| 36 FormatterWorker.JavaScriptFormatter = function(tokenizer, builder) | 36 FormatterWorker.JavaScriptFormatter = function(tokenizer, builder) |
| 37 { | 37 { |
| 38 this._tokenizer = tokenizer; | 38 this._tokenizer = tokenizer; |
| 39 this._builder = builder; | 39 this._builder = builder; |
| 40 this._token = null; | 40 this._token = null; |
| 41 this._nextToken = this._tokenizer.next(); | 41 this._nextToken = this._tokenizer.next(); |
| 42 } | 42 } |
| 43 | 43 |
| 44 FormatterWorker.JavaScriptFormatter._identifierRegex = /^[$A-Z_][0-9A-Z_$]*$/i; |
| 45 |
| 44 FormatterWorker.JavaScriptFormatter.prototype = { | 46 FormatterWorker.JavaScriptFormatter.prototype = { |
| 45 format: function() | 47 format: function() |
| 46 { | 48 { |
| 47 this._parseSourceElements(FormatterWorker.JavaScriptTokens.EOS); | 49 this._parseSourceElements(FormatterWorker.JavaScriptTokens.EOS); |
| 48 this._consume(FormatterWorker.JavaScriptTokens.EOS); | 50 this._consume(FormatterWorker.JavaScriptTokens.EOS); |
| 49 }, | 51 }, |
| 50 | 52 |
| 51 /** | 53 /** |
| 52 * @return {number} | 54 * @return {number} |
| 53 */ | 55 */ |
| (...skipping 30 matching lines...) Expand all Loading... |
| 84 /** | 86 /** |
| 85 * @param {number} token | 87 * @param {number} token |
| 86 */ | 88 */ |
| 87 _expect: function(token) | 89 _expect: function(token) |
| 88 { | 90 { |
| 89 var next = this._next(); | 91 var next = this._next(); |
| 90 if (next !== token) | 92 if (next !== token) |
| 91 throw "Unexpected token: expected " + token + ", actual " + next; | 93 throw "Unexpected token: expected " + token + ", actual " + next; |
| 92 }, | 94 }, |
| 93 | 95 |
| 96 _expectGeneralIdentifier: function() |
| 97 { |
| 98 var next = this._next(); |
| 99 if (next !== FormatterWorker.JavaScriptTokens.IDENTIFIER && !FormatterWo
rker.JavaScriptFormatter._identifierRegex.test(this._token.value)) |
| 100 throw "Unexpected token: expected javascript identifier, actual " +
this._token.value; |
| 101 }, |
| 102 |
| 94 _expectSemicolon: function() | 103 _expectSemicolon: function() |
| 95 { | 104 { |
| 96 if (this._peek() === FormatterWorker.JavaScriptTokens.SEMICOLON) | 105 if (this._peek() === FormatterWorker.JavaScriptTokens.SEMICOLON) |
| 97 this._consume(FormatterWorker.JavaScriptTokens.SEMICOLON); | 106 this._consume(FormatterWorker.JavaScriptTokens.SEMICOLON); |
| 98 }, | 107 }, |
| 99 | 108 |
| 100 /** | 109 /** |
| 101 * @return {boolean} | 110 * @return {boolean} |
| 102 */ | 111 */ |
| 103 _hasLineTerminatorBeforeNext: function() | 112 _hasLineTerminatorBeforeNext: function() |
| (...skipping 411 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 515 this._parseExpression(); | 524 this._parseExpression(); |
| 516 this._expect(FormatterWorker.JavaScriptTokens.RBRACK); | 525 this._expect(FormatterWorker.JavaScriptTokens.RBRACK); |
| 517 break; | 526 break; |
| 518 | 527 |
| 519 case FormatterWorker.JavaScriptTokens.LPAREN: | 528 case FormatterWorker.JavaScriptTokens.LPAREN: |
| 520 this._parseArguments(); | 529 this._parseArguments(); |
| 521 break; | 530 break; |
| 522 | 531 |
| 523 case FormatterWorker.JavaScriptTokens.PERIOD: | 532 case FormatterWorker.JavaScriptTokens.PERIOD: |
| 524 this._consume(FormatterWorker.JavaScriptTokens.PERIOD); | 533 this._consume(FormatterWorker.JavaScriptTokens.PERIOD); |
| 525 this._expect(FormatterWorker.JavaScriptTokens.IDENTIFIER); | 534 this._expectGeneralIdentifier(); |
| 526 break; | 535 break; |
| 527 | 536 |
| 528 default: | 537 default: |
| 529 return; | 538 return; |
| 530 } | 539 } |
| 531 } | 540 } |
| 532 }, | 541 }, |
| 533 | 542 |
| 534 _parseNewExpression: function() | 543 _parseNewExpression: function() |
| 535 { | 544 { |
| (...skipping 20 matching lines...) Expand all Loading... |
| 556 while (true) { | 565 while (true) { |
| 557 switch (this._peek()) { | 566 switch (this._peek()) { |
| 558 case FormatterWorker.JavaScriptTokens.LBRACK: | 567 case FormatterWorker.JavaScriptTokens.LBRACK: |
| 559 this._consume(FormatterWorker.JavaScriptTokens.LBRACK); | 568 this._consume(FormatterWorker.JavaScriptTokens.LBRACK); |
| 560 this._parseExpression(); | 569 this._parseExpression(); |
| 561 this._expect(FormatterWorker.JavaScriptTokens.RBRACK); | 570 this._expect(FormatterWorker.JavaScriptTokens.RBRACK); |
| 562 break; | 571 break; |
| 563 | 572 |
| 564 case FormatterWorker.JavaScriptTokens.PERIOD: | 573 case FormatterWorker.JavaScriptTokens.PERIOD: |
| 565 this._consume(FormatterWorker.JavaScriptTokens.PERIOD); | 574 this._consume(FormatterWorker.JavaScriptTokens.PERIOD); |
| 566 this._expect(FormatterWorker.JavaScriptTokens.IDENTIFIER); | 575 this._expectGeneralIdentifier(); |
| 567 break; | 576 break; |
| 568 | 577 |
| 569 case FormatterWorker.JavaScriptTokens.LPAREN: | 578 case FormatterWorker.JavaScriptTokens.LPAREN: |
| 570 this._parseArguments(); | 579 this._parseArguments(); |
| 571 break; | 580 break; |
| 572 | 581 |
| 573 default: | 582 default: |
| 574 return; | 583 return; |
| 575 } | 584 } |
| 576 } | 585 } |
| (...skipping 442 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1019 { | 1028 { |
| 1020 var token = FormatterWorker.JavaScriptTokensByType[uglifyToken.type]; | 1029 var token = FormatterWorker.JavaScriptTokensByType[uglifyToken.type]; |
| 1021 if (typeof token === "number") | 1030 if (typeof token === "number") |
| 1022 return token; | 1031 return token; |
| 1023 token = FormatterWorker.JavaScriptTokensByValue[uglifyToken.value]; | 1032 token = FormatterWorker.JavaScriptTokensByValue[uglifyToken.value]; |
| 1024 if (typeof token === "number") | 1033 if (typeof token === "number") |
| 1025 return token; | 1034 return token; |
| 1026 throw "Unknown token type " + uglifyToken.type; | 1035 throw "Unknown token type " + uglifyToken.type; |
| 1027 } | 1036 } |
| 1028 } | 1037 } |
| OLD | NEW |