Prefix keywords with underscore in tokTypes object
This commit is contained in:
2
acorn.js
2
acorn.js
@@ -328,7 +328,7 @@
|
||||
parenL: _parenL, parenR: _parenR, comma: _comma, semi: _semi, colon: _colon,
|
||||
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];
|
||||
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
|
||||
|
||||
@@ -291,60 +291,60 @@
|
||||
var starttype = token.type, node = startNode();
|
||||
|
||||
switch (starttype) {
|
||||
case tt.break: case tt.continue:
|
||||
case tt._break: case tt._continue:
|
||||
next();
|
||||
var isBreak = starttype === tt.break;
|
||||
var isBreak = starttype === tt._break;
|
||||
node.label = token.type === tt.name ? parseIdent() : null;
|
||||
semicolon();
|
||||
return finishNode(node, isBreak ? "BreakStatement" : "ContinueStatement");
|
||||
|
||||
case tt.debugger:
|
||||
case tt._debugger:
|
||||
next();
|
||||
semicolon();
|
||||
return finishNode(node, "DebuggerStatement");
|
||||
|
||||
case tt.do:
|
||||
case tt._do:
|
||||
next();
|
||||
node.body = parseStatement();
|
||||
node.test = eat(tt.while) ? parseParenExpression() : dummyIdent();
|
||||
node.test = eat(tt._while) ? parseParenExpression() : dummyIdent();
|
||||
semicolon();
|
||||
return finishNode(node, "DoWhileStatement");
|
||||
|
||||
case tt.for:
|
||||
case tt._for:
|
||||
next();
|
||||
pushCx();
|
||||
expect(tt.parenL);
|
||||
if (token.type === tt.semi) return parseFor(node, null);
|
||||
if (token.type === tt.var) {
|
||||
if (token.type === tt._var) {
|
||||
var init = startNode();
|
||||
next();
|
||||
parseVar(init, true);
|
||||
if (init.declarations.length === 1 && eat(tt.in))
|
||||
if (init.declarations.length === 1 && eat(tt._in))
|
||||
return parseForIn(node, init);
|
||||
return parseFor(node, init);
|
||||
}
|
||||
var init = parseExpression(false, true);
|
||||
if (eat(tt.in)) {return parseForIn(node, checkLVal(init));}
|
||||
if (eat(tt._in)) {return parseForIn(node, checkLVal(init));}
|
||||
return parseFor(node, init);
|
||||
|
||||
case tt.function:
|
||||
case tt._function:
|
||||
next();
|
||||
return parseFunction(node, true);
|
||||
|
||||
case tt.if:
|
||||
case tt._if:
|
||||
next();
|
||||
node.test = parseParenExpression();
|
||||
node.consequent = parseStatement();
|
||||
node.alternate = eat(tt.else) ? parseStatement() : null;
|
||||
node.alternate = eat(tt._else) ? parseStatement() : null;
|
||||
return finishNode(node, "IfStatement");
|
||||
|
||||
case tt.return:
|
||||
case tt._return:
|
||||
next();
|
||||
if (eat(tt.semi) || canInsertSemicolon()) node.argument = null;
|
||||
else { node.argument = parseExpression(); semicolon(); }
|
||||
return finishNode(node, "ReturnStatement");
|
||||
|
||||
case tt.switch:
|
||||
case tt._switch:
|
||||
var blockIndent = curIndent, line = curLineStart;
|
||||
next();
|
||||
node.discriminant = parseParenExpression();
|
||||
@@ -353,8 +353,8 @@
|
||||
expect(tt.braceL);
|
||||
|
||||
for (var cur; !closesBlock(tt.braceR, blockIndent, line);) {
|
||||
if (token.type === tt.case || token.type === tt.default) {
|
||||
var isCase = token.type === tt.case;
|
||||
if (token.type === tt._case || token.type === tt._default) {
|
||||
var isCase = token.type === tt._case;
|
||||
if (cur) finishNode(cur, "SwitchCase");
|
||||
node.cases.push(cur = startNode());
|
||||
cur.consequent = [];
|
||||
@@ -376,17 +376,17 @@
|
||||
eat(tt.braceR);
|
||||
return finishNode(node, "SwitchStatement");
|
||||
|
||||
case tt.throw:
|
||||
case tt._throw:
|
||||
next();
|
||||
node.argument = parseExpression();
|
||||
semicolon();
|
||||
return finishNode(node, "ThrowStatement");
|
||||
|
||||
case tt.try:
|
||||
case tt._try:
|
||||
next();
|
||||
node.block = parseBlock();
|
||||
node.handler = null;
|
||||
if (token.type === tt.catch) {
|
||||
if (token.type === tt._catch) {
|
||||
var clause = startNode();
|
||||
next();
|
||||
expect(tt.parenL);
|
||||
@@ -396,23 +396,23 @@
|
||||
clause.body = parseBlock();
|
||||
node.handler = finishNode(clause, "CatchClause");
|
||||
}
|
||||
node.finalizer = eat(tt.finally) ? parseBlock() : null;
|
||||
node.finalizer = eat(tt._finally) ? parseBlock() : null;
|
||||
if (!node.handler && !node.finalizer) return node.block;
|
||||
return finishNode(node, "TryStatement");
|
||||
|
||||
case tt.var:
|
||||
case tt._var:
|
||||
next();
|
||||
node = parseVar(node);
|
||||
semicolon();
|
||||
return node;
|
||||
|
||||
case tt.while:
|
||||
case tt._while:
|
||||
next();
|
||||
node.test = parseParenExpression();
|
||||
node.body = parseStatement();
|
||||
return finishNode(node, "WhileStatement");
|
||||
|
||||
case tt.with:
|
||||
case tt._with:
|
||||
next();
|
||||
node.object = parseParenExpression();
|
||||
node.body = parseStatement();
|
||||
@@ -542,7 +542,7 @@
|
||||
function parseExprOp(left, minPrec, noIn, indent, line) {
|
||||
if (curLineStart != line && curIndent < indent && tokenStartsLine()) return left;
|
||||
var prec = token.type.binop;
|
||||
if (prec != null && (!noIn || token.type !== tt.in)) {
|
||||
if (prec != null && (!noIn || token.type !== tt._in)) {
|
||||
if (prec > minPrec) {
|
||||
var node = startNodeFrom(left);
|
||||
node.left = left;
|
||||
@@ -628,7 +628,7 @@
|
||||
|
||||
function parseExprAtom() {
|
||||
switch (token.type) {
|
||||
case tt.this:
|
||||
case tt._this:
|
||||
var node = startNode();
|
||||
next();
|
||||
return finishNode(node, "ThisExpression");
|
||||
@@ -641,7 +641,7 @@
|
||||
next();
|
||||
return finishNode(node, "Literal");
|
||||
|
||||
case tt.null: case tt.true: case tt.false:
|
||||
case tt._null: case tt._true: case tt._false:
|
||||
var node = startNode();
|
||||
node.value = token.type.atomValue;
|
||||
node.raw = token.type.keyword
|
||||
@@ -666,12 +666,12 @@
|
||||
case tt.braceL:
|
||||
return parseObj();
|
||||
|
||||
case tt.function:
|
||||
case tt._function:
|
||||
var node = startNode();
|
||||
next();
|
||||
return parseFunction(node, false);
|
||||
|
||||
case tt.new:
|
||||
case tt._new:
|
||||
return parseNew();
|
||||
|
||||
default:
|
||||
|
||||
@@ -208,7 +208,7 @@ tokenizer.</p> </td> <td class="code"> <di
|
||||
<span class="nx">parenL</span><span class="o">:</span> <span class="nx">_parenL</span><span class="p">,</span> <span class="nx">parenR</span><span class="o">:</span> <span class="nx">_parenR</span><span class="p">,</span> <span class="nx">comma</span><span class="o">:</span> <span class="nx">_comma</span><span class="p">,</span> <span class="nx">semi</span><span class="o">:</span> <span class="nx">_semi</span><span class="p">,</span> <span class="nx">colon</span><span class="o">:</span> <span class="nx">_colon</span><span class="p">,</span>
|
||||
<span class="nx">dot</span><span class="o">:</span> <span class="nx">_dot</span><span class="p">,</span> <span class="nx">question</span><span class="o">:</span> <span class="nx">_question</span><span class="p">,</span> <span class="nx">slash</span><span class="o">:</span> <span class="nx">_slash</span><span class="p">,</span> <span class="nx">eq</span><span class="o">:</span> <span class="nx">_eq</span><span class="p">,</span> <span class="nx">name</span><span class="o">:</span> <span class="nx">_name</span><span class="p">,</span> <span class="nx">eof</span><span class="o">:</span> <span class="nx">_eof</span><span class="p">,</span>
|
||||
<span class="nx">num</span><span class="o">:</span> <span class="nx">_num</span><span class="p">,</span> <span class="nx">regexp</span><span class="o">:</span> <span class="nx">_regexp</span><span class="p">,</span> <span class="nx">string</span><span class="o">:</span> <span class="nx">_string</span><span class="p">};</span>
|
||||
<span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">kw</span> <span class="k">in</span> <span class="nx">keywordTypes</span><span class="p">)</span> <span class="nx">exports</span><span class="p">.</span><span class="nx">tokTypes</span><span class="p">[</span><span class="nx">kw</span><span class="p">]</span> <span class="o">=</span> <span class="nx">keywordTypes</span><span class="p">[</span><span class="nx">kw</span><span class="p">];</span></pre></div> </td> </tr> <tr id="section-36"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-36">¶</a> </div> <p>This is a trick taken from Esprima. It turns out that, on
|
||||
<span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">kw</span> <span class="k">in</span> <span class="nx">keywordTypes</span><span class="p">)</span> <span class="nx">exports</span><span class="p">.</span><span class="nx">tokTypes</span><span class="p">[</span><span class="s2">"_"</span> <span class="o">+</span> <span class="nx">kw</span><span class="p">]</span> <span class="o">=</span> <span class="nx">keywordTypes</span><span class="p">[</span><span class="nx">kw</span><span class="p">];</span></pre></div> </td> </tr> <tr id="section-36"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-36">¶</a> </div> <p>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 <code>switch</code> statement is faster than
|
||||
a regular expression, and on Chrome the two are about on par.
|
||||
|
||||
Reference in New Issue
Block a user