From 372e1d42f398ceaffe27151348af67084f121d27 Mon Sep 17 00:00:00 2001 From: Ingvar Stepanyan Date: Mon, 4 Aug 2014 13:47:28 +0300 Subject: [PATCH] Split ES6 Template logic from main functionality. --- acorn.js | 157 ++++++++++++++++------------ index.html | 301 ++++++++++++++++++++++++++++------------------------- 2 files changed, 254 insertions(+), 204 deletions(-) diff --git a/acorn.js b/acorn.js index 5c09efb41e..3d5b6450f6 100644 --- a/acorn.js +++ b/acorn.js @@ -535,11 +535,11 @@ // `tokRegexpAllowed`, and skips the space after the token, so that // the next one's `tokStart` will point at the right position. - function finishToken(type, val) { + function finishToken(type, val, shouldSkipSpace) { tokEnd = tokPos; if (options.locations) tokEndLoc = new Position; tokType = type; - if (type !== _bquote || inTemplate) skipSpace(); + if (shouldSkipSpace !== false) skipSpace(); tokVal = val; tokRegexpAllowed = type.beforeExpr; if (options.onToken) { @@ -720,24 +720,32 @@ return finishOp(code === 61 ? _eq : _prefix, 1); } - function getTokenFromCode(code) { - // Special rules work inside ES6 template strings. - if (inTemplate) { - // '`' and '${' have special meanings, but they should follow string (can be empty) - if (tokType === _string) { - if (code === 96) { // '`' - ++tokPos; - return finishToken(_bquote); - } - if (code === 36 && input.charCodeAt(tokPos + 1) === 123) { // '${' - tokPos += 2; - return finishToken(_dollarBraceL); - } + // Get token inside ES6 template (special rules work there). + + function getTemplateToken(code) { + // '`' and '${' have special meanings, but they should follow + // string (can be empty) + if (tokType === _string) { + if (code === 96) { // '`' + ++tokPos; + return finishToken(_bquote); + } else + if (code === 36 && input.charCodeAt(tokPos + 1) === 123) { // '${' + tokPos += 2; + return finishToken(_dollarBraceL); } - // anything else is considered string literal - return readString(); } + if (code === 125) { // '}' + ++tokPos; + return finishToken(_braceR, undefined, false); + } + + // anything else is considered string literal + return readTmplString(); + } + + function getTokenFromCode(code) { switch (code) { // The interpretation of a dot depends on whether it is followed // by a digit or another two dots. @@ -759,7 +767,7 @@ case 96: // '`' if (options.ecmaVersion >= 6) { ++tokPos; - return finishToken(_bquote); + return finishToken(_bquote, undefined, false); } case 48: // '0' @@ -819,9 +827,12 @@ if (tokPos >= inputLen) return finishToken(_eof); var code = input.charCodeAt(tokPos); + + if (inTemplate) return getTemplateToken(code); + // Identifier or keyword. '\uXXXX' sequences are allowed in // identifiers, so '\' also dispatches to that. - if (!inTemplate && (isIdentifierStart(code) || code === 92 /* '\' */)) return readWord(); + if (isIdentifierStart(code) || code === 92 /* '\' */) return readWord(); var tok = getTokenFromCode(code); @@ -954,62 +965,46 @@ } function readString(quote) { - if (!inTemplate) tokPos++; + ++tokPos; var out = ""; for (;;) { if (tokPos >= inputLen) raise(tokStart, "Unterminated string constant"); var ch = input.charCodeAt(tokPos); - if (inTemplate) { - if (ch === 96 || ch === 36 && input.charCodeAt(tokPos + 1) === 123) // '`', '${' - return finishToken(_string, out); - } else if (ch === quote) { + if (ch === quote) { ++tokPos; return finishToken(_string, out); } if (ch === 92) { // '\' - ch = input.charCodeAt(++tokPos); - var octal = /^[0-7]+/.exec(input.slice(tokPos, tokPos + 3)); - if (octal) octal = octal[0]; - while (octal && parseInt(octal, 8) > 255) octal = octal.slice(0, -1); - if (octal === "0") octal = null; - ++tokPos; - if (octal) { - if (strict) raise(tokPos - 2, "Octal literal in strict mode"); - out += String.fromCharCode(parseInt(octal, 8)); - tokPos += octal.length - 1; - } else { - switch (ch) { - case 110: out += "\n"; break; // 'n' -> '\n' - case 114: out += "\r"; break; // 'r' -> '\r' - case 120: out += String.fromCharCode(readHexChar(2)); break; // 'x' - case 117: out += readCodePoint(); break; // 'u' - case 85: out += String.fromCharCode(readHexChar(8)); break; // 'U' - case 116: out += "\t"; break; // 't' -> '\t' - case 98: out += "\b"; break; // 'b' -> '\b' - case 118: out += "\u000b"; break; // 'v' -> '\u000b' - case 102: out += "\f"; break; // 'f' -> '\f' - case 48: out += "\0"; break; // 0 -> '\0' - case 13: if (input.charCodeAt(tokPos) === 10) ++tokPos; // '\r\n' - case 10: // ' \n' - if (options.locations) { tokLineStart = tokPos; ++tokCurLine; } - break; - default: out += String.fromCharCode(ch); break; - } - } + out += readEscapedChar(); } else { ++tokPos; if (newline.test(String.fromCharCode(ch))) { - if (inTemplate) { - if (ch === 13 && input.charCodeAt(tokPos) === 10) { - ++tokPos; - ch = 10; - } - if (options.locations) { - ++tokCurLine; - tokLineStart = tokPos; - } - } else { - raise(tokStart, "Unterminated string constant"); + raise(tokStart, "Unterminated string constant"); + } + out += String.fromCharCode(ch); // '\' + } + } + } + + function readTmplString() { + var out = ""; + for (;;) { + if (tokPos >= inputLen) raise(tokStart, "Unterminated string constant"); + var ch = input.charCodeAt(tokPos); + if (ch === 96 || ch === 36 && input.charCodeAt(tokPos + 1) === 123) // '`', '${' + return finishToken(_string, out); + if (ch === 92) { // '\' + out += readEscapedChar(); + } else { + ++tokPos; + if (newline.test(String.fromCharCode(ch))) { + if (ch === 13 && input.charCodeAt(tokPos) === 10) { + ++tokPos; + ch = 10; + } + if (options.locations) { + ++tokCurLine; + tokLineStart = tokPos; } } out += String.fromCharCode(ch); // '\' @@ -1017,6 +1012,40 @@ } } + // Used to read escaped characters + + function readEscapedChar() { + var ch = input.charCodeAt(++tokPos); + var octal = /^[0-7]+/.exec(input.slice(tokPos, tokPos + 3)); + if (octal) octal = octal[0]; + while (octal && parseInt(octal, 8) > 255) octal = octal.slice(0, -1); + if (octal === "0") octal = null; + ++tokPos; + if (octal) { + if (strict) raise(tokPos - 2, "Octal literal in strict mode"); + tokPos += octal.length - 1; + return String.fromCharCode(parseInt(octal, 8)); + } else { + switch (ch) { + case 110: return "\n"; // 'n' -> '\n' + case 114: return "\r"; // 'r' -> '\r' + case 120: return String.fromCharCode(readHexChar(2)); // 'x' + case 117: return readCodePoint(); // 'u' + case 85: return String.fromCharCode(readHexChar(8)); // 'U' + case 116: return "\t"; // 't' -> '\t' + case 98: return "\b"; // 'b' -> '\b' + case 118: return "\u000b"; // 'v' -> '\u000b' + case 102: return "\f"; // 'f' -> '\f' + case 48: return "\0"; // 0 -> '\0' + case 13: if (input.charCodeAt(tokPos) === 10) ++tokPos; // '\r\n' + case 10: // ' \n' + if (options.locations) { tokLineStart = tokPos; ++tokCurLine; } + return ""; + default: return String.fromCharCode(ch); + } + } + } + // Used to read character escape sequences ('\x', '\u', '\U'). function readHexChar(len) { diff --git a/index.html b/index.html index df9cba43ff..797684e1ab 100644 --- a/index.html +++ b/index.html @@ -326,11 +326,11 @@ line break). Used to count lines.

