diff --git a/acorn.js b/acorn.js index 0534957b43..0c1ece6c1e 100644 --- a/acorn.js +++ b/acorn.js @@ -599,7 +599,7 @@ var next = input.charCodeAt(tokPos + 1); if (next === code) { if (next == 45 && input.charCodeAt(tokPos + 2) == 62 && lastEnd < tokLineStart) { - // A '-->' line comment + // A `-->` line comment tokPos += 3; skipLineComment(); skipSpace(); @@ -620,7 +620,8 @@ return finishOp(_bin8, size); } if (next == 33 && code == 60 && input.charCodeAt(tokPos + 2) == 45 && - input.charCodeAt(tokPos + 3) == 45) { // '' line comment

        tokPos += 3;
+      if (next == 45 && input.charCodeAt(tokPos + 2) == 62 && lastEnd < tokLineStart) {

A --> line comment

        tokPos += 3;
         skipLineComment();
         skipSpace();
         return readToken();
@@ -416,8 +416,7 @@ into it.

return finishOp(_bin8, size); } if (next == 33 && code == 60 && input.charCodeAt(tokPos + 2) == 45 && - input.charCodeAt(tokPos + 3) == 45) { // '<!--', an XML-style comment that should be interpreted as a line comment - tokPos += 4; + input.charCodeAt(tokPos + 3) == 45) {

<!--, an XML-style comment that should be interpreted as a line comment

      tokPos += 4;
       skipLineComment();
       skipSpace();
       return readToken();
@@ -434,9 +433,9 @@ into it.

} function getTokenFromCode(code) { - switch(code) {

The interpretation of a dot depends on whether it is followed + switch(code) {

The interpretation of a dot depends on whether it is followed by a digit.

    case 46: // '.'
-      return readToken_dot();

Punctuation tokens.

    case 40: ++tokPos; return finishToken(_parenL);
+      return readToken_dot();

Punctuation tokens.

    case 40: ++tokPos; return finishToken(_parenL);
     case 41: ++tokPos; return finishToken(_parenR);
     case 59: ++tokPos; return finishToken(_semi);
     case 44: ++tokPos; return finishToken(_comma);
@@ -445,12 +444,12 @@ by a digit.

case 123: ++tokPos; return finishToken(_braceL); case 125: ++tokPos; return finishToken(_braceR); case 58: ++tokPos; return finishToken(_colon); - case 63: ++tokPos; return finishToken(_question);

'0x' is a hexadecimal number.

    case 48: // '0'
+    case 63: ++tokPos; return finishToken(_question);

'0x' is a hexadecimal number.

    case 48: // '0'
       var next = input.charCodeAt(tokPos + 1);
-      if (next === 120 || next === 88) return readHexNumber();

Anything else beginning with a digit is an integer, octal + if (next === 120 || next === 88) return readHexNumber();

Anything else beginning with a digit is an integer, octal number, or float.

    case 49: case 50: case 51: case 52: case 53: case 54: case 55: case 56: case 57: // 1-9
-      return readNumber(false);

Quotes produce strings.

    case 34: case 39: // '"', "'"
-      return readString(code);

Operators are parsed inline in tiny state machines. '=' (61) is + return readNumber(false);

Quotes produce strings.

    case 34: case 39: // '"', "'"
+      return readString(code);

Operators are parsed inline in tiny state machines. '=' (61) is often referred to. finishOp simply skips the amount of characters it is given as second argument, and returns a token of the type given by its first argument.

    case 47: // '/'
@@ -488,12 +487,12 @@ of the type given by its first argument.

if (forceRegexp) return readRegexp(); if (tokPos >= inputLen) return finishToken(_eof); - var code = input.charCodeAt(tokPos);

Identifier or keyword. '\uXXXX' sequences are allowed in + var code = input.charCodeAt(tokPos);

Identifier or keyword. '\uXXXX' sequences are allowed in identifiers, so '\' also dispatches to that.

    if (isIdentifierStart(code) || code === 92 /* '\' */) return readWord();
 
     var tok = getTokenFromCode(code);
 
-    if (tok === false) {

If we are here, we either found a non-ASCII identifier + if (tok === false) {

If we are here, we either found a non-ASCII identifier character, or something that's entirely disallowed.

      var ch = String.fromCharCode(code);
       if (ch === "\\" || nonASCIIidentifierStart.test(ch)) return readWord();
       raise(tokPos, "Unexpected character '" + ch + "'");
@@ -505,7 +504,7 @@ character, or something that's entirely disallowed.

var str = input.slice(tokPos, tokPos + size); tokPos += size; finishToken(type, str); - }

Parse a regular expression. Some context-awareness is necessary, + }

Parse a regular expression. Some context-awareness is necessary, since a '/' inside a '[]' set does not end the expression.

  function readRegexp() {
     var content = "", escaped, inClass, start = tokPos;
     for (;;) {
@@ -521,11 +520,11 @@ since a '/' inside a '[]' set does not end the expression.

++tokPos; } var content = input.slice(start, tokPos); - ++tokPos;

Need to use readWord1 because '\uXXXX' sequences are allowed + ++tokPos;

Need to use readWord1 because '\uXXXX' sequences are allowed here (don't ask).

    var mods = readWord1();
     if (mods && !/^[gmsiy]*$/.test(mods)) raise(start, "Invalid regexp flag");
     return finishToken(_regexp, new RegExp(content, mods));
-  }

Read an integer in the given radix. Return null if zero digits + }

Read an integer in the given radix. Return null if zero digits were read, the integer value otherwise. When len is given, this will return null unless the integer has exactly len digits.

  function readInt(radix, len) {
     var start = tokPos, total = 0;
@@ -550,7 +549,7 @@ will return null unless the integer has exactly len di
     if (val == null) raise(tokStart + 2, "Expected hexadecimal number");
     if (isIdentifierStart(input.charCodeAt(tokPos))) raise(tokPos, "Identifier directly after number");
     return finishToken(_num, val);
-  }

Read an integer, octal integer, or floating-point number.

  function readNumber(startsWithDot) {
+  }

Read an integer, octal integer, or floating-point number.

  function readNumber(startsWithDot) {
     var start = tokPos, isFloat = false, octal = input.charCodeAt(tokPos) === 48;
     if (!startsWithDot && readInt(10) === null) raise(start, "Invalid number");
     if (input.charCodeAt(tokPos) === 46) {
@@ -573,7 +572,7 @@ will return null unless the integer has exactly len di
     else if (/[89]/.test(str) || strict) raise(start, "Invalid number");
     else val = parseInt(str, 8);
     return finishToken(_num, val);
-  }

Read a string value, interpreting backslash-escapes.

  function readString(quote) {
+  }

Read a string value, interpreting backslash-escapes.

  function readString(quote) {
     tokPos++;
     var out = "";
     for (;;) {
@@ -619,13 +618,13 @@ will return null unless the integer has exactly len di
         ++tokPos;
       }
     }
-  }

Used to read character escape sequences ('\x', '\u', '\U').

  function readHexChar(len) {
+  }

Used to read character escape sequences ('\x', '\u', '\U').

  function readHexChar(len) {
     var n = readInt(16, len);
     if (n === null) raise(tokStart, "Bad character escape sequence");
     return n;
-  }

Used to signal to callers of readWord1 whether the word + }

Used to signal to callers of readWord1 whether the word contained any escape sequences. This is needed because words with -escape sequences must not be interpreted as keywords.

  var containsEsc;

Read an identifier, and return it as a string. Sets containsEsc +escape sequences must not be interpreted as keywords.

  var containsEsc;

Read an identifier, and return it as a string. Sets containsEsc to whether the word contained a '\u' escape.

Only builds up the word character-by-character when it actually @@ -655,7 +654,7 @@ containeds an escape, as a micro-optimization.

first = false; } return containsEsc ? word : input.slice(start, tokPos); - }

Read an identifier or keyword token. Will check for reserved + }

Read an identifier or keyword token. Will check for reserved words when necessary.

  function readWord() {
     var word = readWord1();
     var type = _name;
@@ -667,7 +666,7 @@ words when necessary.

raise(tokStart, "The keyword '" + word + "' is reserved"); } return finishToken(type, word); - }

