Index: Source/devtools/front_end/JavaScriptFormatter.js |
diff --git a/Source/devtools/front_end/JavaScriptFormatter.js b/Source/devtools/front_end/JavaScriptFormatter.js |
index 9536289dd7376b40add3eb58fba400b490f3a8ad..ba5296731604d6513f2ad00e854a852e6159b833 100644 |
--- a/Source/devtools/front_end/JavaScriptFormatter.js |
+++ b/Source/devtools/front_end/JavaScriptFormatter.js |
@@ -28,207 +28,12 @@ |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
*/ |
-function FormattedContentBuilder(content, mapping, originalOffset, formattedOffset, indentString) |
-{ |
- this._originalContent = content; |
- this._originalOffset = originalOffset; |
- this._lastOriginalPosition = 0; |
- |
- this._formattedContent = []; |
- this._formattedContentLength = 0; |
- this._formattedOffset = formattedOffset; |
- this._lastFormattedPosition = 0; |
- |
- this._mapping = mapping; |
- |
- this._lineNumber = 0; |
- this._nestingLevel = 0; |
- this._indentString = indentString; |
- this._cachedIndents = {}; |
-} |
- |
-FormattedContentBuilder.prototype = { |
- addToken: function(token) |
- { |
- for (var i = 0; i < token.comments_before.length; ++i) |
- this._addComment(token.comments_before[i]); |
- |
- while (this._lineNumber < token.line) { |
- this._addText("\n"); |
- this._addIndent(); |
- this._needNewLine = false; |
- this._lineNumber += 1; |
- } |
- |
- if (this._needNewLine) { |
- this._addText("\n"); |
- this._addIndent(); |
- this._needNewLine = false; |
- } |
- |
- this._addMappingIfNeeded(token.pos); |
- this._addText(this._originalContent.substring(token.pos, token.endPos)); |
- this._lineNumber = token.endLine; |
- }, |
- |
- addSpace: function() |
- { |
- this._addText(" "); |
- }, |
- |
- addNewLine: function() |
- { |
- this._needNewLine = true; |
- }, |
- |
- increaseNestingLevel: function() |
- { |
- this._nestingLevel += 1; |
- }, |
- |
- decreaseNestingLevel: function() |
- { |
- this._nestingLevel -= 1; |
- }, |
- |
- content: function() |
- { |
- return this._formattedContent.join(""); |
- }, |
- |
- mapping: function() |
- { |
- return { original: this._originalPositions, formatted: this._formattedPositions }; |
- }, |
- |
- _addIndent: function() |
- { |
- if (this._cachedIndents[this._nestingLevel]) { |
- this._addText(this._cachedIndents[this._nestingLevel]); |
- return; |
- } |
- |
- var fullIndent = ""; |
- for (var i = 0; i < this._nestingLevel; ++i) |
- fullIndent += this._indentString; |
- this._addText(fullIndent); |
- |
- // Cache a maximum of 20 nesting level indents. |
- if (this._nestingLevel <= 20) |
- this._cachedIndents[this._nestingLevel] = fullIndent; |
- }, |
- |
- _addComment: function(comment) |
- { |
- if (this._lineNumber < comment.line) { |
- for (var j = this._lineNumber; j < comment.line; ++j) |
- this._addText("\n"); |
- this._lineNumber = comment.line; |
- this._needNewLine = false; |
- this._addIndent(); |
- } else |
- this.addSpace(); |
- |
- this._addMappingIfNeeded(comment.pos); |
- if (comment.type === "comment1") |
- this._addText("//"); |
- else |
- this._addText("/*"); |
- |
- this._addText(comment.value); |
- |
- if (comment.type !== "comment1") { |
- this._addText("*/"); |
- var position; |
- while ((position = comment.value.indexOf("\n", position + 1)) !== -1) |
- this._lineNumber += 1; |
- } |
- }, |
- |
- _addText: function(text) |
- { |
- this._formattedContent.push(text); |
- this._formattedContentLength += text.length; |
- }, |
- |
- _addMappingIfNeeded: function(originalPosition) |
- { |
- if (originalPosition - this._lastOriginalPosition === this._formattedContentLength - this._lastFormattedPosition) |
- return; |
- this._mapping.original.push(this._originalOffset + originalPosition); |
- this._lastOriginalPosition = originalPosition; |
- this._mapping.formatted.push(this._formattedOffset + this._formattedContentLength); |
- this._lastFormattedPosition = this._formattedContentLength; |
- } |
-} |
- |
-var tokens = [ |
- ["EOS"], |
- ["LPAREN", "("], ["RPAREN", ")"], ["LBRACK", "["], ["RBRACK", "]"], ["LBRACE", "{"], ["RBRACE", "}"], ["COLON", ":"], ["SEMICOLON", ";"], ["PERIOD", "."], ["CONDITIONAL", "?"], |
- ["INC", "++"], ["DEC", "--"], |
- ["ASSIGN", "="], ["ASSIGN_BIT_OR", "|="], ["ASSIGN_BIT_XOR", "^="], ["ASSIGN_BIT_AND", "&="], ["ASSIGN_SHL", "<<="], ["ASSIGN_SAR", ">>="], ["ASSIGN_SHR", ">>>="], |
- ["ASSIGN_ADD", "+="], ["ASSIGN_SUB", "-="], ["ASSIGN_MUL", "*="], ["ASSIGN_DIV", "/="], ["ASSIGN_MOD", "%="], |
- ["COMMA", ","], ["OR", "||"], ["AND", "&&"], ["BIT_OR", "|"], ["BIT_XOR", "^"], ["BIT_AND", "&"], ["SHL", "<<"], ["SAR", ">>"], ["SHR", ">>>"], |
- ["ADD", "+"], ["SUB", "-"], ["MUL", "*"], ["DIV", "/"], ["MOD", "%"], |
- ["EQ", "=="], ["NE", "!="], ["EQ_STRICT", "==="], ["NE_STRICT", "!=="], ["LT", "<"], ["GT", ">"], ["LTE", "<="], ["GTE", ">="], |
- ["INSTANCEOF", "instanceof"], ["IN", "in"], ["NOT", "!"], ["BIT_NOT", "~"], ["DELETE", "delete"], ["TYPEOF", "typeof"], ["VOID", "void"], |
- ["BREAK", "break"], ["CASE", "case"], ["CATCH", "catch"], ["CONTINUE", "continue"], ["DEBUGGER", "debugger"], ["DEFAULT", "default"], ["DO", "do"], ["ELSE", "else"], ["FINALLY", "finally"], |
- ["FOR", "for"], ["FUNCTION", "function"], ["IF", "if"], ["NEW", "new"], ["RETURN", "return"], ["SWITCH", "switch"], ["THIS", "this"], ["THROW", "throw"], ["TRY", "try"], ["VAR", "var"], |
- ["WHILE", "while"], ["WITH", "with"], ["NULL_LITERAL", "null"], ["TRUE_LITERAL", "true"], ["FALSE_LITERAL", "false"], ["NUMBER"], ["STRING"], ["IDENTIFIER"], ["CONST", "const"] |
-]; |
- |
-var Tokens = {}; |
-for (var i = 0; i < tokens.length; ++i) |
- Tokens[tokens[i][0]] = i; |
- |
-var TokensByValue = {}; |
-for (var i = 0; i < tokens.length; ++i) { |
- if (tokens[i][1]) |
- TokensByValue[tokens[i][1]] = i; |
-} |
- |
-var TokensByType = { |
- "eof": Tokens.EOS, |
- "name": Tokens.IDENTIFIER, |
- "num": Tokens.NUMBER, |
- "regexp": Tokens.DIV, |
- "string": Tokens.STRING |
-}; |
- |
-function Tokenizer(content) |
-{ |
- this._readNextToken = parse.tokenizer(content); |
- this._state = this._readNextToken.context(); |
-} |
- |
-Tokenizer.prototype = { |
- content: function() |
- { |
- return this._state.text; |
- }, |
- |
- next: function(forceRegexp) |
- { |
- var uglifyToken = this._readNextToken(forceRegexp); |
- uglifyToken.endPos = this._state.pos; |
- uglifyToken.endLine = this._state.line; |
- uglifyToken.token = this._convertUglifyToken(uglifyToken); |
- return uglifyToken; |
- }, |
- |
- _convertUglifyToken: function(uglifyToken) |
- { |
- var token = TokensByType[uglifyToken.type]; |
- if (typeof token === "number") |
- return token; |
- token = TokensByValue[uglifyToken.value]; |
- if (typeof token === "number") |
- return token; |
- throw "Unknown token type " + uglifyToken.type; |
- } |
-} |
- |
-function JavaScriptFormatter(tokenizer, builder) |
+/** |
+ * @constructor |
+ * @param {FormatterWorker.JavaScriptTokenizer} tokenizer |
+ * @param {FormatterWorker.JavaScriptFormattedContentBuilder} builder |
+ */ |
+FormatterWorker.JavaScriptFormatter = function(tokenizer, builder) |
{ |
this._tokenizer = tokenizer; |
this._builder = builder; |
@@ -236,21 +41,27 @@ function JavaScriptFormatter(tokenizer, builder) |
this._nextToken = this._tokenizer.next(); |
} |
-JavaScriptFormatter.prototype = { |
+FormatterWorker.JavaScriptFormatter.prototype = { |
format: function() |
{ |
- this._parseSourceElements(Tokens.EOS); |
- this._consume(Tokens.EOS); |
+ this._parseSourceElements(FormatterWorker.JavaScriptTokens.EOS); |
+ this._consume(FormatterWorker.JavaScriptTokens.EOS); |
}, |
+ /** |
+ * @return {string} |
+ */ |
_peek: function() |
{ |
return this._nextToken.token; |
}, |
+ /** |
+ * @return {string} |
+ */ |
_next: function() |
{ |
- if (this._token && this._token.token === Tokens.EOS) |
+ if (this._token && this._token.token === FormatterWorker.JavaScriptTokens.EOS) |
throw "Unexpected EOS token"; |
this._builder.addToken(this._nextToken); |
@@ -260,6 +71,9 @@ JavaScriptFormatter.prototype = { |
return this._token.token; |
}, |
+ /** |
+ * @param {string} token |
+ */ |
_consume: function(token) |
{ |
var next = this._next(); |
@@ -267,6 +81,9 @@ JavaScriptFormatter.prototype = { |
throw "Unexpected token in consume: expected " + token + ", actual " + next; |
}, |
+ /** |
+ * @param {string} token |
+ */ |
_expect: function(token) |
{ |
var next = this._next(); |
@@ -276,15 +93,21 @@ JavaScriptFormatter.prototype = { |
_expectSemicolon: function() |
{ |
- if (this._peek() === Tokens.SEMICOLON) |
- this._consume(Tokens.SEMICOLON); |
+ if (this._peek() === FormatterWorker.JavaScriptTokens.SEMICOLON) |
+ this._consume(FormatterWorker.JavaScriptTokens.SEMICOLON); |
}, |
+ /** |
+ * @return {boolean} |
+ */ |
_hasLineTerminatorBeforeNext: function() |
{ |
return this._nextToken.nlb; |
}, |
+ /** |
+ * @param {string} endToken |
+ */ |
_parseSourceElements: function(endToken) |
{ |
while (this._peek() !== endToken) { |
@@ -295,7 +118,7 @@ JavaScriptFormatter.prototype = { |
_parseStatementOrBlock: function() |
{ |
- if (this._peek() === Tokens.LBRACE) { |
+ if (this._peek() === FormatterWorker.JavaScriptTokens.LBRACE) { |
this._builder.addSpace(); |
this._parseBlock(); |
return true; |
@@ -310,38 +133,38 @@ JavaScriptFormatter.prototype = { |
_parseStatement: function() |
{ |
switch (this._peek()) { |
- case Tokens.LBRACE: |
+ case FormatterWorker.JavaScriptTokens.LBRACE: |
return this._parseBlock(); |
- case Tokens.CONST: |
- case Tokens.VAR: |
+ case FormatterWorker.JavaScriptTokens.CONST: |
+ case FormatterWorker.JavaScriptTokens.VAR: |
return this._parseVariableStatement(); |
- case Tokens.SEMICOLON: |
+ case FormatterWorker.JavaScriptTokens.SEMICOLON: |
return this._next(); |
- case Tokens.IF: |
+ case FormatterWorker.JavaScriptTokens.IF: |
return this._parseIfStatement(); |
- case Tokens.DO: |
+ case FormatterWorker.JavaScriptTokens.DO: |
return this._parseDoWhileStatement(); |
- case Tokens.WHILE: |
+ case FormatterWorker.JavaScriptTokens.WHILE: |
return this._parseWhileStatement(); |
- case Tokens.FOR: |
+ case FormatterWorker.JavaScriptTokens.FOR: |
return this._parseForStatement(); |
- case Tokens.CONTINUE: |
+ case FormatterWorker.JavaScriptTokens.CONTINUE: |
return this._parseContinueStatement(); |
- case Tokens.BREAK: |
+ case FormatterWorker.JavaScriptTokens.BREAK: |
return this._parseBreakStatement(); |
- case Tokens.RETURN: |
+ case FormatterWorker.JavaScriptTokens.RETURN: |
return this._parseReturnStatement(); |
- case Tokens.WITH: |
+ case FormatterWorker.JavaScriptTokens.WITH: |
return this._parseWithStatement(); |
- case Tokens.SWITCH: |
+ case FormatterWorker.JavaScriptTokens.SWITCH: |
return this._parseSwitchStatement(); |
- case Tokens.THROW: |
+ case FormatterWorker.JavaScriptTokens.THROW: |
return this._parseThrowStatement(); |
- case Tokens.TRY: |
+ case FormatterWorker.JavaScriptTokens.TRY: |
return this._parseTryStatement(); |
- case Tokens.FUNCTION: |
+ case FormatterWorker.JavaScriptTokens.FUNCTION: |
return this._parseFunctionDeclaration(); |
- case Tokens.DEBUGGER: |
+ case FormatterWorker.JavaScriptTokens.DEBUGGER: |
return this._parseDebuggerStatement(); |
default: |
return this._parseExpressionOrLabelledStatement(); |
@@ -350,23 +173,23 @@ JavaScriptFormatter.prototype = { |
_parseFunctionDeclaration: function() |
{ |
- this._expect(Tokens.FUNCTION); |
+ this._expect(FormatterWorker.JavaScriptTokens.FUNCTION); |
this._builder.addSpace(); |
- this._expect(Tokens.IDENTIFIER); |
+ this._expect(FormatterWorker.JavaScriptTokens.IDENTIFIER); |
this._parseFunctionLiteral() |
}, |
_parseBlock: function() |
{ |
- this._expect(Tokens.LBRACE); |
+ this._expect(FormatterWorker.JavaScriptTokens.LBRACE); |
this._builder.addNewLine(); |
this._builder.increaseNestingLevel(); |
- while (this._peek() !== Tokens.RBRACE) { |
+ while (this._peek() !== FormatterWorker.JavaScriptTokens.RBRACE) { |
this._parseStatement(); |
this._builder.addNewLine(); |
} |
this._builder.decreaseNestingLevel(); |
- this._expect(Tokens.RBRACE); |
+ this._expect(FormatterWorker.JavaScriptTokens.RBRACE); |
}, |
_parseVariableStatement: function() |
@@ -377,34 +200,34 @@ JavaScriptFormatter.prototype = { |
_parseVariableDeclarations: function() |
{ |
- if (this._peek() === Tokens.VAR) |
- this._consume(Tokens.VAR); |
+ if (this._peek() === FormatterWorker.JavaScriptTokens.VAR) |
+ this._consume(FormatterWorker.JavaScriptTokens.VAR); |
else |
- this._consume(Tokens.CONST) |
+ this._consume(FormatterWorker.JavaScriptTokens.CONST) |
this._builder.addSpace(); |
var isFirstVariable = true; |
do { |
if (!isFirstVariable) { |
- this._consume(Tokens.COMMA); |
+ this._consume(FormatterWorker.JavaScriptTokens.COMMA); |
this._builder.addSpace(); |
} |
isFirstVariable = false; |
- this._expect(Tokens.IDENTIFIER); |
- if (this._peek() === Tokens.ASSIGN) { |
+ this._expect(FormatterWorker.JavaScriptTokens.IDENTIFIER); |
+ if (this._peek() === FormatterWorker.JavaScriptTokens.ASSIGN) { |
this._builder.addSpace(); |
- this._consume(Tokens.ASSIGN); |
+ this._consume(FormatterWorker.JavaScriptTokens.ASSIGN); |
this._builder.addSpace(); |
this._parseAssignmentExpression(); |
} |
- } while (this._peek() === Tokens.COMMA); |
+ } while (this._peek() === FormatterWorker.JavaScriptTokens.COMMA); |
}, |
_parseExpressionOrLabelledStatement: function() |
{ |
this._parseExpression(); |
- if (this._peek() === Tokens.COLON) { |
- this._expect(Tokens.COLON); |
+ if (this._peek() === FormatterWorker.JavaScriptTokens.COLON) { |
+ this._expect(FormatterWorker.JavaScriptTokens.COLON); |
this._builder.addSpace(); |
this._parseStatement(); |
} |
@@ -413,21 +236,21 @@ JavaScriptFormatter.prototype = { |
_parseIfStatement: function() |
{ |
- this._expect(Tokens.IF); |
+ this._expect(FormatterWorker.JavaScriptTokens.IF); |
this._builder.addSpace(); |
- this._expect(Tokens.LPAREN); |
+ this._expect(FormatterWorker.JavaScriptTokens.LPAREN); |
this._parseExpression(); |
- this._expect(Tokens.RPAREN); |
+ this._expect(FormatterWorker.JavaScriptTokens.RPAREN); |
var isBlock = this._parseStatementOrBlock(); |
- if (this._peek() === Tokens.ELSE) { |
+ if (this._peek() === FormatterWorker.JavaScriptTokens.ELSE) { |
if (isBlock) |
this._builder.addSpace(); |
else |
this._builder.addNewLine(); |
this._next(); |
- if (this._peek() === Tokens.IF) { |
+ if (this._peek() === FormatterWorker.JavaScriptTokens.IF) { |
this._builder.addSpace(); |
this._parseStatement(); |
} else |
@@ -437,31 +260,31 @@ JavaScriptFormatter.prototype = { |
_parseContinueStatement: function() |
{ |
- this._expect(Tokens.CONTINUE); |
+ this._expect(FormatterWorker.JavaScriptTokens.CONTINUE); |
var token = this._peek(); |
- if (!this._hasLineTerminatorBeforeNext() && token !== Tokens.SEMICOLON && token !== Tokens.RBRACE && token !== Tokens.EOS) { |
+ if (!this._hasLineTerminatorBeforeNext() && token !== FormatterWorker.JavaScriptTokens.SEMICOLON && token !== FormatterWorker.JavaScriptTokens.RBRACE && token !== FormatterWorker.JavaScriptTokens.EOS) { |
this._builder.addSpace(); |
- this._expect(Tokens.IDENTIFIER); |
+ this._expect(FormatterWorker.JavaScriptTokens.IDENTIFIER); |
} |
this._expectSemicolon(); |
}, |
_parseBreakStatement: function() |
{ |
- this._expect(Tokens.BREAK); |
+ this._expect(FormatterWorker.JavaScriptTokens.BREAK); |
var token = this._peek(); |
- if (!this._hasLineTerminatorBeforeNext() && token !== Tokens.SEMICOLON && token !== Tokens.RBRACE && token !== Tokens.EOS) { |
+ if (!this._hasLineTerminatorBeforeNext() && token !== FormatterWorker.JavaScriptTokens.SEMICOLON && token !== FormatterWorker.JavaScriptTokens.RBRACE && token !== FormatterWorker.JavaScriptTokens.EOS) { |
this._builder.addSpace(); |
- this._expect(Tokens.IDENTIFIER); |
+ this._expect(FormatterWorker.JavaScriptTokens.IDENTIFIER); |
} |
this._expectSemicolon(); |
}, |
_parseReturnStatement: function() |
{ |
- this._expect(Tokens.RETURN); |
+ this._expect(FormatterWorker.JavaScriptTokens.RETURN); |
var token = this._peek(); |
- if (!this._hasLineTerminatorBeforeNext() && token !== Tokens.SEMICOLON && token !== Tokens.RBRACE && token !== Tokens.EOS) { |
+ if (!this._hasLineTerminatorBeforeNext() && token !== FormatterWorker.JavaScriptTokens.SEMICOLON && token !== FormatterWorker.JavaScriptTokens.RBRACE && token !== FormatterWorker.JavaScriptTokens.EOS) { |
this._builder.addSpace(); |
this._parseExpression(); |
} |
@@ -470,27 +293,27 @@ JavaScriptFormatter.prototype = { |
_parseWithStatement: function() |
{ |
- this._expect(Tokens.WITH); |
+ this._expect(FormatterWorker.JavaScriptTokens.WITH); |
this._builder.addSpace(); |
- this._expect(Tokens.LPAREN); |
+ this._expect(FormatterWorker.JavaScriptTokens.LPAREN); |
this._parseExpression(); |
- this._expect(Tokens.RPAREN); |
+ this._expect(FormatterWorker.JavaScriptTokens.RPAREN); |
this._parseStatementOrBlock(); |
}, |
_parseCaseClause: function() |
{ |
- if (this._peek() === Tokens.CASE) { |
- this._expect(Tokens.CASE); |
+ if (this._peek() === FormatterWorker.JavaScriptTokens.CASE) { |
+ this._expect(FormatterWorker.JavaScriptTokens.CASE); |
this._builder.addSpace(); |
this._parseExpression(); |
} else |
- this._expect(Tokens.DEFAULT); |
- this._expect(Tokens.COLON); |
+ this._expect(FormatterWorker.JavaScriptTokens.DEFAULT); |
+ this._expect(FormatterWorker.JavaScriptTokens.COLON); |
this._builder.addNewLine(); |
this._builder.increaseNestingLevel(); |
- while (this._peek() !== Tokens.CASE && this._peek() !== Tokens.DEFAULT && this._peek() !== Tokens.RBRACE) { |
+ while (this._peek() !== FormatterWorker.JavaScriptTokens.CASE && this._peek() !== FormatterWorker.JavaScriptTokens.DEFAULT && this._peek() !== FormatterWorker.JavaScriptTokens.RBRACE) { |
this._parseStatement(); |
this._builder.addNewLine(); |
} |
@@ -499,25 +322,25 @@ JavaScriptFormatter.prototype = { |
_parseSwitchStatement: function() |
{ |
- this._expect(Tokens.SWITCH); |
+ this._expect(FormatterWorker.JavaScriptTokens.SWITCH); |
this._builder.addSpace(); |
- this._expect(Tokens.LPAREN); |
+ this._expect(FormatterWorker.JavaScriptTokens.LPAREN); |
this._parseExpression(); |
- this._expect(Tokens.RPAREN); |
+ this._expect(FormatterWorker.JavaScriptTokens.RPAREN); |
this._builder.addSpace(); |
- this._expect(Tokens.LBRACE); |
+ this._expect(FormatterWorker.JavaScriptTokens.LBRACE); |
this._builder.addNewLine(); |
this._builder.increaseNestingLevel(); |
- while (this._peek() !== Tokens.RBRACE) |
+ while (this._peek() !== FormatterWorker.JavaScriptTokens.RBRACE) |
this._parseCaseClause(); |
this._builder.decreaseNestingLevel(); |
- this._expect(Tokens.RBRACE); |
+ this._expect(FormatterWorker.JavaScriptTokens.RBRACE); |
}, |
_parseThrowStatement: function() |
{ |
- this._expect(Tokens.THROW); |
+ this._expect(FormatterWorker.JavaScriptTokens.THROW); |
this._builder.addSpace(); |
this._parseExpression(); |
this._expectSemicolon(); |
@@ -525,25 +348,25 @@ JavaScriptFormatter.prototype = { |
_parseTryStatement: function() |
{ |
- this._expect(Tokens.TRY); |
+ this._expect(FormatterWorker.JavaScriptTokens.TRY); |
this._builder.addSpace(); |
this._parseBlock(); |
var token = this._peek(); |
- if (token === Tokens.CATCH) { |
+ if (token === FormatterWorker.JavaScriptTokens.CATCH) { |
this._builder.addSpace(); |
- this._consume(Tokens.CATCH); |
+ this._consume(FormatterWorker.JavaScriptTokens.CATCH); |
this._builder.addSpace(); |
- this._expect(Tokens.LPAREN); |
- this._expect(Tokens.IDENTIFIER); |
- this._expect(Tokens.RPAREN); |
+ this._expect(FormatterWorker.JavaScriptTokens.LPAREN); |
+ this._expect(FormatterWorker.JavaScriptTokens.IDENTIFIER); |
+ this._expect(FormatterWorker.JavaScriptTokens.RPAREN); |
this._builder.addSpace(); |
this._parseBlock(); |
token = this._peek(); |
} |
- if (token === Tokens.FINALLY) { |
- this._consume(Tokens.FINALLY); |
+ if (token === FormatterWorker.JavaScriptTokens.FINALLY) { |
+ this._consume(FormatterWorker.JavaScriptTokens.FINALLY); |
this._builder.addSpace(); |
this._parseBlock(); |
} |
@@ -551,41 +374,41 @@ JavaScriptFormatter.prototype = { |
_parseDoWhileStatement: function() |
{ |
- this._expect(Tokens.DO); |
+ this._expect(FormatterWorker.JavaScriptTokens.DO); |
var isBlock = this._parseStatementOrBlock(); |
if (isBlock) |
this._builder.addSpace(); |
else |
this._builder.addNewLine(); |
- this._expect(Tokens.WHILE); |
+ this._expect(FormatterWorker.JavaScriptTokens.WHILE); |
this._builder.addSpace(); |
- this._expect(Tokens.LPAREN); |
+ this._expect(FormatterWorker.JavaScriptTokens.LPAREN); |
this._parseExpression(); |
- this._expect(Tokens.RPAREN); |
+ this._expect(FormatterWorker.JavaScriptTokens.RPAREN); |
this._expectSemicolon(); |
}, |
_parseWhileStatement: function() |
{ |
- this._expect(Tokens.WHILE); |
+ this._expect(FormatterWorker.JavaScriptTokens.WHILE); |
this._builder.addSpace(); |
- this._expect(Tokens.LPAREN); |
+ this._expect(FormatterWorker.JavaScriptTokens.LPAREN); |
this._parseExpression(); |
- this._expect(Tokens.RPAREN); |
+ this._expect(FormatterWorker.JavaScriptTokens.RPAREN); |
this._parseStatementOrBlock(); |
}, |
_parseForStatement: function() |
{ |
- this._expect(Tokens.FOR); |
+ this._expect(FormatterWorker.JavaScriptTokens.FOR); |
this._builder.addSpace(); |
- this._expect(Tokens.LPAREN); |
- if (this._peek() !== Tokens.SEMICOLON) { |
- if (this._peek() === Tokens.VAR || this._peek() === Tokens.CONST) { |
+ this._expect(FormatterWorker.JavaScriptTokens.LPAREN); |
+ if (this._peek() !== FormatterWorker.JavaScriptTokens.SEMICOLON) { |
+ if (this._peek() === FormatterWorker.JavaScriptTokens.VAR || this._peek() === FormatterWorker.JavaScriptTokens.CONST) { |
this._parseVariableDeclarations(); |
- if (this._peek() === Tokens.IN) { |
+ if (this._peek() === FormatterWorker.JavaScriptTokens.IN) { |
this._builder.addSpace(); |
- this._consume(Tokens.IN); |
+ this._consume(FormatterWorker.JavaScriptTokens.IN); |
this._builder.addSpace(); |
this._parseExpression(); |
} |
@@ -593,17 +416,17 @@ JavaScriptFormatter.prototype = { |
this._parseExpression(); |
} |
- if (this._peek() !== Tokens.RPAREN) { |
- this._expect(Tokens.SEMICOLON); |
+ if (this._peek() !== FormatterWorker.JavaScriptTokens.RPAREN) { |
+ this._expect(FormatterWorker.JavaScriptTokens.SEMICOLON); |
this._builder.addSpace(); |
- if (this._peek() !== Tokens.SEMICOLON) |
+ if (this._peek() !== FormatterWorker.JavaScriptTokens.SEMICOLON) |
this._parseExpression(); |
- this._expect(Tokens.SEMICOLON); |
+ this._expect(FormatterWorker.JavaScriptTokens.SEMICOLON); |
this._builder.addSpace(); |
- if (this._peek() !== Tokens.RPAREN) |
+ if (this._peek() !== FormatterWorker.JavaScriptTokens.RPAREN) |
this._parseExpression(); |
} |
- this._expect(Tokens.RPAREN); |
+ this._expect(FormatterWorker.JavaScriptTokens.RPAREN); |
this._parseStatementOrBlock(); |
}, |
@@ -611,8 +434,8 @@ JavaScriptFormatter.prototype = { |
_parseExpression: function() |
{ |
this._parseAssignmentExpression(); |
- while (this._peek() === Tokens.COMMA) { |
- this._expect(Tokens.COMMA); |
+ while (this._peek() === FormatterWorker.JavaScriptTokens.COMMA) { |
+ this._expect(FormatterWorker.JavaScriptTokens.COMMA); |
this._builder.addSpace(); |
this._parseAssignmentExpression(); |
} |
@@ -622,7 +445,7 @@ JavaScriptFormatter.prototype = { |
{ |
this._parseConditionalExpression(); |
var token = this._peek(); |
- if (Tokens.ASSIGN <= token && token <= Tokens.ASSIGN_MOD) { |
+ if (FormatterWorker.JavaScriptTokens.ASSIGN <= token && token <= FormatterWorker.JavaScriptTokens.ASSIGN_MOD) { |
this._builder.addSpace(); |
this._next(); |
this._builder.addSpace(); |
@@ -633,13 +456,13 @@ JavaScriptFormatter.prototype = { |
_parseConditionalExpression: function() |
{ |
this._parseBinaryExpression(); |
- if (this._peek() === Tokens.CONDITIONAL) { |
+ if (this._peek() === FormatterWorker.JavaScriptTokens.CONDITIONAL) { |
this._builder.addSpace(); |
- this._consume(Tokens.CONDITIONAL); |
+ this._consume(FormatterWorker.JavaScriptTokens.CONDITIONAL); |
this._builder.addSpace(); |
this._parseAssignmentExpression(); |
this._builder.addSpace(); |
- this._expect(Tokens.COLON); |
+ this._expect(FormatterWorker.JavaScriptTokens.COLON); |
this._builder.addSpace(); |
this._parseAssignmentExpression(); |
} |
@@ -649,7 +472,7 @@ JavaScriptFormatter.prototype = { |
{ |
this._parseUnaryExpression(); |
var token = this._peek(); |
- while (Tokens.OR <= token && token <= Tokens.IN) { |
+ while (FormatterWorker.JavaScriptTokens.OR <= token && token <= FormatterWorker.JavaScriptTokens.IN) { |
this._builder.addSpace(); |
this._next(); |
this._builder.addSpace(); |
@@ -661,9 +484,9 @@ JavaScriptFormatter.prototype = { |
_parseUnaryExpression: function() |
{ |
var token = this._peek(); |
- if ((Tokens.NOT <= token && token <= Tokens.VOID) || token === Tokens.ADD || token === Tokens.SUB || token === Tokens.INC || token === Tokens.DEC) { |
+ if ((FormatterWorker.JavaScriptTokens.NOT <= token && token <= FormatterWorker.JavaScriptTokens.VOID) || token === FormatterWorker.JavaScriptTokens.ADD || token === FormatterWorker.JavaScriptTokens.SUB || token === FormatterWorker.JavaScriptTokens.INC || token === FormatterWorker.JavaScriptTokens.DEC) { |
this._next(); |
- if (token === Tokens.DELETE || token === Tokens.TYPEOF || token === Tokens.VOID) |
+ if (token === FormatterWorker.JavaScriptTokens.DELETE || token === FormatterWorker.JavaScriptTokens.TYPEOF || token === FormatterWorker.JavaScriptTokens.VOID) |
this._builder.addSpace(); |
this._parseUnaryExpression(); |
} else |
@@ -674,32 +497,32 @@ JavaScriptFormatter.prototype = { |
{ |
this._parseLeftHandSideExpression(); |
var token = this._peek(); |
- if (!this._hasLineTerminatorBeforeNext() && (token === Tokens.INC || token === Tokens.DEC)) |
+ if (!this._hasLineTerminatorBeforeNext() && (token === FormatterWorker.JavaScriptTokens.INC || token === FormatterWorker.JavaScriptTokens.DEC)) |
this._next(); |
}, |
_parseLeftHandSideExpression: function() |
{ |
- if (this._peek() === Tokens.NEW) |
+ if (this._peek() === FormatterWorker.JavaScriptTokens.NEW) |
this._parseNewExpression(); |
else |
this._parseMemberExpression(); |
while (true) { |
switch (this._peek()) { |
- case Tokens.LBRACK: |
- this._consume(Tokens.LBRACK); |
+ case FormatterWorker.JavaScriptTokens.LBRACK: |
+ this._consume(FormatterWorker.JavaScriptTokens.LBRACK); |
this._parseExpression(); |
- this._expect(Tokens.RBRACK); |
+ this._expect(FormatterWorker.JavaScriptTokens.RBRACK); |
break; |
- case Tokens.LPAREN: |
+ case FormatterWorker.JavaScriptTokens.LPAREN: |
this._parseArguments(); |
break; |
- case Tokens.PERIOD: |
- this._consume(Tokens.PERIOD); |
- this._expect(Tokens.IDENTIFIER); |
+ case FormatterWorker.JavaScriptTokens.PERIOD: |
+ this._consume(FormatterWorker.JavaScriptTokens.PERIOD); |
+ this._expect(FormatterWorker.JavaScriptTokens.IDENTIFIER); |
break; |
default: |
@@ -710,9 +533,9 @@ JavaScriptFormatter.prototype = { |
_parseNewExpression: function() |
{ |
- this._expect(Tokens.NEW); |
+ this._expect(FormatterWorker.JavaScriptTokens.NEW); |
this._builder.addSpace(); |
- if (this._peek() === Tokens.NEW) |
+ if (this._peek() === FormatterWorker.JavaScriptTokens.NEW) |
this._parseNewExpression(); |
else |
this._parseMemberExpression(); |
@@ -720,11 +543,11 @@ JavaScriptFormatter.prototype = { |
_parseMemberExpression: function() |
{ |
- if (this._peek() === Tokens.FUNCTION) { |
- this._expect(Tokens.FUNCTION); |
- if (this._peek() === Tokens.IDENTIFIER) { |
+ if (this._peek() === FormatterWorker.JavaScriptTokens.FUNCTION) { |
+ this._expect(FormatterWorker.JavaScriptTokens.FUNCTION); |
+ if (this._peek() === FormatterWorker.JavaScriptTokens.IDENTIFIER) { |
this._builder.addSpace(); |
- this._expect(Tokens.IDENTIFIER); |
+ this._expect(FormatterWorker.JavaScriptTokens.IDENTIFIER); |
} |
this._parseFunctionLiteral(); |
} else |
@@ -732,18 +555,18 @@ JavaScriptFormatter.prototype = { |
while (true) { |
switch (this._peek()) { |
- case Tokens.LBRACK: |
- this._consume(Tokens.LBRACK); |
+ case FormatterWorker.JavaScriptTokens.LBRACK: |
+ this._consume(FormatterWorker.JavaScriptTokens.LBRACK); |
this._parseExpression(); |
- this._expect(Tokens.RBRACK); |
+ this._expect(FormatterWorker.JavaScriptTokens.RBRACK); |
break; |
- case Tokens.PERIOD: |
- this._consume(Tokens.PERIOD); |
- this._expect(Tokens.IDENTIFIER); |
+ case FormatterWorker.JavaScriptTokens.PERIOD: |
+ this._consume(FormatterWorker.JavaScriptTokens.PERIOD); |
+ this._expect(FormatterWorker.JavaScriptTokens.IDENTIFIER); |
break; |
- case Tokens.LPAREN: |
+ case FormatterWorker.JavaScriptTokens.LPAREN: |
this._parseArguments(); |
break; |
@@ -755,39 +578,39 @@ JavaScriptFormatter.prototype = { |
_parseDebuggerStatement: function() |
{ |
- this._expect(Tokens.DEBUGGER); |
+ this._expect(FormatterWorker.JavaScriptTokens.DEBUGGER); |
this._expectSemicolon(); |
}, |
_parsePrimaryExpression: function() |
{ |
switch (this._peek()) { |
- case Tokens.THIS: |
- return this._consume(Tokens.THIS); |
- case Tokens.NULL_LITERAL: |
- return this._consume(Tokens.NULL_LITERAL); |
- case Tokens.TRUE_LITERAL: |
- return this._consume(Tokens.TRUE_LITERAL); |
- case Tokens.FALSE_LITERAL: |
- return this._consume(Tokens.FALSE_LITERAL); |
- case Tokens.IDENTIFIER: |
- return this._consume(Tokens.IDENTIFIER); |
- case Tokens.NUMBER: |
- return this._consume(Tokens.NUMBER); |
- case Tokens.STRING: |
- return this._consume(Tokens.STRING); |
- case Tokens.ASSIGN_DIV: |
+ case FormatterWorker.JavaScriptTokens.THIS: |
+ return this._consume(FormatterWorker.JavaScriptTokens.THIS); |
+ case FormatterWorker.JavaScriptTokens.NULL_LITERAL: |
+ return this._consume(FormatterWorker.JavaScriptTokens.NULL_LITERAL); |
+ case FormatterWorker.JavaScriptTokens.TRUE_LITERAL: |
+ return this._consume(FormatterWorker.JavaScriptTokens.TRUE_LITERAL); |
+ case FormatterWorker.JavaScriptTokens.FALSE_LITERAL: |
+ return this._consume(FormatterWorker.JavaScriptTokens.FALSE_LITERAL); |
+ case FormatterWorker.JavaScriptTokens.IDENTIFIER: |
+ return this._consume(FormatterWorker.JavaScriptTokens.IDENTIFIER); |
+ case FormatterWorker.JavaScriptTokens.NUMBER: |
+ return this._consume(FormatterWorker.JavaScriptTokens.NUMBER); |
+ case FormatterWorker.JavaScriptTokens.STRING: |
+ return this._consume(FormatterWorker.JavaScriptTokens.STRING); |
+ case FormatterWorker.JavaScriptTokens.ASSIGN_DIV: |
return this._parseRegExpLiteral(); |
- case Tokens.DIV: |
+ case FormatterWorker.JavaScriptTokens.DIV: |
return this._parseRegExpLiteral(); |
- case Tokens.LBRACK: |
+ case FormatterWorker.JavaScriptTokens.LBRACK: |
return this._parseArrayLiteral(); |
- case Tokens.LBRACE: |
+ case FormatterWorker.JavaScriptTokens.LBRACE: |
return this._parseObjectLiteral(); |
- case Tokens.LPAREN: |
- this._consume(Tokens.LPAREN); |
+ case FormatterWorker.JavaScriptTokens.LPAREN: |
+ this._consume(FormatterWorker.JavaScriptTokens.LPAREN); |
this._parseExpression(); |
- this._expect(Tokens.RPAREN); |
+ this._expect(FormatterWorker.JavaScriptTokens.RPAREN); |
return; |
default: |
return this._next(); |
@@ -796,26 +619,26 @@ JavaScriptFormatter.prototype = { |
_parseArrayLiteral: function() |
{ |
- this._expect(Tokens.LBRACK); |
+ this._expect(FormatterWorker.JavaScriptTokens.LBRACK); |
this._builder.increaseNestingLevel(); |
- while (this._peek() !== Tokens.RBRACK) { |
- if (this._peek() !== Tokens.COMMA) |
+ while (this._peek() !== FormatterWorker.JavaScriptTokens.RBRACK) { |
+ if (this._peek() !== FormatterWorker.JavaScriptTokens.COMMA) |
this._parseAssignmentExpression(); |
- if (this._peek() !== Tokens.RBRACK) { |
- this._expect(Tokens.COMMA); |
+ if (this._peek() !== FormatterWorker.JavaScriptTokens.RBRACK) { |
+ this._expect(FormatterWorker.JavaScriptTokens.COMMA); |
this._builder.addSpace(); |
} |
} |
this._builder.decreaseNestingLevel(); |
- this._expect(Tokens.RBRACK); |
+ this._expect(FormatterWorker.JavaScriptTokens.RBRACK); |
}, |
_parseObjectLiteralGetSet: function() |
{ |
var token = this._peek(); |
- if (token === Tokens.IDENTIFIER || token === Tokens.NUMBER || token === Tokens.STRING || |
- Tokens.DELETE <= token && token <= Tokens.FALSE_LITERAL || |
- token === Tokens.INSTANCEOF || token === Tokens.IN || token === Tokens.CONST) { |
+ if (token === FormatterWorker.JavaScriptTokens.IDENTIFIER || token === FormatterWorker.JavaScriptTokens.NUMBER || token === FormatterWorker.JavaScriptTokens.STRING || |
+ FormatterWorker.JavaScriptTokens.DELETE <= token && token <= FormatterWorker.JavaScriptTokens.FALSE_LITERAL || |
+ token === FormatterWorker.JavaScriptTokens.INSTANCEOF || token === FormatterWorker.JavaScriptTokens.IN || token === FormatterWorker.JavaScriptTokens.CONST) { |
this._next(); |
this._parseFunctionLiteral(); |
} |
@@ -823,46 +646,46 @@ JavaScriptFormatter.prototype = { |
_parseObjectLiteral: function() |
{ |
- this._expect(Tokens.LBRACE); |
+ this._expect(FormatterWorker.JavaScriptTokens.LBRACE); |
this._builder.increaseNestingLevel(); |
- while (this._peek() !== Tokens.RBRACE) { |
+ while (this._peek() !== FormatterWorker.JavaScriptTokens.RBRACE) { |
var token = this._peek(); |
switch (token) { |
- case Tokens.IDENTIFIER: |
- this._consume(Tokens.IDENTIFIER); |
+ case FormatterWorker.JavaScriptTokens.IDENTIFIER: |
+ this._consume(FormatterWorker.JavaScriptTokens.IDENTIFIER); |
var name = this._token.value; |
- if ((name === "get" || name === "set") && this._peek() !== Tokens.COLON) { |
+ if ((name === "get" || name === "set") && this._peek() !== FormatterWorker.JavaScriptTokens.COLON) { |
this._builder.addSpace(); |
this._parseObjectLiteralGetSet(); |
- if (this._peek() !== Tokens.RBRACE) { |
- this._expect(Tokens.COMMA); |
+ if (this._peek() !== FormatterWorker.JavaScriptTokens.RBRACE) { |
+ this._expect(FormatterWorker.JavaScriptTokens.COMMA); |
} |
continue; |
} |
break; |
- case Tokens.STRING: |
- this._consume(Tokens.STRING); |
+ case FormatterWorker.JavaScriptTokens.STRING: |
+ this._consume(FormatterWorker.JavaScriptTokens.STRING); |
break; |
- case Tokens.NUMBER: |
- this._consume(Tokens.NUMBER); |
+ case FormatterWorker.JavaScriptTokens.NUMBER: |
+ this._consume(FormatterWorker.JavaScriptTokens.NUMBER); |
break; |
default: |
this._next(); |
} |
- this._expect(Tokens.COLON); |
+ this._expect(FormatterWorker.JavaScriptTokens.COLON); |
this._builder.addSpace(); |
this._parseAssignmentExpression(); |
- if (this._peek() !== Tokens.RBRACE) { |
- this._expect(Tokens.COMMA); |
+ if (this._peek() !== FormatterWorker.JavaScriptTokens.RBRACE) { |
+ this._expect(FormatterWorker.JavaScriptTokens.COMMA); |
} |
} |
this._builder.decreaseNestingLevel(); |
- this._expect(Tokens.RBRACE); |
+ this._expect(FormatterWorker.JavaScriptTokens.RBRACE); |
}, |
_parseRegExpLiteral: function() |
@@ -877,39 +700,325 @@ JavaScriptFormatter.prototype = { |
_parseArguments: function() |
{ |
- this._expect(Tokens.LPAREN); |
- var done = (this._peek() === Tokens.RPAREN); |
+ this._expect(FormatterWorker.JavaScriptTokens.LPAREN); |
+ var done = (this._peek() === FormatterWorker.JavaScriptTokens.RPAREN); |
while (!done) { |
this._parseAssignmentExpression(); |
- done = (this._peek() === Tokens.RPAREN); |
+ done = (this._peek() === FormatterWorker.JavaScriptTokens.RPAREN); |
if (!done) { |
- this._expect(Tokens.COMMA); |
+ this._expect(FormatterWorker.JavaScriptTokens.COMMA); |
this._builder.addSpace(); |
} |
} |
- this._expect(Tokens.RPAREN); |
+ this._expect(FormatterWorker.JavaScriptTokens.RPAREN); |
}, |
_parseFunctionLiteral: function() |
{ |
- this._expect(Tokens.LPAREN); |
- var done = (this._peek() === Tokens.RPAREN); |
+ this._expect(FormatterWorker.JavaScriptTokens.LPAREN); |
+ var done = (this._peek() === FormatterWorker.JavaScriptTokens.RPAREN); |
while (!done) { |
- this._expect(Tokens.IDENTIFIER); |
- done = (this._peek() === Tokens.RPAREN); |
+ this._expect(FormatterWorker.JavaScriptTokens.IDENTIFIER); |
+ done = (this._peek() === FormatterWorker.JavaScriptTokens.RPAREN); |
if (!done) { |
- this._expect(Tokens.COMMA); |
+ this._expect(FormatterWorker.JavaScriptTokens.COMMA); |
this._builder.addSpace(); |
} |
} |
- this._expect(Tokens.RPAREN); |
+ this._expect(FormatterWorker.JavaScriptTokens.RPAREN); |
this._builder.addSpace(); |
- this._expect(Tokens.LBRACE); |
+ this._expect(FormatterWorker.JavaScriptTokens.LBRACE); |
this._builder.addNewLine(); |
this._builder.increaseNestingLevel(); |
- this._parseSourceElements(Tokens.RBRACE); |
+ this._parseSourceElements(FormatterWorker.JavaScriptTokens.RBRACE); |
this._builder.decreaseNestingLevel(); |
- this._expect(Tokens.RBRACE); |
+ this._expect(FormatterWorker.JavaScriptTokens.RBRACE); |
+ } |
+} |
+ |
+/** |
+ * @constructor |
+ * @param {string} content |
+ * @param {{original: Array.<number>, formatted: Array.<number>}} mapping |
+ * @param {number} originalOffset |
+ * @param {number} formattedOffset |
+ * @param {string} indentString |
+ */ |
+FormatterWorker.JavaScriptFormattedContentBuilder = function(content, mapping, originalOffset, formattedOffset, indentString) |
+{ |
+ this._originalContent = content; |
+ this._originalOffset = originalOffset; |
+ this._lastOriginalPosition = 0; |
+ |
+ this._formattedContent = []; |
+ this._formattedContentLength = 0; |
+ this._formattedOffset = formattedOffset; |
+ this._lastFormattedPosition = 0; |
+ |
+ this._mapping = mapping; |
+ |
+ this._lineNumber = 0; |
+ this._nestingLevel = 0; |
+ this._indentString = indentString; |
+ this._cachedIndents = {}; |
+} |
+ |
+FormatterWorker.JavaScriptFormattedContentBuilder.prototype = { |
+ /** |
+ * @param {{comments_before: Array.<string>, line: number, pos: number, endLine: number, nlb: boolean}} token |
+ */ |
+ addToken: function(token) |
+ { |
+ for (var i = 0; i < token.comments_before.length; ++i) |
+ this._addComment(token.comments_before[i]); |
+ |
+ while (this._lineNumber < token.line) { |
+ this._addText("\n"); |
+ this._addIndent(); |
+ this._needNewLine = false; |
+ this._lineNumber += 1; |
+ } |
+ |
+ if (this._needNewLine) { |
+ this._addText("\n"); |
+ this._addIndent(); |
+ this._needNewLine = false; |
+ } |
+ |
+ this._addMappingIfNeeded(token.pos); |
+ this._addText(this._originalContent.substring(token.pos, token.endPos)); |
+ this._lineNumber = token.endLine; |
+ }, |
+ |
+ addSpace: function() |
+ { |
+ this._addText(" "); |
+ }, |
+ |
+ addNewLine: function() |
+ { |
+ this._needNewLine = true; |
+ }, |
+ |
+ increaseNestingLevel: function() |
+ { |
+ this._nestingLevel += 1; |
+ }, |
+ |
+ decreaseNestingLevel: function() |
+ { |
+ this._nestingLevel -= 1; |
+ }, |
+ |
+ /** |
+ * @return {string} |
+ */ |
+ content: function() |
+ { |
+ return this._formattedContent.join(""); |
+ }, |
+ |
+ _addIndent: function() |
+ { |
+ if (this._cachedIndents[this._nestingLevel]) { |
+ this._addText(this._cachedIndents[this._nestingLevel]); |
+ return; |
+ } |
+ |
+ var fullIndent = ""; |
+ for (var i = 0; i < this._nestingLevel; ++i) |
+ fullIndent += this._indentString; |
+ this._addText(fullIndent); |
+ |
+ // Cache a maximum of 20 nesting level indents. |
+ if (this._nestingLevel <= 20) |
+ this._cachedIndents[this._nestingLevel] = fullIndent; |
+ }, |
+ |
+ _addComment: function(comment) |
+ { |
+ if (this._lineNumber < comment.line) { |
+ for (var j = this._lineNumber; j < comment.line; ++j) |
+ this._addText("\n"); |
+ this._lineNumber = comment.line; |
+ this._needNewLine = false; |
+ this._addIndent(); |
+ } else |
+ this.addSpace(); |
+ |
+ this._addMappingIfNeeded(comment.pos); |
+ if (comment.type === "comment1") |
+ this._addText("//"); |
+ else |
+ this._addText("/*"); |
+ |
+ this._addText(comment.value); |
+ |
+ if (comment.type !== "comment1") { |
+ this._addText("*/"); |
+ var position; |
+ while ((position = comment.value.indexOf("\n", position + 1)) !== -1) |
+ this._lineNumber += 1; |
+ } |
+ }, |
+ |
+ /** |
+ * @param {string} text |
+ */ |
+ _addText: function(text) |
+ { |
+ this._formattedContent.push(text); |
+ this._formattedContentLength += text.length; |
+ }, |
+ |
+ /** |
+ * @param {number} originalPosition |
+ */ |
+ _addMappingIfNeeded: function(originalPosition) |
+ { |
+ if (originalPosition - this._lastOriginalPosition === this._formattedContentLength - this._lastFormattedPosition) |
+ return; |
+ this._mapping.original.push(this._originalOffset + originalPosition); |
+ this._lastOriginalPosition = originalPosition; |
+ this._mapping.formatted.push(this._formattedOffset + this._formattedContentLength); |
+ this._lastFormattedPosition = this._formattedContentLength; |
+ } |
+} |
+ |
+FormatterWorker.JavaScriptTokens = {}; |
+FormatterWorker.JavaScriptTokensByValue = {}; |
+ |
+FormatterWorker.JavaScriptTokens.EOS = 0; |
+FormatterWorker.JavaScriptTokens.LPAREN = FormatterWorker.JavaScriptTokensByValue["("] = 1; |
+FormatterWorker.JavaScriptTokens.RPAREN = FormatterWorker.JavaScriptTokensByValue[")"] = 2; |
+FormatterWorker.JavaScriptTokens.LBRACK = FormatterWorker.JavaScriptTokensByValue["["] = 3; |
+FormatterWorker.JavaScriptTokens.RBRACK = FormatterWorker.JavaScriptTokensByValue["]"] = 4; |
+FormatterWorker.JavaScriptTokens.LBRACE = FormatterWorker.JavaScriptTokensByValue["{"] = 5; |
+FormatterWorker.JavaScriptTokens.RBRACE = FormatterWorker.JavaScriptTokensByValue["}"] = 6; |
+FormatterWorker.JavaScriptTokens.COLON = FormatterWorker.JavaScriptTokensByValue[":"] = 7; |
+FormatterWorker.JavaScriptTokens.SEMICOLON = FormatterWorker.JavaScriptTokensByValue[";"] = 8; |
+FormatterWorker.JavaScriptTokens.PERIOD = FormatterWorker.JavaScriptTokensByValue["."] = 9; |
+FormatterWorker.JavaScriptTokens.CONDITIONAL = FormatterWorker.JavaScriptTokensByValue["?"] = 10; |
+FormatterWorker.JavaScriptTokens.INC = FormatterWorker.JavaScriptTokensByValue["++"] = 11; |
+FormatterWorker.JavaScriptTokens.DEC = FormatterWorker.JavaScriptTokensByValue["--"] = 12; |
+FormatterWorker.JavaScriptTokens.ASSIGN = FormatterWorker.JavaScriptTokensByValue["="] = 13; |
+FormatterWorker.JavaScriptTokens.ASSIGN_BIT_OR = FormatterWorker.JavaScriptTokensByValue["|="] = 14; |
+FormatterWorker.JavaScriptTokens.ASSIGN_BIT_XOR = FormatterWorker.JavaScriptTokensByValue["^="] = 15; |
+FormatterWorker.JavaScriptTokens.ASSIGN_BIT_AND = FormatterWorker.JavaScriptTokensByValue["&="] = 16; |
+FormatterWorker.JavaScriptTokens.ASSIGN_SHL = FormatterWorker.JavaScriptTokensByValue["<<="] = 17; |
+FormatterWorker.JavaScriptTokens.ASSIGN_SAR = FormatterWorker.JavaScriptTokensByValue[">>="] = 18; |
+FormatterWorker.JavaScriptTokens.ASSIGN_SHR = FormatterWorker.JavaScriptTokensByValue[">>>="] = 19; |
+FormatterWorker.JavaScriptTokens.ASSIGN_ADD = FormatterWorker.JavaScriptTokensByValue["+="] = 20; |
+FormatterWorker.JavaScriptTokens.ASSIGN_SUB = FormatterWorker.JavaScriptTokensByValue["-="] = 21; |
+FormatterWorker.JavaScriptTokens.ASSIGN_MUL = FormatterWorker.JavaScriptTokensByValue["*="] = 22; |
+FormatterWorker.JavaScriptTokens.ASSIGN_DIV = FormatterWorker.JavaScriptTokensByValue["/="] = 23; |
+FormatterWorker.JavaScriptTokens.ASSIGN_MOD = FormatterWorker.JavaScriptTokensByValue["%="] = 24; |
+FormatterWorker.JavaScriptTokens.COMMA = FormatterWorker.JavaScriptTokensByValue[","] = 25; |
+FormatterWorker.JavaScriptTokens.OR = FormatterWorker.JavaScriptTokensByValue["||"] = 26; |
+FormatterWorker.JavaScriptTokens.AND = FormatterWorker.JavaScriptTokensByValue["&&"] = 27; |
+FormatterWorker.JavaScriptTokens.BIT_OR = FormatterWorker.JavaScriptTokensByValue["|"] = 28; |
+FormatterWorker.JavaScriptTokens.BIT_XOR = FormatterWorker.JavaScriptTokensByValue["^"] = 29; |
+FormatterWorker.JavaScriptTokens.BIT_AND = FormatterWorker.JavaScriptTokensByValue["&"] = 30; |
+FormatterWorker.JavaScriptTokens.SHL = FormatterWorker.JavaScriptTokensByValue["<<"] = 31; |
+FormatterWorker.JavaScriptTokens.SAR = FormatterWorker.JavaScriptTokensByValue[">>"] = 32; |
+FormatterWorker.JavaScriptTokens.SHR = FormatterWorker.JavaScriptTokensByValue[">>>"] = 33; |
+FormatterWorker.JavaScriptTokens.ADD = FormatterWorker.JavaScriptTokensByValue["+"] = 34; |
+FormatterWorker.JavaScriptTokens.SUB = FormatterWorker.JavaScriptTokensByValue["-"] = 35; |
+FormatterWorker.JavaScriptTokens.MUL = FormatterWorker.JavaScriptTokensByValue["*"] = 36; |
+FormatterWorker.JavaScriptTokens.DIV = FormatterWorker.JavaScriptTokensByValue["/"] = 37; |
+FormatterWorker.JavaScriptTokens.MOD = FormatterWorker.JavaScriptTokensByValue["%"] = 38; |
+FormatterWorker.JavaScriptTokens.EQ = FormatterWorker.JavaScriptTokensByValue["=="] = 39; |
+FormatterWorker.JavaScriptTokens.NE = FormatterWorker.JavaScriptTokensByValue["!="] = 40; |
+FormatterWorker.JavaScriptTokens.EQ_STRICT = FormatterWorker.JavaScriptTokensByValue["==="] = 41; |
+FormatterWorker.JavaScriptTokens.NE_STRICT = FormatterWorker.JavaScriptTokensByValue["!=="] = 42; |
+FormatterWorker.JavaScriptTokens.LT = FormatterWorker.JavaScriptTokensByValue["<"] = 43; |
+FormatterWorker.JavaScriptTokens.GT = FormatterWorker.JavaScriptTokensByValue[">"] = 44; |
+FormatterWorker.JavaScriptTokens.LTE = FormatterWorker.JavaScriptTokensByValue["<="] = 45; |
+FormatterWorker.JavaScriptTokens.GTE = FormatterWorker.JavaScriptTokensByValue[">="] = 46; |
+FormatterWorker.JavaScriptTokens.INSTANCEOF = FormatterWorker.JavaScriptTokensByValue["instanceof"] = 47; |
+FormatterWorker.JavaScriptTokens.IN = FormatterWorker.JavaScriptTokensByValue["in"] = 48; |
+FormatterWorker.JavaScriptTokens.NOT = FormatterWorker.JavaScriptTokensByValue["!"] = 49; |
+FormatterWorker.JavaScriptTokens.BIT_NOT = FormatterWorker.JavaScriptTokensByValue["~"] = 50; |
+FormatterWorker.JavaScriptTokens.DELETE = FormatterWorker.JavaScriptTokensByValue["delete"] = 51; |
+FormatterWorker.JavaScriptTokens.TYPEOF = FormatterWorker.JavaScriptTokensByValue["typeof"] = 52; |
+FormatterWorker.JavaScriptTokens.VOID = FormatterWorker.JavaScriptTokensByValue["void"] = 53; |
+FormatterWorker.JavaScriptTokens.BREAK = FormatterWorker.JavaScriptTokensByValue["break"] = 54; |
+FormatterWorker.JavaScriptTokens.CASE = FormatterWorker.JavaScriptTokensByValue["case"] = 55; |
+FormatterWorker.JavaScriptTokens.CATCH = FormatterWorker.JavaScriptTokensByValue["catch"] = 56; |
+FormatterWorker.JavaScriptTokens.CONTINUE = FormatterWorker.JavaScriptTokensByValue["continue"] = 57; |
+FormatterWorker.JavaScriptTokens.DEBUGGER = FormatterWorker.JavaScriptTokensByValue["debugger"] = 58; |
+FormatterWorker.JavaScriptTokens.DEFAULT = FormatterWorker.JavaScriptTokensByValue["default"] = 59; |
+FormatterWorker.JavaScriptTokens.DO = FormatterWorker.JavaScriptTokensByValue["do"] = 60; |
+FormatterWorker.JavaScriptTokens.ELSE = FormatterWorker.JavaScriptTokensByValue["else"] = 61; |
+FormatterWorker.JavaScriptTokens.FINALLY = FormatterWorker.JavaScriptTokensByValue["finally"] = 62; |
+FormatterWorker.JavaScriptTokens.FOR = FormatterWorker.JavaScriptTokensByValue["for"] = 63; |
+FormatterWorker.JavaScriptTokens.FUNCTION = FormatterWorker.JavaScriptTokensByValue["function"] = 64; |
+FormatterWorker.JavaScriptTokens.IF = FormatterWorker.JavaScriptTokensByValue["if"] = 65; |
+FormatterWorker.JavaScriptTokens.NEW = FormatterWorker.JavaScriptTokensByValue["new"] = 66; |
+FormatterWorker.JavaScriptTokens.RETURN = FormatterWorker.JavaScriptTokensByValue["return"] = 67; |
+FormatterWorker.JavaScriptTokens.SWITCH = FormatterWorker.JavaScriptTokensByValue["switch"] = 68; |
+FormatterWorker.JavaScriptTokens.THIS = FormatterWorker.JavaScriptTokensByValue["this"] = 69; |
+FormatterWorker.JavaScriptTokens.THROW = FormatterWorker.JavaScriptTokensByValue["throw"] = 70; |
+FormatterWorker.JavaScriptTokens.TRY = FormatterWorker.JavaScriptTokensByValue["try"] = 71; |
+FormatterWorker.JavaScriptTokens.VAR = FormatterWorker.JavaScriptTokensByValue["var"] = 72; |
+FormatterWorker.JavaScriptTokens.WHILE = FormatterWorker.JavaScriptTokensByValue["while"] = 73; |
+FormatterWorker.JavaScriptTokens.WITH = FormatterWorker.JavaScriptTokensByValue["with"] = 74; |
+FormatterWorker.JavaScriptTokens.NULL_LITERAL = FormatterWorker.JavaScriptTokensByValue["null"] = 75; |
+FormatterWorker.JavaScriptTokens.TRUE_LITERAL = FormatterWorker.JavaScriptTokensByValue["true"] = 76; |
+FormatterWorker.JavaScriptTokens.FALSE_LITERAL = FormatterWorker.JavaScriptTokensByValue["false"] = 77; |
+FormatterWorker.JavaScriptTokens.NUMBER = 78; |
+FormatterWorker.JavaScriptTokens.STRING = 79; |
+FormatterWorker.JavaScriptTokens.IDENTIFIER = 80; |
+FormatterWorker.JavaScriptTokens.CONST = FormatterWorker.JavaScriptTokensByValue["const"] = 81; |
+ |
+FormatterWorker.JavaScriptTokensByType = { |
+ "eof": FormatterWorker.JavaScriptTokens.EOS, |
+ "name": FormatterWorker.JavaScriptTokens.IDENTIFIER, |
+ "num": FormatterWorker.JavaScriptTokens.NUMBER, |
+ "regexp": FormatterWorker.JavaScriptTokens.DIV, |
+ "string": FormatterWorker.JavaScriptTokens.STRING |
+}; |
+ |
+/** |
+ * @constructor |
+ * @param {string} content |
+ */ |
+FormatterWorker.JavaScriptTokenizer = function(content) |
+{ |
+ this._readNextToken = parse.tokenizer(content); |
+ this._state = this._readNextToken.context(); |
+} |
+ |
+FormatterWorker.JavaScriptTokenizer.prototype = { |
+ /** |
+ * @return {string} |
+ */ |
+ content: function() |
+ { |
+ return this._state.text; |
+ }, |
+ |
+ /** |
+ * @param {boolean=} forceRegexp |
+ */ |
+ next: function(forceRegexp) |
+ { |
+ var uglifyToken = this._readNextToken(forceRegexp); |
+ uglifyToken.endPos = this._state.pos; |
+ uglifyToken.endLine = this._state.line; |
+ uglifyToken.token = this._convertUglifyToken(uglifyToken); |
+ return uglifyToken; |
+ }, |
+ |
+ _convertUglifyToken: function(uglifyToken) |
+ { |
+ var token = FormatterWorker.JavaScriptTokensByType[uglifyToken.type]; |
+ if (typeof token === "number") |
+ return token; |
+ token = FormatterWorker.JavaScriptTokensByValue[uglifyToken.value]; |
+ if (typeof token === "number") |
+ return token; |
+ throw "Unknown token type " + uglifyToken.type; |
} |
} |