Skip to content

Commit

Permalink
Implement ES7 Async/Await
Browse files Browse the repository at this point in the history
  • Loading branch information
sebmck committed Nov 10, 2014
1 parent 9d3580b commit ac42c10
Show file tree
Hide file tree
Showing 2 changed files with 874 additions and 20 deletions.
127 changes: 107 additions & 20 deletions acorn.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,13 @@
comments.push(comment);
};
}
isKeyword = options.ecmaVersion >= 6 ? isEcma6Keyword : isEcma5AndLessKeyword;
if (options.ecmaVersion >= 7) {
isKeyword = isEcma7Keyword;
} else if (options.ecmaVersion === 6) {
isKeyword = isEcma6Keyword;
} else {
isKeyword = isEcma5AndLessKeyword;
}
}

// The `getLineInfo` function is mostly useful when the
Expand Down Expand Up @@ -289,7 +295,7 @@
// that `break` and `continue` have somewhere to jump to, and
// `strict` indicates whether strict mode is on.

var inFunction, inGenerator, labels, strict;
var inFunction, inGenerator, inAsync, labels, strict;

// This counter is used for checking that arrow expressions did
// not contain nested parentheses in argument list.
Expand All @@ -305,7 +311,7 @@
function initParserState() {
lastStart = lastEnd = tokPos;
if (options.locations) lastEndLoc = new Position;
inFunction = inGenerator = strict = false;
inFunction = inGenerator = inAsync = strict = false;
labels = [];
readToken();
}
Expand Down Expand Up @@ -368,6 +374,7 @@
var _class = {keyword: "class"}, _extends = {keyword: "extends", beforeExpr: true};
var _export = {keyword: "export"}, _import = {keyword: "import"};
var _yield = {keyword: "yield", beforeExpr: true};
var _async = {keyword: "async"}, _await = {keyword: "await", beforeExpr: true};

// The keywords that denote values.

Expand All @@ -394,7 +401,8 @@
"void": {keyword: "void", prefix: true, beforeExpr: true},
"delete": {keyword: "delete", prefix: true, beforeExpr: true},
"class": _class, "extends": _extends,
"export": _export, "import": _import, "yield": _yield};
"export": _export, "import": _import, "yield": _yield,
"await": _await, "async": _async};

// Punctuation token types. Again, the `type` property is purely for debugging.

Expand Down Expand Up @@ -517,7 +525,11 @@

var isEcma5AndLessKeyword = makePredicate(ecma5AndLessKeywords);

var isEcma6Keyword = makePredicate(ecma5AndLessKeywords + " let const class extends export import yield");
var ecma6AndLessKeywords = ecma5AndLessKeywords + " let const class extends export import yield";

var isEcma6Keyword = makePredicate(ecma6AndLessKeywords);

var isEcma7Keyword = makePredicate(ecma6AndLessKeywords + " async await");

var isKeyword = isEcma5AndLessKeyword;

Expand Down Expand Up @@ -1539,6 +1551,7 @@
case _debugger: return parseDebuggerStatement(node);
case _do: return parseDoStatement(node);
case _for: return parseForStatement(node);
case _async: return parseAsync(node, true);
case _function: return parseFunctionStatement(node);
case _class: return parseClass(node, true);
case _if: return parseIfStatement(node);
Expand Down Expand Up @@ -1640,7 +1653,46 @@

function parseFunctionStatement(node) {
next();
return parseFunction(node, true);
return parseFunction(node, true, false);
}

function parseAsync(node, isStatement) {
if (options.ecmaVersion < 7) {
unexpected();
}

next();

switch (tokType) {
case _function:
next();
return parseFunction(node, isStatement, true);

if (!isStatement) unexpected();

case _name:
var id = parseIdent(tokType !== _name);
if (eat(_arrow)) {
return parseArrowExpression(node, [id], true);
}

case _parenL:
var oldParenL = ++metParenL;
var exprList = [];
next();
if (tokType !== _parenR) {
var val = parseExpression();
exprList = val.type === "SequenceExpression" ? val.expressions : [val];
}
expect(_parenR);
// if '=>' follows '(...)', convert contents to arguments
if (metParenL === oldParenL && eat(_arrow)) {
return parseArrowExpression(node, exprList, true);
}

default:
unexpected();
}
}

function parseIfStatement(node) {
Expand Down Expand Up @@ -2023,6 +2075,9 @@
case _yield:
if (inGenerator) return parseYield();

case _await:
if (inAsync) return parseAwait();

case _name:
var start = storeCurrentPos();
var id = parseIdent(tokType !== _name);
Expand Down Expand Up @@ -2104,10 +2159,13 @@
case _braceL:
return parseObj();

case _async:
return parseAsync(startNode(), false);

case _function:
var node = startNode();
next();
return parseFunction(node, false);
return parseFunction(node, false, false);

case _class:
return parseClass(startNode(), false);
Expand Down Expand Up @@ -2192,7 +2250,11 @@
if (options.allowTrailingCommas && eat(_braceR)) break;
} else first = false;

