aboutsummaryrefslogtreecommitdiff
path: root/xtask
diff options
context:
space:
mode:
Diffstat (limited to 'xtask')
-rw-r--r--xtask/src/ast_src.rs9
-rw-r--r--xtask/src/codegen/gen_syntax.rs42
2 files changed, 25 insertions, 26 deletions
diff --git a/xtask/src/ast_src.rs b/xtask/src/ast_src.rs
index ad661218f..0e3d89a07 100644
--- a/xtask/src/ast_src.rs
+++ b/xtask/src/ast_src.rs
@@ -242,14 +242,13 @@ pub(crate) struct AstNodeSrc {
242#[derive(Debug, Eq, PartialEq)] 242#[derive(Debug, Eq, PartialEq)]
243pub(crate) enum Field { 243pub(crate) enum Field {
244 Token(String), 244 Token(String),
245 Node { name: String, src: FieldSrc }, 245 Node { name: String, ty: String, valence: Valence },
246} 246}
247 247
248#[derive(Debug, Eq, PartialEq)] 248#[derive(Debug, Eq, PartialEq)]
249pub(crate) enum FieldSrc { 249pub(crate) enum Valence {
250 Shorthand, 250 Optional,
251 Optional(String), 251 Many,
252 Many(String),
253} 252}
254 253
255#[derive(Debug)] 254#[derive(Debug)]
diff --git a/xtask/src/codegen/gen_syntax.rs b/xtask/src/codegen/gen_syntax.rs
index f79cd972e..b435d8a9c 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, Field, KindsSrc, Valence, KINDS_SRC},
17 codegen::{self, update, Mode}, 17 codegen::{self, update, Mode},
18 project_root, Result, 18 project_root, Result,
19}; 19};
@@ -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 { valence: Valence::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 {
@@ -476,19 +476,13 @@ impl Field {
476 }; 476 };
477 format_ident!("{}_token", name) 477 format_ident!("{}_token", name)
478 } 478 }
479 Field::Node { name, src } => match src { 479 Field::Node { name, .. } => format_ident!("{}", name),
480 FieldSrc::Shorthand => format_ident!("{}", to_lower_snake_case(name)),
481 _ => format_ident!("{}", name),
482 },
483 } 480 }
484 } 481 }
485 fn ty(&self) -> proc_macro2::Ident { 482 fn ty(&self) -> proc_macro2::Ident {
486 match self { 483 match self {
487 Field::Token(_) => format_ident!("SyntaxToken"), 484 Field::Token(_) => format_ident!("SyntaxToken"),
488 Field::Node { name, src } => match src { 485 Field::Node { ty, .. } => format_ident!("{}", ty),
489 FieldSrc::Optional(ty) | FieldSrc::Many(ty) => format_ident!("{}", ty),
490 FieldSrc::Shorthand => format_ident!("{}", name),
491 },
492 } 486 }
493 } 487 }
494} 488}
@@ -550,7 +544,9 @@ fn lower_rule(acc: &mut Vec<Field>, grammar: &Grammar, rule: &Rule) {
550 544
551 match rule { 545 match rule {
552 Rule::Node(node) => { 546 Rule::Node(node) => {
553 let field = Field::Node { name: grammar[*node].name.clone(), src: FieldSrc::Shorthand }; 547 let ty = grammar[*node].name.clone();
548 let name = to_lower_snake_case(&ty);
549 let field = Field::Node { name, ty, valence: Valence::Optional };
554 acc.push(field); 550 acc.push(field);
555 } 551 }
556 Rule::Token(token) => { 552 Rule::Token(token) => {
@@ -565,9 +561,9 @@ fn lower_rule(acc: &mut Vec<Field>, grammar: &Grammar, rule: &Rule) {
565 } 561 }
566 Rule::Rep(inner) => { 562 Rule::Rep(inner) => {
567 if let Rule::Node(node) = &**inner { 563 if let Rule::Node(node) = &**inner {
568 let name = grammar[*node].name.clone(); 564 let ty = grammar[*node].name.clone();
569 let label = pluralize(&to_lower_snake_case(&name)); 565 let name = pluralize(&to_lower_snake_case(&ty));
570 let field = Field::Node { name: label.clone(), src: FieldSrc::Many(name) }; 566 let field = Field::Node { name, ty, valence: Valence::Many };
571 acc.push(field); 567 acc.push(field);
572 return; 568 return;
573 } 569 }
@@ -582,11 +578,13 @@ fn lower_rule(acc: &mut Vec<Field>, grammar: &Grammar, rule: &Rule) {
582 Rule::Node(node) => node, 578 Rule::Node(node) => node,
583 _ => todo!("{:?}", rule), 579 _ => todo!("{:?}", rule),
584 }; 580 };
581 let ty = grammar[*node].name.clone();
585 let field = Field::Node { 582 let field = Field::Node {
586 name: label.clone(), 583 name: label.clone(),
587 src: match &**rule { 584 ty,
588 Rule::Rep(_) => FieldSrc::Many(grammar[*node].name.clone()), 585 valence: match &**rule {
589 _ => FieldSrc::Optional(grammar[*node].name.clone()), 586 Rule::Rep(_) => Valence::Many,
587 _ => Valence::Optional,
590 }, 588 },
591 }; 589 };
592 acc.push(field); 590 acc.push(field);
@@ -620,9 +618,9 @@ fn lower_comma_list(acc: &mut Vec<Field>, grammar: &Grammar, rule: &Rule) -> boo
620 [comma, Rule::Node(n)] if comma == &**trailing_comma && n == node => (), 618 [comma, Rule::Node(n)] if comma == &**trailing_comma && n == node => (),
621 _ => return false, 619 _ => return false,
622 } 620 }
623 let name = grammar[*node].name.clone(); 621 let ty = grammar[*node].name.clone();
624 let label = pluralize(&to_lower_snake_case(&name)); 622 let name = pluralize(&to_lower_snake_case(&ty));
625 let field = Field::Node { name: label.clone(), src: FieldSrc::Many(name) }; 623 let field = Field::Node { name, ty, valence: Valence::Many };
626 acc.push(field); 624 acc.push(field);
627 true 625 true
628} 626}
@@ -656,7 +654,9 @@ fn extract_enums(ast: &mut AstSrc) {
656 } 654 }
657 if to_remove.len() == enm.variants.len() { 655 if to_remove.len() == enm.variants.len() {
658 node.remove_field(to_remove); 656 node.remove_field(to_remove);
659 node.fields.push(Field::Node { name: enm.name.clone(), src: FieldSrc::Shorthand }); 657 let ty = enm.name.clone();
658 let name = to_lower_snake_case(&ty);
659 node.fields.push(Field::Node { name, ty, valence: Valence::Optional });
660 } 660 }
661 } 661 }
662 } 662 }