Rust: add MacroStmts and MacroItems

This commit is contained in:
Arthur Baars
2024-10-01 15:17:17 +02:00
parent 7986fc7036
commit 69f0e8bcf7
3 changed files with 31 additions and 3 deletions

View File

@@ -538,9 +538,6 @@ fn main() -> std::io::Result<()> {
.parse()
.unwrap();
let mut grammar = codegen::grammar::lower(&grammar);
grammar
.nodes
.retain(|x| x.name != "MacroStmts" && x.name != "MacroItems");
grammar.enums.retain(|x| x.name != "Adt");

View File

@@ -1004,6 +1004,17 @@ impl Translator {
label
}
pub(crate) fn emit_macro_items(&mut self, node: ast::MacroItems) -> Label<generated::MacroItems> {
let items = node.items().map(|x| self.emit_item(x)).collect();
let label = self.trap.emit(generated::MacroItems {
id: TrapId::Star,
items,
});
self.emit_location(label, &node);
self.emit_tokens(label.into(), node.syntax().children_with_tokens());
label
}
pub(crate) fn emit_macro_pat(&mut self, node: ast::MacroPat) -> Label<generated::MacroPat> {
let macro_call = node.macro_call().map(|x| self.emit_macro_call(x));
let label = self.trap.emit(generated::MacroPat {
@@ -1032,6 +1043,19 @@ impl Translator {
label
}
pub(crate) fn emit_macro_stmts(&mut self, node: ast::MacroStmts) -> Label<generated::MacroStmts> {
let expr = node.expr().map(|x| self.emit_expr(x));
let statements = node.statements().map(|x| self.emit_stmt(x)).collect();
let label = self.trap.emit(generated::MacroStmts {
id: TrapId::Star,
expr,
statements,
});
self.emit_location(label, &node);
self.emit_tokens(label.into(), node.syntax().children_with_tokens());
label
}
pub(crate) fn emit_macro_type(&mut self, node: ast::MacroType) -> Label<generated::MacroType> {
let macro_call = node.macro_call().map(|x| self.emit_macro_call(x));
let label = self.trap.emit(generated::MacroType {

7
rust/schema/ast.py generated
View File

@@ -304,6 +304,9 @@ class MacroDef(Item):
class MacroExpr(Expr):
macro_call: optional["MacroCall"] | child
class MacroItems(AstNode):
items: list["Item"] | child
class MacroPat(Pat):
macro_call: optional["MacroCall"] | child
@@ -313,6 +316,10 @@ class MacroRules(Item):
token_tree: optional["TokenTree"] | child
visibility: optional["Visibility"] | child
class MacroStmts(AstNode):
expr: optional["Expr"] | child
statements: list["Stmt"] | child
class MacroType(TypeRef):
macro_call: optional["MacroCall"] | child