aboutsummaryrefslogtreecommitdiff
path: root/MoonParser/moon_parser.cpp
diff options
context:
space:
mode:
authorLi Jin <dragon-fly@qq.com>2019-10-29 11:25:27 +0800
committerLi Jin <dragon-fly@qq.com>2019-10-29 11:25:27 +0800
commit975167856ed0b11c2ede03c6eb750ca4e4a6a7fc (patch)
treefa4369fe7e7d49c63cae93d6c5b52b78116f58cd /MoonParser/moon_parser.cpp
parent726fee3152c81fdac7e3ad5f663bfbea8f99ddd8 (diff)
downloadyuescript-975167856ed0b11c2ede03c6eb750ca4e4a6a7fc.tar.gz
yuescript-975167856ed0b11c2ede03c6eb750ca4e4a6a7fc.tar.bz2
yuescript-975167856ed0b11c2ede03c6eb750ca4e4a6a7fc.zip
complete moon compiler in C++.
Diffstat (limited to 'MoonParser/moon_parser.cpp')
-rw-r--r--MoonParser/moon_parser.cpp31
1 files changed, 20 insertions, 11 deletions
diff --git a/MoonParser/moon_parser.cpp b/MoonParser/moon_parser.cpp
index d709e29..2b93c22 100644
--- a/MoonParser/moon_parser.cpp
+++ b/MoonParser/moon_parser.cpp
@@ -1,5 +1,7 @@
1#include "moon_parser.h" 1#include "moon_parser.h"
2 2
3namespace MoonP {
4
3std::unordered_set<std::string> State::luaKeywords = { 5std::unordered_set<std::string> State::luaKeywords = {
4 "and", "break", "do", "else", "elseif", 6 "and", "break", "do", "else", "elseif",
5 "end", "false", "for", "function", "if", 7 "end", "false", "for", "function", "if",
@@ -14,8 +16,8 @@ std::unordered_set<std::string> State::keywords = {
14 "in", "local", "nil", "not", "or", 16 "in", "local", "nil", "not", "or",
15 "repeat", "return", "then", "true", "until", 17 "repeat", "return", "then", "true", "until",
16 "while", // Lua keywords 18 "while", // Lua keywords
17 "class", "continue", "export", "extends", 19 "class", "continue", "export", "extends", "from",
18 "import", "switch", "when", "unless", "using", 20 "import", "switch", "unless", "using", "when",
19 "with" // Moon keywords 21 "with" // Moon keywords
20}; 22};
21 23
@@ -35,10 +37,7 @@ rule Name = (range('a', 'z') | range('A', 'Z') | '_') >> *AlphaNum;
35rule Num = 37rule Num =
36 ( 38 (
37 "0x" >> 39 "0x" >>
38 +(range('0', '9') | range('a', 'f') | range('A', 'F')) >> 40 +(range('0', '9') | range('a', 'f') | range('A', 'F'))
39 -(-set("uU") >> set("lL") >> set("lL"))
40 ) | (
41 +range('0', '9') >> -set("uU") >> set("lL") >> set("lL")
42 ) | ( 41 ) | (
43 ( 42 (
44 (+range('0', '9') >> -('.' >> +range('0', '9'))) | 43 (+range('0', '9') >> -('.' >> +range('0', '9'))) |
@@ -56,9 +55,17 @@ rule Seperator = true_();
56rule Variable = user(Name, [](const item_t& item) { 55rule Variable = user(Name, [](const item_t& item) {
57 State* st = reinterpret_cast<State*>(item.user_data); 56 State* st = reinterpret_cast<State*>(item.user_data);
58 for (auto it = item.begin; it != item.end; ++it) st->buffer += static_cast<char>(*it); 57 for (auto it = item.begin; it != item.end; ++it) st->buffer += static_cast<char>(*it);
59 auto it = st->keywords.find(st->buffer); 58 auto it = State::keywords.find(st->buffer);
59 st->buffer.clear();
60 return it == State::keywords.end();
61});
62
63rule LuaKeyword = user(Name, [](const item_t& item) {
64 State* st = reinterpret_cast<State*>(item.user_data);
65 for (auto it = item.begin; it != item.end; ++it) st->buffer += static_cast<char>(*it);
66 auto it = State::luaKeywords.find(st->buffer);
60 st->buffer.clear(); 67 st->buffer.clear();
61 return it == st->keywords.end(); 68 return it != State::luaKeywords.end();
62}); 69});
63 70
64rule self = expr('@'); 71rule self = expr('@');
@@ -344,7 +351,7 @@ extern rule Invoke, Slice;
344rule Index = symx('[') >> Exp >> sym(']'); 351rule Index = symx('[') >> Exp >> sym(']');
345rule ChainItem = Invoke | DotChainItem | Slice | Index; 352rule ChainItem = Invoke | DotChainItem | Slice | Index;
346rule DotChainItem = symx('.') >> Name; 353rule DotChainItem = symx('.') >> Name;
347rule ColonChainItem = symx('\\') >> Name; 354rule ColonChainItem = symx('\\') >> (LuaKeyword | Name);
348rule invoke_chain = Invoke >> -ChainItems; 355rule invoke_chain = Invoke >> -ChainItems;
349rule ColonChain = ColonChainItem >> -invoke_chain; 356rule ColonChain = ColonChainItem >> -invoke_chain;
350 357
@@ -480,7 +487,7 @@ rule SimpleValue =
480 TblComprehension | TableLit | Comprehension | FunLit | 487 TblComprehension | TableLit | Comprehension | FunLit |
481 (Space >> Num); 488 (Space >> Num);
482 489
483rule Assignment = ExpList >> (Update | Assign); 490rule ExpListAssign = ExpList >> -(Update | Assign);
484 491
485rule if_else_line = key("if") >> Exp >> (key("else") >> Exp | default_value); 492rule if_else_line = key("if") >> Exp >> (key("else") >> Exp | default_value);
486rule unless_line = key("unless") >> Exp; 493rule unless_line = key("unless") >> Exp;
@@ -490,7 +497,7 @@ rule Statement =
490( 497(
491 Import | While | For | ForEach | 498 Import | While | For | ForEach |
492 Return | Local | Export | Space >> BreakLoop | 499 Return | Local | Export | Space >> BreakLoop |
493 Assignment | ExpList 500 ExpListAssign
494) >> Space >> 501) >> Space >>
495-statement_appendix; 502-statement_appendix;
496 503
@@ -502,3 +509,5 @@ rule Block = Seperator >> Line >> *(+Break >> Line);
502 509
503rule Shebang = expr("#!") >> *(not_(Stop) >> Any); 510rule Shebang = expr("#!") >> *(not_(Stop) >> Any);
504rule File = White >> -Shebang >> Block >> eof(); 511rule File = White >> -Shebang >> Block >> eof();
512
513} // namespace MoonP