diff options
Diffstat (limited to '')
-rwxr-xr-x | src/yuescript/yue_parser.cpp | 75 |
1 files changed, 37 insertions, 38 deletions
diff --git a/src/yuescript/yue_parser.cpp b/src/yuescript/yue_parser.cpp index 5628062..9257fab 100755 --- a/src/yuescript/yue_parser.cpp +++ b/src/yuescript/yue_parser.cpp | |||
@@ -71,12 +71,12 @@ YueParser::YueParser() { | |||
71 | #define sym(str) (Space >> str) | 71 | #define sym(str) (Space >> str) |
72 | #define symx(str) expr(str) | 72 | #define symx(str) expr(str) |
73 | #define ensure(patt, finally) ((patt) >> (finally) | (finally) >> Cut) | 73 | #define ensure(patt, finally) ((patt) >> (finally) | (finally) >> Cut) |
74 | #define key(str) (Space >> str >> not_(AlphaNum)) | 74 | #define key(str) (str >> not_(AlphaNum)) |
75 | #define disable_do(patt) (DisableDo >> ((patt) >> EnableDo | EnableDo >> Cut)) | 75 | #define disable_do(patt) (DisableDo >> ((patt) >> EnableDo | EnableDo >> Cut)) |
76 | #define disable_chain(patt) (DisableChain >> ((patt) >> EnableChain | EnableChain >> Cut)) | 76 | #define disable_chain(patt) (DisableChain >> ((patt) >> EnableChain | EnableChain >> Cut)) |
77 | #define disable_do_chain(patt) (DisableDoChain >> ((patt) >> EnableDoChain | EnableDoChain >> Cut)) | 77 | #define disable_do_chain(patt) (DisableDoChain >> ((patt) >> EnableDoChain | EnableDoChain >> Cut)) |
78 | #define disable_arg_table_block(patt) (DisableArgTableBlock >> ((patt) >> EnableArgTableBlock | EnableArgTableBlock >> Cut)) | 78 | #define disable_arg_table_block(patt) (DisableArgTableBlock >> ((patt) >> EnableArgTableBlock | EnableArgTableBlock >> Cut)) |
79 | #define plain_body_with(str) (-key(str) >> InBlock | key(str) >> Statement) | 79 | #define plain_body_with(str) (-(Space >> key(str)) >> InBlock | Space >> key(str) >> Statement) |
80 | #define plain_body (InBlock | Statement) | 80 | #define plain_body (InBlock | Statement) |
81 | 81 | ||
82 | Variable = pl::user(Name, [](const item_t& item) { | 82 | Variable = pl::user(Name, [](const item_t& item) { |
@@ -182,12 +182,12 @@ YueParser::YueParser() { | |||
182 | Attrib = (expr("const") | expr("close")) >> not_(AlphaNum); | 182 | Attrib = (expr("const") | expr("close")) >> not_(AlphaNum); |
183 | Local = key("local") >> (Space >> local_flag | local_values); | 183 | Local = key("local") >> (Space >> local_flag | local_values); |
184 | 184 | ||
185 | LocalAttrib = Space >> Attrib >> NameList >> Assign; | 185 | LocalAttrib = Attrib >> NameList >> Assign; |
186 | 186 | ||
187 | colon_import_name = sym('\\') >> Space >> Variable; | 187 | colon_import_name = sym('\\') >> Space >> Variable; |
188 | ImportName = colon_import_name | Space >> Variable; | 188 | ImportName = colon_import_name | Space >> Variable; |
189 | ImportNameList = Seperator >> *SpaceBreak >> ImportName >> *((+SpaceBreak | sym(',') >> *SpaceBreak) >> ImportName); | 189 | ImportNameList = Seperator >> *SpaceBreak >> ImportName >> *((+SpaceBreak | sym(',') >> *SpaceBreak) >> ImportName); |
190 | ImportFrom = ImportNameList >> *SpaceBreak >> key("from") >> Exp; | 190 | ImportFrom = ImportNameList >> *SpaceBreak >> Space >> key("from") >> Exp; |
191 | 191 | ||
192 | import_literal_inner = (range('a', 'z') | range('A', 'Z') | set("_-")) >> *(AlphaNum | '-'); | 192 | import_literal_inner = (range('a', 'z') | range('A', 'Z') | set("_-")) >> *(AlphaNum | '-'); |
193 | import_literal_chain = Seperator >> import_literal_inner >> *(expr('.') >> import_literal_inner); | 193 | import_literal_chain = Seperator >> import_literal_inner >> *(expr('.') >> import_literal_inner); |
@@ -208,11 +208,11 @@ YueParser::YueParser() { | |||
208 | -import_tab_lines >> | 208 | -import_tab_lines >> |
209 | White >> sym('}') | KeyValue >> *(sym(',') >> KeyValue)); | 209 | White >> sym('}') | KeyValue >> *(sym(',') >> KeyValue)); |
210 | 210 | ||
211 | ImportAs = ImportLiteral >> -(key("as") >> (ImportTabLit | Space >> Variable)); | 211 | ImportAs = ImportLiteral >> -(Space >> key("as") >> Space >> (ImportTabLit | Variable | import_all_macro)); |
212 | 212 | ||
213 | Import = key("import") >> (ImportAs | ImportFrom); | 213 | Import = key("import") >> (ImportAs | ImportFrom); |
214 | 214 | ||
215 | Label = Space >> expr("::") >> LabelName >> expr("::"); | 215 | Label = expr("::") >> LabelName >> expr("::"); |
216 | 216 | ||
217 | Goto = key("goto") >> Space >> LabelName; | 217 | Goto = key("goto") >> Space >> LabelName; |
218 | 218 | ||
@@ -222,9 +222,9 @@ YueParser::YueParser() { | |||
222 | 222 | ||
223 | WithExp = ExpList >> -Assign; | 223 | WithExp = ExpList >> -Assign; |
224 | 224 | ||
225 | With = key("with") >> -existential_op >> disable_do_chain(WithExp) >> plain_body_with("do"); | 225 | With = Space >> key("with") >> -existential_op >> disable_do_chain(WithExp) >> plain_body_with("do"); |
226 | SwitchCase = key("when") >> disable_chain(ExpList) >> plain_body_with("then"); | 226 | SwitchCase = Space >> key("when") >> disable_chain(ExpList) >> plain_body_with("then"); |
227 | SwitchElse = key("else") >> plain_body; | 227 | SwitchElse = Space >> key("else") >> plain_body; |
228 | 228 | ||
229 | SwitchBlock = *EmptyLine >> | 229 | SwitchBlock = *EmptyLine >> |
230 | Advance >> Seperator >> | 230 | Advance >> Seperator >> |
@@ -233,18 +233,18 @@ YueParser::YueParser() { | |||
233 | -(+SpaceBreak >> SwitchElse) >> | 233 | -(+SpaceBreak >> SwitchElse) >> |
234 | PopIndent; | 234 | PopIndent; |
235 | 235 | ||
236 | Switch = key("switch") >> disable_do(Exp) >> -key("do") | 236 | Switch = Space >> key("switch") >> disable_do(Exp) >> -(Space >> key("do")) |
237 | >> -Space >> Break >> SwitchBlock; | 237 | >> -Space >> Break >> SwitchBlock; |
238 | 238 | ||
239 | IfCond = disable_chain(Exp >> -Assign); | 239 | IfCond = disable_chain(Exp >> -Assign); |
240 | IfElseIf = -(Break >> *EmptyLine >> CheckIndent) >> key("elseif") >> IfCond >> plain_body_with("then"); | 240 | IfElseIf = -(Break >> *EmptyLine >> CheckIndent) >> Space >> key("elseif") >> IfCond >> plain_body_with("then"); |
241 | IfElse = -(Break >> *EmptyLine >> CheckIndent) >> key("else") >> plain_body; | 241 | IfElse = -(Break >> *EmptyLine >> CheckIndent) >> Space >> key("else") >> plain_body; |
242 | IfType = (expr("if") | expr("unless")) >> not_(AlphaNum); | 242 | IfType = (expr("if") | expr("unless")) >> not_(AlphaNum); |
243 | If = Space >> IfType >> IfCond >> plain_body_with("then") >> *IfElseIf >> -IfElse; | 243 | If = Space >> IfType >> IfCond >> plain_body_with("then") >> *IfElseIf >> -IfElse; |
244 | 244 | ||
245 | WhileType = (expr("while") | expr("until")) >> not_(AlphaNum); | 245 | WhileType = (expr("while") | expr("until")) >> not_(AlphaNum); |
246 | While = Space >> WhileType >> disable_do_chain(Exp) >> plain_body_with("do"); | 246 | While = WhileType >> disable_do_chain(Exp) >> plain_body_with("do"); |
247 | Repeat = key("repeat") >> Body >> Break >> *EmptyLine >> CheckIndent >> key("until") >> Exp; | 247 | Repeat = key("repeat") >> Body >> Break >> *EmptyLine >> CheckIndent >> Space >> key("until") >> Exp; |
248 | 248 | ||
249 | for_step_value = sym(',') >> Exp; | 249 | for_step_value = sym(',') >> Exp; |
250 | for_args = Space >> Variable >> sym('=') >> Exp >> sym(',') >> Exp >> -for_step_value; | 250 | for_args = Space >> Variable >> sym('=') >> Exp >> sym(',') >> Exp >> -for_step_value; |
@@ -253,10 +253,10 @@ YueParser::YueParser() { | |||
253 | 253 | ||
254 | for_in = star_exp | ExpList; | 254 | for_in = star_exp | ExpList; |
255 | 255 | ||
256 | ForEach = key("for") >> AssignableNameList >> key("in") >> | 256 | ForEach = key("for") >> AssignableNameList >> Space >> key("in") >> |
257 | disable_do_chain(for_in) >> plain_body_with("do"); | 257 | disable_do_chain(for_in) >> plain_body_with("do"); |
258 | 258 | ||
259 | Do = pl::user(key("do"), [](const item_t& item) { | 259 | Do = pl::user(Space >> key("do"), [](const item_t& item) { |
260 | State* st = reinterpret_cast<State*>(item.user_data); | 260 | State* st = reinterpret_cast<State*>(item.user_data); |
261 | return st->doStack.empty() || st->doStack.top(); | 261 | return st->doStack.empty() || st->doStack.top(); |
262 | }) >> Body; | 262 | }) >> Body; |
@@ -299,15 +299,15 @@ YueParser::YueParser() { | |||
299 | return true; | 299 | return true; |
300 | }); | 300 | }); |
301 | 301 | ||
302 | Comprehension = sym('[') >> Exp >> CompInner >> sym(']'); | 302 | Comprehension = sym('[') >> Exp >> Space >> CompInner >> sym(']'); |
303 | comp_value = sym(',') >> Exp; | 303 | comp_value = sym(',') >> Exp; |
304 | TblComprehension = sym('{') >> Exp >> -comp_value >> CompInner >> sym('}'); | 304 | TblComprehension = sym('{') >> Exp >> -comp_value >> Space >> CompInner >> sym('}'); |
305 | 305 | ||
306 | CompInner = Seperator >> (CompForEach | CompFor) >> *CompClause; | 306 | CompInner = Seperator >> (CompForEach | CompFor) >> *CompClause; |
307 | star_exp = sym('*') >> Exp; | 307 | star_exp = sym('*') >> Exp; |
308 | CompForEach = key("for") >> AssignableNameList >> key("in") >> (star_exp | Exp); | 308 | CompForEach = key("for") >> AssignableNameList >> Space >> key("in") >> (star_exp | Exp); |
309 | CompFor = key("for") >> Space >> Variable >> sym('=') >> Exp >> sym(',') >> Exp >> -for_step_value; | 309 | CompFor = key("for") >> Space >> Variable >> sym('=') >> Exp >> sym(',') >> Exp >> -for_step_value; |
310 | CompClause = CompFor | CompForEach | key("when") >> Exp; | 310 | CompClause = Space >> (CompFor | CompForEach | key("when") >> Exp); |
311 | 311 | ||
312 | Assign = sym('=') >> Seperator >> (With | If | Switch | TableBlock | Exp >> *(Space >> set(",;") >> Exp)); | 312 | Assign = sym('=') >> Seperator >> (With | If | Switch | TableBlock | Exp >> *(Space >> set(",;") >> Exp)); |
313 | 313 | ||
@@ -491,10 +491,10 @@ YueParser::YueParser() { | |||
491 | ClassBlock = +SpaceBreak >> Advance >> Seperator >> ClassLine >> *(+SpaceBreak >> ClassLine) >> PopIndent; | 491 | ClassBlock = +SpaceBreak >> Advance >> Seperator >> ClassLine >> *(+SpaceBreak >> ClassLine) >> PopIndent; |
492 | 492 | ||
493 | ClassDecl = | 493 | ClassDecl = |
494 | key("class") >> not_(expr(':')) >> | 494 | Space >> key("class") >> not_(expr(':')) >> |
495 | -Assignable >> | 495 | -Assignable >> |
496 | -(key("extends") >> PreventIndent >> ensure(Exp, PopIndent)) >> | 496 | -(Space >> key("extends") >> PreventIndent >> ensure(Exp, PopIndent)) >> |
497 | -(key("using") >> PreventIndent >> ensure(ExpList, PopIndent)) >> | 497 | -(Space >> key("using") >> PreventIndent >> ensure(ExpList, PopIndent)) >> |
498 | -ClassBlock; | 498 | -ClassBlock; |
499 | 499 | ||
500 | global_values = NameList >> -(sym('=') >> (TableBlock | ExpListLow)); | 500 | global_values = NameList >> -(sym('=') >> (TableBlock | ExpListLow)); |
@@ -507,13 +507,13 @@ YueParser::YueParser() { | |||
507 | State* st = reinterpret_cast<State*>(item.user_data); | 507 | State* st = reinterpret_cast<State*>(item.user_data); |
508 | st->exportCount++; | 508 | st->exportCount++; |
509 | return true; | 509 | return true; |
510 | }) >> (pl::user(export_default >> Exp, [](const item_t& item) { | 510 | }) >> (pl::user(Space >> export_default >> Exp, [](const item_t& item) { |
511 | State* st = reinterpret_cast<State*>(item.user_data); | 511 | State* st = reinterpret_cast<State*>(item.user_data); |
512 | bool isValid = !st->exportDefault && st->exportCount == 1; | 512 | bool isValid = !st->exportDefault && st->exportCount == 1; |
513 | st->exportDefault = true; | 513 | st->exportDefault = true; |
514 | return isValid; | 514 | return isValid; |
515 | }) | 515 | }) |
516 | | (not_(export_default) >> pl::user(true_(), [](const item_t& item) { | 516 | | (not_(Space >> export_default) >> pl::user(true_(), [](const item_t& item) { |
517 | State* st = reinterpret_cast<State*>(item.user_data); | 517 | State* st = reinterpret_cast<State*>(item.user_data); |
518 | if (st->exportDefault && st->exportCount > 1) { | 518 | if (st->exportDefault && st->exportCount > 1) { |
519 | return false; | 519 | return false; |
@@ -521,7 +521,7 @@ YueParser::YueParser() { | |||
521 | return true; | 521 | return true; |
522 | } | 522 | } |
523 | }) >> ExpList >> -Assign) | 523 | }) >> ExpList >> -Assign) |
524 | | Macro) >> not_(Space >> statement_appendix); | 524 | | Space >> Macro) >> not_(Space >> statement_appendix); |
525 | 525 | ||
526 | variable_pair = sym(':') >> Variable >> not_('#'); | 526 | variable_pair = sym(':') >> Variable >> not_('#'); |
527 | 527 | ||
@@ -560,16 +560,17 @@ YueParser::YueParser() { | |||
560 | ) | 560 | ) |
561 | ); | 561 | ); |
562 | 562 | ||
563 | outer_var_shadow = key("using") >> (NameList | Space >> expr("nil")); | 563 | outer_var_shadow = Space >> key("using") >> (NameList | Space >> expr("nil")); |
564 | 564 | ||
565 | FnArgsDef = sym('(') >> White >> -FnArgDefList >> -outer_var_shadow >> White >> sym(')'); | 565 | FnArgsDef = sym('(') >> White >> -FnArgDefList >> -outer_var_shadow >> White >> sym(')'); |
566 | fn_arrow = expr("->") | expr("=>"); | 566 | fn_arrow = expr("->") | expr("=>"); |
567 | FunLit = -FnArgsDef >> Space >> fn_arrow >> -Body; | 567 | FunLit = -FnArgsDef >> Space >> fn_arrow >> -Body; |
568 | 568 | ||
569 | MacroName = expr('$') >> -Name; | 569 | MacroName = expr('$') >> Name; |
570 | macro_args_def = sym('(') >> White >> -FnArgDefList >> White >> sym(')'); | 570 | macro_args_def = sym('(') >> White >> -FnArgDefList >> White >> sym(')'); |
571 | MacroLit = -macro_args_def >> Space >> expr("->") >> Body; | 571 | MacroLit = -macro_args_def >> Space >> expr("->") >> Body; |
572 | Macro = key("macro") >> Space >> Name >> sym('=') >> MacroLit; | 572 | Macro = key("macro") >> Space >> Name >> sym('=') >> MacroLit; |
573 | MacroInPlace = expr('$') >> Space >> expr("->") >> Body; | ||
573 | 574 | ||
574 | NameList = Seperator >> Space >> Variable >> *(sym(',') >> Space >> Variable); | 575 | NameList = Seperator >> Space >> Variable >> *(sym(',') >> Space >> Variable); |
575 | NameOrDestructure = Space >> Variable | TableLit; | 576 | NameOrDestructure = Space >> Variable | TableLit; |
@@ -578,7 +579,7 @@ YueParser::YueParser() { | |||
578 | fn_arrow_back = expr('<') >> set("-="); | 579 | fn_arrow_back = expr('<') >> set("-="); |
579 | Backcall = -FnArgsDef >> Space >> fn_arrow_back >> Space >> ChainValue; | 580 | Backcall = -FnArgsDef >> Space >> fn_arrow_back >> Space >> ChainValue; |
580 | 581 | ||
581 | PipeBody = Seperator >> Space >> PipeOperator >> unary_exp >> *(+SpaceBreak >> CheckIndent >> Space >> PipeOperator >> unary_exp); | 582 | PipeBody = Seperator >> PipeOperator >> unary_exp >> *(+SpaceBreak >> CheckIndent >> Space >> PipeOperator >> unary_exp); |
582 | 583 | ||
583 | ExpList = Seperator >> Exp >> *(sym(',') >> Exp); | 584 | ExpList = Seperator >> Exp >> *(sym(',') >> Exp); |
584 | ExpListLow = Seperator >> Exp >> *(Space >> set(",;") >> Exp); | 585 | ExpListLow = Seperator >> Exp >> *(Space >> set(",;") >> Exp); |
@@ -606,24 +607,22 @@ YueParser::YueParser() { | |||
606 | 607 | ||
607 | const_value = (expr("nil") | expr("true") | expr("false")) >> not_(AlphaNum); | 608 | const_value = (expr("nil") | expr("true") | expr("false")) >> not_(AlphaNum); |
608 | 609 | ||
609 | SimpleValue = | 610 | SimpleValue = Space >> (const_value | |
610 | (Space >> const_value) | | ||
611 | If | Switch | With | ClassDecl | ForEach | For | While | Do | | 611 | If | Switch | With | ClassDecl | ForEach | For | While | Do | |
612 | (Space >> unary_value) | | 612 | unary_value | TblComprehension | TableLit | Comprehension | |
613 | TblComprehension | TableLit | Comprehension | FunLit | | 613 | FunLit | Num); |
614 | (Space >> Num); | ||
615 | 614 | ||
616 | ExpListAssign = ExpList >> -(Update | Assign); | 615 | ExpListAssign = ExpList >> -(Update | Assign); |
617 | 616 | ||
618 | if_line = key("if") >> Exp >> -Assign; | 617 | if_line = Space >> key("if") >> Exp >> -Assign; |
619 | unless_line = key("unless") >> Exp; | 618 | unless_line = Space >> key("unless") >> Exp; |
620 | 619 | ||
621 | statement_appendix = (if_line | unless_line | CompInner) >> Space; | 620 | statement_appendix = (if_line | unless_line | CompInner) >> Space; |
622 | statement_sep = and_(*SpaceBreak >> CheckIndent >> Space >> (set("($'\"") | expr("[[") | expr("[="))); | 621 | statement_sep = and_(*SpaceBreak >> CheckIndent >> Space >> (set("($'\"") | expr("[[") | expr("[="))); |
623 | Statement = ( | 622 | Statement = Space >> ( |
624 | Import | While | Repeat | For | ForEach | | 623 | Import | While | Repeat | For | ForEach | |
625 | Return | Local | Global | Export | Macro | | 624 | Return | Local | Global | Export | Macro | |
626 | Space >> BreakLoop | Label | Goto | LocalAttrib | | 625 | MacroInPlace | BreakLoop | Label | Goto | LocalAttrib | |
627 | Backcall | PipeBody | ExpListAssign | 626 | Backcall | PipeBody | ExpListAssign |
628 | ) >> Space >> | 627 | ) >> Space >> |
629 | -statement_appendix >> -statement_sep; | 628 | -statement_appendix >> -statement_sep; |