skipSpace(); }

Called at the end of every token. Sets tokEnd, tokVal, and tokRegexpAllowed, and skips the space after the token, so that -the next one's tokStart will point at the right position.

  function finishToken(type, val) {
+the next one's tokStart will point at the right position.

  function finishToken(type, val, shouldSkipSpace) {
     tokEnd = tokPos;
     if (options.locations) tokEndLoc = new Position;
     tokType = type;
-    if (type !== _bquote || inTemplate) skipSpace();
+    if (shouldSkipSpace !== false) skipSpace();
     tokVal = val;
     tokRegexpAllowed = type.beforeExpr;
     if (options.onToken) {
@@ -495,20 +495,25 @@ into it.

return finishToken(_arrow); } return finishOp(code === 61 ? _eq : _prefix, 1); - } - - function getTokenFromCode(code) {

Special rules work inside ES6 template strings.

    if (inTemplate) {

'`' and '${' have special meanings, but they should follow string (can be empty)

      if (tokType === _string) {
-        if (code === 96) { // '`'
-          ++tokPos;
-          return finishToken(_bquote);
-        }
-        if (code === 36 && input.charCodeAt(tokPos + 1) === 123) { // '${'
-          tokPos += 2;
-          return finishToken(_dollarBraceL);
-        }
-      }

anything else is considered string literal

      return readString();
+  }

Get token inside ES6 template (special rules work there).

  function getTemplateToken(code) {

'`' and '${' have special meanings, but they should follow +string (can be empty)

    if (tokType === _string) {
+      if (code === 96) { // '`'
+        ++tokPos;
+        return finishToken(_bquote);
+      } else
+      if (code === 36 && input.charCodeAt(tokPos + 1) === 123) { // '${'
+        tokPos += 2;
+        return finishToken(_dollarBraceL);
+      }
     }
 
+    if (code === 125) { // '}'
+      ++tokPos;
+      return finishToken(_braceR, undefined, false);
+    }

anything else is considered string literal

    return readTmplString();
+  }
+
+  function getTokenFromCode(code) {
     switch (code) {

The interpretation of a dot depends on whether it is followed by a digit or another two dots.

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

Punctuation tokens.

    case 40: ++tokPos; return finishToken(_parenL);
@@ -525,7 +530,7 @@ by a digit or another two dots.

case 96: // '`' if (options.ecmaVersion >= 6) { ++tokPos; - return finishToken(_bquote); + return finishToken(_bquote, undefined, false); } case 48: // '0' @@ -575,8 +580,10 @@ 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 -identifiers, so '\' also dispatches to that.

    if (!inTemplate && (isIdentifierStart(code) || code === 92 /* '\' */)) return readWord();
+    var code = input.charCodeAt(tokPos);
+
+    if (inTemplate) return getTemplateToken(code);

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);
 
@@ -686,74 +693,88 @@ will return null unless the integer has exactly len di
   }
 
   function readString(quote) {
-    if (!inTemplate) tokPos++;
+    ++tokPos;
     var out = "";
     for (;;) {
       if (tokPos >= inputLen) raise(tokStart, "Unterminated string constant");
       var ch = input.charCodeAt(tokPos);
-      if (inTemplate) {
-        if (ch === 96 || ch === 36 && input.charCodeAt(tokPos + 1) === 123) // '`', '${'
-          return finishToken(_string, out);
-      } else if (ch === quote) {
+      if (ch === quote) {
         ++tokPos;
         return finishToken(_string, out);
       }
       if (ch === 92) { // '\'
-        ch = input.charCodeAt(++tokPos);
-        var octal = /^[0-7]+/.exec(input.slice(tokPos, tokPos + 3));
-        if (octal) octal = octal[0];
-        while (octal && parseInt(octal, 8) > 255) octal = octal.slice(0, -1);
-        if (octal === "0") octal = null;
-        ++tokPos;
-        if (octal) {
-          if (strict) raise(tokPos - 2, "Octal literal in strict mode");
-          out += String.fromCharCode(parseInt(octal, 8));
-          tokPos += octal.length - 1;
-        } else {
-          switch (ch) {
-          case 110: out += "\n"; break; // 'n' -> '\n'
-          case 114: out += "\r"; break; // 'r' -> '\r'
-          case 120: out += String.fromCharCode(readHexChar(2)); break; // 'x'
-          case 117: out += readCodePoint(); break; // 'u'
-          case 85: out += String.fromCharCode(readHexChar(8)); break; // 'U'
-          case 116: out += "\t"; break; // 't' -> '\t'
-          case 98: out += "\b"; break; // 'b' -> '\b'
-          case 118: out += "\u000b"; break; // 'v' -> '\u000b'
-          case 102: out += "\f"; break; // 'f' -> '\f'
-          case 48: out += "\0"; break; // 0 -> '\0'
-          case 13: if (input.charCodeAt(tokPos) === 10) ++tokPos; // '\r\n'
-          case 10: // ' \n'
-            if (options.locations) { tokLineStart = tokPos; ++tokCurLine; }
-            break;
-          default: out += String.fromCharCode(ch); break;
-          }
-        }
+        out += readEscapedChar();
       } else {
         ++tokPos;
         if (newline.test(String.fromCharCode(ch))) {
-          if (inTemplate) {
-            if (ch === 13 && input.charCodeAt(tokPos) === 10) {
-              ++tokPos;
-              ch = 10;
-            }
-            if (options.locations) {
-              ++tokCurLine;
-              tokLineStart = tokPos;
-            }
-          } else {
-            raise(tokStart, "Unterminated string constant");
+          raise(tokStart, "Unterminated string constant");
+        }
+        out += String.fromCharCode(ch); // '\'
+      }
+    }
+  }
+
+  function readTmplString() {
+    var out = "";
+    for (;;) {
+      if (tokPos >= inputLen) raise(tokStart, "Unterminated string constant");
+      var ch = input.charCodeAt(tokPos);
+      if (ch === 96 || ch === 36 && input.charCodeAt(tokPos + 1) === 123) // '`', '${'
+        return finishToken(_string, out);
+      if (ch === 92) { // '\'
+        out += readEscapedChar();
+      } else {
+        ++tokPos;
+        if (newline.test(String.fromCharCode(ch))) {
+          if (ch === 13 && input.charCodeAt(tokPos) === 10) {
+            ++tokPos;
+            ch = 10;
+          }
+          if (options.locations) {
+            ++tokCurLine;
+            tokLineStart = tokPos;
           }
         }
         out += String.fromCharCode(ch); // '\'
       }
     }
-  }

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

  function readHexChar(len) {
+  }

Used to read escaped characters

  function readEscapedChar() {
+    var ch = input.charCodeAt(++tokPos);
+    var octal = /^[0-7]+/.exec(input.slice(tokPos, tokPos + 3));
+    if (octal) octal = octal[0];
+    while (octal && parseInt(octal, 8) > 255) octal = octal.slice(0, -1);
+    if (octal === "0") octal = null;
+    ++tokPos;
+    if (octal) {
+      if (strict) raise(tokPos - 2, "Octal literal in strict mode");
+      tokPos += octal.length - 1;
+      return String.fromCharCode(parseInt(octal, 8));
+    } else {
+      switch (ch) {
+        case 110: return "\n"; // 'n' -> '\n'
+        case 114: return "\r"; // 'r' -> '\r'
+        case 120: return String.fromCharCode(readHexChar(2)); // 'x'
+        case 117: return readCodePoint(); // 'u'
+        case 85: return String.fromCharCode(readHexChar(8)); // 'U'
+        case 116: return "\t"; // 't' -> '\t'
+        case 98: return "\b"; // 'b' -> '\b'
+        case 118: return "\u000b"; // 'v' -> '\u000b'
+        case 102: return "\f"; // 'f' -> '\f'
+        case 48: return "\0"; // 0 -> '\0'
+        case 13: if (input.charCodeAt(tokPos) === 10) ++tokPos; // '\r\n'
+        case 10: // ' \n'
+          if (options.locations) { tokLineStart = tokPos; ++tokCurLine; }
+          return "";
+        default: return String.fromCharCode(ch);
+      }
+    }
+  }

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 @@ -783,14 +804,14 @@ 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;
     if (!containsEsc && isKeyword(word))
       type = keywordTypes[word];
     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]) @@ -804,12 +825,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 = tokStart;
@@ -821,7 +842,7 @@ tests ("use strict"; 010; -- should fail).

} skipSpace(); readToken(); - }

