Skip to content

Commit 6803bf3

Browse files
authored
Merge pull request #19851 from github/redsun82/rust-emission-trait
Rust: refactor `pre_emit!` and `post_emit!` to a trait
2 parents 942cfc3 + d0c7550 commit 6803bf3

File tree

8 files changed

+369
-459
lines changed

8 files changed

+369
-459
lines changed

rust/ast-generator/src/main.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,32 @@ fn property_name(type_name: &str, field_name: &str) -> String {
5252
name.to_owned()
5353
}
5454

55+
fn has_special_emission(type_name: &str) -> bool {
56+
matches!(
57+
type_name,
58+
"Item"
59+
| "AssocItem"
60+
| "ExternItem"
61+
| "Meta"
62+
| "MacroCall"
63+
| "Fn"
64+
| "Struct"
65+
| "Enum"
66+
| "Union"
67+
| "Trait"
68+
| "Module"
69+
| "Variant"
70+
| "PathExpr"
71+
| "RecordExpr"
72+
| "PathPat"
73+
| "RecordPat"
74+
| "TupleStructPat"
75+
| "MethodCallExpr"
76+
| "PathSegment"
77+
| "Const"
78+
)
79+
}
80+
5581
fn to_lower_snake_case(s: &str) -> String {
5682
let mut buf = String::with_capacity(s.len());
5783
let mut prev = false;
@@ -355,6 +381,7 @@ struct ExtractorEnumInfo {
355381
snake_case_name: String,
356382
ast_name: String,
357383
variants: Vec<EnumVariantInfo>,
384+
has_special_emission: bool,
358385
}
359386

360387
#[derive(Serialize, Default)]
@@ -376,6 +403,7 @@ struct ExtractorNodeInfo {
376403
ast_name: String,
377404
fields: Vec<ExtractorNodeFieldInfo>,
378405
has_attrs: bool,
406+
has_special_emission: bool,
379407
}
380408

381409
#[derive(Serialize)]
@@ -406,6 +434,7 @@ fn enum_to_extractor_info(node: &AstEnumSrc) -> Option<ExtractorEnumInfo> {
406434
}
407435
})
408436
.collect(),
437+
has_special_emission: has_special_emission(&node.name),
409438
})
410439
}
411440

@@ -460,6 +489,7 @@ fn node_to_extractor_info(node: &AstNodeSrc) -> ExtractorNodeInfo {
460489
ast_name: node.name.clone(),
461490
fields,
462491
has_attrs,
492+
has_special_emission: has_special_emission(&node.name),
463493
}
464494
}
465495

rust/ast-generator/templates/extractor.mustache

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
//! Generated by `ast-generator`, do not edit by hand.
22

33
use super::base::Translator;
4-
use super::mappings::TextValue;
5-
use crate::{pre_emit,post_emit};
4+
use super::mappings::{TextValue, HasTrapClass, Emission};
65
use crate::generated;
76
use crate::trap::{Label, TrapId};
87
use ra_ap_syntax::ast::{
@@ -13,29 +12,23 @@ use ra_ap_syntax::ast::{
1312
use ra_ap_syntax::{AstNode, ast};
1413

1514
impl Translator<'_> {
16-
fn emit_else_branch(&mut self, node: &ast::ElseBranch) -> Option<Label<generated::Expr>> {
17-
match node {
18-
ast::ElseBranch::IfExpr(inner) => self.emit_if_expr(inner).map(Into::into),
19-
ast::ElseBranch::Block(inner) => self.emit_block_expr(inner).map(Into::into),
20-
}
21-
}
2215
{{#enums}}
2316

2417
pub(crate) fn emit_{{snake_case_name}}(&mut self, node: &ast::{{ast_name}}) -> Option<Label<generated::{{name}}>> {
25-
pre_emit!({{name}}, self, node);
18+
{{>pre_emission}}
2619
let label = match node {
2720
{{#variants}}
2821
ast::{{ast_name}}::{{variant_ast_name}}(inner) => self.emit_{{snake_case_name}}(inner).map(Into::into),
2922
{{/variants}}
3023
}?;
31-
post_emit!({{name}}, self, node, label);
24+
{{>post_emission}}
3225
Some(label)
3326
}
3427
{{/enums}}
3528
{{#nodes}}
3629

3730
pub(crate) fn emit_{{snake_case_name}}(&mut self, node: &ast::{{ast_name}}) -> Option<Label<generated::{{name}}>> {
38-
pre_emit!({{name}}, self, node);
31+
{{>pre_emission}}
3932
{{#has_attrs}}
4033
if self.should_be_excluded(node) { return None; }
4134
{{/has_attrs}}
@@ -65,9 +58,15 @@ impl Translator<'_> {
6558
{{/fields}}
6659
});
6760
self.emit_location(label, node);
68-
post_emit!({{name}}, self, node, label);
61+
{{>post_emission}}
6962
self.emit_tokens(node, label.into(), node.syntax().children_with_tokens());
7063
Some(label)
7164
}
7265
{{/nodes}}
7366
}
67+
{{#enums}}
68+
{{>trap_class_mapping}}
69+
{{/enums}}
70+
{{#nodes}}
71+
{{>trap_class_mapping}}
72+
{{/nodes}}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{{#has_special_emission}}
2+
self.post_emit(node, label);
3+
{{/has_special_emission}}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{{#has_special_emission}}
2+
if let Some(label) = self.pre_emit(node) {
3+
return Some(label);
4+
}
5+
{{/has_special_emission}}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{{#has_special_emission}}
2+
3+
impl HasTrapClass for ast::{{ast_name}} {
4+
type TrapClass = generated::{{name}};
5+
}
6+
{{/has_special_emission}}

0 commit comments

Comments
 (0)