From 965166cdfd8086a51a2ae3987a36d5eed4e20910 Mon Sep 17 00:00:00 2001 From: Marijn Haverbeke Date: Wed, 16 Jan 2013 16:47:21 +0100 Subject: [PATCH] Small fixes noticed while going over the when writing loose parser --- acorn.js | 20 ++++++++++---------- index.html | 20 ++++++++++---------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/acorn.js b/acorn.js index ebd9ed4d18..f473bad68d 100644 --- a/acorn.js +++ b/acorn.js @@ -133,15 +133,14 @@ t.type = tokType; t.value = tokVal; return t; } - getToken.jumpTo = function(pos) { + getToken.jumpTo = function(pos, reAllowed) { tokPos = pos; if (options.locations) { tokCurLine = tokLineStart = 0; tokLineStartNext = nextLineStart(); } var ch = input.charAt(pos - 1); - tokRegexpAllowed = !ch || /[\[\{\(,;:?\/*=+\-~!|&%^<>]/.test(ch) || - /[enwfd]/.test(ch) && /\b(keywords|case|else|return|throw|new|in|(instance|type)of|delete|void)$/.test(input.slice(pos - 9, pos + 1)); + tokRegexpAllowed = reAllowed; skipSpace(); }; return getToken; @@ -210,7 +209,7 @@ var loc = getLineInfo(input, pos); message += " (" + loc.line + ":" + loc.column + ")"; var err = new SyntaxError(message); - err.pos = pos; err.loc = loc; + err.pos = pos; err.loc = loc; err.raisedAt = tokPos; throw err; } @@ -312,7 +311,8 @@ exports.tokTypes = {bracketL: _bracketL, bracketR: _bracketR, braceL: _braceL, braceR: _braceR, parenL: _parenL, parenR: _parenR, comma: _comma, semi: _semi, colon: _colon, - dot: _dot, question: _question, slash: _slash, eq: _eq}; + dot: _dot, question: _question, slash: _slash, eq: _eq, name: _name, eof: _eof, + num: _num, regexp: _regexp, string: _string}; for (var kw in keywordTypes) exports.tokTypes[kw] = keywordTypes[kw]; // This is a trick taken from Esprima. It turns out that, on @@ -928,13 +928,13 @@ // Start an AST node, attaching a start offset. - function node_t(s) { + function node_t() { this.type = null; this.start = tokStart; this.end = null; } - function node_loc_t(s) { + function node_loc_t() { this.start = tokStartLoc; this.end = null; if (sourceFile !== null) this.source = sourceFile; @@ -1056,7 +1056,7 @@ first = false; } return finishNode(node, "Program"); - }; + } var loopLabel = {kind: "loop"}, switchLabel = {kind: "switch"}; @@ -1564,7 +1564,7 @@ function parseNew() { var node = startNode(); next(); - node.callee = parseSubscripts(parseExprAtom(false), true); + node.callee = parseSubscripts(parseExprAtom(), true); if (eat(_parenL)) node.arguments = parseExprList(_parenR, false); else node.arguments = []; return finishNode(node, "NewExpression"); @@ -1591,7 +1591,7 @@ isGetSet = sawGetSet = true; kind = prop.kind = prop.key.name; prop.key = parsePropertyName(); - if (!tokType === _parenL) unexpected(); + if (tokType !== _parenL) unexpected(); prop.value = parseFunction(startNode(), false); } else unexpected(); diff --git a/index.html b/index.html index 5b682d79bd..d2a46e16c1 100644 --- a/index.html +++ b/index.html @@ -92,15 +92,14 @@ reset the internal state, and invalidate existing tokenizers.

t.type = tokType; t.value = tokVal; return t; } - getToken.jumpTo = function(pos) { + getToken.jumpTo = function(pos, reAllowed) { tokPos = pos; if (options.locations) { tokCurLine = tokLineStart = 0; tokLineStartNext = nextLineStart(); } var ch = input.charAt(pos - 1); - tokRegexpAllowed = !ch || /[\[\{\(,;:?\/*=+\-~!|&%^<>]/.test(ch) || - /[enwfd]/.test(ch) && /\b(keywords|case|else|return|throw|new|in|(instance|type)of|delete|void)$/.test(input.slice(pos - 9, pos + 1)); + tokRegexpAllowed = reAllowed; skipSpace(); }; return getToken; @@ -131,7 +130,7 @@ message.

var loc = getLineInfo(input, pos); message += " (" + loc.line + ":" + loc.column + ")"; var err = new SyntaxError(message); - err.pos = pos; err.loc = loc; + err.pos = pos; err.loc = loc; err.raisedAt = tokPos; throw err; }

Token types

The assignment of fine-grained, information-carrying type objects allows the tokenizer to store the information it has about a @@ -195,7 +194,8 @@ in AssignmentExpression nodes.

var _bin10 = {binop: 10, beforeExpr: true};

Provide access to the token types for external users of the tokenizer.

  exports.tokTypes = {bracketL: _bracketL, bracketR: _bracketR, braceL: _braceL, braceR: _braceR,
                       parenL: _parenL, parenR: _parenR, comma: _comma, semi: _semi, colon: _colon,
-                      dot: _dot, question: _question, slash: _slash, eq: _eq};
+                      dot: _dot, question: _question, slash: _slash, eq: _eq, name: _name, eof: _eof,
+                      num: _num, regexp: _regexp, string: _string};
   for (var kw in keywordTypes) exports.tokTypes[kw] = keywordTypes[kw];

This is a trick taken from Esprima. It turns out that, on non-Chrome browsers, to check whether a string is in a set, a predicate containing a big ugly switch statement is faster than @@ -660,13 +660,13 @@ tests ("use strict"; 010; -- should fail).

tokPos = lastEnd; skipSpace(); readToken(); - }

Start an AST node, attaching a start offset.

  function node_t(s) {
+  }

Start an AST node, attaching a start offset.

  function node_t() {
     this.type = null;
     this.start = tokStart;
     this.end = null;
   }
 
-  function node_loc_t(s) {
+  function node_loc_t() {
     this.start = tokStartLoc;
     this.end = null;
     if (sourceFile !== null) this.source = sourceFile;
@@ -746,7 +746,7 @@ to its body instead of creating a new node.

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

Parse a single statement.

@@ -1169,7 +1169,7 @@ 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();
     next();
-    node.callee = parseSubscripts(parseExprAtom(false), true);
+    node.callee = parseSubscripts(parseExprAtom(), true);
     if (eat(_parenL)) node.arguments = parseExprList(_parenR, false);
     else node.arguments = [];
     return finishNode(node, "NewExpression");
@@ -1192,7 +1192,7 @@ least, not without wrapping it in parentheses. Thus, it uses the 

isGetSet = sawGetSet = true; kind = prop.kind = prop.key.name; prop.key = parsePropertyName(); - if (!tokType === _parenL) unexpected(); + if (tokType !== _parenL) unexpected(); prop.value = parseFunction(startNode(), false); } else unexpected();

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