From b7219ac489e47091f17091a08d7ef50980d68972 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Sat, 13 Feb 2021 20:26:43 +0000 Subject: support `import` statements (#4646) --- lib/parse.js | 50 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) (limited to 'lib/parse.js') diff --git a/lib/parse.js b/lib/parse.js index 27803f2e..e287c40e 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -47,7 +47,7 @@ var KEYWORDS = "break case catch const continue debugger default delete do else finally for function if in instanceof let new return switch throw try typeof var void while with"; var KEYWORDS_ATOM = "false null true"; var RESERVED_WORDS = [ - "await abstract boolean byte char class double enum export extends final float goto implements import int interface long native package private protected public short static super synchronized this throws transient volatile yield", + "abstract async await boolean byte char class double enum export extends final float goto implements import int interface long native package private protected public short static super synchronized this throws transient volatile yield", KEYWORDS_ATOM, KEYWORDS, ].join(" "); @@ -844,6 +844,9 @@ function parse($TEXT, options) { case "await": if (S.in_async) return simple_statement(); break; + case "import": + next(); + return import_(); case "yield": if (S.in_generator) return simple_statement(); break; @@ -1272,6 +1275,51 @@ function parse($TEXT, options) { }); } + function import_() { + var all = null; + var def = as_symbol(AST_SymbolImport, true); + var props = null; + if (def ? (def.key = "", is("punc", ",") && next()) : !is("string")) { + if (is("operator", "*")) { + next(); + expect_token("name", "as"); + all = as_symbol(AST_SymbolImport); + all.key = "*"; + } else { + expect("{"); + props = []; + while (is("name") || is_identifier_string(S.token.value)) { + var alias; + if (is_token(peek(), "name", "as")) { + var key = S.token.value; + next(); + next(); + alias = as_symbol(AST_SymbolImport); + alias.key = key; + } else { + alias = as_symbol(AST_SymbolImport); + alias.key = alias.name; + } + props.push(alias); + if (!is("punc", "}")) expect(","); + } + expect("}"); + } + } + if (all || def || props) expect_token("name", "from"); + if (!is("string")) unexpected(); + var path = S.token; + next(); + semicolon(); + return new AST_Import({ + all: all, + default: def, + path: path.value, + properties: props, + quote: path.quote, + }); + } + function block_() { expect("{"); var a = []; -- cgit v1.2.3