|
| 1 | +//! Typed AST node wrappers for ergonomic tree access. |
| 2 | +
|
| 3 | +use crate::{SyntaxKind, SyntaxNode, SyntaxToken}; |
| 4 | +use rowan::ast::AstNode; |
| 5 | + |
| 6 | +/// Trait for all AST nodes. |
| 7 | +pub trait BamlAstNode: AstNode<Language = crate::BamlLanguage> { |
| 8 | + /// Get the syntax kind of this node. |
| 9 | + fn kind(&self) -> SyntaxKind { |
| 10 | + self.syntax().kind() |
| 11 | + } |
| 12 | +} |
| 13 | + |
| 14 | +/// Macro to define AST node types. |
| 15 | +macro_rules! ast_node { |
| 16 | + ($name:ident, $kind:ident) => { |
| 17 | + #[derive(Debug, Clone, PartialEq, Eq, Hash)] |
| 18 | + pub struct $name { |
| 19 | + syntax: SyntaxNode, |
| 20 | + } |
| 21 | + |
| 22 | + impl BamlAstNode for $name {} |
| 23 | + |
| 24 | + impl AstNode for $name { |
| 25 | + type Language = crate::BamlLanguage; |
| 26 | + |
| 27 | + fn can_cast(kind: <Self::Language as rowan::Language>::Kind) -> bool { |
| 28 | + kind == SyntaxKind::$kind.into() |
| 29 | + } |
| 30 | + |
| 31 | + fn cast(syntax: SyntaxNode) -> Option<Self> { |
| 32 | + if Self::can_cast(syntax.kind()) { |
| 33 | + Some(Self { syntax }) |
| 34 | + } else { |
| 35 | + None |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + fn syntax(&self) -> &SyntaxNode { |
| 40 | + &self.syntax |
| 41 | + } |
| 42 | + } |
| 43 | + }; |
| 44 | +} |
| 45 | + |
| 46 | +// Define all AST node types |
| 47 | +ast_node!(SourceFile, SOURCE_FILE); |
| 48 | +ast_node!(FunctionDef, FUNCTION_DEF); |
| 49 | +ast_node!(ClassDef, CLASS_DEF); |
| 50 | +ast_node!(EnumDef, ENUM_DEF); |
| 51 | +ast_node!(ClientDef, CLIENT_DEF); |
| 52 | +ast_node!(TestDef, TEST_DEF); |
| 53 | +ast_node!(RetryPolicyDef, RETRY_POLICY_DEF); |
| 54 | +ast_node!(TemplateStringDef, TEMPLATE_STRING_DEF); |
| 55 | +ast_node!(TypeAliasDef, TYPE_ALIAS_DEF); |
| 56 | + |
| 57 | +ast_node!(ParameterList, PARAMETER_LIST); |
| 58 | +ast_node!(Parameter, PARAMETER); |
| 59 | +ast_node!(FunctionBody, FUNCTION_BODY); |
| 60 | +ast_node!(Field, FIELD); |
| 61 | +ast_node!(EnumVariant, ENUM_VARIANT); |
| 62 | +ast_node!(ConfigBlock, CONFIG_BLOCK); |
| 63 | +ast_node!(ConfigItem, CONFIG_ITEM); |
| 64 | + |
| 65 | +ast_node!(TypeExpr, TYPE_EXPR); |
| 66 | +ast_node!(Attribute, ATTRIBUTE); |
| 67 | +ast_node!(BlockAttribute, BLOCK_ATTRIBUTE); |
| 68 | + |
| 69 | +ast_node!(Expr, EXPR); |
| 70 | +ast_node!(LetStmt, LET_STMT); |
| 71 | +ast_node!(IfExpr, IF_EXPR); |
| 72 | +ast_node!(WhileStmt, WHILE_STMT); |
| 73 | +ast_node!(ForExpr, FOR_EXPR); |
| 74 | +ast_node!(BlockExpr, BLOCK_EXPR); |
| 75 | + |
| 76 | +// Implement accessor methods |
| 77 | +impl SourceFile { |
| 78 | + /// Iterate over all top-level items in the file. |
| 79 | + pub fn items(&self) -> impl Iterator<Item = Item> { |
| 80 | + self.syntax.children().filter_map(Item::cast) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +impl FunctionDef { |
| 85 | + /// Get the function name. |
| 86 | + pub fn name(&self) -> Option<SyntaxToken> { |
| 87 | + self.syntax |
| 88 | + .children_with_tokens() |
| 89 | + .filter_map(rowan::NodeOrToken::into_token) |
| 90 | + .filter(|token| { |
| 91 | + token.kind() == SyntaxKind::WORD && token.parent() == Some(self.syntax.clone()) |
| 92 | + }) |
| 93 | + .nth(1) // Skip the "function" keyword, get the second WORD |
| 94 | + } |
| 95 | + |
| 96 | + /// Get the parameter list. |
| 97 | + pub fn param_list(&self) -> Option<ParameterList> { |
| 98 | + self.syntax.children().find_map(ParameterList::cast) |
| 99 | + } |
| 100 | + |
| 101 | + /// Get the return type. |
| 102 | + pub fn return_type(&self) -> Option<TypeExpr> { |
| 103 | + self.syntax.children().find_map(TypeExpr::cast) |
| 104 | + } |
| 105 | + |
| 106 | + /// Get the function body. |
| 107 | + pub fn body(&self) -> Option<FunctionBody> { |
| 108 | + self.syntax.children().find_map(FunctionBody::cast) |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +impl ParameterList { |
| 113 | + /// Get all parameters. |
| 114 | + pub fn params(&self) -> impl Iterator<Item = Parameter> { |
| 115 | + self.syntax.children().filter_map(Parameter::cast) |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +impl ClassDef { |
| 120 | + /// Get the class name. |
| 121 | + pub fn name(&self) -> Option<SyntaxToken> { |
| 122 | + self.syntax |
| 123 | + .children_with_tokens() |
| 124 | + .filter_map(rowan::NodeOrToken::into_token) |
| 125 | + .filter(|token| { |
| 126 | + token.kind() == SyntaxKind::WORD && token.parent() == Some(self.syntax.clone()) |
| 127 | + }) |
| 128 | + .nth(1) // Skip the "class" keyword, get the second WORD |
| 129 | + } |
| 130 | + |
| 131 | + /// Get all fields. |
| 132 | + pub fn fields(&self) -> impl Iterator<Item = Field> { |
| 133 | + self.syntax.children().filter_map(Field::cast) |
| 134 | + } |
| 135 | + |
| 136 | + /// Get block attributes (@@dynamic). |
| 137 | + pub fn block_attributes(&self) -> impl Iterator<Item = BlockAttribute> { |
| 138 | + self.syntax.children().filter_map(BlockAttribute::cast) |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +impl Field { |
| 143 | + /// Get the field name. |
| 144 | + pub fn name(&self) -> Option<SyntaxToken> { |
| 145 | + self.syntax |
| 146 | + .children_with_tokens() |
| 147 | + .filter_map(rowan::NodeOrToken::into_token) |
| 148 | + .find(|token| token.kind() == SyntaxKind::WORD) |
| 149 | + } |
| 150 | + |
| 151 | + /// Get the field type. |
| 152 | + pub fn ty(&self) -> Option<TypeExpr> { |
| 153 | + self.syntax.children().find_map(TypeExpr::cast) |
| 154 | + } |
| 155 | + |
| 156 | + /// Get field attributes (@alias, @description, etc.). |
| 157 | + pub fn attributes(&self) -> impl Iterator<Item = Attribute> { |
| 158 | + self.syntax.children().filter_map(Attribute::cast) |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +/// Enum for any top-level item. |
| 163 | +#[derive(Debug, Clone, PartialEq, Eq, Hash)] |
| 164 | +pub enum Item { |
| 165 | + Function(FunctionDef), |
| 166 | + Class(ClassDef), |
| 167 | + Enum(EnumDef), |
| 168 | + Client(ClientDef), |
| 169 | + Test(TestDef), |
| 170 | + RetryPolicy(RetryPolicyDef), |
| 171 | + TemplateString(TemplateStringDef), |
| 172 | + TypeAlias(TypeAliasDef), |
| 173 | +} |
| 174 | + |
| 175 | +impl AstNode for Item { |
| 176 | + type Language = crate::BamlLanguage; |
| 177 | + |
| 178 | + fn can_cast(kind: <Self::Language as rowan::Language>::Kind) -> bool { |
| 179 | + matches!( |
| 180 | + kind, |
| 181 | + SyntaxKind::FUNCTION_DEF |
| 182 | + | SyntaxKind::CLASS_DEF |
| 183 | + | SyntaxKind::ENUM_DEF |
| 184 | + | SyntaxKind::CLIENT_DEF |
| 185 | + | SyntaxKind::TEST_DEF |
| 186 | + | SyntaxKind::RETRY_POLICY_DEF |
| 187 | + | SyntaxKind::TEMPLATE_STRING_DEF |
| 188 | + | SyntaxKind::TYPE_ALIAS_DEF |
| 189 | + ) |
| 190 | + } |
| 191 | + |
| 192 | + fn cast(syntax: SyntaxNode) -> Option<Self> { |
| 193 | + match syntax.kind() { |
| 194 | + SyntaxKind::FUNCTION_DEF => Some(Item::Function(FunctionDef { syntax })), |
| 195 | + SyntaxKind::CLASS_DEF => Some(Item::Class(ClassDef { syntax })), |
| 196 | + SyntaxKind::ENUM_DEF => Some(Item::Enum(EnumDef { syntax })), |
| 197 | + SyntaxKind::CLIENT_DEF => Some(Item::Client(ClientDef { syntax })), |
| 198 | + SyntaxKind::TEST_DEF => Some(Item::Test(TestDef { syntax })), |
| 199 | + SyntaxKind::RETRY_POLICY_DEF => Some(Item::RetryPolicy(RetryPolicyDef { syntax })), |
| 200 | + SyntaxKind::TEMPLATE_STRING_DEF => { |
| 201 | + Some(Item::TemplateString(TemplateStringDef { syntax })) |
| 202 | + } |
| 203 | + SyntaxKind::TYPE_ALIAS_DEF => Some(Item::TypeAlias(TypeAliasDef { syntax })), |
| 204 | + _ => None, |
| 205 | + } |
| 206 | + } |
| 207 | + |
| 208 | + fn syntax(&self) -> &SyntaxNode { |
| 209 | + match self { |
| 210 | + Item::Function(it) => it.syntax(), |
| 211 | + Item::Class(it) => it.syntax(), |
| 212 | + Item::Enum(it) => it.syntax(), |
| 213 | + Item::Client(it) => it.syntax(), |
| 214 | + Item::Test(it) => it.syntax(), |
| 215 | + Item::RetryPolicy(it) => it.syntax(), |
| 216 | + Item::TemplateString(it) => it.syntax(), |
| 217 | + Item::TypeAlias(it) => it.syntax(), |
| 218 | + } |
| 219 | + } |
| 220 | +} |
0 commit comments