Fix up the syntax tree for macro 2.0

This commit is contained in:
Lukas Wirth 2024-07-03 10:41:19 +02:00
parent 26c7bfd0b4
commit 013b6a883f
9 changed files with 226 additions and 169 deletions

View file

@ -6,7 +6,8 @@ use crate::{
ast::{
self,
operators::{ArithOp, BinaryOp, CmpOp, LogicOp, Ordering, RangeOp, UnaryOp},
support, AstChildren, AstNode,
support, ArgList, AstChildren, AstNode, BlockExpr, ClosureExpr, Const, Expr, Fn,
FormatArgsArg, FormatArgsExpr, MacroDef, Static, TokenTree,
},
AstToken,
SyntaxKind::*,
@ -435,3 +436,57 @@ impl AstNode for CallableExpr {
}
}
}
impl MacroDef {
fn tts(&self) -> (Option<ast::TokenTree>, Option<ast::TokenTree>) {
let mut types = support::children(self.syntax());
let first = types.next();
let second = types.next();
(first, second)
}
pub fn args(&self) -> Option<TokenTree> {
match self.tts() {
(Some(args), Some(_)) => Some(args),
_ => None,
}
}
pub fn body(&self) -> Option<TokenTree> {
match self.tts() {
(Some(body), None) | (_, Some(body)) => Some(body),
_ => None,
}
}
}
impl ClosureExpr {
pub fn body(&self) -> Option<Expr> {
support::child(&self.syntax)
}
}
impl Const {
pub fn body(&self) -> Option<Expr> {
support::child(&self.syntax)
}
}
impl Fn {
pub fn body(&self) -> Option<BlockExpr> {
support::child(&self.syntax)
}
}
impl Static {
pub fn body(&self) -> Option<Expr> {
support::child(&self.syntax)
}
}
impl FormatArgsExpr {
pub fn args(&self) -> AstChildren<FormatArgsArg> {
support::children(&self.syntax)
}
}
impl ArgList {
pub fn args(&self) -> AstChildren<Expr> {
support::children(&self.syntax)
}
}

View file

@ -20,7 +20,6 @@ pub struct ArgList {
pub(crate) syntax: SyntaxNode,
}
impl ArgList {
pub fn args(&self) -> AstChildren<Expr> { support::children(&self.syntax) }
pub fn l_paren_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['(']) }
pub fn r_paren_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![')']) }
}
@ -191,7 +190,6 @@ pub struct ClosureExpr {
}
impl ast::HasAttrs for ClosureExpr {}
impl ClosureExpr {
pub fn body(&self) -> Option<Expr> { support::child(&self.syntax) }
pub fn generic_param_list(&self) -> Option<GenericParamList> { support::child(&self.syntax) }
pub fn param_list(&self) -> Option<ParamList> { support::child(&self.syntax) }
pub fn ret_type(&self) -> Option<RetType> { support::child(&self.syntax) }
@ -211,7 +209,6 @@ impl ast::HasDocComments for Const {}
impl ast::HasName for Const {}
impl ast::HasVisibility for Const {}
impl Const {
pub fn body(&self) -> Option<Expr> { support::child(&self.syntax) }
pub fn ty(&self) -> Option<Type> { support::child(&self.syntax) }
pub fn colon_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![:]) }
pub fn semicolon_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![;]) }
@ -356,7 +353,6 @@ impl ast::HasName for Fn {}
impl ast::HasVisibility for Fn {}
impl Fn {
pub fn abi(&self) -> Option<Abi> { support::child(&self.syntax) }
pub fn body(&self) -> Option<BlockExpr> { support::child(&self.syntax) }
pub fn param_list(&self) -> Option<ParamList> { support::child(&self.syntax) }
pub fn ret_type(&self) -> Option<RetType> { support::child(&self.syntax) }
pub fn semicolon_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![;]) }
@ -418,7 +414,6 @@ pub struct FormatArgsExpr {
}
impl ast::HasAttrs for FormatArgsExpr {}
impl FormatArgsExpr {
pub fn args(&self) -> AstChildren<FormatArgsArg> { support::children(&self.syntax) }
pub fn template(&self) -> Option<Expr> { support::child(&self.syntax) }
pub fn pound_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![#]) }
pub fn l_paren_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['(']) }
@ -652,8 +647,6 @@ impl ast::HasDocComments for MacroDef {}
impl ast::HasName for MacroDef {}
impl ast::HasVisibility for MacroDef {}
impl MacroDef {
pub fn args(&self) -> Option<TokenTree> { support::child(&self.syntax) }
pub fn body(&self) -> Option<TokenTree> { support::child(&self.syntax) }
pub fn macro_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![macro]) }
}
@ -1224,7 +1217,6 @@ impl ast::HasDocComments for Static {}
impl ast::HasName for Static {}
impl ast::HasVisibility for Static {}
impl Static {
pub fn body(&self) -> Option<Expr> { support::child(&self.syntax) }
pub fn ty(&self) -> Option<Type> { support::child(&self.syntax) }
pub fn colon_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![:]) }
pub fn semicolon_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![;]) }