Conflicts:
	.gitignore
	README.md
	acorn.js
	package.json
	test/run.js
	test/tests-harmony.js
	test/tests-jsx.js
This commit is contained in:
Sebastian McKenzie 2015-01-23 08:07:09 +11:00
commit af7630b963
9 changed files with 1475 additions and 759 deletions

2
.gitignore vendored
View File

@ -1,4 +1,4 @@
/node_modules
/.idea
/.tern-port
acorn_csp.js
/acorn_csp.js

1259
acorn.js

File diff suppressed because it is too large Load Diff

View File

@ -66,6 +66,8 @@
ahead.length = 0;
token = ahead.shift() || readToken(forceRegexp);
if (options.onToken)
options.onToken(token);
if (token.start >= nextLineStart) {
while (token.start >= nextLineStart) {
@ -101,8 +103,10 @@
replace = {start: e.pos, end: pos, type: tt.regexp, value: re};
} else if (/template/.test(msg)) {
replace = {start: e.pos, end: pos,
type: input.charAt(e.pos) == "`" ? tt.template : tt.templateContinued,
value: input.slice(e.pos + 1, pos)};
type: tt.template,
value: input.slice(e.pos, pos)};
} else if (/comment/.test(msg)) {
replace = fetchToken.current();
} else {
replace = false;
}
@ -253,14 +257,6 @@
return node;
}
function finishNodeAt(node, type, pos) {
if (options.locations) { node.loc.end = pos[1]; pos = pos[0]; }
node.type = type;
node.end = pos;
if (options.ranges) node.range[1] = pos;
return node;
}
function dummyIdent() {
var dummy = startNode();
dummy.name = "✖";
@ -304,6 +300,7 @@
case "ObjectPattern":
case "ArrayPattern":
case "SpreadElement":
case "AssignmentPattern":
return expr;
default:
@ -625,20 +622,18 @@
function parseMaybeUnary(noIn) {
if (token.type.prefix) {
var node = startNode(), update = token.type.isUpdate, nodeType;
if (token.type === tt.ellipsis) {
nodeType = "SpreadElement";
} else {
nodeType = update ? "UpdateExpression" : "UnaryExpression";
node.operator = token.value;
node.prefix = true;
}
var node = startNode(), update = token.type.isUpdate;
node.operator = token.value;
node.prefix = true;
next();
node.argument = parseMaybeUnary(noIn);
if (update) node.argument = checkLVal(node.argument);
return finishNode(node, nodeType);
return finishNode(node, update ? "UpdateExpression" : "UnaryExpression");
} else if (token.type === tt.ellipsis) {
var node = startNode();
next();
node.argument = parseMaybeUnary(noIn);
return finishNode(node, "SpreadElement");
}
var start = storeCurrentPos();
var expr = parseExprSubscripts();
@ -692,7 +687,7 @@
node.callee = base;
node.arguments = parseExprList(tt.parenR);
base = finishNode(node, "CallExpression");
} else if (token.type == tt.template) {
} else if (token.type == tt.backQuote) {
var node = startNodeAt(start);
node.tag = base;
node.quasi = parseTemplate();
@ -785,7 +780,7 @@
}
return finishNode(node, "YieldExpression");
case tt.template:
case tt.backQuote:
return parseTemplate();
default:
@ -808,36 +803,35 @@
}
function parseTemplateElement() {
var elem = startNodeAt(options.locations ? [token.start + 1, token.startLoc.offset(1)] : token.start + 1);
elem.value = token.value;
elem.tail = input.charCodeAt(token.end - 1) !== 123; // '{'
var endOff = elem.tail ? 1 : 2;
var endPos = options.locations ? [token.end - endOff, token.endLoc.offset(-endOff)] : token.end - endOff;
var elem = startNode();
elem.value = {
raw: input.slice(token.start, token.end),
cooked: token.value
};
next();
return finishNodeAt(elem, "TemplateElement", endPos);
elem.tail = token.type === tt.backQuote;
return finishNode(elem, "TemplateElement");
}
function parseTemplate() {
var node = startNode();
next();
node.expressions = [];
var curElt = parseTemplateElement();
node.quasis = [curElt];
while (!curElt.tail) {
var next = parseExpression();
if (isDummy(next)) {
node.quasis[node.quasis.length - 1].tail = true;
break;
}
node.expressions.push(next);
if (token.type === tt.templateContinued) {
node.quasis.push(curElt = parseTemplateElement());
next();
node.expressions.push(parseExpression());
if (expect(tt.braceR)) {
curElt = parseTemplateElement();
} else {
curElt = startNode();
curElt.value = {cooked: "", raw: ""};
curElt.value = {cooked: '', raw: ''};
curElt.tail = true;
node.quasis.push(curElt);
}
node.quasis.push(curElt);
}
expect(tt.backQuote);
return finishNode(node, "TemplateLiteral");
}
@ -858,10 +852,11 @@
eat(tt.braceL);
if (curIndent + 1 < indent) { indent = curIndent; line = curLineStart; }
while (!closes(tt.braceR, indent, line)) {
if (isClass && semicolon()) continue;
var prop = startNode(), isGenerator;
if (options.ecmaVersion >= 6) {
if (isClass) {
if (prop['static'] = (token.type === tt.name && token.value === "static")) next();
prop['static'] = false;
} else {
prop.method = false;
prop.shorthand = false;
@ -870,6 +865,16 @@
}
parsePropertyName(prop);
if (isDummy(prop.key)) { if (isDummy(parseExpression(true))) next(); eat(tt.comma); continue; }
if (isClass) {
if (prop.key.type === "Identifier" && !prop.computed && prop.key.name === "static" &&
(token.type != tt.parenL && token.type != tt.braceL)) {
prop['static'] = true;
isGenerator = eat(tt.star);
parsePropertyName(prop);
} else {
prop['static'] = false;
}
}
if (!isClass && eat(tt.colon)) {
prop.kind = "init";
prop.value = parseExpression(true);
@ -882,7 +887,7 @@
}
prop.value = parseMethod(isGenerator);
} else if (options.ecmaVersion >= 5 && prop.key.type === "Identifier" &&
(prop.key.name === "get" || prop.key.name === "set") &&
!prop.computed && (prop.key.name === "get" || prop.key.name === "set") &&
(token.type != tt.comma && token.type != tt.braceR)) {
prop.kind = prop.key.name;
parsePropertyName(prop);
@ -898,7 +903,6 @@
if (isClass) {
node.body.body.push(finishNode(prop, "MethodDefinition"));
semicolon();
} else {
node.properties.push(finishNode(prop, "Property"));
eat(tt.comma);
@ -942,7 +946,6 @@
function parseIdent() {
var node = startNode();
node.name = token.type === tt.name ? token.value : token.type.keyword;
fetchToken.noRegexp();
next();
return finishNode(node, "Identifier");
}

View File

@ -4,12 +4,12 @@ var path = require("path");
var fs = require("fs");
var acorn = require("../acorn.js");
var infile, parsed, options = {}, silent = false, compact = false;
var infile, parsed, tokens, options = {}, silent = false, compact = false, tokenize = false;
function help(status) {
var print = (status == 0) ? console.log : console.error;
print("usage: " + path.basename(process.argv[1]) + " [--ecma3|--ecma5|--ecma6] [--strictSemicolons]");
print(" [--locations] [--compact] [--silent] [--help] [--] infile");
print(" [--tokenize] [--locations] [--compact] [--silent] [--help] [--] infile");
process.exit(status);
}
@ -26,16 +26,29 @@ for (var i = 2; i < process.argv.length; ++i) {
else if (arg == "--silent") silent = true;
else if (arg == "--compact") compact = true;
else if (arg == "--help") help(0);
else if (arg == "--tokenize") tokenize = true;
else help(1);
}
try {
var code = fs.readFileSync(infile, "utf8");
parsed = acorn.parse(code, options);
if (!tokenize)
parsed = acorn.parse(code, options);
else {
var get = acorn.tokenize(code, options);
tokens = [];
while (true) {
var token = get();
tokens.push(token);
if (token.type.type == "eof")
break;
}
}
} catch(e) {
console.log(e.message);
process.exit(1);
}
if (!silent)
console.log(JSON.stringify(parsed, null, compact ? null : 2));
console.log(JSON.stringify(tokenize ? tokens : parsed, null, compact ? null : 2));

View File

@ -8,6 +8,7 @@
<script src="tests-jsx.js" charset="utf-8"></script>
<script src="tests.js" charset="utf-8"></script>
<script src="tests-harmony.js" charset="utf-8"></script>
<script src="tests-jsx.js" charset="utf-8"></script>
</head>
<body>
<ul id="log"></ul>

View File

@ -3328,8 +3328,7 @@ test("[for (x of array) x]", {
loc: {
start: {line: 1, column: 1},
end: {line: 1, column: 17}
},
of: true
}
}],
body: {
type: "Identifier",
@ -3412,8 +3411,7 @@ test("[for (x of array) for (y of array2) if (x === test) x]", {
loc: {
start: {line: 1, column: 1},
end: {line: 1, column: 17}
},
of: true
}
},
{
type: "ComprehensionBlock",
@ -3436,8 +3434,7 @@ test("[for (x of array) for (y of array2) if (x === test) x]", {
loc: {
start: {line: 1, column: 18},
end: {line: 1, column: 35}
},
of: true
}
}
],
body: {
@ -3521,8 +3518,7 @@ test("(for (x of array) for (y of array2) if (x === test) x)", {
loc: {
start: {line: 1, column: 1},
end: {line: 1, column: 17}
},
of: true
}
},
{
type: "ComprehensionBlock",
@ -3545,8 +3541,7 @@ test("(for (x of array) for (y of array2) if (x === test) x)", {
loc: {
start: {line: 1, column: 18},
end: {line: 1, column: 35}
},
of: true
}
}
],
body: {
@ -3617,8 +3612,7 @@ test("[for ([,x] of array) for ({[start.x]: x, [start.y]: y} of array2) x]", {
loc: {
start: {line: 1, column: 1},
end: {line: 1, column: 20}
},
of: true
}
},
{
type: "ComprehensionBlock",
@ -3728,8 +3722,7 @@ test("[for ([,x] of array) for ({[start.x]: x, [start.y]: y} of array2) x]", {
loc: {
start: {line: 1, column: 21},
end: {line: 1, column: 65}
},
of: true
}
}
],
body: {
@ -5278,14 +5271,6 @@ test("import $ from \"jquery\"", {
specifiers: [{
type: "ImportSpecifier",
id: {
type: "Identifier",
name: "default",
loc: {
start: {line: 1, column: 7},
end: {line: 1, column: 8}
}
},
name: {
type: "Identifier",
name: "$",
loc: {
@ -5293,6 +5278,7 @@ test("import $ from \"jquery\"", {
end: {line: 1, column: 8}
}
},
name: null,
loc: {
start: {line: 1, column: 7},
end: {line: 1, column: 8}
@ -5455,21 +5441,15 @@ test("import crypto, { decrypt, encrypt as enc } from \"crypto\"", {
end: {line: 1, column: 13}
},
id: {
type: "Identifier",
loc: {
start: {line: 1, column: 7},
end: {line: 1, column: 13}
},
name: "default"
},
name: {
type: "Identifier",
loc: {
start: {line: 1, column: 7},
end: {line: 1, column: 13}
},
name: "crypto"
}
},
name: null,
default: true
},
{
type: "ImportSpecifier",
@ -6478,6 +6458,72 @@ test("var A = class extends B {}", {
locations: true
});
test("class A extends class B extends C {} {}", {
type: "Program",
body: [{
type: "ClassDeclaration",
id: {
type: "Identifier",
name: "A",
loc: {
start: {line: 1, column: 6},
end: {line: 1, column: 7}
}
},
superClass: {
type: "ClassExpression",
id: {
type: "Identifier",
name: "B",
loc: {
start: {line: 1, column: 22},
end: {line: 1, column: 23}
}
},
superClass: {
type: "Identifier",
name: "C",
loc: {
start: {line: 1, column: 32},
end: {line: 1, column: 33}
}
},
body: {
type: "ClassBody",
body: [],
loc: {
start: {line: 1, column: 34},
end: {line: 1, column: 36}
}
},
loc: {
start: {line: 1, column: 16},
end: {line: 1, column: 36}
}
},
body: {
type: "ClassBody",
body: [],
loc: {
start: {line: 1, column: 37},
end: {line: 1, column: 39}
}
},
loc: {
start: {line: 1, column: 0},
end: {line: 1, column: 39}
}
}],
loc: {
start: {line: 1, column: 0},
end: {line: 1, column: 39}
}
}, {
ecmaVersion: 6,
ranges: true,
locations: true
});
test("class A {get() {}}", {
type: "Program",
body: [{
@ -8813,6 +8859,42 @@ test("class A { foo() {} get foo() {} }",{
locations: true
});
test("class Semicolon { ; }", {
type: "Program",
loc: {
start: {line: 1, column: 0},
end: {line: 1, column: 21}
},
body: [{
type: "ClassDeclaration",
loc: {
start: {line: 1, column: 0},
end: {line: 1, column: 21}
},
id: {
type: "Identifier",
loc: {
start: {line: 1, column: 6},
end: {line: 1, column: 15}
},
name: "Semicolon"
},
superClass: null,
body: {
type: "ClassBody",
loc: {
start: {line: 1, column: 16},
end: {line: 1, column: 21}
},
body: []
}
}]
}, {
ecmaVersion: 6,
ranges: true,
locations: true
});
// ES6: Computed Properties
test("({[x]: 10})", {
@ -13809,9 +13891,9 @@ testFail("import { foo, bar }", "Unexpected token (1:19)", {ecmaVersion: 6});
testFail("import foo from bar", "Unexpected token (1:16)", {ecmaVersion: 6});
testFail("((a)) => 42", "Unexpected token (1:6)", {ecmaVersion: 6});
testFail("((a)) => 42", "Unexpected token (1:1)", {ecmaVersion: 6});
testFail("(a, (b)) => 42", "Unexpected token (1:9)", {ecmaVersion: 6});
testFail("(a, (b)) => 42", "Unexpected token (1:4)", {ecmaVersion: 6});
testFail("\"use strict\"; (eval = 10) => 42", "Assigning to eval in strict mode (1:15)", {ecmaVersion: 6});
@ -14031,7 +14113,7 @@ testFail("class A extends yield B { }", "Unexpected token (1:22)", {ecmaVersion:
testFail("class default", "Unexpected token (1:6)", {ecmaVersion: 6});
testFail("`test", "Unterminated template (1:0)", {ecmaVersion: 6});
testFail("`test", "Unterminated template (1:1)", {ecmaVersion: 6});
testFail("switch `test`", "Unexpected token (1:7)", {ecmaVersion: 6});
@ -14069,7 +14151,7 @@ testFail("\"use strict\"; function x({ b: { a } }, [{ b: { a } }]){}", "Argument
testFail("\"use strict\"; function x(a, ...[a]){}", "Argument name clash in strict mode (1:32)", {ecmaVersion: 6});
testFail("(...a, b) => {}", "Unexpected token (1:1)", {ecmaVersion: 6});
testFail("(...a, b) => {}", "Unexpected token (1:5)", {ecmaVersion: 6});
testFail("([ 5 ]) => {}", "Unexpected token (1:3)", {ecmaVersion: 6});
@ -14144,9 +14226,9 @@ test("[...a, ] = b", {
locations: true
});
testFail("if (b,...a, );", "Unexpected token (1:12)", {ecmaVersion: 6});
testFail("if (b,...a, );", "Unexpected token (1:6)", {ecmaVersion: 6});
testFail("(b, ...a)", "Unexpected token (1:9)", {ecmaVersion: 6});
testFail("(b, ...a)", "Unexpected token (1:4)", {ecmaVersion: 6});
testFail("switch (cond) { case 10: let a = 20; ", "Unexpected token (1:37)", {ecmaVersion: 6});
@ -14290,13 +14372,11 @@ test("import foo, * as bar from 'baz';", {
{
type: "ImportSpecifier",
id: {
type: "Identifier",
name: "default"
},
name: {
type: "Identifier",
name: "foo"
}
},
name: null,
default: true
},
{
type: "ImportBatchSpecifier",
@ -14545,3 +14625,259 @@ test("var [localVar = defaultValue] = obj", {
locations: true,
loose: false
});
// https://github.com/marijnh/acorn/issues/191
test("try {} catch ({message}) {}", {
type: "Program",
range: [0, 27],
body: [{
type: "TryStatement",
range: [0, 27],
block: {
type: "BlockStatement",
range: [4, 6],
body: []
},
handler: {
type: "CatchClause",
range: [7, 27],
param: {
type: "ObjectPattern",
range: [14, 23],
properties: [{
type: "Property",
range: [15, 22],
method: false,
shorthand: true,
computed: false,
key: {
type: "Identifier",
range: [15, 22],
name: "message"
},
kind: "init",
value: {
type: "Identifier",
range: [15, 22],
name: "message"
}
}]
},
guard: null,
body: {
type: "BlockStatement",
range: [25, 27],
body: []
}
},
guardedHandlers: [],
finalizer: null
}]
}, {
ecmaVersion: 6,
ranges: true,
locations: true,
loose: false
});
// https://github.com/marijnh/acorn/issues/192
test("class A { static() {} }", {
type: "Program",
range: [0, 23],
body: [{
type: "ClassDeclaration",
range: [0, 23],
id: {
type: "Identifier",
range: [6, 7],
name: "A"
},
superClass: null,
body: {
type: "ClassBody",
range: [8, 23],
body: [{
type: "MethodDefinition",
range: [10, 21],
computed: false,
key: {
type: "Identifier",
range: [10, 16],
name: "static"
},
static: false,
kind: "",
value: {
type: "FunctionExpression",
range: [16, 21],
id: null,
params: [],
defaults: [],
rest: null,
generator: false,
body: {
type: "BlockStatement",
range: [19, 21],
body: []
},
expression: false
}
}]
}
}]
}, {
ecmaVersion: 6,
ranges: true,
locations: true
});
test("class A { *static() {} }", {
type: "Program",
range: [0, 24],
body: [{
type: "ClassDeclaration",
range: [0, 24],
id: {
type: "Identifier",
range: [6, 7],
name: "A"
},
superClass: null,
body: {
type: "ClassBody",
range: [8, 24],
body: [{
type: "MethodDefinition",
range: [10, 22],
computed: false,
key: {
type: "Identifier",
range: [11, 17],
name: "static"
},
static: false,
kind: "",
value: {
type: "FunctionExpression",
range: [17, 22],
id: null,
params: [],
defaults: [],
rest: null,
generator: true,
body: {
type: "BlockStatement",
range: [20, 22],
body: []
},
expression: false
}
}]
}
}]
}, {
ecmaVersion: 6,
ranges: true,
locations: true
});
test("`${/\d/.exec('1')[0]}`", {
"type": "Program",
"start": 0,
"end": 21,
"body": [
{
"type": "ExpressionStatement",
"start": 0,
"end": 21,
"expression": {
"type": "TemplateLiteral",
"start": 0,
"end": 21,
"expressions": [
{
"type": "MemberExpression",
"start": 3,
"end": 19,
"object": {
"type": "CallExpression",
"start": 3,
"end": 16,
"callee": {
"type": "MemberExpression",
"start": 3,
"end": 11,
"object": {
"type": "Literal",
"start": 3,
"end": 6,
"regex": {
"pattern": "d",
"flags": ""
},
"value": {},
"raw": "/d/"
},
"property": {
"type": "Identifier",
"start": 7,
"end": 11,
"name": "exec"
},
"computed": false
},
"arguments": [
{
"type": "Literal",
"start": 12,
"end": 15,
"value": "1",
"raw": "'1'"
}
]
},
"property": {
"type": "Literal",
"start": 17,
"end": 18,
"value": 0,
"raw": "0"
},
"computed": true
}
],
"quasis": [
{
"type": "TemplateElement",
"start": 1,
"end": 1,
"value": {
"raw": "",
"cooked": ""
},
"tail": false
},
{
"type": "TemplateElement",
"start": 20,
"end": 20,
"value": {
"raw": "",
"cooked": ""
},
"tail": true
}
]
}
}
]
}, {
ecmaVersion: 6
});
testFail("if (1) let x = 10;", "Unexpected token (1:7)", {ecmaVersion: 6});
testFail("for (;;) const x = 10;", "Unexpected token (1:9)", {ecmaVersion: 6});
testFail("while (1) function foo(){}", "Unexpected token (1:10)", {ecmaVersion: 6});
testFail("if (1) ; else class Cls {}", "Unexpected token (1:14)", {ecmaVersion: 6});

File diff suppressed because it is too large Load Diff

View File

@ -26687,6 +26687,22 @@ test("a.in / b", {
]
});
// A number of slash-disambiguation corner cases
test("return {} / 2", {}, {allowReturnOutsideFunction: true});
test("return\n{}\n/foo/", {}, {allowReturnOutsideFunction: true});
test("+{} / 2", {});
test("{}\n/foo/", {});
test("x++\n{}\n/foo/", {});
test("{{}\n/foo/}", {});
test("while (1) /foo/", {});
test("(1) / 2", {});
test("({a: [1]}+[]) / 2", {});
test("{[1]}\n/foo/", {});
test("switch(a) { case 1: {}\n/foo/ }", {});
test("({1: {} / 2})", {});
test("+x++ / 2", {});
test("foo.in\n{}\n/foo/", {});
test("{}/=/", {
type: "Program",
body: [
@ -28757,6 +28773,7 @@ var tokTypes = acorn.tokTypes;
test('var x = (1 + 2)', {}, {
locations: true,
loose: false,
onToken: [
{
type: tokTypes._var,

View File

@ -285,7 +285,15 @@
c(node.object, st, "Expression");
if (node.computed) c(node.property, st, "Expression");
};
base.Identifier = base.Literal = base.ExportDeclaration = base.ImportDeclaration = ignore;
base.ExportDeclaration = function (node, st, c) {
c(node.declaration, st);
};
base.ImportDeclaration = function (node, st, c) {
node.specifiers.forEach(function (specifier) {
c(specifier, st);
});
};
base.ImportSpecifier = base.ImportBatchSpecifier = base.Identifier = base.Literal = ignore;
base.TaggedTemplateExpression = function(node, st, c) {
c(node.tag, st, "Expression");