From 0777356cbe599b3f88bdfa476e3ffa64bb3a3a8c Mon Sep 17 00:00:00 2001 From: Li Jin Date: Thu, 15 Oct 2020 18:00:47 +0800 Subject: add a new macro type support to insert raw codes to output. --- src/MoonP/moon_ast.h | 10 +--------- src/MoonP/moon_compiler.cpp | 11 +++++++++-- src/MoonP/moon_parser.cpp | 19 ++++++++----------- src/MoonP/moon_parser.h | 2 -- 4 files changed, 18 insertions(+), 24 deletions(-) (limited to 'src') diff --git a/src/MoonP/moon_ast.h b/src/MoonP/moon_ast.h index dfe70ea..7e480b5 100644 --- a/src/MoonP/moon_ast.h +++ b/src/MoonP/moon_ast.h @@ -35,15 +35,7 @@ public: \ }; \ template<> constexpr int id() { return COUNTER_READ; } -AST_LEAF(Decimal) -AST_END(Decimal) - -AST_LEAF(Integer) -AST_END(Integer) - -AST_NODE(Num) - ast_sel num; - AST_MEMBER(Num, &num) +AST_LEAF(Num) AST_END(Num) AST_LEAF(Name) diff --git a/src/MoonP/moon_compiler.cpp b/src/MoonP/moon_compiler.cpp index f5835db..debc43a 100644 --- a/src/MoonP/moon_compiler.cpp +++ b/src/MoonP/moon_compiler.cpp @@ -53,7 +53,7 @@ inline std::string s(std::string_view sv) { return std::string(sv); } -const std::string_view version = "0.4.17"sv; +const std::string_view version = "0.4.18"sv; const std::string_view extension = "mp"sv; class MoonCompilerImpl { @@ -3070,6 +3070,8 @@ private: // to convert its whole text content str = _parser.toString(exp->backcalls.front()); } + } else if (auto lstr = ast_cast(arg)) { + str = _parser.toString(lstr->content); } else { bool multiLineStr = false; BLOCK_START @@ -3116,7 +3118,7 @@ private: std::tie(type, codes) = expandMacroStr(chainValue); std::string targetType(usage != ExpUsage::Common || chainList.size() > 2 ? "expr"sv : "block"sv); if (type == "lua"sv) { - if (targetType != "block"sv) { + if (!allowBlockMacroReturn && targetType != "block"sv) { throw std::logic_error(_info.errorMessage("lua macro can only be placed where block macro is allowed"sv, x)); } auto macroChunk = s("=(macro "sv) + _parser.toString(x->name) + ')'; @@ -3127,6 +3129,11 @@ private: throw std::logic_error(_info.errorMessage(err, x)); } return {nullptr, nullptr, std::move(codes)}; + } else if (type == "text"sv) { + if (!allowBlockMacroReturn && targetType != "block"sv) { + throw std::logic_error(_info.errorMessage("text macro can only be placed where block macro is allowed"sv, x)); + } + return {nullptr, nullptr, std::move(codes)}; } else if (!allowBlockMacroReturn && type != targetType) { throw std::logic_error(_info.errorMessage(s("macro type mismatch, "sv) + targetType + s(" expected, got "sv) + type, x)); } diff --git a/src/MoonP/moon_parser.cpp b/src/MoonP/moon_parser.cpp index 6485e46..e984997 100644 --- a/src/MoonP/moon_parser.cpp +++ b/src/MoonP/moon_parser.cpp @@ -51,22 +51,19 @@ MoonParser::MoonParser() { EmptyLine = SpaceBreak; AlphaNum = range('a', 'z') | range('A', 'Z') | range('0', '9') | '_'; Name = (range('a', 'z') | range('A', 'Z') | '_') >> *AlphaNum; - Decimal = ( + Num = ( + "0x" >> + +(range('0', '9') | range('a', 'f') | range('A', 'F')) >> + -(-set("uU") >> set("lL") >> set("lL")) + ) | ( + +range('0', '9') >> -set("uU") >> set("lL") >> set("lL") + ) | ( ( +range('0', '9') >> -('.' >> +range('0', '9')) ) | ( '.' >> +range('0', '9') ) ) >> -(set("eE") >> -expr('-') >> +range('0', '9')); - Integer = - ( - "0x" >> - +(range('0', '9') | range('a', 'f') | range('A', 'F')) >> - -(-set("uU") >> set("lL") >> set("lL")) - ) | ( - +range('0', '9') >> -set("uU") >> set("lL") >> set("lL") - ); - Num = Integer | Decimal; Cut = false_(); Seperator = true_(); @@ -513,7 +510,7 @@ MoonParser::MoonParser() { FunLit = -FnArgsDef >> Space >> fn_arrow >> -Body; MacroName = expr('$') >> Name; - macro_type = expr("expr") | expr("block") | expr("lua"); + macro_type = expr("expr") | expr("block") | expr("lua") | expr("text"); macro_args_def = sym('(') >> White >> -FnArgDefList >> White >> sym(')'); MacroLit = -macro_args_def >> Space >> expr("->") >> Body; Macro = key("macro") >> Space >> macro_type >> Space >> Name >> sym('=') >> MacroLit; diff --git a/src/MoonP/moon_parser.h b/src/MoonP/moon_parser.h index bd62e86..bbd58e1 100644 --- a/src/MoonP/moon_parser.h +++ b/src/MoonP/moon_parser.h @@ -183,8 +183,6 @@ private: rule Line; rule Shebang; - AST_RULE(Decimal) - AST_RULE(Integer) AST_RULE(Num) AST_RULE(Name) AST_RULE(Variable) -- cgit v1.2.3-55-g6feb