Parser

A recursive descent parser operates by defining functions for all + }

Parser

A recursive descent parser operates by defining functions for all syntactic elements, and recursively calling those, each function advancing the input stream and returning an AST node. Precedence of constructs (for example, the fact that !x[1] means !(x[1]) @@ -681,12 +680,12 @@ way, it'll receive the node for x[1] already parsed, and wraps operator precedence, because it is much more compact than using the technique outlined above, which uses different, nesting functions to specify precedence, for all of the ten binary -precedence levels that JavaScript defines.

Parser utilities

Continue to the next token.

  function next() {
+precedence levels that JavaScript defines.

Parser utilities

Continue to the next token.

  function next() {
     lastStart = tokStart;
     lastEnd = tokEnd;
     lastEndLoc = tokEndLoc;
     readToken();
-  }

Enter strict mode. Re-reads the next token to please pedantic + }

Enter strict mode. Re-reads the next token to please pedantic tests ("use strict"; 010; -- should fail).

  function setStrict(strct) {
     strict = strct;
     tokPos = lastEnd;
@@ -696,7 +695,7 @@ tests ("use strict"; 010; -- should fail).

} skipSpace(); readToken(); - }

Start an AST node, attaching a start offset.

  function node_t() {
+  }

Start an AST node, attaching a start offset.

  function node_t() {
     this.type = null;
     this.start = tokStart;
     this.end = null;
@@ -715,7 +714,7 @@ tests ("use strict"; 010; -- should fail).

if (options.ranges) node.range = [tokStart, 0]; return node; - }

