aboutsummaryrefslogtreecommitdiff
path: root/src/MoonP/moon_parser.cpp
diff options
context:
space:
mode:
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
3Permission 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
5The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
7THE 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
11namespace pl = parserlib;
2 12
3namespace MoonP { 13namespace MoonP {
4 14
@@ -35,15 +45,18 @@ rule EmptyLine = SpaceBreak;
35rule AlphaNum = range('a', 'z') | range('A', 'Z') | range('0', '9') | '_'; 45rule AlphaNum = range('a', 'z') | range('A', 'Z') | range('0', '9') | '_';
36rule Name = (range('a', 'z') | range('A', 'Z') | '_') >> *AlphaNum; 46rule Name = (range('a', 'z') | range('A', 'Z') | '_') >> *AlphaNum;
37rule Num = 47rule 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 );
47rule Cut = false_(); 60rule Cut = false_();
48rule Seperator = true_(); 61rule 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
55rule Variable = user(Name, [](const item_t& item) { 68rule 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
63rule LuaKeyword = user(Name, [](const item_t& item) { 76rule 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);
77rule KeyName = SelfName | Space >> Name; 90rule KeyName = SelfName | Space >> Name;
78rule VarArg = Space >> "..."; 91rule VarArg = Space >> "...";
79 92
80rule check_indent = user(Indent, [](const item_t& item) { 93rule 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});
91rule CheckIndent = and_(check_indent); 104rule CheckIndent = and_(check_indent);
92 105
93rule advance = user(Indent, [](const item_t& item) { 106rule 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});
109rule Advance = and_(advance); 122rule Advance = and_(advance);
110 123
111rule push_indent = user(Indent, [](const item_t& item) { 124rule 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});
123rule PushIndent = and_(push_indent); 136rule PushIndent = and_(push_indent);
124 137
125rule PreventIndent = user(true_(), [](const item_t& item) { 138rule 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
131rule PopIndent = user(true_(), [](const item_t& item) { 144rule 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;
166rule SwitchBlock = *EmptyLine >> 179rule 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
173rule Switch = key("switch") >> 186rule 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
202rule Do = user(key("do") >> Body, [](const item_t& item) 215rule 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
208rule DisableDo = user(true_(), [](const item_t& item) 221rule 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
215rule PopDo = user(true_(), [](const item_t& item) 228rule 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);
299rule lua_string_open = '[' >> *expr('=') >> '['; 312rule lua_string_open = '[' >> *expr('=') >> '[';
300rule lua_string_close = ']' >> *expr('=') >> ']'; 313rule lua_string_close = ']' >> *expr('=') >> ']';
301 314
302rule LuaStringOpen = user(lua_string_open, [](const item_t& item) 315rule 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
310rule LuaStringClose = user(lua_string_close, [](const item_t& item) 323rule 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
317rule LuaStringContent = *(not_(LuaStringClose) >> (Break | Any)); 330rule LuaStringContent = *(not_(LuaStringClose) >> (Break | Any));
318 331
319rule LuaString = user(LuaStringOpen >> -Break >> LuaStringContent >> LuaStringClose, [](const item_t& item) 332rule 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;