diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2020-07-30 16:11:20 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2020-07-30 16:11:20 +0100 |
commit | 61d9d6caf89dd59af5435adecb5c76fe0bba42a1 (patch) | |
tree | 2d9a5573f667fd15d27cffc471d98612a7343d1e /xtask/src/codegen/gen_syntax.rs | |
parent | a257fd06b36d2e7310e5e38823f6636343a37331 (diff) | |
parent | d549f6164c89bed66432ab5e5ea6e38cc8b4da6b (diff) |
Merge #5598
5598: Simplify codegen r=matklad a=matklad
bors r+
🤖
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'xtask/src/codegen/gen_syntax.rs')
-rw-r--r-- | xtask/src/codegen/gen_syntax.rs | 42 |
1 files changed, 21 insertions, 21 deletions
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}; | |||
13 | use ungrammar::{Grammar, Rule}; | 13 | use ungrammar::{Grammar, Rule}; |
14 | 14 | ||
15 | use crate::{ | 15 | use 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 | ||
432 | impl Field { | 432 | impl 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 | } |