Start a node whose start offset information should be based on + }

Start a node whose start offset information should be based on the start of another node. For example, a binary operator node is only started after its left-hand side has already been parsed.

  function startNodeFrom(other) {
     var node = new node_t();
@@ -728,7 +727,7 @@ only started after its left-hand side has already been parsed.

< node.range = [other.range[0], 0]; return node; - }

Finish an AST node, adding type and end properties.

  function finishNode(node, type) {
+  }

Finish an AST node, adding type and end properties.

  function finishNode(node, type) {
     node.type = type;
     node.end = lastEnd;
     if (options.locations)
@@ -736,34 +735,34 @@ only started after its left-hand side has already been parsed.

< if (options.ranges) node.range[1] = lastEnd; return node; - }

Test whether a statement node is the string literal "use strict".

  function isUseStrict(stmt) {
+  }

Test whether a statement node is the string literal "use strict".

  function isUseStrict(stmt) {
     return options.ecmaVersion >= 5 && stmt.type === "ExpressionStatement" &&
       stmt.expression.type === "Literal" && stmt.expression.value === "use strict";
-  }

Predicate that tests whether the next token is of the given + }

Predicate that tests whether the next token is of the given type, and if yes, consumes it as a side effect.

  function eat(type) {
     if (tokType === type) {
       next();
       return true;
     }
-  }

Test whether a semicolon can be inserted at the current position.

  function canInsertSemicolon() {
+  }

Test whether a semicolon can be inserted at the current position.

  function canInsertSemicolon() {
     return !options.strictSemicolons &&
       (tokType === _eof || tokType === _braceR || newline.test(input.slice(lastEnd, tokStart)));
-  }

Consume a semicolon, or, failing that, see if we are allowed to + }

Consume a semicolon, or, failing that, see if we are allowed to pretend that there is a semicolon at this position.

  function semicolon() {
     if (!eat(_semi) && !canInsertSemicolon()) unexpected();
-  }

Expect a token of a given type. If found, consume it, otherwise, + }

Expect a token of a given type. If found, consume it, otherwise, raise an unexpected token error.

  function expect(type) {
     if (tokType === type) next();
     else unexpected();
-  }

Raise an unexpected token error.

  function unexpected() {
+  }

Raise an unexpected token error.

  function unexpected() {
     raise(tokStart, "Unexpected token");
-  }

Verify that a node is an lval — something that can be assigned + }

