From 7ac3519dc40bb6f6ab979c9cd35fd585f25d1c25 Mon Sep 17 00:00:00 2001 From: Li Jin Date: Thu, 23 Jan 2020 14:35:14 +0800 Subject: fix Moonscript issue 156, update new 'import' functions. --- src/MoonP/moon_ast.cpp | 4 + src/MoonP/moon_ast.h | 25 +++- src/MoonP/moon_compiler.cpp | 348 ++++++++++++++++++++++++++------------------ src/MoonP/moon_parser.cpp | 21 ++- 4 files changed, 250 insertions(+), 148 deletions(-) (limited to 'src') diff --git a/src/MoonP/moon_ast.cpp b/src/MoonP/moon_ast.cpp index f3fe31e..8b8a674 100644 --- a/src/MoonP/moon_ast.cpp +++ b/src/MoonP/moon_ast.cpp @@ -29,6 +29,10 @@ AST_IMPL(Seperator) AST_IMPL(NameList) AST_IMPL(Local) AST_IMPL(colon_import_name) +AST_IMPL(import_literal_inner) +AST_IMPL(ImportLiteral) +AST_IMPL(ImportFrom) +AST_IMPL(ImportAs) AST_IMPL(Import) AST_IMPL(ExpListLow) AST_IMPL(ExpList) diff --git a/src/MoonP/moon_ast.h b/src/MoonP/moon_ast.h index 8b80af3..f8d2099 100644 --- a/src/MoonP/moon_ast.h +++ b/src/MoonP/moon_ast.h @@ -106,12 +106,33 @@ AST_NODE(colon_import_name, "colon_import_name"_id) AST_END(colon_import_name) class Exp_t; +class TableLit_t; -AST_NODE(Import, "Import"_id) +AST_LEAF(import_literal_inner, "import_literal_inner"_id) +AST_END(import_literal_inner) + +AST_NODE(ImportLiteral, "ImportLiteral"_id) + ast_ptr sep; + ast_sel_list inners; + AST_MEMBER(ImportLiteral, &sep, &inners) +AST_END(ImportLiteral) + +AST_NODE(ImportAs, "ImportAs"_id) + ast_ptr literal; + ast_sel target; + AST_MEMBER(ImportAs, &literal, &target) +AST_END(ImportAs) + +AST_NODE(ImportFrom, "ImportFrom"_id) ast_ptr sep; ast_sel_list names; ast_ptr exp; - AST_MEMBER(Import, &sep, &names, &exp) + AST_MEMBER(ImportFrom, &sep, &names, &exp) +AST_END(ImportFrom) + +AST_NODE(Import, "Import"_id) + ast_sel content; + AST_MEMBER(Import, &content) AST_END(Import) AST_NODE(ExpListLow, "ExpListLow"_id) diff --git a/src/MoonP/moon_compiler.cpp b/src/MoonP/moon_compiler.cpp index 9164f6a..264ddc7 100644 --- a/src/MoonP/moon_compiler.cpp +++ b/src/MoonP/moon_compiler.cpp @@ -15,7 +15,6 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI #include #include #include -#include using namespace std::string_view_literals; #include "MoonP/parser.hpp" #include "MoonP/moon_ast.h" @@ -698,9 +697,9 @@ private: auto value = simpleValue->value.get(); bool specialSingleValue = true; switch (value->getId()) { - case "If"_id: transformIf(static_cast(value), out); break; - case "ClassDecl"_id: transformClassDecl(static_cast(value), out); break; - case "Unless"_id: transformUnless(static_cast(value), out); break; + case "If"_id: transformIf(static_cast(value), out, ExpUsage::Common); break; + case "ClassDecl"_id: transformClassDecl(static_cast(value), out, ExpUsage::Common); break; + case "Unless"_id: transformUnless(static_cast(value), out, ExpUsage::Common); break; case "Switch"_id: transformSwitch(static_cast(value), out); break; case "With"_id: transformWith(static_cast(value), out); break; case "ForEach"_id: transformForEach(static_cast(value), out); break; @@ -900,8 +899,8 @@ private: } }); switch (value->getId()) { - case "If"_id: transformIf(static_cast(value), temp); break; - case "Unless"_id: transformUnless(static_cast(value), temp); break; + case "If"_id: transformIf(static_cast(value), temp, ExpUsage::Common); break; + case "Unless"_id: transformUnless(static_cast(value), temp, ExpUsage::Common); break; } out.push_back(join(temp)); return; @@ -941,14 +940,14 @@ private: case "Comprehension"_id: { auto expList = assignment->expList.get(); std::string preDefine = getPredefine(assignment); - transformCompInPlace(static_cast(value), expList, out); + transformComprehension(static_cast(value), out, ExpUsage::Assignment, expList); out.back() = preDefine + out.back(); return; } case "TblComprehension"_id: { auto expList = assignment->expList.get(); std::string preDefine = getPredefine(assignment); - transformTblCompInPlace(static_cast(value), expList, out); + transformTblComprehension(static_cast(value), out, ExpUsage::Assignment, expList); out.back() = preDefine + out.back(); return; } @@ -1123,7 +1122,11 @@ private: case "variable_pair"_id: { auto vp = static_cast(pair); auto name = toString(vp->name); - pairs.push_back({true, name, s("."sv) + name}); + if (State::keywords.find(name) != State::keywords.end()) { + pairs.push_back({true, name, s("[\""sv) + name + s("\"]"sv)}); + } else { + pairs.push_back({true, name, s("."sv) + name}); + } break; } case "normal_pair"_id: { @@ -1136,9 +1139,15 @@ private: if (ast_cast(item) || item->getByPath()) { auto subPairs = destructFromExp(exp); + auto name = toString(key); for (auto& p : subPairs) { - pairs.push_back({p.isVariable, p.name, - s("."sv) + toString(key) + p.structure}); + if (State::keywords.find(name) != State::keywords.end()) { + pairs.push_back({p.isVariable, p.name, + s("[\""sv) + name + s("\"]"sv) + p.structure}); + } else { + pairs.push_back({p.isVariable, p.name, + s("."sv) + name + p.structure}); + } } } else { bool lintGlobal = _config.lintGlobalVariable; @@ -1151,11 +1160,20 @@ private: varName = std::move(temp.back()); } _config.lintGlobalVariable = lintGlobal; - pairs.push_back({ - isVariable, - varName, - s("."sv) + toString(key) - }); + auto name = toString(key); + if (State::keywords.find(name) != State::keywords.end()) { + pairs.push_back({ + isVariable, + varName, + s("[\""sv) + name + s("\"]"sv) + }); + } else { + pairs.push_back({ + isVariable, + varName, + s("."sv) + name + }); + } } break; } @@ -1333,7 +1351,7 @@ private: } } - void transformCond(const node_container& nodes, str_list& out, ExpUsage usage = ExpUsage::Common, bool unless = false) { + void transformCond(const node_container& nodes, str_list& out, ExpUsage usage, bool unless = false) { std::vector> ns(false); for (auto it = nodes.rbegin(); it != nodes.rend(); ++it) { ns.push_back(*it); @@ -1497,11 +1515,11 @@ private: out.push_back(join(temp)); } - void transformIf(If_t* ifNode, str_list& out, ExpUsage usage = ExpUsage::Common) { + void transformIf(If_t* ifNode, str_list& out, ExpUsage usage) { transformCond(ifNode->nodes.objects(), out, usage); } - void transformUnless(Unless_t* unless, str_list& out, ExpUsage usage = ExpUsage::Common) { + void transformUnless(Unless_t* unless, str_list& out, ExpUsage usage) { transformCond(unless->nodes.objects(), out, usage, true); } @@ -1539,7 +1557,7 @@ private: case "simple_table"_id: transform_simple_table(static_cast(item), out); break; case "ChainValue"_id: { auto chainValue = static_cast(item); - transformChainValue(chainValue, out); + transformChainValue(chainValue, out, ExpUsage::Closure); break; } case "String"_id: transformString(static_cast(item), out); break; @@ -1597,9 +1615,9 @@ private: case "While"_id: transformWhileClosure(static_cast(value), out); break; case "Do"_id: transformDoClosure(static_cast(value), out); break; case "unary_exp"_id: transform_unary_exp(static_cast(value), out); break; - case "TblComprehension"_id: transformTblCompClosure(static_cast(value), out); break; + case "TblComprehension"_id: transformTblComprehension(static_cast(value), out, ExpUsage::Closure); break; case "TableLit"_id: transformTableLit(static_cast(value), out); break; - case "Comprehension"_id: transformCompClosure(static_cast(value), out); break; + case "Comprehension"_id: transformComprehension(static_cast(value), out, ExpUsage::Closure); break; case "FunLit"_id: transformFunLit(static_cast(value), out); break; case "Num"_id: transformNum(static_cast(value), out); break; default: break; @@ -1771,10 +1789,10 @@ private: auto value = simpleValue->value.get(); switch (value->getId()) { case "Comprehension"_id: - transformCompReturn(static_cast(value), out); + transformComprehension(static_cast(value), out, ExpUsage::Return); return; case "TblComprehension"_id: - transformTblCompReturn(static_cast(value), out); + transformTblComprehension(static_cast(value), out, ExpUsage::Return); return; case "With"_id: transformWith(static_cast(value), out, nullptr, true); @@ -2223,12 +2241,14 @@ private: auto funLit = toAst(s("(...)-> "sv) + fnVar + s(" "sv) + baseVar + s(", ..."sv), Exp, x); switch (usage) { case ExpUsage::Closure: - case ExpUsage::Return: - transformExp(funLit, temp); - _buf << temp.front(); - _buf << *(++temp.begin()); - _buf << indent() << "return " << temp.back() << nll(x); + case ExpUsage::Return: { + auto returnNode = x->new_ptr(); + auto expListLow = x->new_ptr(); + expListLow->exprs.push_back(funLit); + returnNode->valueList.set(expListLow); + transformReturn(returnNode, temp); break; + } case ExpUsage::Assignment: { auto assign = x->new_ptr(); assign->values.push_back(funLit); @@ -2395,7 +2415,7 @@ private: chainValue->items.push_back(callable); auto invoke = x->new_ptr(); chainValue->items.push_back(invoke); - transformChainValue(chainValue, out); + transformChainValue(chainValue, out, ExpUsage::Closure); return; } transformColonChainItem(colonItem, temp); @@ -2439,7 +2459,7 @@ private: } } - void transformChainValue(ChainValue_t* chainValue, str_list& out, ExpUsage usage = ExpUsage::Closure, ExpList_t* assignList = nullptr) { + void transformChainValue(ChainValue_t* chainValue, str_list& out, ExpUsage usage, ExpList_t* assignList = nullptr) { const auto& chainList = chainValue->items.objects(); if (transformChainEndWithEOP(chainList, out, usage, assignList)) { return; @@ -2548,12 +2568,21 @@ private: out.push_back(clearBuf()); } - void transformComprehension(Comprehension_t* comp, str_list& out) { + void transformComprehension(Comprehension_t* comp, str_list& out, ExpUsage usage, ExpList_t* assignList = nullptr) { + auto x = comp; + switch (usage) { + case ExpUsage::Closure: + case ExpUsage::Assignment: + pushScope(); + break; + default: + break; + } str_list temp; - std::string accum = getUnusedName("_accum_"); - std::string len = getUnusedName("_len_"); - addToScope(accum); - addToScope(len); + std::string accumVar = getUnusedName("_accum_"sv); + std::string lenVar = getUnusedName("_len_"sv); + addToScope(accumVar); + addToScope(lenVar); auto compInner = comp->forLoop.get(); for (auto item : compInner->items.objects()) { switch (item->getId()) { @@ -2570,61 +2599,60 @@ private: break; } } - transformExp(comp->value.to(), temp); - auto value = temp.back(); + { + auto assignLeft = toAst(accumVar + s("["sv) + lenVar + s("]"sv), ExpList, x); + auto assign = x->new_ptr(); + assign->values.push_back(comp->value); + auto assignment = x->new_ptr(); + assignment->expList.set(assignLeft); + assignment->action.set(assign); + transformAssignment(assignment, temp); + } + auto assignStr = temp.back(); temp.pop_back(); for (size_t i = 0; i < compInner->items.objects().size(); ++i) { popScope(); } - _buf << indent() << "local "sv << accum << " = { }"sv << nll(comp); - _buf << indent() << "local "sv << len << " = 1"sv << nll(comp); + _buf << indent() << "local "sv << accumVar << " = { }"sv << nll(comp); + _buf << indent() << "local "sv << lenVar << " = 1"sv << nll(comp); _buf << join(temp); - _buf << indent(int(temp.size())) << accum << "["sv << len << "] = "sv << value << nll(comp); - _buf << indent(int(temp.size())) << len << " = "sv << len << " + 1"sv << nll(comp); + _buf << assignStr; + _buf << indent(int(temp.size())) << lenVar << " = "sv << lenVar << " + 1"sv << nll(comp); for (int ind = int(temp.size()) - 1; ind > -1 ; --ind) { _buf << indent(ind) << "end"sv << nll(comp); } - out.push_back(accum); - out.push_back(clearBuf()); - } - - void transformCompInPlace(Comprehension_t* comp, ExpList_t* expList, str_list& out) { - auto x = comp; - str_list temp; - auto ind = indent(); - pushScope(); - transformComprehension(comp, temp); - auto assign = x->new_ptr(); - assign->values.push_back(toAst(temp.front(), Exp, x)); - auto assignment = x->new_ptr(); - assignment->expList.set(expList); - assignment->action.set(assign); - transformAssignment(assignment, temp); - out.push_back( - ind + s("do"sv) + nll(comp) + - *(++temp.begin()) + - temp.back()); - popScope(); - out.back() = out.back() + indent() + s("end"sv) + nlr(comp); - } - - void transformCompReturn(Comprehension_t* comp, str_list& out) { - str_list temp; - transformComprehension(comp, temp); - out.push_back(temp.back() + indent() + s("return "sv) + temp.front() + nlr(comp)); - } - - void transformCompClosure(Comprehension_t* comp, str_list& out) { - str_list temp; - std::string before = s("(function()"sv) + nll(comp); - pushScope(); - transformComprehension(comp, temp); - out.push_back( - before + - temp.back() + - indent() + s("return "sv) + temp.front() + nlr(comp)); - popScope(); - out.back() = out.back() + indent() + s("end)()"sv); + switch (usage) { + case ExpUsage::Common: + break; + case ExpUsage::Closure: { + out.push_back(clearBuf()); + out.back().append(indent() + s("return "sv) + accumVar + nlr(comp)); + popScope(); + out.back().insert(0, s("(function()"sv) + nll(comp)); + out.back().append(indent() + s("end)()"sv)); + break; + } + case ExpUsage::Assignment: { + out.push_back(clearBuf()); + auto assign = x->new_ptr(); + assign->values.push_back(toAst(accumVar, Exp, x)); + auto assignment = x->new_ptr(); + assignment->expList.set(assignList); + assignment->action.set(assign); + transformAssignment(assignment, temp); + popScope(); + out.back() = indent() + s("do"sv) + nll(comp) + + out.back() + temp.back() + + indent() + s("end"sv) + nlr(comp); + break; + } + case ExpUsage::Return: + out.push_back(clearBuf()); + out.back().append(indent() + s("return "sv) + accumVar + nlr(comp)); + break; + default: + break; + } } void transformForEachHead(AssignableNameList_t* nameList, ast_node* loopTarget, str_list& out) { @@ -2698,7 +2726,7 @@ private: if (listVar.empty()) { listVar = getUnusedName("_list_"); varBefore.push_back(listVar); - transformChainValue(chain, temp); + transformChainValue(chain, temp, ExpUsage::Closure); _buf << indent() << "local "sv << listVar << " = "sv << temp.back() << nll(nameList); } std::string maxVar; @@ -2862,9 +2890,9 @@ private: std::string transformForInner(For_t* forNode, str_list& out) { auto x = forNode; - std::string accum = getUnusedName("_accum_"); + std::string accum = getUnusedName("_accum_"sv); addToScope(accum); - std::string len = getUnusedName("_len_"); + std::string len = getUnusedName("_len_"sv); addToScope(len); _buf << indent() << "local "sv << accum << " = { }"sv << nll(forNode); _buf << indent() << "local "sv << len << " = 1"sv << nll(forNode); @@ -2930,9 +2958,9 @@ private: std::string transformForEachInner(ForEach_t* forEach, str_list& out) { auto x = forEach; - std::string accum = getUnusedName("_accum_"); + std::string accum = getUnusedName("_accum_"sv); addToScope(accum); - std::string len = getUnusedName("_len_"); + std::string len = getUnusedName("_len_"sv); addToScope(len); _buf << indent() << "local "sv << accum << " = { }"sv << nll(forEach); _buf << indent() << "local "sv << len << " = 1"sv << nll(forEach); @@ -3107,7 +3135,7 @@ private: out.push_back(join(temp)); } - void transformClassDecl(ClassDecl_t* classDecl, str_list& out, ExpUsage usage = ExpUsage::Common, ExpList_t* expList = nullptr) { + void transformClassDecl(ClassDecl_t* classDecl, str_list& out, ExpUsage usage, ExpList_t* expList = nullptr) { str_list temp; auto x = classDecl; auto body = classDecl->body.get(); @@ -3482,7 +3510,7 @@ private: } } if (withVar.empty()) { - withVar = getUnusedName("_with_"); + withVar = getUnusedName("_with_"sv); auto assignment = x->new_ptr(); assignment->expList.set(toAst(withVar, ExpList, x)); auto assign = x->new_ptr(); @@ -3524,7 +3552,7 @@ private: } else { withVar = singleVariableFrom(with->valueList); if (withVar.empty()) { - withVar = getUnusedName("_with_"); + withVar = getUnusedName("_with_"sv); auto assignment = x->new_ptr(); assignment->expList.set(toAst(withVar, ExpList, x)); auto assign = x->new_ptr(); @@ -3621,7 +3649,7 @@ private: markVarExported(ExportMode::Any, true); addExportedVar(toString(classDecl->name->item)); } - transformClassDecl(classDecl, out); + transformClassDecl(classDecl, out, ExpUsage::Common); break; } case "export_op"_id: @@ -3690,9 +3718,18 @@ private: transformTable(table, table->pairs.objects(), out); } - void transformTblComprehension(TblComprehension_t* comp, str_list& out) { + void transformTblComprehension(TblComprehension_t* comp, str_list& out, ExpUsage usage, ExpList_t* assignList = nullptr) { + switch (usage) { + case ExpUsage::Closure: + case ExpUsage::Assignment: + pushScope(); + break; + default: + break; + } + auto x = comp; str_list kv; - std::string tbl = getUnusedName("_tbl_"); + std::string tbl = getUnusedName("_tbl_"sv); addToScope(tbl); str_list temp; auto compInner = comp->forLoop.get(); @@ -3722,8 +3759,8 @@ private: _buf << join(temp); pushScope(); if (!comp->value) { - auto keyVar = getUnusedName("_key_"); - auto valVar = getUnusedName("_val_"); + auto keyVar = getUnusedName("_key_"sv); + auto valVar = getUnusedName("_val_"sv); _buf << indent(int(temp.size()) - 1) << "local "sv << keyVar << ", "sv << valVar << " = "sv << kv.front() << nll(comp); kv.front() = keyVar; kv.push_back(valVar); @@ -3735,48 +3772,33 @@ private: popScope(); _buf << indent() << "end"sv << nll(comp); out.push_back(tbl); - out.push_back(clearBuf()); - } - - void transformTblCompInPlace(TblComprehension_t* comp, ExpList_t* expList, str_list& out) { - auto x = comp; - str_list temp; - auto ind = indent(); - pushScope(); - transformTblComprehension(comp, temp); - auto assign = x->new_ptr(); - assign->values.push_back(toAst(temp.front(), Exp, x)); - auto assignment = x->new_ptr(); - assignment->expList.set(expList); - assignment->action.set(assign); - transformAssignment(assignment, temp); - out.push_back( - ind + s("do"sv) + nll(comp) + - *(++temp.begin()) + - temp.back()); - popScope(); - out.back() = out.back() + indent() + s("end"sv) + nlr(comp); - } - - void transformTblCompReturn(TblComprehension_t* comp, str_list& out) { - str_list temp; - transformTblComprehension(comp, temp); - out.push_back(temp.back() + indent() + s("return "sv) + temp.front() + nlr(comp)); - } - - void transformTblCompClosure(TblComprehension_t* comp, str_list& out) { - str_list temp; - std::string before = s("(function()"sv) + nll(comp); - pushScope(); - transformTblComprehension(comp, temp); - const auto& tbVar = temp.front(); - const auto& compBody = temp.back(); - out.push_back( - before + - compBody + - indent() + s("return "sv) + tbVar + nlr(comp)); - popScope(); - out.back() = out.back() + indent() + s("end)()"sv); + switch (usage) { + case ExpUsage::Closure: + out.push_back(clearBuf() + indent() + s("return "sv) + tbl + nlr(comp)); + popScope(); + out.back().insert(0, s("(function()"sv) + nll(comp)); + out.back().append(indent() + s("end)()"sv)); + break; + case ExpUsage::Assignment: { + out.push_back(clearBuf()); + auto assign = x->new_ptr(); + assign->values.push_back(toAst(tbl, Exp, x)); + auto assignment = x->new_ptr(); + assignment->expList.set(assignList); + assignment->action.set(assign); + transformAssignment(assignment, temp); + out.back().append(temp.back()); + popScope(); + out.back().insert(0, indent() + s("do"sv) + nll(comp)); + out.back().append(indent() + s("end"sv) + nlr(comp)); + break; + } + case ExpUsage::Return: + out.push_back(clearBuf() + indent() + s("return "sv) + tbl + nlr(comp)); + break; + default: + break; + } } void transformCompFor(CompFor_t* comp, str_list& out) { @@ -3823,7 +3845,7 @@ private: out.push_back(join(temp)); } - void transformImport(Import_t* import, str_list& out) { + void transformImportFrom(ImportFrom_t* import, str_list& out) { str_list temp; auto x = import; auto objVar = singleVariableFrom(import->exp); @@ -3917,6 +3939,54 @@ private: out.push_back(join(temp)); } + void transformImportAs(ImportAs_t* import, str_list& out) { + auto x = import; + if (!import->target) { + auto name = toString(import->literal->inners.back()); + replace(name, "-"sv, "_"sv); + replace(name, " "sv, "_"sv); + import->target.set(toAst(name, Variable, x)); + } + auto target = import->target.get(); + auto value = x->new_ptr(); + if (auto var = ast_cast(target)) { + auto callable = x->new_ptr(); + callable->item.set(var); + auto chainValue = x->new_ptr(); + chainValue->items.push_back(callable); + value->item.set(chainValue); + } else { + auto tableLit = ast_to(target); + auto simpleValue = x->new_ptr(); + simpleValue->value.set(tableLit); + value->item.set(simpleValue); + } + auto exp = x->new_ptr(); + exp->value.set(value); + auto assignList = x->new_ptr(); + assignList->exprs.push_back(exp); + auto assign = x->new_ptr(); + assign->values.push_back(toAst(s("require ") + toString(import->literal), Exp, x)); + auto assignment = x->new_ptr(); + assignment->expList.set(assignList); + assignment->action.set(assign); + transformAssignment(assignment, out); + } + + void transformImport(Import_t* import, str_list& out) { + auto content = import->content.get(); + switch (content->getId()) { + case "ImportAs"_id: + transformImportAs(static_cast(content), out); + break; + case "ImportFrom"_id: + transformImportFrom(static_cast(content), out); + break; + default: + break; + } + } + void transformWhileInPlace(While_t* whileNode, str_list& out, ExpList_t* expList = nullptr) { auto x = whileNode; str_list temp; diff --git a/src/MoonP/moon_parser.cpp b/src/MoonP/moon_parser.cpp index 7cc4129..d3c0ed2 100644 --- a/src/MoonP/moon_parser.cpp +++ b/src/MoonP/moon_parser.cpp @@ -26,9 +26,9 @@ std::unordered_set State::keywords = { "in", "local", "nil", "not", "or", "repeat", "return", "then", "true", "until", "while", // Lua keywords - "class", "continue", "export", "extends", "from", - "import", "switch", "unless", "using", "when", - "with" // Moon keywords + "as", "class", "continue", "export", "extends", + "from", "import", "switch", "unless", "using", + "when", "with" // Moon keywords }; rule plain_space = *set(" \t"); @@ -160,9 +160,16 @@ rule colon_import_name = sym('\\') >> Space >> Variable; rule ImportName = colon_import_name | Space >> Variable; rule ImportNameList = Seperator >> *SpaceBreak >> ImportName >> *((+SpaceBreak | sym(',') >> *SpaceBreak) >> ImportName); -extern rule Exp; +extern rule Exp, TableLit; -rule Import = key("import") >> ImportNameList >> *SpaceBreak >> key("from") >> Exp; +rule import_literal_inner = (range('a', 'z') | range('A', 'Z') | set("_-")) >> *(AlphaNum | '-'); +rule import_literal_chain = Seperator >> import_literal_inner >> *(expr('.') >> import_literal_inner); +rule ImportLiteral = sym('\'') >> import_literal_chain >> symx('\'') | sym('"') >> import_literal_chain >> symx('"'); + +rule ImportFrom = ImportNameList >> *SpaceBreak >> key("from") >> Exp; +rule ImportAs = ImportLiteral >> -(key("as") >> (Space >> Variable | TableLit)); + +rule Import = key("import") >> (ImportAs | ImportFrom); rule BreakLoop = (expr("break") | expr("continue")) >> not_(AlphaNum); extern rule ExpListLow, ExpList, Assign; @@ -301,12 +308,12 @@ rule Value = SimpleValue | simple_table | ChainValue | String; extern rule LuaString; rule single_string_inner = expr("\\'") | "\\\\" | not_(expr('\'')) >> Any; -rule SingleString = symx('\'') >> *single_string_inner >> sym('\''); +rule SingleString = symx('\'') >> *single_string_inner >> symx('\''); rule interp = symx("#{") >> Exp >> sym('}'); rule double_string_plain = expr("\\\"") | "\\\\" | not_(expr('"')) >> Any; rule double_string_inner = +(not_(interp) >> double_string_plain); rule double_string_content = double_string_inner | interp; -rule DoubleString = symx('"') >> Seperator >> *double_string_content >> sym('"'); +rule DoubleString = symx('"') >> Seperator >> *double_string_content >> symx('"'); rule String = Space >> (DoubleString | SingleString | LuaString); rule lua_string_open = '[' >> *expr('=') >> '['; -- cgit v1.2.3-55-g6feb