From 7c84db45fdc2d4c40943df17d42fc8b20d23c311 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Thu, 19 Mar 2015 01:48:44 +1100 Subject: [PATCH] more estree updates - finish flow parsing --- acorn.js | 75 +++++++++++++++++++++++++++++++++---------- plugins/flow.js | 43 +++++++++++++++++-------- plugins/jsx.js | 30 ++++++++--------- test/tests-babel.js | 1 + test/tests-flow.js | 8 ++--- test/tests-harmony.js | 49 +++++++++++++++++++++------- 6 files changed, 145 insertions(+), 61 deletions(-) diff --git a/acorn.js b/acorn.js index 4e0ceb83f0..9c5c8c6dcc 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 @@ -347,6 +349,7 @@ kw("with"); kw("new", beforeExpr); kw("this"); + kw("super"); kw("class"); kw("extends", beforeExpr); kw("export"); @@ -421,6 +424,10 @@ var isStrictReservedWord = makePredicate("implements interface let package private protected public static yield"); + // ECMAScript 6 reserved words. + + var isReservedWord6 = makePredicate("enum await"); + // The forbidden variable names in strict mode. var isStrictBadIdWord = makePredicate("eval arguments"); @@ -431,7 +438,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 @@ -548,6 +555,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 @@ -583,9 +591,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.inModule = this.options.sourceType === "module"; + this.strict = options.strictMode === false ? false : this.inModule; + // Flags to track whether we are in a function, a generator. + this.inFunction = this.inGenerator = this.inAsync = false; // Labels in scope. this.labels = []; @@ -623,6 +633,25 @@ this.nextToken(); }; + var STATE_KEYS = ["lastTokStartLoc", "lastTokEndLoc", "lastTokStart", "lastTokEnd", "startLoc", "endLoc", "start", "pos", "end", "type", "value"]; + + pp.getState = function () { + var state = {}; + for (var i = 0; i < STATE_KEYS.length; i++) { + var key = STATE_KEYS[i]; + state[key] = this[key]; + } + return state; + }; + + pp.lookahead = function() { + var old = this.getState(); + this.next(); + var curr = this.getState(); + for (var key in old) this[key] = old[key]; + return curr; + }; + pp.getToken = function() { this.next(); return new Token(this); @@ -964,6 +993,7 @@ } if (next == 33 && code == 60 && this.input.charCodeAt(this.pos + 2) == 45 && this.input.charCodeAt(this.pos + 3) == 45) { + if (this.inModule) unexpected(); // `