Verify that a node is an lval — something that can be assigned to.

  function checkLVal(expr) {
     if (expr.type !== "Identifier" && expr.type !== "MemberExpression")
       raise(expr.start, "Assigning to rvalue");
     if (strict && expr.type === "Identifier" && isStrictBadIdWord(expr.name))
       raise(expr.start, "Assigning to " + expr.name + " in strict mode");
-  }

Statement parsing

Parse a program. Initializes the parser, reads any number of + }

Statement parsing

Parse a program. Initializes the parser, reads any number of statements, and wraps them in a Program node. Optionally takes a program argument. If present, the statements will be appended to its body instead of creating a new node.

  function parseTopLevel(program) {
@@ -784,7 +783,7 @@ to its body instead of creating a new node.

return finishNode(node, "Program"); } - var loopLabel = {kind: "loop"}, switchLabel = {kind: "switch"};

Parse a single statement.

+ var loopLabel = {kind: "loop"}, switchLabel = {kind: "switch"};

Parse a single statement.

If expecting a statement and finding a slash operator, parse a regular expression literal. This is to handle cases like @@ -793,7 +792,7 @@ does not help.

if (tokType === _slash || tokType === _assign && tokVal == "/=") readToken(true); - var starttype = tokType, node = startNode();

Most types of statements are recognized by the keyword they + var starttype = tokType, node = startNode();

Most types of statements are recognized by the keyword they start with. Many are trivial to parse, some require a bit of complexity.

    switch (starttype) {
     case _break: case _continue:
@@ -804,7 +803,7 @@ complexity.

else { node.label = parseIdent(); semicolon(); - }

Verify that there is an actual destination to break or + }

Verify that there is an actual destination to break or continue to.

      for (var i = 0; i < labels.length; ++i) {
         var lab = labels[i];
         if (node.label == null || lab.name === node.label.name) {
@@ -828,7 +827,7 @@ continue to.

< expect(_while); node.test = parseParenExpression(); semicolon(); - return finishNode(node, "DoWhileStatement");

Disambiguating between a for and a for/in loop is + return finishNode(node, "DoWhileStatement");

Disambiguating between a for and a for/in loop is non-trivial. Basically, we have to parse the init var statement or expression, disallowing the in operator (see the second parameter to parseExpression), and then check @@ -864,7 +863,7 @@ a regular for loop.

case _return: if (!inFunction) raise(tokStart, "'return' outside of function"); - next();

In return (and break/continue), the keywords with + next();

In return (and break/continue), the keywords with optional arguments, we eagerly look for a semicolon or the possibility to insert one.

      if (eat(_semi) || canInsertSemicolon()) node.argument = null;
       else { node.argument = parseExpression(); semicolon(); }
@@ -875,7 +874,7 @@ possibility to insert one.

node.discriminant = parseParenExpression(); node.cases = []; expect(_braceL); - labels.push(switchLabel);

Statements under must be grouped (by label) in SwitchCase + labels.push(switchLabel);

Statements under must be grouped (by label) in SwitchCase nodes. cur is used to keep the node that we are currently adding statements to.

      for (var cur, sawDefault; tokType != _braceR;) {
         if (tokType === _case || tokType === _default) {
@@ -956,7 +955,7 @@ adding statements to.

case _semi: next(); - return finishNode(node, "EmptyStatement");

If the statement does not start with a statement keyword or a + return finishNode(node, "EmptyStatement");

If the statement does not start with a statement keyword or a brace, it's an ExpressionStatement or LabeledStatement. We simply start parsing an expression, and afterwards, if the next token is a colon and the expression was a simple @@ -977,13 +976,13 @@ Identifier node, we switch to interpreting it as a label.

return finishNode(node, "ExpressionStatement"); } } - }

Used for constructs like switch and if that insist on + }

Used for constructs like switch and if that insist on parentheses around their expression.

  function parseParenExpression() {
     expect(_parenL);
     var val = parseExpression();
     expect(_parenR);
     return val;
-  }

Parse a semicolon-enclosed block of statements, handling "use + }

