aboutsummaryrefslogtreecommitdiff
path: root/xtask/src
diff options
context:
space:
mode:
Diffstat (limited to 'xtask/src')
-rw-r--r--xtask/src/ast_src.rs2
-rw-r--r--xtask/src/codegen/gen_syntax.rs35
-rw-r--r--xtask/src/codegen/rust.ungram27
3 files changed, 50 insertions, 14 deletions
diff --git a/xtask/src/ast_src.rs b/xtask/src/ast_src.rs
index 083727c5c..3a58217c4 100644
--- a/xtask/src/ast_src.rs
+++ b/xtask/src/ast_src.rs
@@ -100,7 +100,7 @@ pub(crate) const KINDS_SRC: KindsSrc = KindsSrc {
100 "RET_TYPE", 100 "RET_TYPE",
101 "EXTERN_CRATE", 101 "EXTERN_CRATE",
102 "MODULE", 102 "MODULE",
103 "USE_ITEM", 103 "USE",
104 "STATIC_DEF", 104 "STATIC_DEF",
105 "CONST_DEF", 105 "CONST_DEF",
106 "TRAIT_DEF", 106 "TRAIT_DEF",
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..449b0242f 100644
--- a/xtask/src/codegen/rust.ungram
+++ b/xtask/src/codegen/rust.ungram
@@ -17,7 +17,7 @@ Item =
17| TraitDef 17| TraitDef
18| TypeAliasDef 18| TypeAliasDef
19| UnionDef 19| UnionDef
20| UseItem 20| Use
21 21
22Module = 22Module =
23 Attr* Visibility? 'mod' Name 23 Attr* Visibility? 'mod' Name
@@ -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
35Use =
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