diff options
Diffstat (limited to 'xtask/src/codegen/gen_syntax.rs')
-rw-r--r-- | xtask/src/codegen/gen_syntax.rs | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/xtask/src/codegen/gen_syntax.rs b/xtask/src/codegen/gen_syntax.rs index 9b49712c1..c77fc8a8d 100644 --- a/xtask/src/codegen/gen_syntax.rs +++ b/xtask/src/codegen/gen_syntax.rs | |||
@@ -543,6 +543,10 @@ fn lower_enum(grammar: &Grammar, rule: &Rule) -> Option<Vec<String>> { | |||
543 | } | 543 | } |
544 | 544 | ||
545 | fn lower_rule(acc: &mut Vec<Field>, grammar: &Grammar, rule: &Rule) { | 545 | fn lower_rule(acc: &mut Vec<Field>, grammar: &Grammar, rule: &Rule) { |
546 | if lower_comma_list(acc, grammar, rule) { | ||
547 | return; | ||
548 | } | ||
549 | |||
546 | match rule { | 550 | match rule { |
547 | Rule::Node(node) => { | 551 | Rule::Node(node) => { |
548 | let field = Field::Node { name: grammar[*node].name.clone(), src: FieldSrc::Shorthand }; | 552 | let field = Field::Node { name: grammar[*node].name.clone(), src: FieldSrc::Shorthand }; |
@@ -595,6 +599,37 @@ fn lower_rule(acc: &mut Vec<Field>, grammar: &Grammar, rule: &Rule) { | |||
595 | } | 599 | } |
596 | } | 600 | } |
597 | 601 | ||
602 | // (T (',' T)* ','?)? | ||
603 | fn lower_comma_list(acc: &mut Vec<Field>, grammar: &Grammar, rule: &Rule) -> bool { | ||
604 | let rule = match rule { | ||
605 | Rule::Opt(it) => it, | ||
606 | _ => return false, | ||
607 | }; | ||
608 | let rule = match &**rule { | ||
609 | Rule::Seq(it) => it, | ||
610 | _ => return false, | ||
611 | }; | ||
612 | let (node, repeat, trailing_comma) = match rule.as_slice() { | ||
613 | [Rule::Node(node), Rule::Rep(repeat), Rule::Opt(trailing_comma)] => { | ||
614 | (node, repeat, trailing_comma) | ||
615 | } | ||
616 | _ => return false, | ||
617 | }; | ||
618 | let repeat = match &**repeat { | ||
619 | Rule::Seq(it) => it, | ||
620 | _ => return false, | ||
621 | }; | ||
622 | match repeat.as_slice() { | ||
623 | [comma, Rule::Node(n)] if comma == &**trailing_comma && n == node => (), | ||
624 | _ => return false, | ||
625 | } | ||
626 | let name = grammar[*node].name.clone(); | ||
627 | let label = pluralize(&to_lower_snake_case(&name)); | ||
628 | let field = Field::Node { name: label.clone(), src: FieldSrc::Many(name) }; | ||
629 | acc.push(field); | ||
630 | true | ||
631 | } | ||
632 | |||
598 | fn deduplicate_fields(ast: &mut AstSrc) { | 633 | fn deduplicate_fields(ast: &mut AstSrc) { |
599 | for node in &mut ast.nodes { | 634 | for node in &mut ast.nodes { |
600 | let mut i = 0; | 635 | let mut i = 0; |