Start an AST node, attaching a start offset.

  function Node() {
+  }

Start an AST node, attaching a start offset.

  function Node() {
     this.type = null;
     this.start = tokStart;
     this.end = null;
@@ -844,7 +865,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();
@@ -857,7 +878,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)
@@ -865,10 +886,10 @@ 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();
@@ -876,20 +897,20 @@ type, and if yes, consumes it as a side effect.

} else { return false; } - }

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) {
     eat(type) || unexpected();
-  }

Raise an unexpected token error.

  function unexpected(pos) {
+  }

Raise an unexpected token error.

  function unexpected(pos) {
     raise(pos != null ? pos : tokStart, "Unexpected token");
-  }

Checks if hash object has a property.

  function has(obj, propName) {
+  }

Checks if hash object has a property.

  function has(obj, propName) {
     return Object.prototype.hasOwnProperty.call(obj, propName);
-  }

Convert existing expression atom to assignable pattern + }

Convert existing expression atom to assignable pattern if possible.

  function toAssignable(node, allowSpread, checkType) {
     if (options.ecmaVersion >= 6 && node) {
       switch (node.type) {
@@ -927,10 +948,10 @@ if possible.

< } } return node; - }

Checks if node can be assignable spread argument.

  function checkSpreadAssign(node) {
+  }