Parse a semicolon-enclosed block of statements, handling "use strict" declarations when allowStrict is true (used for function bodies).

  function parseBlock(allowStrict) {
     var node = startNode(), first = true, strict = false, oldStrict;
@@ -1000,7 +999,7 @@ function bodies).

} if (strict && !oldStrict) setStrict(false); return finishNode(node, "BlockStatement"); - }

Parse a regular for loop. The disambiguation code in + }

Parse a regular for loop. The disambiguation code in parseStatement will already have parsed the init statement or expression.

  function parseFor(node, init) {
     node.init = init;
@@ -1012,14 +1011,14 @@ expression.

node.body = parseStatement(); labels.pop(); return finishNode(node, "ForStatement"); - }

Parse a for/in loop.

  function parseForIn(node, init) {
+  }

Parse a for/in loop.

  function parseForIn(node, init) {
     node.left = init;
     node.right = parseExpression();
     expect(_parenR);
     node.body = parseStatement();
     labels.pop();
     return finishNode(node, "ForInStatement");
-  }

Parse a list of variable declarations.

  function parseVar(node, noIn) {
+  }

Parse a list of variable declarations.

  function parseVar(node, noIn) {
     node.declarations = [];
     node.kind = "var";
     for (;;) {
@@ -1032,11 +1031,11 @@ expression.

if (!eat(_comma)) break; } return finishNode(node, "VariableDeclaration"); - }

Expression parsing

These nest, from the most general expression type at the top to + }

Expression parsing

These nest, from the most general expression type at the top to 'atomic', nondivisible expression types at the bottom. Most of the functions will simply let the function(s) below them parse, and, if the syntactic construct they handle is present, wrap -the AST node that the inner parser gave them in another node.

Parse a full expression. The arguments are used to forbid comma +the AST node that the inner parser gave them in another node.

Parse a full expression. The arguments are used to forbid comma sequences (in argument lists, array literals, or object literals) or the in operator (in for loops initalization expressions).

  function parseExpression(noComma, noIn) {
     var expr = parseMaybeAssign(noIn);
@@ -1047,7 +1046,7 @@ or the in operator (in for loops initalization expressions).

return finishNode(node, "SequenceExpression"); } return expr; - }

Parse an assignment expression. This includes applications of + }

Parse an assignment expression. This includes applications of operators like +=.

  function parseMaybeAssign(noIn) {
     var left = parseMaybeConditional(noIn);
     if (tokType.isAssign) {
@@ -1060,7 +1059,7 @@ operators like +=.

return finishNode(node, "AssignmentExpression"); } return left; - }

Parse a ternary conditional (?:) operator.

  function parseMaybeConditional(noIn) {
+  }

Parse a ternary conditional (?:) operator.

  function parseMaybeConditional(noIn) {
     var expr = parseExprOps(noIn);
     if (eat(_question)) {
       var node = startNodeFrom(expr);
@@ -1071,9 +1070,9 @@ operators like +=.

return finishNode(node, "ConditionalExpression"); } return expr; - }

Start the precedence parser.

  function parseExprOps(noIn) {
+  }

Start the precedence parser.

  function parseExprOps(noIn) {
     return parseExprOp(parseMaybeUnary(), -1, noIn);
-  }

Parse binary operators with the operator precedence parsing + }

Parse binary operators with the operator precedence parsing algorithm. left is the left-hand side of the operator. minPrec provides context that allows the function to stop and defer further parser to one of its callers when it encounters an @@ -1091,7 +1090,7 @@ operator that has a lower precedence than the set it is parsing.

} } return left; - }

Parse unary operators, both prefix and postfix.

  function parseMaybeUnary() {
+  }

Parse unary operators, both prefix and postfix.

  function parseMaybeUnary() {
     if (tokType.prefix) {
       var node = startNode(), update = tokType.isUpdate;
       node.operator = tokVal;
@@ -1116,7 +1115,7 @@ operator that has a lower precedence than the set it is parsing.

expr = finishNode(node, "UpdateExpression"); } return expr; - }

Parse call, dot, and []-subscript expressions.

  function parseExprSubscripts() {
+  }

