aboutsummaryrefslogtreecommitdiff
path: root/xtask
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-07-30 13:06:04 +0100
committerAleksey Kladov <[email protected]>2020-07-30 13:20:33 +0100
commite381c02ef304fdeafde1c94afd1a10c2085ab716 (patch)
treed4335d349b6c63720f598a21fd1848aea56548a5 /xtask
parent9042009b7f1ba0f85e892ac5184fa4542d0c10f5 (diff)
Add comma list to use tree
Diffstat (limited to 'xtask')
-rw-r--r--xtask/src/codegen/gen_syntax.rs35
-rw-r--r--xtask/src/codegen/rust.ungram25
2 files changed, 48 insertions, 12 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
545fn lower_rule(acc: &mut Vec<Field>, grammar: &Grammar, rule: &Rule) { 545fn 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)* ','?)?
603fn 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
598fn deduplicate_fields(ast: &mut AstSrc) { 633fn 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;
diff --git a/xtask/src/codegen/rust.ungram b/xtask/src/codegen/rust.ungram
index e6e7c7518..2ba68457f 100644
--- a/xtask/src/codegen/rust.ungram
+++ b/xtask/src/codegen/rust.ungram
@@ -29,6 +29,19 @@ ItemList =
29ExternCrate = 29ExternCrate =
30 Attr* Visibility? 'extern' 'crate' (NameRef | 'self') Rename? ';' 30 Attr* Visibility? 'extern' 'crate' (NameRef | 'self') Rename? ';'
31 31
32Rename =
33 'as' (Name | '_')
34
35UseItem =
36 Attr* Visibility? 'use' UseTree ';'
37
38UseTree =
39 (Path? '::')? ('*' | UseTreeList )
40| Path Rename?
41
42UseTreeList =
43 '{' (UseTree (',' UseTree)* ','?)? '}'
44
32FnDef = 45FnDef =
33 Attr* Visibility? Abi? 'const' 'default' 'async' 'unsafe' 'fn' Name TypeParamList? 46 Attr* Visibility? Abi? 'const' 'default' 'async' 'unsafe' 'fn' Name TypeParamList?
34 ParamList RetType? 47 ParamList RetType?
@@ -395,18 +408,6 @@ Param =
395 Attr* Pat (':' ascribed_type:TypeRef) 408 Attr* Pat (':' ascribed_type:TypeRef)
396| '...' 409| '...'
397 410
398UseItem =
399 Attr* Visibility? 'use' UseTree ';'
400
401UseTree =
402 Path ('::' ('*' | UseTreeList)) Rename?
403
404UseTreeList =
405 '{' UseTree* '}'
406
407Rename =
408 'as' Name
409
410Path = 411Path =
411 (qualifier:Path '::')? segment:PathSegment 412 (qualifier:Path '::')? segment:PathSegment
412 413