Checks if node can be assignable spread argument.

  function checkSpreadAssign(node) {
     if (node.type !== "Identifier" && node.type !== "ArrayPattern")
       unexpected(node.start);
-  }

Verify that argument names are not repeated, and it does not + }

Verify that argument names are not repeated, and it does not try to bind the words eval or arguments.

  function checkFunctionParam(param, nameHash) {
     switch (param.type) {
       case "Identifier":
@@ -951,7 +972,7 @@ try to bind the words eval or arguments.

checkFunctionParam(param.elements[i], nameHash); break; } - }

Check if property name clashes with already added. + }

Check if property name clashes with already added. Object/class 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.

  function checkPropClash(prop, propHash) {
@@ -976,7 +997,7 @@ strict mode, init properties are also not allowed to be repeated.

}; } other[kind] = true; - }

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, isBinding) {
     switch (expr.type) {
       case "Identifier":
@@ -1008,7 +1029,7 @@ to.

default: raise(expr.start, "Assigning to rvalue"); } - }

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) {
@@ -1029,7 +1050,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 @@ -1038,7 +1059,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: return parseBreakContinueStatement(node, starttype.keyword);
@@ -1058,7 +1079,7 @@ complexity.

case _braceL: return parseBlock(); // no point creating a function for this case _semi: return parseEmptyStatement(node); case _export: return parseExport(node); - case _import: return parseImport(node);

If the statement does not start with a statement keyword or a + case _import: return parseImport(node);

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 @@ -1078,7 +1099,7 @@ Identifier node, we switch to interpreting it as a label.

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) {
@@ -1106,7 +1127,7 @@ continue to.

< semicolon(); return finishNode(node, "DoWhileStatement"); } -