Parse call, dot, and []-subscript expressions.

  function parseExprSubscripts() {
     return parseSubscripts(parseExprAtom());
   }
 
@@ -1140,7 +1139,7 @@ operator that has a lower precedence than the set it is parsing.

node.arguments = parseExprList(_parenR, false); return parseSubscripts(finishNode(node, "CallExpression"), noCalls); } else return base; - }

Parse an atomic expression — either a single token that is an + }

Parse an atomic expression — either a single token that is an expression, an expression started by a keyword like function or new, or an expression wrapped in punctuation like (), [], or {}.

  function parseExprAtom() {
@@ -1200,7 +1199,7 @@ or {}.

default: unexpected(); } - }

New's precedence is slightly tricky. It must allow its argument + }

New's precedence is slightly tricky. It must allow its argument to be a [] or dot subscript expression, but not a call — at least, not without wrapping it in parentheses. Thus, it uses the

  function parseNew() {
     var node = startNode();
@@ -1209,7 +1208,7 @@ least, not without wrapping it in parentheses. Thus, it uses the

if (eat(_parenL)) node.arguments = parseExprList(_parenR, false); else node.arguments = empty; return finishNode(node, "NewExpression"); - }

Parse an object literal.

  function parseObj() {
+  }

Parse an object literal.

  function parseObj() {
     var node = startNode(), first = true, sawGetSet = false;
     node.properties = [];
     next();
@@ -1230,7 +1229,7 @@ least, not without wrapping it in parentheses. Thus, it uses the

prop.key = parsePropertyName(); if (tokType !== _parenL) unexpected(); prop.value = parseFunction(startNode(), false); - } else unexpected();

getters and setters are not allowed to clash — either with + } else unexpected();

getters and setters are not allowed to clash — either with each other or with an init property — and in strict mode, init properties are also not allowed to be repeated.

      if (prop.key.type === "Identifier" && (strict || sawGetSet)) {
         for (var i = 0; i < node.properties.length; ++i) {
@@ -1251,7 +1250,7 @@ init properties are also not allowed to be repeated.

function parsePropertyName() { if (tokType === _num || tokType === _string) return parseExprAtom(); return parseIdent(true); - }

Parse a function declaration or literal (depending on the + }

Parse a function declaration or literal (depending on the isStatement parameter).

  function parseFunction(node, isStatement) {
     if (tokType === _name) node.id = parseIdent();
     else if (isStatement) unexpected();
@@ -1262,11 +1261,11 @@ init properties are also not allowed to be repeated.

while (!eat(_parenR)) { if (!first) expect(_comma); else first = false; node.params.push(parseIdent()); - }

Start a new scope with regard to labels and the inFunction + }

Start a new scope with regard to labels and the inFunction flag (restore them to their old value afterwards).

    var oldInFunc = inFunction, oldLabels = labels;
     inFunction = true; labels = [];
     node.body = parseBlock(true);
-    inFunction = oldInFunc; labels = oldLabels;

If this is a strict mode function, verify that argument names + inFunction = oldInFunc; labels = oldLabels;

If this is a strict mode function, verify that argument names are not repeated, and it does not try to bind the words eval or arguments.

    if (strict || node.body.body.length && isUseStrict(node.body.body[0])) {
       for (var i = node.id ? -1 : 0; i < node.params.length; ++i) {
@@ -1279,7 +1278,7 @@ or arguments.

} return finishNode(node, isStatement ? "FunctionDeclaration" : "FunctionExpression"); - }

Parses a comma-separated list of expressions, and returns them as + }

Parses a comma-separated list of expressions, and returns them as an array. close is the token type that ends the list, and allowEmpty can be turned on to allow subsequent commas with nothing in between them to be parsed as null (which is needed @@ -1295,7 +1294,7 @@ for array literals).

else elts.push(parseExpression(true)); } return elts; - }

Parse the next token as an identifier. If liberal is true (used + }

Parse the next token as an identifier. If liberal is true (used when parsing properties), it will also convert keywords into identifiers.

  function parseIdent(liberal) {
     var node = startNode();