aboutsummaryrefslogtreecommitdiff
path: root/xtask/src/codegen
diff options
context:
space:
mode:
Diffstat (limited to 'xtask/src/codegen')
-rw-r--r--xtask/src/codegen/gen_syntax.rs83
-rw-r--r--xtask/src/codegen/rust.ungram164
2 files changed, 121 insertions, 126 deletions
diff --git a/xtask/src/codegen/gen_syntax.rs b/xtask/src/codegen/gen_syntax.rs
index c77fc8a8d..e993a750c 100644
--- a/xtask/src/codegen/gen_syntax.rs
+++ b/xtask/src/codegen/gen_syntax.rs
@@ -13,7 +13,7 @@ use quote::{format_ident, quote};
13use ungrammar::{Grammar, Rule}; 13use ungrammar::{Grammar, Rule};
14 14
15use crate::{ 15use crate::{
16 ast_src::{AstEnumSrc, AstNodeSrc, AstSrc, Field, FieldSrc, KindsSrc, KINDS_SRC}, 16 ast_src::{AstEnumSrc, AstNodeSrc, AstSrc, Cardinality, Field, KindsSrc, KINDS_SRC},
17 codegen::{self, update, Mode}, 17 codegen::{self, update, Mode},
18 project_root, Result, 18 project_root, Result,
19}; 19};
@@ -307,7 +307,7 @@ fn generate_syntax_kinds(grammar: KindsSrc<'_>) -> Result<String> {
307 307
308 let ast = quote! { 308 let ast = quote! {
309 #![allow(bad_style, missing_docs, unreachable_pub)] 309 #![allow(bad_style, missing_docs, unreachable_pub)]
310 /// The kind of syntax node, e.g. `IDENT`, `USE_KW`, or `STRUCT_DEF`. 310 /// The kind of syntax node, e.g. `IDENT`, `USE_KW`, or `STRUCT`.
311 #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] 311 #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
312 #[repr(u16)] 312 #[repr(u16)]
313 pub enum SyntaxKind { 313 pub enum SyntaxKind {
@@ -431,7 +431,7 @@ fn pluralize(s: &str) -> String {
431 431
432impl Field { 432impl Field {
433 fn is_many(&self) -> bool { 433 fn is_many(&self) -> bool {
434 matches!(self, Field::Node { src: FieldSrc::Many(_), .. }) 434 matches!(self, Field::Node { cardinality: Cardinality::Many, .. })
435 } 435 }
436 fn token_kind(&self) -> Option<proc_macro2::TokenStream> { 436 fn token_kind(&self) -> Option<proc_macro2::TokenStream> {
437 match self { 437 match self {
@@ -471,23 +471,18 @@ impl Field {
471 "::" => "coloncolon", 471 "::" => "coloncolon",
472 "#" => "pound", 472 "#" => "pound",
473 "?" => "question_mark", 473 "?" => "question_mark",
474 "," => "comma",
474 _ => name, 475 _ => name,
475 }; 476 };
476 format_ident!("{}_token", name) 477 format_ident!("{}_token", name)
477 } 478 }
478 Field::Node { name, src } => match src { 479 Field::Node { name, .. } => format_ident!("{}", name),
479 FieldSrc::Shorthand => format_ident!("{}", to_lower_snake_case(name)),
480 _ => format_ident!("{}", name),
481 },
482 } 480 }
483 } 481 }
484 fn ty(&self) -> proc_macro2::Ident { 482 fn ty(&self) -> proc_macro2::Ident {
485 match self { 483 match self {
486 Field::Token(_) => format_ident!("SyntaxToken"), 484 Field::Token(_) => format_ident!("SyntaxToken"),
487 Field::Node { name, src } => match src { 485 Field::Node { ty, .. } => format_ident!("{}", ty),
488 FieldSrc::Optional(ty) | FieldSrc::Many(ty) => format_ident!("{}", ty),
489 FieldSrc::Shorthand => format_ident!("{}", name),
490 },
491 } 486 }
492 } 487 }
493} 488}
@@ -514,7 +509,7 @@ fn lower(grammar: &Grammar) -> AstSrc {
514 } 509 }
515 None => { 510 None => {
516 let mut fields = Vec::new(); 511 let mut fields = Vec::new();
517 lower_rule(&mut fields, grammar, rule); 512 lower_rule(&mut fields, grammar, None, rule);
518 res.nodes.push(AstNodeSrc { doc: Vec::new(), name, traits: Vec::new(), fields }); 513 res.nodes.push(AstNodeSrc { doc: Vec::new(), name, traits: Vec::new(), fields });
519 } 514 }
520 } 515 }
@@ -542,17 +537,20 @@ fn lower_enum(grammar: &Grammar, rule: &Rule) -> Option<Vec<String>> {
542 Some(variants) 537 Some(variants)
543} 538}
544 539
545fn lower_rule(acc: &mut Vec<Field>, grammar: &Grammar, rule: &Rule) { 540fn lower_rule(acc: &mut Vec<Field>, grammar: &Grammar, label: Option<&String>, rule: &Rule) {
546 if lower_comma_list(acc, grammar, rule) { 541 if lower_comma_list(acc, grammar, label, rule) {
547 return; 542 return;
548 } 543 }
549 544
550 match rule { 545 match rule {
551 Rule::Node(node) => { 546 Rule::Node(node) => {
552 let field = Field::Node { name: grammar[*node].name.clone(), src: FieldSrc::Shorthand }; 547 let ty = grammar[*node].name.clone();
548 let name = label.cloned().unwrap_or_else(|| to_lower_snake_case(&ty));
549 let field = Field::Node { name, ty, cardinality: Cardinality::Optional };
553 acc.push(field); 550 acc.push(field);
554 } 551 }
555 Rule::Token(token) => { 552 Rule::Token(token) => {
553 assert!(label.is_none());
556 let mut name = grammar[*token].name.clone(); 554 let mut name = grammar[*token].name.clone();
557 if name != "int_number" && name != "string" { 555 if name != "int_number" && name != "string" {
558 if "[]{}()".contains(&name) { 556 if "[]{}()".contains(&name) {
@@ -564,48 +562,35 @@ fn lower_rule(acc: &mut Vec<Field>, grammar: &Grammar, rule: &Rule) {
564 } 562 }
565 Rule::Rep(inner) => { 563 Rule::Rep(inner) => {
566 if let Rule::Node(node) = &**inner { 564 if let Rule::Node(node) = &**inner {
567 let name = grammar[*node].name.clone(); 565 let ty = grammar[*node].name.clone();
568 let label = pluralize(&to_lower_snake_case(&name)); 566 let name = label.cloned().unwrap_or_else(|| pluralize(&to_lower_snake_case(&ty)));
569 let field = Field::Node { name: label.clone(), src: FieldSrc::Many(name) }; 567 let field = Field::Node { name, ty, cardinality: Cardinality::Many };
570 acc.push(field); 568 acc.push(field);
571 return; 569 return;
572 } 570 }
573 todo!("{:?}", rule) 571 todo!("{:?}", rule)
574 } 572 }
575 Rule::Labeled { label, rule } => { 573 Rule::Labeled { label: l, rule } => {
576 let node = match &**rule { 574 assert!(label.is_none());
577 Rule::Rep(inner) | Rule::Opt(inner) => match &**inner { 575 lower_rule(acc, grammar, Some(l), rule);
578 Rule::Node(node) => node,
579 _ => todo!("{:?}", rule),
580 },
581 Rule::Node(node) => node,
582 _ => todo!("{:?}", rule),
583 };
584 let field = Field::Node {
585 name: label.clone(),
586 src: match &**rule {
587 Rule::Rep(_) => FieldSrc::Many(grammar[*node].name.clone()),
588 _ => FieldSrc::Optional(grammar[*node].name.clone()),
589 },
590 };
591 acc.push(field);
592 } 576 }
593 Rule::Seq(rules) | Rule::Alt(rules) => { 577 Rule::Seq(rules) | Rule::Alt(rules) => {
594 for rule in rules { 578 for rule in rules {
595 lower_rule(acc, grammar, rule) 579 lower_rule(acc, grammar, label, rule)
596 } 580 }
597 } 581 }
598 Rule::Opt(rule) => lower_rule(acc, grammar, rule), 582 Rule::Opt(rule) => lower_rule(acc, grammar, label, rule),
599 } 583 }
600} 584}
601 585
602// (T (',' T)* ','?)? 586// (T (',' T)* ','?)
603fn lower_comma_list(acc: &mut Vec<Field>, grammar: &Grammar, rule: &Rule) -> bool { 587fn lower_comma_list(
588 acc: &mut Vec<Field>,
589 grammar: &Grammar,
590 label: Option<&String>,
591 rule: &Rule,
592) -> bool {
604 let rule = match rule { 593 let rule = match rule {
605 Rule::Opt(it) => it,
606 _ => return false,
607 };
608 let rule = match &**rule {
609 Rule::Seq(it) => it, 594 Rule::Seq(it) => it,
610 _ => return false, 595 _ => return false,
611 }; 596 };
@@ -623,9 +608,9 @@ fn lower_comma_list(acc: &mut Vec<Field>, grammar: &Grammar, rule: &Rule) -> boo
623 [comma, Rule::Node(n)] if comma == &**trailing_comma && n == node => (), 608 [comma, Rule::Node(n)] if comma == &**trailing_comma && n == node => (),
624 _ => return false, 609 _ => return false,
625 } 610 }
626 let name = grammar[*node].name.clone(); 611 let ty = grammar[*node].name.clone();
627 let label = pluralize(&to_lower_snake_case(&name)); 612 let name = label.cloned().unwrap_or_else(|| pluralize(&to_lower_snake_case(&ty)));
628 let field = Field::Node { name: label.clone(), src: FieldSrc::Many(name) }; 613 let field = Field::Node { name, ty, cardinality: Cardinality::Many };
629 acc.push(field); 614 acc.push(field);
630 true 615 true
631} 616}
@@ -659,7 +644,9 @@ fn extract_enums(ast: &mut AstSrc) {
659 } 644 }
660 if to_remove.len() == enm.variants.len() { 645 if to_remove.len() == enm.variants.len() {
661 node.remove_field(to_remove); 646 node.remove_field(to_remove);
662 node.fields.push(Field::Node { name: enm.name.clone(), src: FieldSrc::Shorthand }); 647 let ty = enm.name.clone();
648 let name = to_lower_snake_case(&ty);
649 node.fields.push(Field::Node { name, ty, cardinality: Cardinality::Optional });
663 } 650 }
664 } 651 }
665 } 652 }
@@ -670,7 +657,7 @@ fn extract_struct_traits(ast: &mut AstSrc) {
670 ("AttrsOwner", &["attrs"]), 657 ("AttrsOwner", &["attrs"]),
671 ("NameOwner", &["name"]), 658 ("NameOwner", &["name"]),
672 ("VisibilityOwner", &["visibility"]), 659 ("VisibilityOwner", &["visibility"]),
673 ("TypeParamsOwner", &["type_param_list", "where_clause"]), 660 ("GenericParamsOwner", &["generic_param_list", "where_clause"]),
674 ("TypeBoundsOwner", &["type_bound_list", "colon_token"]), 661 ("TypeBoundsOwner", &["type_bound_list", "colon_token"]),
675 ("ModuleItemOwner", &["items"]), 662 ("ModuleItemOwner", &["items"]),
676 ("TypeAscriptionOwner", &["ascribed_type"]), 663 ("TypeAscriptionOwner", &["ascribed_type"]),
diff --git a/xtask/src/codegen/rust.ungram b/xtask/src/codegen/rust.ungram
index 449b0242f..ef7c3e50e 100644
--- a/xtask/src/codegen/rust.ungram
+++ b/xtask/src/codegen/rust.ungram
@@ -4,19 +4,19 @@ SourceFile =
4 Item* 4 Item*
5 5
6Item = 6Item =
7 ConstDef 7 Const
8| EnumDef 8| Enum
9| ExternBlock 9| ExternBlock
10| ExternCrate 10| ExternCrate
11| FnDef 11| Fn
12| ImplDef 12| ImplDef
13| MacroCall 13| MacroCall
14| Module 14| Module
15| StaticDef 15| Static
16| StructDef 16| Struct
17| TraitDef 17| TraitDef
18| TypeAliasDef 18| TypeAlias
19| UnionDef 19| Union
20| Use 20| Use
21 21
22Module = 22Module =
@@ -42,73 +42,94 @@ UseTree =
42UseTreeList = 42UseTreeList =
43 '{' (UseTree (',' UseTree)* ','?)? '}' 43 '{' (UseTree (',' UseTree)* ','?)? '}'
44 44
45FnDef = 45Fn =
46 Attr* Visibility? Abi? 'const' 'default' 'async' 'unsafe' 'fn' Name TypeParamList? 46 Attr* Visibility?
47 ParamList RetType? 47 'default'? ('async' | 'const')? 'unsafe'? Abi?
48 'fn' Name GenericParamList? ParamList RetType?
48 WhereClause? 49 WhereClause?
49 (body:BlockExpr | ';') 50 (body:BlockExpr | ';')
50 51
52Abi =
53 'extern' 'string'?
54
55ParamList =
56 '('(
57 (Param (',' Param)* ','?)?
58 | SelfParam ','?
59 | SelfParam ',' (Param (',' Param)* ','?)
60 )')'
61
62SelfParam =
63 Attr* (
64 ('&' 'lifetime'?)? 'mut'? 'self'
65 | 'mut'? 'self' ':' ascribed_type:TypeRef
66 )
67
68Param =
69 Attr* Pat (':' ascribed_type:TypeRef)
70| '...'
71
51RetType = 72RetType =
52 '->' TypeRef 73 '->' TypeRef
53 74
54StructDef = 75TypeAlias =
55 Attr* Visibility? 'struct' Name TypeParamList? ( 76 Attr* Visibility? 'default'? 'type' Name GenericParamList? (':' TypeBoundList?)? WhereClause?
56 WhereClause? (RecordFieldDefList | ';') 77 '=' TypeRef ';'
57 | TupleFieldDefList WhereClause? ';'
58 )
59 78
60UnionDef = 79Struct =
61 Attr* Visibility? 'union' Name TypeParamList? WhereClause? 80 Attr* Visibility? 'struct' Name GenericParamList? (
62 RecordFieldDefList 81 WhereClause? (RecordFieldList | ';')
82 | TupleFieldList WhereClause? ';'
83 )
63 84
64RecordFieldDefList = 85RecordFieldList =
65 '{' fields:RecordFieldDef* '}' 86 '{' fields:(RecordField (',' RecordField)* ','?)? '}'
66 87
67RecordFieldDef = 88RecordField =
68 Attr* Visibility? Name ':' ascribed_type:TypeRef 89 Attr* Visibility? Name ':' ascribed_type:TypeRef
69 90
70TupleFieldDefList = 91TupleFieldList =
71 '(' fields:TupleFieldDef* ')' 92 '(' fields:(TupleField (',' TupleField)* ','?)? ')'
93
94TupleField =
95 Attr* Visibility? TypeRef
72 96
73TupleFieldDef = 97FieldList =
74 Attr* Visibility? Name TypeRef 98 RecordFieldList
99| TupleFieldList
75 100
76FieldDefList = 101Enum =
77 RecordFieldDefList 102 Attr* Visibility? 'enum' Name GenericParamList? WhereClause?
78| TupleFieldDefList 103 VariantList
79 104
80EnumDef = 105VariantList =
81 Attr* Visibility? 'enum' Name TypeParamList? WhereClause? 106 '{' (Variant (',' Variant)* ','?)? '}'
82 variant_list:EnumVariantList
83 107
84EnumVariantList = 108Variant =
85 '{' variants:EnumVariant* '}' 109 Attr* Visibility? Name FieldList ('=' Expr)?
86 110
87EnumVariant = 111Union =
88 Attr* Visibility? Name FieldDefList ('=' Expr)? 112 Attr* Visibility? 'union' Name GenericParamList? WhereClause?
113 RecordFieldList
114
115Const =
116 Attr* Visibility? 'default'? 'const' (Name | '_') ':' ascribed_type:TypeRef
117 '=' body:Expr ';'
118
119Static =
120 Attr* Visibility? 'static'? 'mut'? Name ':' ascribed_type:TypeRef
121 '=' body:Expr ';'
89 122
90TraitDef = 123TraitDef =
91 Attr* Visibility? 'unsafe'? 'auto'? 'trait' Name TypeParamList 124 Attr* Visibility? 'unsafe'? 'auto'? 'trait' Name GenericParamList
92 (':' TypeBoundList?)? WhereClause 125 (':' TypeBoundList?)? WhereClause
93 AssocItemList 126 AssocItemList
94 127
95AssocItemList = 128AssocItemList =
96 '{' AssocItem* '}' 129 '{' AssocItem* '}'
97 130
98ConstDef =
99 Attr* Visibility? 'default'? 'const' Name ':' ascribed_type:TypeRef
100 '=' body:Expr ';'
101
102StaticDef =
103 Attr* Visibility? 'static'? 'mut'? 'static' Name ':' ascribed_type:TypeRef
104 '=' body:Expr ';'
105
106TypeAliasDef =
107 Attr* Visibility? 'default'? 'type' Name TypeParamList? WhereClause? (':' TypeBoundList?)?
108 '=' TypeRef ';'
109
110ImplDef = 131ImplDef =
111 Attr* Visibility? 'const'? 'default'? 'unsafe'? 'impl' TypeParamList? '!'? 'for' 132 Attr* Visibility? 'const'? 'default'? 'unsafe'? 'impl' GenericParamList? '!'? 'for'
112 WhereClause? 133 WhereClause?
113 AssocItemList 134 AssocItemList
114 135
@@ -143,7 +164,7 @@ FnPointerType =
143 Abi 'unsafe'? 'fn' ParamList RetType? 164 Abi 'unsafe'? 'fn' ParamList RetType?
144 165
145ForType = 166ForType =
146 'for' TypeParamList TypeRef 167 'for' GenericParamList TypeRef
147 168
148ImplTraitType = 169ImplTraitType =
149 'impl' TypeBoundList 170 'impl' TypeBoundList
@@ -263,16 +284,16 @@ MatchArm =
263MatchGuard = 284MatchGuard =
264 'if' Expr 285 'if' Expr
265 286
266RecordLit = 287RecordExpr =
267 Path RecordFieldList 288 Path RecordExprFieldList
268 289
269RecordFieldList = 290RecordExprFieldList =
270 '{' 291 '{'
271 fields:RecordField* 292 fields:RecordExprField*
272 ('..' spread:Expr)? 293 ('..' spread:Expr)?
273 '}' 294 '}'
274 295
275RecordField = 296RecordExprField =
276 Attr* NameRef (':' Expr)? 297 Attr* NameRef (':' Expr)?
277 298
278OrPat = 299OrPat =
@@ -358,7 +379,7 @@ MacroStmts =
358Attr = 379Attr =
359 '#' '!'? '[' Path ('=' input:AttrInput)? ']' 380 '#' '!'? '[' Path ('=' input:AttrInput)? ']'
360 381
361TypeParamList = 382GenericParamList =
362 '<' 383 '<'
363 TypeParam* 384 TypeParam*
364 LifetimeParam* 385 LifetimeParam*
@@ -383,14 +404,11 @@ TypeBoundList =
383 bounds:TypeBound* 404 bounds:TypeBound*
384 405
385WherePred = 406WherePred =
386 ('for' TypeParamList)? ('lifetime' | TypeRef) ':' TypeBoundList 407 ('for' GenericParamList)? ('lifetime' | TypeRef) ':' TypeBoundList
387 408
388WhereClause = 409WhereClause =
389 'where' predicates:WherePred* 410 'where' predicates:WherePred*
390 411
391Abi =
392 'string'
393
394ExprStmt = 412ExprStmt =
395 Attr* Expr ';' 413 Attr* Expr ';'
396 414
@@ -398,16 +416,6 @@ LetStmt =
398 Attr* 'let' Pat (':' ascribed_type:TypeRef) 416 Attr* 'let' Pat (':' ascribed_type:TypeRef)
399 '=' initializer:Expr ';' 417 '=' initializer:Expr ';'
400 418
401ParamList =
402 '(' SelfParam Param* ')'
403
404SelfParam =
405 Attr* ('&' 'lifetime'?)? 'mut'? 'self' (':' ascribed_type:TypeRef)
406
407Param =
408 Attr* Pat (':' ascribed_type:TypeRef)
409| '...'
410
411Path = 419Path =
412 (qualifier:Path '::')? segment:PathSegment 420 (qualifier:Path '::')? segment:PathSegment
413 421
@@ -445,9 +453,9 @@ MetaItem =
445 Path '=' AttrInput nested_meta_items:MetaItem* 453 Path '=' AttrInput nested_meta_items:MetaItem*
446 454
447AdtDef = 455AdtDef =
448 StructDef 456 Struct
449| EnumDef 457| Enum
450| UnionDef 458| Union
451 459
452TypeRef = 460TypeRef =
453 ParenType 461 ParenType
@@ -465,13 +473,13 @@ TypeRef =
465| DynTraitType 473| DynTraitType
466 474
467AssocItem = 475AssocItem =
468 FnDef 476 Fn
469| TypeAliasDef 477| TypeAlias
470| ConstDef 478| Const
471| MacroCall 479| MacroCall
472 480
473ExternItem = 481ExternItem =
474 FnDef | StaticDef 482 Fn | Static
475 483
476AttrInput = 484AttrInput =
477 Literal 485 Literal
@@ -514,7 +522,7 @@ Expr =
514| BlockExpr 522| BlockExpr
515| ReturnExpr 523| ReturnExpr
516| MatchExpr 524| MatchExpr
517| RecordLit 525| RecordExpr
518| CallExpr 526| CallExpr
519| IndexExpr 527| IndexExpr
520| MethodCallExpr 528| MethodCallExpr