Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions rust/ast-generator/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,32 @@ fn property_name(type_name: &str, field_name: &str) -> String {
name.to_owned()
}

fn has_special_emission(type_name: &str) -> bool {
matches!(
type_name,
"Item"
| "AssocItem"
| "ExternItem"
| "Meta"
| "MacroCall"
| "Fn"
| "Struct"
| "Enum"
| "Union"
| "Trait"
| "Module"
| "Variant"
| "PathExpr"
| "RecordExpr"
| "PathPat"
| "RecordPat"
| "TupleStructPat"
| "MethodCallExpr"
| "PathSegment"
| "Const"
)
}

fn to_lower_snake_case(s: &str) -> String {
let mut buf = String::with_capacity(s.len());
let mut prev = false;
Expand Down Expand Up @@ -355,6 +381,7 @@ struct ExtractorEnumInfo {
snake_case_name: String,
ast_name: String,
variants: Vec<EnumVariantInfo>,
has_special_emission: bool,
}

#[derive(Serialize, Default)]
Expand All @@ -376,6 +403,7 @@ struct ExtractorNodeInfo {
ast_name: String,
fields: Vec<ExtractorNodeFieldInfo>,
has_attrs: bool,
has_special_emission: bool,
}

#[derive(Serialize)]
Expand Down Expand Up @@ -406,6 +434,7 @@ fn enum_to_extractor_info(node: &AstEnumSrc) -> Option<ExtractorEnumInfo> {
}
})
.collect(),
has_special_emission: has_special_emission(&node.name),
})
}

Expand Down Expand Up @@ -460,6 +489,7 @@ fn node_to_extractor_info(node: &AstNodeSrc) -> ExtractorNodeInfo {
ast_name: node.name.clone(),
fields,
has_attrs,
has_special_emission: has_special_emission(&node.name),
}
}

Expand Down
23 changes: 11 additions & 12 deletions rust/ast-generator/templates/extractor.mustache
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
//! Generated by `ast-generator`, do not edit by hand.

use super::base::Translator;
use super::mappings::TextValue;
use crate::{pre_emit,post_emit};
use super::mappings::{TextValue, HasTrapClass, Emission};
use crate::generated;
use crate::trap::{Label, TrapId};
use ra_ap_syntax::ast::{
Expand All @@ -13,29 +12,23 @@ use ra_ap_syntax::ast::{
use ra_ap_syntax::{AstNode, ast};

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

pub(crate) fn emit_{{snake_case_name}}(&mut self, node: &ast::{{ast_name}}) -> Option<Label<generated::{{name}}>> {
pre_emit!({{name}}, self, node);
{{>pre_emission}}
let label = match node {
{{#variants}}
ast::{{ast_name}}::{{variant_ast_name}}(inner) => self.emit_{{snake_case_name}}(inner).map(Into::into),
{{/variants}}
}?;
post_emit!({{name}}, self, node, label);
{{>post_emission}}
Some(label)
}
{{/enums}}
{{#nodes}}

pub(crate) fn emit_{{snake_case_name}}(&mut self, node: &ast::{{ast_name}}) -> Option<Label<generated::{{name}}>> {
pre_emit!({{name}}, self, node);
{{>pre_emission}}
{{#has_attrs}}
if self.should_be_excluded(node) { return None; }
{{/has_attrs}}
Expand Down Expand Up @@ -65,9 +58,15 @@ impl Translator<'_> {
{{/fields}}
});
self.emit_location(label, node);
post_emit!({{name}}, self, node, label);
{{>post_emission}}
self.emit_tokens(node, label.into(), node.syntax().children_with_tokens());
Some(label)
}
{{/nodes}}
}
{{#enums}}
{{>trap_class_mapping}}
{{/enums}}
{{#nodes}}
{{>trap_class_mapping}}
{{/nodes}}
3 changes: 3 additions & 0 deletions rust/ast-generator/templates/post_emission.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{{#has_special_emission}}
self.post_emit(node, label);
{{/has_special_emission}}
5 changes: 5 additions & 0 deletions rust/ast-generator/templates/pre_emission.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{{#has_special_emission}}
if let Some(label) = self.pre_emit(node) {
return Some(label);
}
{{/has_special_emission}}
6 changes: 6 additions & 0 deletions rust/ast-generator/templates/trap_class_mapping.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{{#has_special_emission}}

impl HasTrapClass for ast::{{ast_name}} {
type TrapClass = generated::{{name}};
}
{{/has_special_emission}}
Loading