diff options
| author | Li Jin <dragon-fly@qq.com> | 2020-01-10 16:30:34 +0800 |
|---|---|---|
| committer | Li Jin <dragon-fly@qq.com> | 2020-01-10 16:30:34 +0800 |
| commit | 52a6536103f46c26a3ba9b149b0fe7b40d524d8c (patch) | |
| tree | 67e4759f8e1ea922079d0e162d84ecba5e558261 /src/MoonP/moon_parser.cpp | |
| parent | 975167856ed0b11c2ede03c6eb750ca4e4a6a7fc (diff) | |
| download | yuescript-52a6536103f46c26a3ba9b149b0fe7b40d524d8c.tar.gz yuescript-52a6536103f46c26a3ba9b149b0fe7b40d524d8c.tar.bz2 yuescript-52a6536103f46c26a3ba9b149b0fe7b40d524d8c.zip | |
update.
Diffstat (limited to '')
| -rw-r--r-- | src/MoonP/moon_parser.cpp (renamed from MoonParser/moon_parser.cpp) | 61 |
1 files changed, 37 insertions, 24 deletions
diff --git a/MoonParser/moon_parser.cpp b/src/MoonP/moon_parser.cpp index 2b93c22..7b5183e 100644 --- a/MoonParser/moon_parser.cpp +++ b/src/MoonP/moon_parser.cpp | |||
| @@ -1,4 +1,14 @@ | |||
| 1 | #include "moon_parser.h" | 1 | /* Copyright (c) 2020 Jin Li, http://www.luvfight.me |
| 2 | |||
| 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
| 4 | |||
| 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
| 6 | |||
| 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ | ||
| 8 | |||
| 9 | #include "MoonP/moon_parser.h" | ||
| 10 | |||
| 11 | namespace pl = parserlib; | ||
| 2 | 12 | ||
| 3 | namespace MoonP { | 13 | namespace MoonP { |
| 4 | 14 | ||
| @@ -35,15 +45,18 @@ rule EmptyLine = SpaceBreak; | |||
| 35 | rule AlphaNum = range('a', 'z') | range('A', 'Z') | range('0', '9') | '_'; | 45 | rule AlphaNum = range('a', 'z') | range('A', 'Z') | range('0', '9') | '_'; |
| 36 | rule Name = (range('a', 'z') | range('A', 'Z') | '_') >> *AlphaNum; | 46 | rule Name = (range('a', 'z') | range('A', 'Z') | '_') >> *AlphaNum; |
| 37 | rule Num = | 47 | rule Num = |
| 48 | ( | ||
| 49 | "0x" >> | ||
| 50 | +(range('0', '9') | range('a', 'f') | range('A', 'F')) >> | ||
| 51 | -(-set("uU") >> set("lL") >> set("lL")) | ||
| 52 | ) | ( | ||
| 53 | +range('0', '9') >> -set("uU") >> set("lL") >> set("lL") | ||
| 54 | ) | ( | ||
| 38 | ( | 55 | ( |
| 39 | "0x" >> | 56 | (+range('0', '9') >> -('.' >> +range('0', '9'))) | |
| 40 | +(range('0', '9') | range('a', 'f') | range('A', 'F')) | 57 | ('.' >> +range('0', '9')) |
| 41 | ) | ( | 58 | ) >> -(set("eE") >> -expr('-') >> +range('0', '9')) |
| 42 | ( | 59 | ); |
| 43 | (+range('0', '9') >> -('.' >> +range('0', '9'))) | | ||
| 44 | ('.' >> +range('0', '9')) | ||
| 45 | ) >> -(set("eE") >> -expr('-') >> +range('0', '9')) | ||
| 46 | ); | ||
| 47 | rule Cut = false_(); | 60 | rule Cut = false_(); |
| 48 | rule Seperator = true_(); | 61 | rule Seperator = true_(); |
| 49 | 62 | ||
| @@ -52,7 +65,7 @@ rule Seperator = true_(); | |||
| 52 | #define ensure(patt, finally) (((patt) >> (finally)) | ((finally) >> (Cut))) | 65 | #define ensure(patt, finally) (((patt) >> (finally)) | ((finally) >> (Cut))) |
| 53 | #define key(str) (Space >> str >> not_(AlphaNum)) | 66 | #define key(str) (Space >> str >> not_(AlphaNum)) |
| 54 | 67 | ||
| 55 | rule Variable = user(Name, [](const item_t& item) { | 68 | rule Variable = pl::user(Name, [](const item_t& item) { |
| 56 | State* st = reinterpret_cast<State*>(item.user_data); | 69 | State* st = reinterpret_cast<State*>(item.user_data); |
| 57 | for (auto it = item.begin; it != item.end; ++it) st->buffer += static_cast<char>(*it); | 70 | for (auto it = item.begin; it != item.end; ++it) st->buffer += static_cast<char>(*it); |
| 58 | auto it = State::keywords.find(st->buffer); | 71 | auto it = State::keywords.find(st->buffer); |
| @@ -60,7 +73,7 @@ rule Variable = user(Name, [](const item_t& item) { | |||
| 60 | return it == State::keywords.end(); | 73 | return it == State::keywords.end(); |
| 61 | }); | 74 | }); |
| 62 | 75 | ||
| 63 | rule LuaKeyword = user(Name, [](const item_t& item) { | 76 | rule LuaKeyword = pl::user(Name, [](const item_t& item) { |
| 64 | State* st = reinterpret_cast<State*>(item.user_data); | 77 | State* st = reinterpret_cast<State*>(item.user_data); |
| 65 | for (auto it = item.begin; it != item.end; ++it) st->buffer += static_cast<char>(*it); | 78 | for (auto it = item.begin; it != item.end; ++it) st->buffer += static_cast<char>(*it); |
| 66 | auto it = State::luaKeywords.find(st->buffer); | 79 | auto it = State::luaKeywords.find(st->buffer); |
| @@ -77,7 +90,7 @@ rule SelfName = Space >> (self_class_name | self_class | self_name | self); | |||
| 77 | rule KeyName = SelfName | Space >> Name; | 90 | rule KeyName = SelfName | Space >> Name; |
| 78 | rule VarArg = Space >> "..."; | 91 | rule VarArg = Space >> "..."; |
| 79 | 92 | ||
| 80 | rule check_indent = user(Indent, [](const item_t& item) { | 93 | rule check_indent = pl::user(Indent, [](const item_t& item) { |
| 81 | int indent = 0; | 94 | int indent = 0; |
| 82 | for (input_it i = item.begin; i != item.end; ++i) { | 95 | for (input_it i = item.begin; i != item.end; ++i) { |
| 83 | switch (*i) { | 96 | switch (*i) { |
| @@ -90,7 +103,7 @@ rule check_indent = user(Indent, [](const item_t& item) { | |||
| 90 | }); | 103 | }); |
| 91 | rule CheckIndent = and_(check_indent); | 104 | rule CheckIndent = and_(check_indent); |
| 92 | 105 | ||
| 93 | rule advance = user(Indent, [](const item_t& item) { | 106 | rule advance = pl::user(Indent, [](const item_t& item) { |
| 94 | int indent = 0; | 107 | int indent = 0; |
| 95 | for (input_it i = item.begin; i != item.end; ++i) { | 108 | for (input_it i = item.begin; i != item.end; ++i) { |
| 96 | switch (*i) { | 109 | switch (*i) { |
| @@ -108,7 +121,7 @@ rule advance = user(Indent, [](const item_t& item) { | |||
| 108 | }); | 121 | }); |
| 109 | rule Advance = and_(advance); | 122 | rule Advance = and_(advance); |
| 110 | 123 | ||
| 111 | rule push_indent = user(Indent, [](const item_t& item) { | 124 | rule push_indent = pl::user(Indent, [](const item_t& item) { |
| 112 | int indent = 0; | 125 | int indent = 0; |
| 113 | for (input_it i = item.begin; i != item.end; ++i) { | 126 | for (input_it i = item.begin; i != item.end; ++i) { |
| 114 | switch (*i) { | 127 | switch (*i) { |
| @@ -122,13 +135,13 @@ rule push_indent = user(Indent, [](const item_t& item) { | |||
| 122 | }); | 135 | }); |
| 123 | rule PushIndent = and_(push_indent); | 136 | rule PushIndent = and_(push_indent); |
| 124 | 137 | ||
| 125 | rule PreventIndent = user(true_(), [](const item_t& item) { | 138 | rule PreventIndent = pl::user(true_(), [](const item_t& item) { |
| 126 | State* st = reinterpret_cast<State*>(item.user_data); | 139 | State* st = reinterpret_cast<State*>(item.user_data); |
| 127 | st->indents.push(-1); | 140 | st->indents.push(-1); |
| 128 | return true; | 141 | return true; |
| 129 | }); | 142 | }); |
| 130 | 143 | ||
| 131 | rule PopIndent = user(true_(), [](const item_t& item) { | 144 | rule PopIndent = pl::user(true_(), [](const item_t& item) { |
| 132 | State* st = reinterpret_cast<State*>(item.user_data); | 145 | State* st = reinterpret_cast<State*>(item.user_data); |
| 133 | st->indents.pop(); | 146 | st->indents.pop(); |
| 134 | return true; | 147 | return true; |
| @@ -166,8 +179,8 @@ rule SwitchElse = key("else") >> Body; | |||
| 166 | rule SwitchBlock = *EmptyLine >> | 179 | rule SwitchBlock = *EmptyLine >> |
| 167 | Advance >> Seperator >> | 180 | Advance >> Seperator >> |
| 168 | SwitchCase >> | 181 | SwitchCase >> |
| 169 | *(+Break >> SwitchCase) >> | 182 | *(+SpaceBreak >> SwitchCase) >> |
| 170 | -(+Break >> SwitchElse) >> | 183 | -(+SpaceBreak >> SwitchElse) >> |
| 171 | PopIndent; | 184 | PopIndent; |
| 172 | 185 | ||
| 173 | rule Switch = key("switch") >> | 186 | rule Switch = key("switch") >> |
| @@ -199,20 +212,20 @@ rule ForEach = key("for") >> AssignableNameList >> key("in") >> | |||
| 199 | DisableDo >> ensure(for_in, PopDo) >> | 212 | DisableDo >> ensure(for_in, PopDo) >> |
| 200 | -key("do") >> Body; | 213 | -key("do") >> Body; |
| 201 | 214 | ||
| 202 | rule Do = user(key("do") >> Body, [](const item_t& item) | 215 | rule Do = pl::user(key("do") >> Body, [](const item_t& item) |
| 203 | { | 216 | { |
| 204 | State* st = reinterpret_cast<State*>(item.user_data); | 217 | State* st = reinterpret_cast<State*>(item.user_data); |
| 205 | return st->doStack.empty() || st->doStack.top(); | 218 | return st->doStack.empty() || st->doStack.top(); |
| 206 | }); | 219 | }); |
| 207 | 220 | ||
| 208 | rule DisableDo = user(true_(), [](const item_t& item) | 221 | rule DisableDo = pl::user(true_(), [](const item_t& item) |
| 209 | { | 222 | { |
| 210 | State* st = reinterpret_cast<State*>(item.user_data); | 223 | State* st = reinterpret_cast<State*>(item.user_data); |
| 211 | st->doStack.push(false); | 224 | st->doStack.push(false); |
| 212 | return true; | 225 | return true; |
| 213 | }); | 226 | }); |
| 214 | 227 | ||
| 215 | rule PopDo = user(true_(), [](const item_t& item) | 228 | rule PopDo = pl::user(true_(), [](const item_t& item) |
| 216 | { | 229 | { |
| 217 | State* st = reinterpret_cast<State*>(item.user_data); | 230 | State* st = reinterpret_cast<State*>(item.user_data); |
| 218 | st->doStack.pop(); | 231 | st->doStack.pop(); |
| @@ -299,7 +312,7 @@ rule String = Space >> (DoubleString | SingleString | LuaString); | |||
| 299 | rule lua_string_open = '[' >> *expr('=') >> '['; | 312 | rule lua_string_open = '[' >> *expr('=') >> '['; |
| 300 | rule lua_string_close = ']' >> *expr('=') >> ']'; | 313 | rule lua_string_close = ']' >> *expr('=') >> ']'; |
| 301 | 314 | ||
| 302 | rule LuaStringOpen = user(lua_string_open, [](const item_t& item) | 315 | rule LuaStringOpen = pl::user(lua_string_open, [](const item_t& item) |
| 303 | { | 316 | { |
| 304 | size_t count = std::distance(item.begin, item.end); | 317 | size_t count = std::distance(item.begin, item.end); |
| 305 | State* st = reinterpret_cast<State*>(item.user_data); | 318 | State* st = reinterpret_cast<State*>(item.user_data); |
| @@ -307,7 +320,7 @@ rule LuaStringOpen = user(lua_string_open, [](const item_t& item) | |||
| 307 | return true; | 320 | return true; |
| 308 | }); | 321 | }); |
| 309 | 322 | ||
| 310 | rule LuaStringClose = user(lua_string_close, [](const item_t& item) | 323 | rule LuaStringClose = pl::user(lua_string_close, [](const item_t& item) |
| 311 | { | 324 | { |
| 312 | size_t count = std::distance(item.begin, item.end); | 325 | size_t count = std::distance(item.begin, item.end); |
| 313 | State* st = reinterpret_cast<State*>(item.user_data); | 326 | State* st = reinterpret_cast<State*>(item.user_data); |
| @@ -316,7 +329,7 @@ rule LuaStringClose = user(lua_string_close, [](const item_t& item) | |||
| 316 | 329 | ||
| 317 | rule LuaStringContent = *(not_(LuaStringClose) >> (Break | Any)); | 330 | rule LuaStringContent = *(not_(LuaStringClose) >> (Break | Any)); |
| 318 | 331 | ||
| 319 | rule LuaString = user(LuaStringOpen >> -Break >> LuaStringContent >> LuaStringClose, [](const item_t& item) | 332 | rule LuaString = pl::user(LuaStringOpen >> -Break >> LuaStringContent >> LuaStringClose, [](const item_t& item) |
| 320 | { | 333 | { |
| 321 | State* st = reinterpret_cast<State*>(item.user_data); | 334 | State* st = reinterpret_cast<State*>(item.user_data); |
| 322 | st->stringOpen = -1; | 335 | st->stringOpen = -1; |