Disambiguating between a for and a for/in or for/of +

Disambiguating between a for and a for/in or for/of 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 @@ -1152,7 +1173,7 @@ is a regular for loop.

function parseReturnStatement(node) { if (!inFunction && !options.allowReturnOutsideFunction) 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(); }
@@ -1164,7 +1185,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) {
@@ -1266,13 +1287,13 @@ adding statements to.

node.expression = expr; semicolon(); 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;
@@ -1289,7 +1310,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;
@@ -1301,7 +1322,7 @@ expression.

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

Parse a for/in and for/of loop, which are almost + }

Parse a for/in and for/of loop, which are almost same from parser's perspective.

  function parseForIn(node, init) {
     var type = tokType === _in ? "ForInStatement" : "ForOfStatement";
     next();
@@ -1311,7 +1332,7 @@ same from parser's perspective.

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

Parse a list of variable declarations.

  function parseVar(node, noIn, kind) {
+  }

Parse a list of variable declarations.

  function parseVar(node, noIn, kind) {
     node.declarations = [];
     node.kind = kind;
     for (;;) {
@@ -1323,11 +1344,11 @@ same from parser's perspective.

if (!eat(_comma)) break; } return node; - }

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);
@@ -1338,7 +1359,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) {
@@ -1351,7 +1372,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);
@@ -1362,9 +1383,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 @@ -1383,7 +1404,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;
@@ -1408,7 +1429,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());
   }
 
