aboutsummaryrefslogtreecommitdiff
path: root/src/MoonP/moon_parser.cpp
diff options
context:
space:
mode:
authorLi Jin <dragon-fly@qq.com>2020-03-24 10:19:30 +0800
committerLi Jin <dragon-fly@qq.com>2020-03-24 10:19:30 +0800
commit2447d158241aeaaf9c0b1ab21a08db7a40e5cef3 (patch)
treea8c17610aae77e61ee713b090546e3ae7d35f967 /src/MoonP/moon_parser.cpp
parent14d7e02285857226e26288c1ac83a14eb4fbd478 (diff)
downloadyuescript-2447d158241aeaaf9c0b1ab21a08db7a40e5cef3.tar.gz
yuescript-2447d158241aeaaf9c0b1ab21a08db7a40e5cef3.tar.bz2
yuescript-2447d158241aeaaf9c0b1ab21a08db7a40e5cef3.zip
add goto statement support.
Diffstat (limited to 'src/MoonP/moon_parser.cpp')
-rw-r--r--src/MoonP/moon_parser.cpp33
1 files changed, 23 insertions, 10 deletions
diff --git a/src/MoonP/moon_parser.cpp b/src/MoonP/moon_parser.cpp
index c8a3a23..872e30e 100644
--- a/src/MoonP/moon_parser.cpp
+++ b/src/MoonP/moon_parser.cpp
@@ -15,18 +15,18 @@ using namespace std::string_view_literals;
15 15
16std::unordered_set<std::string> LuaKeywords = { 16std::unordered_set<std::string> LuaKeywords = {
17 "and", "break", "do", "else", "elseif", 17 "and", "break", "do", "else", "elseif",
18 "end", "false", "for", "function", "if", 18 "end", "false", "for", "function", "goto",
19 "in", "local", "nil", "not", "or", 19 "if", "in", "local", "nil", "not",
20 "repeat", "return", "then", "true", "until", 20 "or", "repeat", "return", "then", "true",
21 "while" 21 "until", "while"
22}; 22};
23 23
24std::unordered_set<std::string> Keywords = { 24std::unordered_set<std::string> Keywords = {
25 "and", "break", "do", "else", "elseif", 25 "and", "break", "do", "else", "elseif",
26 "end", "false", "for", "function", "if", 26 "end", "false", "for", "function", "goto",
27 "in", "local", "nil", "not", "or", 27 "if", "in", "local", "nil", "not",
28 "repeat", "return", "then", "true", "until", 28 "or", "repeat", "return", "then", "true",
29 "while", // Lua keywords 29 "until", "while", // Lua keywords
30 "as", "class", "continue", "export", "extends", 30 "as", "class", "continue", "export", "extends",
31 "from", "global", "import", "macro", "switch", 31 "from", "global", "import", "macro", "switch",
32 "unless", "using", "when", "with" // Moon keywords 32 "unless", "using", "when", "with" // Moon keywords
@@ -85,6 +85,14 @@ MoonParser::MoonParser() {
85 return isValid; 85 return isValid;
86 }); 86 });
87 87
88 LabelName = pl::user(Name, [](const item_t& item) {
89 State* st = reinterpret_cast<State*>(item.user_data);
90 for (auto it = item.begin; it != item.end; ++it) st->buffer += static_cast<char>(*it);
91 auto isValid = LuaKeywords.find(st->buffer) == LuaKeywords.end();
92 st->buffer.clear();
93 return isValid;
94 });
95
88 LuaKeyword = pl::user(Name, [](const item_t& item) { 96 LuaKeyword = pl::user(Name, [](const item_t& item) {
89 State* st = reinterpret_cast<State*>(item.user_data); 97 State* st = reinterpret_cast<State*>(item.user_data);
90 for (auto it = item.begin; it != item.end; ++it) st->buffer += static_cast<char>(*it); 98 for (auto it = item.begin; it != item.end; ++it) st->buffer += static_cast<char>(*it);
@@ -191,9 +199,14 @@ MoonParser::MoonParser() {
191 199
192 Import = key("import") >> (ImportAs | ImportFrom); 200 Import = key("import") >> (ImportAs | ImportFrom);
193 201
202 Label = Space >> expr("::") >> LabelName >> expr("::");
203
204 Goto = key("goto") >> Space >> LabelName;
205
194 BreakLoop = (expr("break") | expr("continue")) >> not_(AlphaNum); 206 BreakLoop = (expr("break") | expr("continue")) >> not_(AlphaNum);
195 207
196 Return = key("return") >> -ExpListLow; 208 Return = key("return") >> -ExpListLow;
209
197 WithExp = ExpList >> -Assign; 210 WithExp = ExpList >> -Assign;
198 211
199 With = key("with") >> DisableDo >> ensure(WithExp, PopDo) >> -key("do") >> Body; 212 With = key("with") >> DisableDo >> ensure(WithExp, PopDo) >> -key("do") >> Body;
@@ -527,8 +540,8 @@ MoonParser::MoonParser() {
527 Statement = ( 540 Statement = (
528 Import | While | For | ForEach | 541 Import | While | For | ForEach |
529 Return | Local | Global | Export | 542 Return | Local | Global | Export |
530 Macro | Space >> BreakLoop | 543 Macro | Space >> BreakLoop | Label |
531 Backcall | ExpListAssign 544 Goto | Backcall | ExpListAssign
532 ) >> Space >> 545 ) >> Space >>
533 -statement_appendix; 546 -statement_appendix;
534 547