diff options
author | Li Jin <dragon-fly@qq.com> | 2020-10-15 18:00:47 +0800 |
---|---|---|
committer | Li Jin <dragon-fly@qq.com> | 2020-10-15 18:00:47 +0800 |
commit | 0777356cbe599b3f88bdfa476e3ffa64bb3a3a8c (patch) | |
tree | 9157e07408ecda84ee65f5db36db917fa688e1ad | |
parent | 46f6429cd61315efd337346559598c855e29f921 (diff) | |
download | yuescript-0777356cbe599b3f88bdfa476e3ffa64bb3a3a8c.tar.gz yuescript-0777356cbe599b3f88bdfa476e3ffa64bb3a3a8c.tar.bz2 yuescript-0777356cbe599b3f88bdfa476e3ffa64bb3a3a8c.zip |
add a new macro type support to insert raw codes to output.
-rw-r--r-- | src/MoonP/moon_ast.h | 10 | ||||
-rw-r--r-- | src/MoonP/moon_compiler.cpp | 11 | ||||
-rw-r--r-- | src/MoonP/moon_parser.cpp | 19 | ||||
-rw-r--r-- | src/MoonP/moon_parser.h | 2 |
4 files changed, 18 insertions, 24 deletions
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: \ | |||
35 | }; \ | 35 | }; \ |
36 | template<> constexpr int id<type##_t>() { return COUNTER_READ; } | 36 | template<> constexpr int id<type##_t>() { return COUNTER_READ; } |
37 | 37 | ||
38 | AST_LEAF(Decimal) | 38 | AST_LEAF(Num) |
39 | AST_END(Decimal) | ||
40 | |||
41 | AST_LEAF(Integer) | ||
42 | AST_END(Integer) | ||
43 | |||
44 | AST_NODE(Num) | ||
45 | ast_sel<true, Decimal_t, Integer_t> num; | ||
46 | AST_MEMBER(Num, &num) | ||
47 | AST_END(Num) | 39 | AST_END(Num) |
48 | 40 | ||
49 | AST_LEAF(Name) | 41 | 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) { | |||
53 | return std::string(sv); | 53 | return std::string(sv); |
54 | } | 54 | } |
55 | 55 | ||
56 | const std::string_view version = "0.4.17"sv; | 56 | const std::string_view version = "0.4.18"sv; |
57 | const std::string_view extension = "mp"sv; | 57 | const std::string_view extension = "mp"sv; |
58 | 58 | ||
59 | class MoonCompilerImpl { | 59 | class MoonCompilerImpl { |
@@ -3070,6 +3070,8 @@ private: | |||
3070 | // to convert its whole text content | 3070 | // to convert its whole text content |
3071 | str = _parser.toString(exp->backcalls.front()); | 3071 | str = _parser.toString(exp->backcalls.front()); |
3072 | } | 3072 | } |
3073 | } else if (auto lstr = ast_cast<LuaString_t>(arg)) { | ||
3074 | str = _parser.toString(lstr->content); | ||
3073 | } else { | 3075 | } else { |
3074 | bool multiLineStr = false; | 3076 | bool multiLineStr = false; |
3075 | BLOCK_START | 3077 | BLOCK_START |
@@ -3116,7 +3118,7 @@ private: | |||
3116 | std::tie(type, codes) = expandMacroStr(chainValue); | 3118 | std::tie(type, codes) = expandMacroStr(chainValue); |
3117 | std::string targetType(usage != ExpUsage::Common || chainList.size() > 2 ? "expr"sv : "block"sv); | 3119 | std::string targetType(usage != ExpUsage::Common || chainList.size() > 2 ? "expr"sv : "block"sv); |
3118 | if (type == "lua"sv) { | 3120 | if (type == "lua"sv) { |
3119 | if (targetType != "block"sv) { | 3121 | if (!allowBlockMacroReturn && targetType != "block"sv) { |
3120 | throw std::logic_error(_info.errorMessage("lua macro can only be placed where block macro is allowed"sv, x)); | 3122 | throw std::logic_error(_info.errorMessage("lua macro can only be placed where block macro is allowed"sv, x)); |
3121 | } | 3123 | } |
3122 | auto macroChunk = s("=(macro "sv) + _parser.toString(x->name) + ')'; | 3124 | auto macroChunk = s("=(macro "sv) + _parser.toString(x->name) + ')'; |
@@ -3127,6 +3129,11 @@ private: | |||
3127 | throw std::logic_error(_info.errorMessage(err, x)); | 3129 | throw std::logic_error(_info.errorMessage(err, x)); |
3128 | } | 3130 | } |
3129 | return {nullptr, nullptr, std::move(codes)}; | 3131 | return {nullptr, nullptr, std::move(codes)}; |
3132 | } else if (type == "text"sv) { | ||
3133 | if (!allowBlockMacroReturn && targetType != "block"sv) { | ||
3134 | throw std::logic_error(_info.errorMessage("text macro can only be placed where block macro is allowed"sv, x)); | ||
3135 | } | ||
3136 | return {nullptr, nullptr, std::move(codes)}; | ||
3130 | } else if (!allowBlockMacroReturn && type != targetType) { | 3137 | } else if (!allowBlockMacroReturn && type != targetType) { |
3131 | throw std::logic_error(_info.errorMessage(s("macro type mismatch, "sv) + targetType + s(" expected, got "sv) + type, x)); | 3138 | throw std::logic_error(_info.errorMessage(s("macro type mismatch, "sv) + targetType + s(" expected, got "sv) + type, x)); |
3132 | } | 3139 | } |
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() { | |||
51 | EmptyLine = SpaceBreak; | 51 | EmptyLine = SpaceBreak; |
52 | AlphaNum = range('a', 'z') | range('A', 'Z') | range('0', '9') | '_'; | 52 | AlphaNum = range('a', 'z') | range('A', 'Z') | range('0', '9') | '_'; |
53 | Name = (range('a', 'z') | range('A', 'Z') | '_') >> *AlphaNum; | 53 | Name = (range('a', 'z') | range('A', 'Z') | '_') >> *AlphaNum; |
54 | Decimal = ( | 54 | Num = ( |
55 | "0x" >> | ||
56 | +(range('0', '9') | range('a', 'f') | range('A', 'F')) >> | ||
57 | -(-set("uU") >> set("lL") >> set("lL")) | ||
58 | ) | ( | ||
59 | +range('0', '9') >> -set("uU") >> set("lL") >> set("lL") | ||
60 | ) | ( | ||
55 | ( | 61 | ( |
56 | +range('0', '9') >> -('.' >> +range('0', '9')) | 62 | +range('0', '9') >> -('.' >> +range('0', '9')) |
57 | ) | ( | 63 | ) | ( |
58 | '.' >> +range('0', '9') | 64 | '.' >> +range('0', '9') |
59 | ) | 65 | ) |
60 | ) >> -(set("eE") >> -expr('-') >> +range('0', '9')); | 66 | ) >> -(set("eE") >> -expr('-') >> +range('0', '9')); |
61 | Integer = | ||
62 | ( | ||
63 | "0x" >> | ||
64 | +(range('0', '9') | range('a', 'f') | range('A', 'F')) >> | ||
65 | -(-set("uU") >> set("lL") >> set("lL")) | ||
66 | ) | ( | ||
67 | +range('0', '9') >> -set("uU") >> set("lL") >> set("lL") | ||
68 | ); | ||
69 | Num = Integer | Decimal; | ||
70 | 67 | ||
71 | Cut = false_(); | 68 | Cut = false_(); |
72 | Seperator = true_(); | 69 | Seperator = true_(); |
@@ -513,7 +510,7 @@ MoonParser::MoonParser() { | |||
513 | FunLit = -FnArgsDef >> Space >> fn_arrow >> -Body; | 510 | FunLit = -FnArgsDef >> Space >> fn_arrow >> -Body; |
514 | 511 | ||
515 | MacroName = expr('$') >> Name; | 512 | MacroName = expr('$') >> Name; |
516 | macro_type = expr("expr") | expr("block") | expr("lua"); | 513 | macro_type = expr("expr") | expr("block") | expr("lua") | expr("text"); |
517 | macro_args_def = sym('(') >> White >> -FnArgDefList >> White >> sym(')'); | 514 | macro_args_def = sym('(') >> White >> -FnArgDefList >> White >> sym(')'); |
518 | MacroLit = -macro_args_def >> Space >> expr("->") >> Body; | 515 | MacroLit = -macro_args_def >> Space >> expr("->") >> Body; |
519 | Macro = key("macro") >> Space >> macro_type >> Space >> Name >> sym('=') >> MacroLit; | 516 | 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: | |||
183 | rule Line; | 183 | rule Line; |
184 | rule Shebang; | 184 | rule Shebang; |
185 | 185 | ||
186 | AST_RULE(Decimal) | ||
187 | AST_RULE(Integer) | ||
188 | AST_RULE(Num) | 186 | AST_RULE(Num) |
189 | AST_RULE(Name) | 187 | AST_RULE(Name) |
190 | AST_RULE(Variable) | 188 | AST_RULE(Variable) |