@@ -1437,7 +1458,7 @@ operator that has a lower precedence than the set it is parsing.

node.quasi = parseTemplate(); return parseSubscripts(finishNode(node, "TaggedTemplateExpression"), noCalls); } 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() {
@@ -1473,7 +1494,7 @@ or {}.

case _parenL: var tokStartLoc1 = tokStartLoc, tokStart1 = tokStart, val, exprList; - next();

check whether this is generator comprehension or regular expression

      if (options.ecmaVersion >= 6 && tokType === _for) {
+      next();

check whether this is generator comprehension or regular expression

      if (options.ecmaVersion >= 6 && tokType === _for) {
         val = parseComprehension(startNode(), true);
       } else {
         var oldParenL = ++metParenL;
@@ -1483,9 +1504,9 @@ or {}.

} else { exprList = []; } - expect(_parenR);

if '=>' follows '(...)', convert contents to arguments

        if (metParenL === oldParenL && eat(_arrow)) {
+        expect(_parenR);

if '=>' follows '(...)', convert contents to arguments

        if (metParenL === oldParenL && eat(_arrow)) {
           val = parseArrowExpression(startNode(), exprList);
-        } else {

forbid '()' before everything but '=>'

          if (!val) unexpected(lastStart);

forbid '...' in sequence expressions

          if (options.ecmaVersion >= 6) {
+        } else {

forbid '()' before everything but '=>'

          if (!val) unexpected(lastStart);

forbid '...' in sequence expressions

          if (options.ecmaVersion >= 6) {
             for (var i = 0; i < exprList.length; i++) {
               if (exprList[i].type === "SpreadElement") unexpected();
             }
@@ -1505,7 +1526,7 @@ or {}.

case _bracketL: var node = startNode(); - next();

check whether this is array comprehension or regular array

      if (options.ecmaVersion >= 6 && tokType === _for) {
+      next();

check whether this is array comprehension or regular array

      if (options.ecmaVersion >= 6 && tokType === _for) {
         return parseComprehension(node, false);
       }
       node.elements = parseExprList(_bracketR, true, true);
@@ -1534,7 +1555,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();
@@ -1543,12 +1564,12 @@ 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 spread element '...expr'

  function parseSpread() {
+  }

Parse spread element '...expr'

  function parseSpread() {
     var node = startNode();
     next();
     node.argument = parseExpression(true);
     return finishNode(node, "SpreadElement");
-  }

Parse template expression.

  function parseTemplate() {
+  }

Parse template expression.

  function parseTemplate() {
     var node = startNode();
     node.expressions = [];
     node.quasis = [];
@@ -1572,7 +1593,7 @@ least, not without wrapping it in parentheses. Thus, it uses the

} inTemplate = false; return finishNode(node, "TemplateLiteral"); - }

Parse an object literal.

  function parseObj() {
+  }

Parse an object literal.

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

} } prop.key = (tokType === _num || tokType === _string) ? parseExprAtom() : parseIdent(true); - }

Initialize empty function node.

  function initFunction(node) {
+  }

Initialize empty function node.

  function initFunction(node) {
     node.id = null;
     node.params = [];
     if (options.ecmaVersion >= 6) {
@@ -1634,7 +1655,7 @@ least, not without wrapping it in parentheses. Thus, it uses the

node.rest = null; node.generator = false; } - }

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

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

  function parseFunction(node, isStatement, allowExpressionBody) {
     initFunction(node);
     if (options.ecmaVersion >= 6) {
@@ -1646,7 +1667,7 @@ least, not without wrapping it in parentheses. Thus, it uses the

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

Parse object or class method.

  function parseMethod(isGenerator) {
+  }

Parse object or class method.

  function parseMethod(isGenerator) {
     var node = startNode();
     initFunction(node);
     parseFunctionParams(node);
@@ -1659,7 +1680,7 @@ least, not without wrapping it in parentheses. Thus, it uses the

} parseFunctionBody(node, allowExpressionBody); return finishNode(node, "FunctionExpression"); - }

Parse arrow function expression with given parameters.

  function parseArrowExpression(node, params) {
+  }

Parse arrow function expression with given parameters.

  function parseArrowExpression(node, params) {
     initFunction(node);
 
     var defaults = node.defaults, hasDefaults = false;
@@ -1687,7 +1708,7 @@ least, not without wrapping it in parentheses. Thus, it uses the

parseFunctionBody(node, true); return finishNode(node, "ArrowFunctionExpression"); - }

Parse function parameters.

  function parseFunctionParams(node) {
+  }

Parse function parameters.

  function parseFunctionParams(node) {
     var defaults = [], hasDefaults = false;
     
     expect(_parenL);
@@ -1714,19 +1735,19 @@ least, not without wrapping it in parentheses. Thus, it uses the

} if (hasDefaults) node.defaults = defaults; - }

Parse function body and check parameters.

  function parseFunctionBody(node, allowExpression) {
+  }

Parse function body and check parameters.

  function parseFunctionBody(node, allowExpression) {
     var isExpression = allowExpression && tokType !== _braceL;
     
     if (isExpression) {
       node.body = parseExpression(true);
       node.expression = true;
-    } else {

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

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

      var oldInFunc = inFunction, oldInGen = inGenerator, oldLabels = labels;
       inFunction = true; inGenerator = node.generator; labels = [];
       node.body = parseBlock(true);
       node.expression = false;
       inFunction = oldInFunc; inGenerator = oldInGen; labels = oldLabels;
-    }

If this is a strict mode function, verify that argument names + }

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 || !isExpression && node.body.body.length && isUseStrict(node.body.body[0])) {
       var nameHash = {};
@@ -1737,7 +1758,7 @@ or arguments.

if (node.rest) checkFunctionParam(node.rest, nameHash); } - }

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

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

  
   function parseClass(node, isStatement) {
     next();
@@ -1771,7 +1792,7 @@ or arguments.

} node.body = finishNode(classBody, "ClassBody"); return finishNode(node, isStatement ? "ClassDeclaration" : "ClassExpression"); - }

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 @@ -1787,7 +1808,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();
@@ -1808,19 +1829,19 @@ identifiers.

