diff options
| author | Li Jin <dragon-fly@qq.com> | 2019-10-29 11:25:27 +0800 |
|---|---|---|
| committer | Li Jin <dragon-fly@qq.com> | 2019-10-29 11:25:27 +0800 |
| commit | 975167856ed0b11c2ede03c6eb750ca4e4a6a7fc (patch) | |
| tree | fa4369fe7e7d49c63cae93d6c5b52b78116f58cd /MoonParser/moon_parser.cpp | |
| parent | 726fee3152c81fdac7e3ad5f663bfbea8f99ddd8 (diff) | |
| download | yuescript-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.cpp | 31 |
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 | ||
| 3 | namespace MoonP { | ||
| 4 | |||
| 3 | std::unordered_set<std::string> State::luaKeywords = { | 5 | std::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; | |||
| 35 | rule Num = | 37 | rule 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_(); | |||
| 56 | rule Variable = user(Name, [](const item_t& item) { | 55 | rule 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 | |||
| 63 | rule 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 | ||
| 64 | rule self = expr('@'); | 71 | rule self = expr('@'); |
| @@ -344,7 +351,7 @@ extern rule Invoke, Slice; | |||
| 344 | rule Index = symx('[') >> Exp >> sym(']'); | 351 | rule Index = symx('[') >> Exp >> sym(']'); |
| 345 | rule ChainItem = Invoke | DotChainItem | Slice | Index; | 352 | rule ChainItem = Invoke | DotChainItem | Slice | Index; |
| 346 | rule DotChainItem = symx('.') >> Name; | 353 | rule DotChainItem = symx('.') >> Name; |
| 347 | rule ColonChainItem = symx('\\') >> Name; | 354 | rule ColonChainItem = symx('\\') >> (LuaKeyword | Name); |
| 348 | rule invoke_chain = Invoke >> -ChainItems; | 355 | rule invoke_chain = Invoke >> -ChainItems; |
| 349 | rule ColonChain = ColonChainItem >> -invoke_chain; | 356 | rule 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 | ||
| 483 | rule Assignment = ExpList >> (Update | Assign); | 490 | rule ExpListAssign = ExpList >> -(Update | Assign); |
| 484 | 491 | ||
| 485 | rule if_else_line = key("if") >> Exp >> (key("else") >> Exp | default_value); | 492 | rule if_else_line = key("if") >> Exp >> (key("else") >> Exp | default_value); |
| 486 | rule unless_line = key("unless") >> Exp; | 493 | rule 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 | ||
| 503 | rule Shebang = expr("#!") >> *(not_(Stop) >> Any); | 510 | rule Shebang = expr("#!") >> *(not_(Stop) >> Any); |
| 504 | rule File = White >> -Shebang >> Block >> eof(); | 511 | rule File = White >> -Shebang >> Block >> eof(); |
| 512 | |||
| 513 | } // namespace MoonP | ||
