From 0473c368e615e94bf0d3d9c23c98349f7ee9f8c6 Mon Sep 17 00:00:00 2001 From: Ingvar Stepanyan Date: Wed, 18 Mar 2015 13:42:49 +0200 Subject: [PATCH] [es6][estree] Add support for `sourceType: script|module` modes. + Fix list of keywords and reserved words in ES6. --- README.md | 3 +++ acorn.js | 42 ++++++++++++++++++++++++------------- acorn_loose.js | 12 +++++------ test/tests-harmony.js | 49 +++++++++++++++++++++++++++++++++---------- 4 files changed, 75 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index b685917d11..3ab7882203 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,9 @@ object referring to that same position. either 3, 5, or 6. This influences support for strict mode, the set of reserved words, and support for new syntax features. Default is 5. +- **sourceType**: Indicate the mode the code should be parsed in. Can be + either `"script"` or `"module"`. + - **onInsertedSemicolon**: If given a callback, that callback will be called whenever a missing semicolon is inserted by the parser. The callback will be given the character offset of the point where the diff --git a/acorn.js b/acorn.js index 5c0412a068..89a8818415 100644 --- a/acorn.js +++ b/acorn.js @@ -51,6 +51,8 @@ // mode, the set of reserved words, support for getters and // setters and other features. ecmaVersion: 5, + // Source type ("script" or "module") for different semantics + sourceType: "script", // `onInsertedSemicolon` can be a callback that will be called // when a semicolon is automatically inserted. It will be passed // th position of the comma as an offset, and if `locations` is @@ -342,6 +344,7 @@ kw("with"); kw("new", beforeExpr); kw("this"); + kw("super"); kw("class"); kw("extends", beforeExpr); kw("export"); @@ -412,6 +415,10 @@ var isReservedWord5 = makePredicate("class enum extends super const export import"); + // ECMAScript 6 reserved words. + + var isReservedWord6 = makePredicate("enum await"); + // The additional reserved words in strict mode. var isStrictReservedWord = makePredicate("implements interface let package private protected public static yield"); @@ -426,7 +433,7 @@ var isEcma5AndLessKeyword = makePredicate(ecma5AndLessKeywords); - var isEcma6Keyword = makePredicate(ecma5AndLessKeywords + " let const class extends export import yield"); + var isEcma6Keyword = makePredicate(ecma5AndLessKeywords + " let const class extends export import yield super"); // ## Character categories @@ -543,6 +550,7 @@ this.loadPlugins(this.options.plugins); this.sourceFile = this.options.sourceFile || null; this.isKeyword = this.options.ecmaVersion >= 6 ? isEcma6Keyword : isEcma5AndLessKeyword; + this.isReservedWord = this.options.ecmaVersion === 3 ? isReservedWord3 : this.options.ecmaVersion === 5 ? isReservedWord5 : isReservedWord6; this.input = String(input); // Set up token state @@ -578,9 +586,11 @@ this.context = [tc.b_stat]; this.exprAllowed = true; - // Flags to track whether we are in strict mode, a function, a - // generator. - this.strict = this.inFunction = this.inGenerator = false; + // Figure out if it's a module code. + this.strict = this.inModule = this.options.sourceType === "module"; + + // Flags to track whether we are in a function, a generator. + this.inFunction = this.inGenerator = false; // Labels in scope. this.labels = []; @@ -946,6 +956,7 @@ } if (next == 33 && code == 60 && this.input.charCodeAt(this.pos + 2) == 45 && this.input.charCodeAt(this.pos + 3) == 45) { + if (this.inModule) unexpected(); // `