From 6113324cd2f6facf5abcb8717e9c7053513aaba0 Mon Sep 17 00:00:00 2001 From: Marijn Haverbeke Date: Sun, 25 Aug 2013 15:01:20 +0200 Subject: [PATCH] Fix bug with parsing slash after operator-keyword property Closes #53 --- acorn.js | 1 + index.html | 20 +++++++++++--------- test/tests.js | 31 ++++++++++++++++++++++++++++++- 3 files changed, 42 insertions(+), 10 deletions(-) diff --git a/acorn.js b/acorn.js index 9808f43da6..e0e8b63510 100644 --- a/acorn.js +++ b/acorn.js @@ -1717,6 +1717,7 @@ function parseIdent(liberal) { var node = startNode(); node.name = tokType === _name ? tokVal : (liberal && !options.forbidReserved && tokType.keyword) || unexpected(); + tokRegexpAllowed = false; next(); return finishNode(node, "Identifier"); } diff --git a/index.html b/index.html index fac2e0328d..2c52cf1987 100644 --- a/index.html +++ b/index.html @@ -14,11 +14,11 @@ https://github.com/marijnh/acorn.git

This file defines the main parser interface. The library also comes with a error-tolerant parser and an -abstract syntax tree walker, defined in other files.

(function(mod) {
+abstract syntax tree walker, defined in other files.

(function(root, mod) {
   if (typeof exports == "object" && typeof module == "object") return mod(exports); // CommonJS
   if (typeof define == "function" && define.amd) return define(["exports"], mod); // AMD
-  mod(this.acorn || (this.acorn = {})); // Plain browser env
-})(function(exports) {
+  mod(root.acorn || (root.acorn = {})); // Plain browser env
+})(this, function(exports) {
   "use strict";
 
   exports.version = "0.3.2";

The main exported interface (under self.acorn when in the @@ -103,7 +103,8 @@ reset the internal state, and invalidate existing tokenizers.

getToken.jumpTo = function(pos, reAllowed) { tokPos = pos; if (options.locations) { - tokCurLine = tokLineStart = lineBreak.lastIndex = 0; + tokCurLine = 1; + tokLineStart = lineBreak.lastIndex = 0; var match; while ((match = lineBreak.exec(input)) && match.index < pos) { ++tokCurLine; @@ -343,7 +344,7 @@ whitespace and comments, and.

} else if (next === 47) { // '/' skipLineComment(); } else break; - } else if ((ch < 14 && ch > 8) || ch === 32 || ch === 160) { // ' ', '\xa0' + } else if (ch === 160) { // '\xa0' ++tokPos; } else if (ch >= 5760 && nonASCIIwhitespace.test(String.fromCharCode(ch))) { ++tokPos; @@ -1057,7 +1058,7 @@ operators like +=.

} return expr; }

Start the precedence parser.

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

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 @@ -1070,19 +1071,19 @@ operator that has a lower precedence than the set it is parsing.

node.left = left; node.operator = tokVal; next(); - node.right = parseExprOp(parseMaybeUnary(noIn), prec, noIn); + node.right = parseExprOp(parseMaybeUnary(), prec, noIn); var node = finishNode(node, /&&|\|\|/.test(node.operator) ? "LogicalExpression" : "BinaryExpression"); return parseExprOp(node, minPrec, noIn); } } return left; - }

Parse unary operators, both prefix and postfix.

  function parseMaybeUnary(noIn) {
+  }

Parse unary operators, both prefix and postfix.

  function parseMaybeUnary() {
     if (tokType.prefix) {
       var node = startNode(), update = tokType.isUpdate;
       node.operator = tokVal;
       node.prefix = true;
       next();
-      node.argument = parseMaybeUnary(noIn);
+      node.argument = parseMaybeUnary();
       if (update) checkLVal(node.argument);
       else if (strict && node.operator === "delete" &&
                node.argument.type === "Identifier")
@@ -1284,6 +1285,7 @@ when parsing properties), it will also convert keywords into
 identifiers.

  function parseIdent(liberal) {
     var node = startNode();
     node.name = tokType === _name ? tokVal : (liberal && !options.forbidReserved && tokType.keyword) || unexpected();
+    tokRegexpAllowed = false;
     next();
     return finishNode(node, "Identifier");
   }
diff --git a/test/tests.js b/test/tests.js
index 87a6707016..8c427868b2 100644
--- a/test/tests.js
+++ b/test/tests.js
@@ -26235,7 +26235,36 @@ test("var a = 1;", {
 }, {
   locations: true,
   sourceFile: "test.js"
-})
+});
+
+test("a.in / b", {
+  type: "Program",
+  body: [
+    {
+      type: "ExpressionStatement",
+      expression: {
+        type: "BinaryExpression",
+        left: {
+          type: "MemberExpression",
+          object: {
+            type: "Identifier",
+            name: "a"
+          },
+          property: {
+            type: "Identifier",
+            name: "in"
+          },
+          computed: false
+        },
+        operator: "/",
+        right: {
+          type: "Identifier",
+          name: "b"
+        }
+      }
+    }
+  ]
+});
 
 // Failure tests