diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2021-02-08 20:28:23 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-02-09 04:28:23 +0800 |
commit | e13d1e996909f68ee643df17fd7d87773c3e82a5 (patch) | |
tree | b519e7a0cd2c3150de25964aaa3399be135eaa7a /lib/parse.js | |
parent | aedc1e7fc9cab772734d559e149c0b4f70454321 (diff) | |
download | tracifyjs-e13d1e996909f68ee643df17fd7d87773c3e82a5.tar.gz tracifyjs-e13d1e996909f68ee643df17fd7d87773c3e82a5.zip |
support `for [await]...of` statements (#4627)
Diffstat (limited to 'lib/parse.js')
-rw-r--r-- | lib/parse.js | 27 |
1 files changed, 19 insertions, 8 deletions
diff --git a/lib/parse.js b/lib/parse.js index d5098ff5..c8f63d80 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -1039,6 +1039,7 @@ function parse($TEXT, options) { } function for_() { + var await = is("name", "await") && next(); expect("("); var init = null; if (!is("punc", ";")) { @@ -1049,16 +1050,26 @@ function parse($TEXT, options) { : is("keyword", "var") ? (next(), var_(true)) : expression(true); - if (is("operator", "in")) { + var ctor; + if (await) { + expect_token("name", "of"); + ctor = AST_ForAwaitOf; + } else if (is("operator", "in")) { + next(); + ctor = AST_ForIn; + } else if (is("name", "of")) { + next(); + ctor = AST_ForOf; + } + if (ctor) { if (init instanceof AST_Definitions) { if (init.definitions.length > 1) { - token_error(init.start, "Only one variable declaration allowed in for..in loop"); + token_error(init.start, "Only one variable declaration allowed in for..in/of loop"); } } else if (!(is_assignable(init) || (init = to_destructured(init)) instanceof AST_Destructured)) { - token_error(init.start, "Invalid left-hand side in for..in loop"); + token_error(init.start, "Invalid left-hand side in for..in/of loop"); } - next(); - return for_in(init); + return for_enum(ctor, init); } } return regular_for(init); @@ -1078,10 +1089,10 @@ function parse($TEXT, options) { }); } - function for_in(init) { + function for_enum(ctor, init) { var obj = expression(); expect(")"); - return new AST_ForIn({ + return new ctor({ init : init, object : obj, body : in_loop(statement) @@ -1523,7 +1534,7 @@ function parse($TEXT, options) { func.end = prev(); return subscripts(func, allow_calls); } - if (is("name")) { + if (is("name") && is_token(peek(), "punc", "=>")) { start = S.token; sym = _make_symbol(AST_SymbolRef, start); next(); |