< tokRegexpAllowed = false; next(); return finishNode(node, "Identifier"); - }

Parses module export declaration.

  function parseExport(node) {
-    next();

export var|const|let|function|class ...;

    if (tokType === _var || tokType === _const || tokType === _let || tokType === _function || tokType === _class) {
+  }

Parses module export declaration.

  function parseExport(node) {
+    next();

export var|const|let|function|class ...;

    if (tokType === _var || tokType === _const || tokType === _let || tokType === _function || tokType === _class) {
       node.declaration = parseStatement();
       node['default'] = false;
       node.specifiers = null;
       node.source = null;
-    } else

export default ...;

    if (eat(_default)) {
+    } else

export default ...;

    if (eat(_default)) {
       node.declaration = parseExpression(true);
       node['default'] = true;
       node.specifiers = null;
       node.source = null;
       semicolon();
-    } else {

export * from '...' + } else {

export * from '...' export { x, y as z } [from '...']

      var isBatch = tokType === _star;
       node.declaration = null;
       node['default'] = false;
@@ -1834,12 +1855,12 @@ export { x, y as z } [from '...']

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

Parses a comma-separated list of module exports.

  function parseExportSpecifiers() {
+  }

Parses a comma-separated list of module exports.

  function parseExportSpecifiers() {
     var nodes = [], first = true;
-    if (tokType === _star) {

export * from '...'

      var node = startNode();
+    if (tokType === _star) {

export * from '...'

      var node = startNode();
       next();
       nodes.push(finishNode(node, "ExportBatchSpecifier"));
-    } else {

export { x, y as z } [from '...']

      expect(_braceL);
+    } else {

export { x, y as z } [from '...']

      expect(_braceL);
       while (!eat(_braceR)) {
         if (!first) {
           expect(_comma);
@@ -1858,8 +1879,8 @@ export { x, y as z } [from '...']

} } return nodes; - }

Parses import declaration.

  function parseImport(node) {
-    next();

import '...';

    if (tokType === _string) {
+  }

Parses import declaration.

  function parseImport(node) {
+    next();

import '...';

    if (tokType === _string) {
       node.specifiers = [];
       node.source = parseExprAtom();
       node.kind = "";
@@ -1867,11 +1888,11 @@ export { x, y as z } [from '...']

node.specifiers = parseImportSpecifiers(); if (tokType !== _name || tokVal !== "from") unexpected(); next(); - node.source = tokType === _string ? parseExprAtom() : unexpected();

only for backward compatibility with Esprima's AST + node.source = tokType === _string ? parseExprAtom() : unexpected();

only for backward compatibility with Esprima's AST (it doesn't support mixed default + named yet)

      node.kind = node.specifiers[0]['default'] ? "default" : "named";
     }
     return finishNode(node, "ImportDeclaration");
-  }

Parses a comma-separated list of module imports.

  function parseImportSpecifiers() {
+  }

Parses a comma-separated list of module imports.

  function parseImportSpecifiers() {
     var nodes = [], first = true;
     if (tokType === _star) {
       var node = startNode();
@@ -1883,7 +1904,7 @@ export { x, y as z } [from '...']

nodes.push(finishNode(node, "ImportBatchSpecifier")); return nodes; } - if (tokType === _name) {

import defaultObj, { x, y as z } from '...'

      var node = startNode();
+    if (tokType === _name) {

import defaultObj, { x, y as z } from '...'

      var node = startNode();
       node.id = parseIdent();
       checkLVal(node.id, true);
       node.name = null;
@@ -1911,7 +1932,7 @@ export { x, y as z } [from '...']

nodes.push(finishNode(node, "ImportSpecifier")); } return nodes; - }

Parses yield expression inside generator.

  function parseYield() {
+  }

Parses yield expression inside generator.

  function parseYield() {
     var node = startNode();
     next();
     if (eat(_semi) || canInsertSemicolon()) {
@@ -1922,7 +1943,7 @@ export { x, y as z } [from '...']

node.argument = parseExpression(true); } return finishNode(node, "YieldExpression"); - }

Parses array and generator comprehensions.

  function parseComprehension(node, isGenerator) {
+  }

Parses array and generator comprehensions.

  function parseComprehension(node, isGenerator) {
     node.blocks = [];
     while (tokType === _for) {
       var block = startNode();
@@ -1931,7 +1952,7 @@ export { x, y as z } [from '...']

block.left = toAssignable(parseExprAtom()); checkLVal(block.left, true); if (tokType !== _name || tokVal !== "of") unexpected(); - next();

of property is here for compatibility with Esprima's AST + next();

of property is here for compatibility with Esprima's AST which also supports deprecated [for (... in ...) expr]

      block.of = true;
       block.right = parseExpression();
       expect(_parenR);