diff options
Diffstat (limited to 'src/yuescript/yue_parser.cpp')
-rw-r--r-- | src/yuescript/yue_parser.cpp | 61 |
1 files changed, 50 insertions, 11 deletions
diff --git a/src/yuescript/yue_parser.cpp b/src/yuescript/yue_parser.cpp index c818098..8ceee9a 100644 --- a/src/yuescript/yue_parser.cpp +++ b/src/yuescript/yue_parser.cpp | |||
@@ -109,6 +109,11 @@ YueParser::YueParser() { | |||
109 | return false; | 109 | return false; |
110 | }); | 110 | }); |
111 | 111 | ||
112 | table_key_pair_error = pl::user(true_(), [](const item_t& item) { | ||
113 | throw ParserError("can not put hash pair in a list"sv, item.begin); | ||
114 | return false; | ||
115 | }); | ||
116 | |||
112 | #define ensure(patt, finally) ((patt) >> (finally) | (finally) >> cut) | 117 | #define ensure(patt, finally) ((patt) >> (finally) | (finally) >> cut) |
113 | 118 | ||
114 | #define key(str) (expr(str) >> not_alpha_num) | 119 | #define key(str) (expr(str) >> not_alpha_num) |
@@ -342,7 +347,7 @@ YueParser::YueParser() { | |||
342 | with_exp = ExpList >> -(space >> Assign); | 347 | with_exp = ExpList >> -(space >> Assign); |
343 | 348 | ||
344 | With = key("with") >> -ExistentialOp >> space >> disable_do_chain_arg_table_block_rule(with_exp) >> space >> body_with("do"); | 349 | With = key("with") >> -ExistentialOp >> space >> disable_do_chain_arg_table_block_rule(with_exp) >> space >> body_with("do"); |
345 | SwitchCase = key("when") >> space >> (disable_chain_rule(disable_arg_table_block_rule(SwitchList)) | In) >> space >> body_with("then"); | 350 | SwitchCase = key("when") >> space >> disable_chain_rule(disable_arg_table_block_rule(SwitchList)) >> space >> body_with("then"); |
346 | switch_else = key("else") >> space >> body; | 351 | switch_else = key("else") >> space >> body; |
347 | 352 | ||
348 | switch_block = | 353 | switch_block = |
@@ -449,7 +454,37 @@ YueParser::YueParser() { | |||
449 | CatchBlock = line_break >> *space_break >> check_indent_match >> space >> key("catch") >> space >> Variable >> space >> in_block; | 454 | CatchBlock = line_break >> *space_break >> check_indent_match >> space >> key("catch") >> space >> Variable >> space >> in_block; |
450 | Try = key("try") >> space >> (in_block | Exp) >> -CatchBlock; | 455 | Try = key("try") >> space >> (in_block | Exp) >> -CatchBlock; |
451 | 456 | ||
452 | Comprehension = '[' >> not_('[') >> space >> disable_for_rule(Exp) >> space >> CompInner >> space >> ']'; | 457 | list_value = |
458 | and_( | ||
459 | VariablePairDef | | ||
460 | NormalPairDef | | ||
461 | MetaVariablePairDef | | ||
462 | MetaNormalPairDef | ||
463 | ) >> table_key_pair_error | | ||
464 | SpreadListExp | | ||
465 | NormalDef; | ||
466 | |||
467 | list_value_list = +(space >> ',' >> space >> list_value); | ||
468 | |||
469 | list_lit_line = ( | ||
470 | push_indent_match >> (space >> list_value >> -list_value_list >> pop_indent | pop_indent) | ||
471 | ) | ( | ||
472 | space | ||
473 | ); | ||
474 | |||
475 | list_lit_lines = space_break >> list_lit_line >> *(-(space >> ',') >> space_break >> list_lit_line) >> -(space >> ','); | ||
476 | |||
477 | Comprehension = '[' >> not_('[') >> | ||
478 | Seperator >> space >> ( | ||
479 | disable_for_rule(list_value) >> space >> ( | ||
480 | CompInner >> space >> ']' | | ||
481 | (list_value_list >> -(space >> ',') | space >> ',') >> -list_lit_lines >> white >> ']' | ||
482 | ) | | ||
483 | list_lit_lines >> white >> ']' | | ||
484 | white >> ']' >> not_(space >> '=') | ||
485 | ); | ||
486 | |||
487 | (space >> disable_for_rule(Exp) >> space >> CompInner >> space >> ']'); | ||
453 | CompValue = ',' >> space >> Exp; | 488 | CompValue = ',' >> space >> Exp; |
454 | TblComprehension = and_('{') >> ('{' >> space >> disable_for_rule(Exp >> space >> -(CompValue >> space)) >> CompInner >> space >> '}' | braces_expression_error); | 489 | TblComprehension = and_('{') >> ('{' >> space >> disable_for_rule(Exp >> space >> -(CompValue >> space)) >> CompInner >> space >> '}' | braces_expression_error); |
455 | 490 | ||
@@ -479,12 +514,11 @@ YueParser::YueParser() { | |||
479 | expo_value = exponential_operator >> *space_break >> space >> Value; | 514 | expo_value = exponential_operator >> *space_break >> space >> Value; |
480 | expo_exp = Value >> *(space >> expo_value); | 515 | expo_exp = Value >> *(space >> expo_value); |
481 | 516 | ||
482 | InRangeOpen = true_(); | ||
483 | InRangeClose = true_(); | ||
484 | NotIn = true_(); | 517 | NotIn = true_(); |
485 | InRange = ('(' >> InRangeOpen | '[' >> InRangeClose) >> space >> Exp >> space >> ',' >> space >> Exp >> space >> (')' >> InRangeOpen | ']' >> InRangeClose); | 518 | InDiscrete = |
486 | InDiscrete = '{' >> Seperator >> space >> exp_not_tab >> *(space >> ',' >> space >> exp_not_tab) >> space >> '}'; | 519 | '[' >> Seperator >> space >> exp_not_tab >> (+(space >> ',' >> space >> exp_not_tab) | space >> ',') >> space >> ']' | |
487 | In = -(key("not") >> NotIn >> space) >> key("in") >> space >> (InRange | InDiscrete | and_(key("not")) >> confusing_unary_not_error | Exp); | 520 | '{' >> Seperator >> space >> exp_not_tab >> *(space >> ',' >> space >> exp_not_tab) >> space >> '}'; |
521 | In = -(key("not") >> NotIn >> space) >> key("in") >> space >> (InDiscrete | and_(key("not")) >> confusing_unary_not_error | Exp); | ||
488 | 522 | ||
489 | UnaryOperator = | 523 | UnaryOperator = |
490 | '-' >> not_(set(">=") | space_one) | | 524 | '-' >> not_(set(">=") | space_one) | |
@@ -635,7 +669,7 @@ YueParser::YueParser() { | |||
635 | space >> ',' >> | 669 | space >> ',' >> |
636 | space >> (Exp | DefaultValue) >> | 670 | space >> (Exp | DefaultValue) >> |
637 | space >> (',' >> space >> Exp | DefaultValue) >> | 671 | space >> (',' >> space >> Exp | DefaultValue) >> |
638 | space >> ']'; | 672 | space >> (']' | slice_expression_error); |
639 | 673 | ||
640 | Invoke = Seperator >> ( | 674 | Invoke = Seperator >> ( |
641 | fn_args | | 675 | fn_args | |
@@ -646,6 +680,7 @@ YueParser::YueParser() { | |||
646 | ); | 680 | ); |
647 | 681 | ||
648 | SpreadExp = "..." >> space >> Exp; | 682 | SpreadExp = "..." >> space >> Exp; |
683 | SpreadListExp = "..." >> space >> Exp; | ||
649 | 684 | ||
650 | table_value = | 685 | table_value = |
651 | VariablePairDef | | 686 | VariablePairDef | |
@@ -667,8 +702,7 @@ YueParser::YueParser() { | |||
667 | 702 | ||
668 | TableLit = | 703 | TableLit = |
669 | space >> '{' >> Seperator >> | 704 | space >> '{' >> Seperator >> |
670 | -(space >> table_value_list) >> | 705 | -(space >> table_value_list >> -(space >> ',')) >> |
671 | -(space >> ',') >> | ||
672 | -table_lit_lines >> | 706 | -table_lit_lines >> |
673 | white >> '}'; | 707 | white >> '}'; |
674 | 708 | ||
@@ -834,7 +868,7 @@ YueParser::YueParser() { | |||
834 | }); | 868 | }); |
835 | 869 | ||
836 | InvokeArgs = | 870 | InvokeArgs = |
837 | not_(set("-~")) >> space >> Seperator >> ( | 871 | not_(set("-~") | "[]") >> space >> Seperator >> ( |
838 | Exp >> *(space >> ',' >> space >> Exp) >> -(space >> invoke_args_with_table) | | 872 | Exp >> *(space >> ',' >> space >> Exp) >> -(space >> invoke_args_with_table) | |
839 | arg_table_block | | 873 | arg_table_block | |
840 | leading_spaces_error | 874 | leading_spaces_error |
@@ -852,6 +886,11 @@ YueParser::YueParser() { | |||
852 | return false; | 886 | return false; |
853 | }); | 887 | }); |
854 | 888 | ||
889 | slice_expression_error = pl::user(true_(), [](const item_t& item) { | ||
890 | throw ParserError("syntax error in slice expression"sv, item.begin); | ||
891 | return false; | ||
892 | }); | ||
893 | |||
855 | SimpleValue = | 894 | SimpleValue = |
856 | TableLit | ConstValue | If | Switch | Try | With | | 895 | TableLit | ConstValue | If | Switch | Try | With | |
857 | ClassDecl | ForEach | For | While | Do | | 896 | ClassDecl | ForEach | For | While | Do | |