diff options
author | Li Jin <dragon-fly@qq.com> | 2019-09-17 00:41:21 +0800 |
---|---|---|
committer | Li Jin <dragon-fly@qq.com> | 2019-09-17 00:41:21 +0800 |
commit | 74c4f6eab47f76b093e17f515204525e00a3b352 (patch) | |
tree | d737b1437f103842f30c96a2787468d474405efb /MoonParser/moon_parser.cpp | |
parent | b65798d7960d797f2b7074c4cc47b8c70a9f5270 (diff) | |
download | yuescript-74c4f6eab47f76b093e17f515204525e00a3b352.tar.gz yuescript-74c4f6eab47f76b093e17f515204525e00a3b352.tar.bz2 yuescript-74c4f6eab47f76b093e17f515204525e00a3b352.zip |
completing spec/class.moon
Diffstat (limited to 'MoonParser/moon_parser.cpp')
-rw-r--r-- | MoonParser/moon_parser.cpp | 94 |
1 files changed, 40 insertions, 54 deletions
diff --git a/MoonParser/moon_parser.cpp b/MoonParser/moon_parser.cpp index 3009ee5..ec8df38 100644 --- a/MoonParser/moon_parser.cpp +++ b/MoonParser/moon_parser.cpp | |||
@@ -20,9 +20,8 @@ rule SomeSpace = +set(" \t") >> -Comment; | |||
20 | rule SpaceBreak = Space >> Break; | 20 | rule SpaceBreak = Space >> Break; |
21 | rule EmptyLine = SpaceBreak; | 21 | rule EmptyLine = SpaceBreak; |
22 | rule AlphaNum = range('a', 'z') | range('A', 'Z') | range('0', '9') | '_'; | 22 | rule AlphaNum = range('a', 'z') | range('A', 'Z') | range('0', '9') | '_'; |
23 | rule _Name = (range('a', 'z') | range('A', 'Z') | '_') >> *AlphaNum; | 23 | rule Name = (range('a', 'z') | range('A', 'Z') | '_') >> *AlphaNum; |
24 | rule SpaceName = Space >> _Name; | 24 | rule Num = |
25 | rule _Num = | ||
26 | ( | 25 | ( |
27 | "0x" >> | 26 | "0x" >> |
28 | +(range('0', '9') | range('a', 'f') | range('A', 'F')) >> | 27 | +(range('0', '9') | range('a', 'f') | range('A', 'F')) >> |
@@ -35,7 +34,6 @@ rule _Num = | |||
35 | ('.' >> +range('0', '9')) | 34 | ('.' >> +range('0', '9')) |
36 | ) >> -(set("eE") >> -expr('-') >> +range('0', '9')) | 35 | ) >> -(set("eE") >> -expr('-') >> +range('0', '9')) |
37 | ); | 36 | ); |
38 | rule Num = Space >> _Num; | ||
39 | rule Cut = false_(); | 37 | rule Cut = false_(); |
40 | rule Seperator = true_(); | 38 | rule Seperator = true_(); |
41 | 39 | ||
@@ -44,8 +42,7 @@ rule Seperator = true_(); | |||
44 | #define ensure(patt, finally) (((patt) >> (finally)) | ((finally) >> (Cut))) | 42 | #define ensure(patt, finally) (((patt) >> (finally)) | ((finally) >> (Cut))) |
45 | #define key(str) (Space >> str >> not_(AlphaNum)) | 43 | #define key(str) (Space >> str >> not_(AlphaNum)) |
46 | 44 | ||
47 | rule Name = user(_Name, [](const item_t& item) | 45 | rule Variable = user(Name, [](const item_t& item) { |
48 | { | ||
49 | State* st = reinterpret_cast<State*>(item.user_data); | 46 | State* st = reinterpret_cast<State*>(item.user_data); |
50 | for (auto it = item.begin; it != item.end; ++it) st->buffer << static_cast<char>(*it); | 47 | for (auto it = item.begin; it != item.end; ++it) st->buffer << static_cast<char>(*it); |
51 | std::string name; | 48 | std::string name; |
@@ -57,21 +54,18 @@ rule Name = user(_Name, [](const item_t& item) | |||
57 | }); | 54 | }); |
58 | 55 | ||
59 | rule self = expr('@'); | 56 | rule self = expr('@'); |
60 | rule self_name = '@' >> _Name; | 57 | rule self_name = '@' >> Name; |
61 | rule self_class = expr("@@"); | 58 | rule self_class = expr("@@"); |
62 | rule self_class_name = "@@" >> _Name; | 59 | rule self_class_name = "@@" >> Name; |
63 | 60 | ||
64 | rule SelfName = Space >> (self_class_name | self_class | self_name | self); | 61 | rule SelfName = Space >> (self_class_name | self_class | self_name | self); |
65 | rule KeyName = SelfName | Space >> _Name; | 62 | rule KeyName = SelfName | Space >> Name; |
66 | rule VarArg = Space >> "..."; | 63 | rule VarArg = Space >> "..."; |
67 | 64 | ||
68 | rule check_indent = user(Indent, [](const item_t& item) | 65 | rule check_indent = user(Indent, [](const item_t& item) { |
69 | { | ||
70 | int indent = 0; | 66 | int indent = 0; |
71 | for (input_it i = item.begin; i != item.end; ++i) | 67 | for (input_it i = item.begin; i != item.end; ++i) { |
72 | { | 68 | switch (*i) { |
73 | switch (*i) | ||
74 | { | ||
75 | case ' ': indent++; break; | 69 | case ' ': indent++; break; |
76 | case '\t': indent += 4; break; | 70 | case '\t': indent += 4; break; |
77 | } | 71 | } |
@@ -81,21 +75,17 @@ rule check_indent = user(Indent, [](const item_t& item) | |||
81 | }); | 75 | }); |
82 | rule CheckIndent = and_(check_indent); | 76 | rule CheckIndent = and_(check_indent); |
83 | 77 | ||
84 | rule advance = user(Indent, [](const item_t& item) | 78 | rule advance = user(Indent, [](const item_t& item) { |
85 | { | ||
86 | int indent = 0; | 79 | int indent = 0; |
87 | for (input_it i = item.begin; i != item.end; ++i) | 80 | for (input_it i = item.begin; i != item.end; ++i) { |
88 | { | 81 | switch (*i) { |
89 | switch (*i) | ||
90 | { | ||
91 | case ' ': indent++; break; | 82 | case ' ': indent++; break; |
92 | case '\t': indent += 4; break; | 83 | case '\t': indent += 4; break; |
93 | } | 84 | } |
94 | } | 85 | } |
95 | State* st = reinterpret_cast<State*>(item.user_data); | 86 | State* st = reinterpret_cast<State*>(item.user_data); |
96 | int top = st->indents.top(); | 87 | int top = st->indents.top(); |
97 | if (top != -1 && indent > top) | 88 | if (top != -1 && indent > top) { |
98 | { | ||
99 | st->indents.push(indent); | 89 | st->indents.push(indent); |
100 | return true; | 90 | return true; |
101 | } | 91 | } |
@@ -103,13 +93,10 @@ rule advance = user(Indent, [](const item_t& item) | |||
103 | }); | 93 | }); |
104 | rule Advance = and_(advance); | 94 | rule Advance = and_(advance); |
105 | 95 | ||
106 | rule push_indent = user(Indent, [](const item_t& item) | 96 | rule push_indent = user(Indent, [](const item_t& item) { |
107 | { | ||
108 | int indent = 0; | 97 | int indent = 0; |
109 | for (input_it i = item.begin; i != item.end; ++i) | 98 | for (input_it i = item.begin; i != item.end; ++i) { |
110 | { | 99 | switch (*i) { |
111 | switch (*i) | ||
112 | { | ||
113 | case ' ': indent++; break; | 100 | case ' ': indent++; break; |
114 | case '\t': indent += 4; break; | 101 | case '\t': indent += 4; break; |
115 | } | 102 | } |
@@ -120,15 +107,13 @@ rule push_indent = user(Indent, [](const item_t& item) | |||
120 | }); | 107 | }); |
121 | rule PushIndent = and_(push_indent); | 108 | rule PushIndent = and_(push_indent); |
122 | 109 | ||
123 | rule PreventIndent = user(true_(), [](const item_t& item) | 110 | rule PreventIndent = user(true_(), [](const item_t& item) { |
124 | { | ||
125 | State* st = reinterpret_cast<State*>(item.user_data); | 111 | State* st = reinterpret_cast<State*>(item.user_data); |
126 | st->indents.push(-1); | 112 | st->indents.push(-1); |
127 | return true; | 113 | return true; |
128 | }); | 114 | }); |
129 | 115 | ||
130 | rule PopIndent = user(true_(), [](const item_t& item) | 116 | rule PopIndent = user(true_(), [](const item_t& item) { |
131 | { | ||
132 | State* st = reinterpret_cast<State*>(item.user_data); | 117 | State* st = reinterpret_cast<State*>(item.user_data); |
133 | st->indents.pop(); | 118 | st->indents.pop(); |
134 | return true; | 119 | return true; |
@@ -143,8 +128,8 @@ extern rule NameList; | |||
143 | rule local_flag = expr('*') | expr('^'); | 128 | rule local_flag = expr('*') | expr('^'); |
144 | rule Local = key("local") >> ((Space >> local_flag) | NameList); | 129 | rule Local = key("local") >> ((Space >> local_flag) | NameList); |
145 | 130 | ||
146 | rule colon_import_name = sym('\\') >> Space >> Name; | 131 | rule colon_import_name = sym('\\') >> Space >> Variable; |
147 | rule ImportName = colon_import_name | Space >> Name; | 132 | rule ImportName = colon_import_name | Space >> Variable; |
148 | rule ImportNameList = Seperator >> *SpaceBreak >> ImportName >> *((+SpaceBreak | sym(',') >> *SpaceBreak) >> ImportName); | 133 | rule ImportNameList = Seperator >> *SpaceBreak >> ImportName >> *((+SpaceBreak | sym(',') >> *SpaceBreak) >> ImportName); |
149 | 134 | ||
150 | extern rule Exp; | 135 | extern rule Exp; |
@@ -183,7 +168,7 @@ rule Unless = key("unless") >> IfCond >> -key("then") >> Body >> Seperator >> *I | |||
183 | rule While = key("while") >> DisableDo >> ensure(Exp, PopDo) >> -key("do") >> Body; | 168 | rule While = key("while") >> DisableDo >> ensure(Exp, PopDo) >> -key("do") >> Body; |
184 | 169 | ||
185 | rule for_step_value = sym(',') >> Exp; | 170 | rule for_step_value = sym(',') >> Exp; |
186 | rule for_args = Space >> Name >> sym('=') >> Exp >> sym(',') >> Exp >> -for_step_value; | 171 | rule for_args = Space >> Variable >> sym('=') >> Exp >> sym(',') >> Exp >> -for_step_value; |
187 | 172 | ||
188 | rule For = key("for") >> DisableDo >> | 173 | rule For = key("for") >> DisableDo >> |
189 | ensure(for_args, PopDo) >> | 174 | ensure(for_args, PopDo) >> |
@@ -230,7 +215,7 @@ extern rule CompForEach, CompFor, CompClause; | |||
230 | rule CompInner = (CompForEach | CompFor) >> Seperator >> *CompClause; | 215 | rule CompInner = (CompForEach | CompFor) >> Seperator >> *CompClause; |
231 | rule star_exp = sym('*') >> Exp; | 216 | rule star_exp = sym('*') >> Exp; |
232 | rule CompForEach = key("for") >> AssignableNameList >> key("in") >> (star_exp | Exp); | 217 | rule CompForEach = key("for") >> AssignableNameList >> key("in") >> (star_exp | Exp); |
233 | rule CompFor = key("for") >> Space >> Name >> sym('=') >> Exp >> sym(',') >> Exp >> -for_step_value; | 218 | rule CompFor = key("for") >> Space >> Variable >> sym('=') >> Exp >> sym(',') >> Exp >> -for_step_value; |
234 | rule CompClause = CompFor | CompForEach | key("when") >> Exp; | 219 | rule CompClause = CompFor | CompForEach | key("when") >> Exp; |
235 | 220 | ||
236 | extern rule TableBlock; | 221 | extern rule TableBlock; |
@@ -269,7 +254,7 @@ rule BinaryOperator = | |||
269 | 254 | ||
270 | extern rule Chain; | 255 | extern rule Chain; |
271 | 256 | ||
272 | rule Assignable = Chain | Space >> Name | SelfName; | 257 | rule Assignable = Chain | Space >> Variable | SelfName; |
273 | 258 | ||
274 | extern rule Value; | 259 | extern rule Value; |
275 | 260 | ||
@@ -324,7 +309,7 @@ rule LuaString = user(LuaStringOpen >> -Break >> LuaStringContent >> LuaStringCl | |||
324 | }); | 309 | }); |
325 | 310 | ||
326 | rule Parens = sym('(') >> *SpaceBreak >> Exp >> *SpaceBreak >> sym(')'); | 311 | rule Parens = sym('(') >> *SpaceBreak >> Exp >> *SpaceBreak >> sym(')'); |
327 | rule Callable = Space >> Name | SelfName | VarArg | Parens; | 312 | rule Callable = Space >> Variable | SelfName | VarArg | Parens; |
328 | rule FnArgsExpList = Exp >> *((Break | sym(',')) >> White >> Exp); | 313 | rule FnArgsExpList = Exp >> *((Break | sym(',')) >> White >> Exp); |
329 | 314 | ||
330 | rule FnArgs = Seperator >> | 315 | rule FnArgs = Seperator >> |
@@ -356,8 +341,8 @@ extern rule Invoke, Slice; | |||
356 | 341 | ||
357 | rule Index = symx('[') >> Exp >> sym(']'); | 342 | rule Index = symx('[') >> Exp >> sym(']'); |
358 | rule ChainItem = Invoke | DotChainItem | Slice | Index; | 343 | rule ChainItem = Invoke | DotChainItem | Slice | Index; |
359 | rule DotChainItem = symx('.') >> _Name; | 344 | rule DotChainItem = symx('.') >> Name; |
360 | rule ColonChainItem = symx('\\') >> _Name; | 345 | rule ColonChainItem = symx('\\') >> Name; |
361 | rule invoke_chain = Invoke >> -ChainItems; | 346 | rule invoke_chain = Invoke >> -ChainItems; |
362 | rule ColonChain = ColonChainItem >> -invoke_chain; | 347 | rule ColonChain = ColonChainItem >> -invoke_chain; |
363 | 348 | ||
@@ -406,7 +391,7 @@ rule TableBlock = +(SpaceBreak) >> Advance >> ensure(TableBlockInner, PopIndent) | |||
406 | extern rule Statement; | 391 | extern rule Statement; |
407 | 392 | ||
408 | rule class_member_list = Seperator >> KeyValue >> *(sym(',') >> KeyValue); | 393 | rule class_member_list = Seperator >> KeyValue >> *(sym(',') >> KeyValue); |
409 | rule ClassLine = CheckIndent >> (class_member_list | Statement | Exp) >> -sym(','); | 394 | rule ClassLine = CheckIndent >> (class_member_list | Statement) >> -sym(','); |
410 | rule ClassBlock = +(SpaceBreak) >> Advance >>Seperator >> ClassLine >> *(+(SpaceBreak) >> ClassLine) >> PopIndent; | 395 | rule ClassBlock = +(SpaceBreak) >> Advance >>Seperator >> ClassLine >> *(+(SpaceBreak) >> ClassLine) >> PopIndent; |
411 | 396 | ||
412 | rule ClassDecl = | 397 | rule ClassDecl = |
@@ -419,7 +404,7 @@ rule export_values = NameList >> -(sym('=') >> ExpListLow); | |||
419 | rule export_op = expr('*') | expr('^'); | 404 | rule export_op = expr('*') | expr('^'); |
420 | rule Export = key("export") >> (ClassDecl | (Space >> export_op) | export_values); | 405 | rule Export = key("export") >> (ClassDecl | (Space >> export_op) | export_values); |
421 | 406 | ||
422 | rule variable_pair = sym(':') >> not_(SomeSpace) >> Space >> Name; | 407 | rule variable_pair = sym(':') >> not_(SomeSpace) >> Space >> Variable; |
423 | 408 | ||
424 | rule normal_pair = | 409 | rule normal_pair = |
425 | ( | 410 | ( |
@@ -436,7 +421,7 @@ rule KeyValue = variable_pair | normal_pair; | |||
436 | rule KeyValueList = KeyValue >> *(sym(',') >> KeyValue); | 421 | rule KeyValueList = KeyValue >> *(sym(',') >> KeyValue); |
437 | rule KeyValueLine = CheckIndent >> KeyValueList >> -sym(','); | 422 | rule KeyValueLine = CheckIndent >> KeyValueList >> -sym(','); |
438 | 423 | ||
439 | rule FnArgDef = (Space >> Name | SelfName) >> -(sym('=') >> Exp); | 424 | rule FnArgDef = (Space >> Variable | SelfName) >> -(sym('=') >> Exp); |
440 | 425 | ||
441 | rule FnArgDefList = Seperator >> | 426 | rule FnArgDefList = Seperator >> |
442 | ( | 427 | ( |
@@ -455,8 +440,8 @@ rule FnArgsDef = sym('(') >> White >> -FnArgDefList >> -outer_var_shadow >> Whit | |||
455 | rule fn_arrow = expr("->") | expr("=>"); | 440 | rule fn_arrow = expr("->") | expr("=>"); |
456 | rule FunLit = -FnArgsDef >> Space >> fn_arrow >> -Body; | 441 | rule FunLit = -FnArgsDef >> Space >> fn_arrow >> -Body; |
457 | 442 | ||
458 | rule NameList = Seperator >> Space >> Name >> *(sym(',') >> Space >> Name); | 443 | rule NameList = Seperator >> Space >> Variable >> *(sym(',') >> Space >> Variable); |
459 | rule NameOrDestructure = Space >> Name | TableLit; | 444 | rule NameOrDestructure = Space >> Variable | TableLit; |
460 | rule AssignableNameList = Seperator >> NameOrDestructure >> *(sym(',') >> NameOrDestructure); | 445 | rule AssignableNameList = Seperator >> NameOrDestructure >> *(sym(',') >> NameOrDestructure); |
461 | 446 | ||
462 | rule ExpList = Seperator >> Exp >> *(sym(',') >> Exp); | 447 | rule ExpList = Seperator >> Exp >> *(sym(',') >> Exp); |
@@ -479,18 +464,19 @@ rule InvokeArgs = | |||
479 | TableBlock | 464 | TableBlock |
480 | ); | 465 | ); |
481 | 466 | ||
482 | rule const_value = key("nil") | key("true") | key("false"); | 467 | rule const_value = (expr("nil") | expr("true") | expr("false")) >> not_(AlphaNum); |
483 | rule minus_exp = sym('-') >> not_(SomeSpace) >> Exp; | 468 | rule minus_exp = expr('-') >> not_(SomeSpace) >> Exp; |
484 | rule sharp_exp = sym('#') >> Exp; | 469 | rule sharp_exp = expr('#') >> Exp; |
485 | rule tilde_exp = sym('~') >> Exp; | 470 | rule tilde_exp = expr('~') >> Exp; |
486 | rule not_exp = key("not") >> Exp; | 471 | rule not_exp = expr("not") >> not_(AlphaNum) >> Exp; |
487 | rule unary_exp = minus_exp | sharp_exp | tilde_exp | not_exp; | 472 | rule unary_exp = minus_exp | sharp_exp | tilde_exp | not_exp; |
488 | 473 | ||
489 | rule SimpleValue = | 474 | rule SimpleValue = |
490 | const_value | | 475 | (Space >> const_value) | |
491 | If | Unless | Switch | With | ClassDecl | ForEach | For | While | Do | | 476 | If | Unless | Switch | With | ClassDecl | ForEach | For | While | Do | |
492 | unary_exp | | 477 | (Space >> unary_exp) | |
493 | TblComprehension | TableLit | Comprehension | FunLit | Num; | 478 | TblComprehension | TableLit | Comprehension | FunLit | |
479 | (Space >> Num); | ||
494 | 480 | ||
495 | rule Assignment = ExpList >> (Update | Assign); | 481 | rule Assignment = ExpList >> (Update | Assign); |
496 | 482 | ||