var prop = startNode(), isGenerator;
var prop = startNode(), isGenerator, isAsync;
if (options.ecmaVersion >= 7) {
isAsync = eat(_async);
if (tokType === _star) unexpected();
}
if (options.ecmaVersion >= 6) {
prop.method = false;
prop.shorthand = false;
Expand All @@ -2205,13 +2267,13 @@
} else if (options.ecmaVersion >= 6 && tokType === _parenL) {
prop.kind = "init";
prop.method = true;
prop.value = parseMethod(isGenerator);
prop.value = parseMethod(isGenerator, isAsync);
} else if (options.ecmaVersion >= 5 && !prop.computed && prop.key.type === "Identifier" &&
(prop.key.name === "get" || prop.key.name === "set")) {
if (isGenerator) unexpected();
if (isGenerator || isAsync) unexpected();
prop.kind = prop.key.name;
parsePropertyName(prop);
prop.value = parseMethod(false);
prop.value = parseMethod(false, false);
} else if (options.ecmaVersion >= 6 && !prop.computed && prop.key.type === "Identifier") {
prop.kind = "init";
prop.value = prop.key;
Expand Down Expand Up @@ -2240,22 +2302,26 @@

// Initialize empty function node.

function initFunction(node) {
function initFunction(node, isAsync) {
node.id = null;
node.params = [];
if (options.ecmaVersion >= 6) {
node.defaults = [];
node.rest = null;
node.generator = false;
}
if (options.ecmaVersion >= 7) {
node.async = isAsync;
}
}

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

function parseFunction(node, isStatement, allowExpressionBody) {
initFunction(node);
function parseFunction(node, isStatement, isAsync, allowExpressionBody) {
initFunction(node, isAsync);
if (options.ecmaVersion >= 6) {
if (node.async && tokType === _star) unexpected();
node.generator = eat(_star);
}
if (isStatement || tokType === _name) {
Expand All @@ -2268,9 +2334,9 @@

// Parse object or class method.

function parseMethod(isGenerator) {
function parseMethod(isGenerator, isAsync) {
var node = startNode();
initFunction(node);
initFunction(node, isAsync);
parseFunctionParams(node);
var allowExpressionBody;
if (options.ecmaVersion >= 6) {
Expand All @@ -2285,8 +2351,8 @@

// Parse arrow function expression with given parameters.

function parseArrowExpression(node, params) {
initFunction(node);
function parseArrowExpression(node, params, isAsync) {
initFunction(node, isAsync);

var defaults = node.defaults, hasDefaults = false;

Expand Down Expand Up @@ -2355,6 +2421,8 @@
function parseFunctionBody(node, allowExpression) {
var isExpression = allowExpression && tokType !== _braceL;

var oldInAsync = inAsync;
inAsync = node.async;
if (isExpression) {
node.body = parseExpression(true);
node.expression = true;
Expand All @@ -2367,6 +2435,7 @@
node.expression = false;
inFunction = oldInFunc; inGenerator = oldInGen; labels = oldLabels;
}
inAsync = oldInAsync;

// If this is a strict mode function, verify that argument names
// are not repeated, and it does not try to bind the words `eval`
Expand Down Expand Up @@ -2400,17 +2469,22 @@
} else {
method['static'] = false;
}
var isAsync = false;
if (options.ecmaVersion >= 7) {
isAsync = eat(_async);
if (tokType === _star) unexpected();
}
var isGenerator = eat(_star);
parsePropertyName(method);
if (tokType === _name && !method.computed && method.key.type === "Identifier" &&
(method.key.name === "get" || method.key.name === "set")) {
if (isGenerator) unexpected();
if (isGenerator || isAsync) unexpected();
method.kind = method.key.name;
parsePropertyName(method);
} else {
method.kind = "";
}
method.value = parseMethod(isGenerator);
method.value = parseMethod(isGenerator, isAsync);
classBody.body.push(finishNode(method, "MethodDefinition"));
eat(_semi);
}
Expand Down Expand Up @@ -2615,6 +2689,19 @@
return finishNode(node, "YieldExpression");
}

// Parses await expression inside async function.

function parseAwait() {
var node = startNode();
next();
if (eat(_semi) || canInsertSemicolon()) {
unexpected();
}
node.delegate = eat(_star);
node.argument = parseExpression(true);
return finishNode(node, "AwaitExpression");
}

// Parses array and generator comprehensions.

function parseComprehension(node, isGenerator) {
Expand Down

0 comments on commit ac42c10

Please sign in to comment.