diff options
Diffstat (limited to 'lib/parse.js')
-rw-r--r-- | lib/parse.js | 117 |
1 files changed, 114 insertions, 3 deletions
diff --git a/lib/parse.js b/lib/parse.js index e287c40e..c6d7cb6f 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -844,6 +844,9 @@ function parse($TEXT, options) { case "await": if (S.in_async) return simple_statement(); break; + case "export": + next(); + return export_(); case "import": next(); return import_(); @@ -1275,6 +1278,115 @@ function parse($TEXT, options) { }); } + function is_alias() { + return is("name") || is_identifier_string(S.token.value); + } + + function export_() { + if (is("operator", "*")) { + next(); + var alias = "*"; + if (is("name", "as")) { + next(); + if (!is_alias()) expect_token("name"); + alias = S.token.value; + next(); + } + expect_token("name", "from"); + var path = S.token; + expect_token("string"); + semicolon(); + return new AST_ExportForeign({ + aliases: [ alias ], + keys: [ "*" ], + path: path.value, + quote: path.quote, + }); + } + if (is("punc", "{")) { + next(); + var aliases = []; + var keys = []; + while (is_alias()) { + var key = S.token; + next(); + keys.push(key); + if (is("name", "as")) { + next(); + if (!is_alias()) expect_token("name"); + aliases.push(S.token.value); + next(); + } else { + aliases.push(key.value); + } + if (!is("punc", "}")) expect(","); + } + expect("}"); + if (is("name", "from")) { + next(); + var path = S.token; + expect_token("string"); + semicolon(); + return new AST_ExportForeign({ + aliases: aliases, + keys: keys.map(function(token) { + return token.value; + }), + path: path.value, + quote: path.quote, + }); + } + semicolon(); + return new AST_ExportReferences({ + properties: keys.map(function(token, index) { + if (!is_token(token, "name")) token_error(token, "Name expected"); + var sym = _make_symbol(AST_SymbolExport, token); + sym.alias = aliases[index]; + return sym; + }), + }); + } + if (is("keyword", "default")) { + next(); + var body = expression(); + semicolon(); + return new AST_ExportDefault({ body: body }); + } + return new AST_ExportDeclaration({ body: export_decl() }); + } + + var export_decl = embed_tokens(function() { + switch (S.token.value) { + case "async": + next(); + expect_token("keyword", "function"); + if (!is("operator", "*")) return function_(AST_AsyncDefun); + next(); + return function_(AST_AsyncGeneratorDefun); + case "const": + next(); + var node = const_(); + semicolon(); + return node; + case "function": + next(); + if (!is("operator", "*")) return function_(AST_Defun); + next(); + return function_(AST_GeneratorDefun); + case "let": + next(); + var node = let_(); + semicolon(); + return node; + case "var": + next(); + var node = var_(); + semicolon(); + return node; + } + unexpected(); + }); + function import_() { var all = null; var def = as_symbol(AST_SymbolImport, true); @@ -1288,7 +1400,7 @@ function parse($TEXT, options) { } else { expect("{"); props = []; - while (is("name") || is_identifier_string(S.token.value)) { + while (is_alias()) { var alias; if (is_token(peek(), "name", "as")) { var key = S.token.value; @@ -1307,9 +1419,8 @@ function parse($TEXT, options) { } } if (all || def || props) expect_token("name", "from"); - if (!is("string")) unexpected(); var path = S.token; - next(); + expect_token("string"); semicolon(); return new AST_Import({ all: all, |