remove ast::*Kind enums

With the new owned trees, we don't need an indirection here
This commit is contained in:
Aleksey Kladov 2019-08-19 13:58:49 +03:00
parent ba2836245b
commit 39e444d701
3 changed files with 398 additions and 391 deletions

File diff suppressed because it is too large Load diff

View file

@ -186,8 +186,8 @@ fn api_walkthrough() {
// Let's fetch the `foo` function.
let mut func = None;
for item in file.items() {
match item.kind() {
ast::ModuleItemKind::FnDef(f) => func = Some(f),
match item {
ast::ModuleItem::FnDef(f) => func = Some(f),
_ => unreachable!(),
}
}
@ -206,12 +206,12 @@ fn api_walkthrough() {
let block: ast::Block = func.body().unwrap();
let expr: ast::Expr = block.expr().unwrap();
// "Enum"-like nodes are represented using the "kind" pattern. It allows us
// to match exhaustively against all flavors of nodes, while maintaining
// internal representation flexibility. The drawback is that one can't write
// nested matches as one pattern.
let bin_expr: ast::BinExpr = match expr.kind() {
ast::ExprKind::BinExpr(e) => e,
// Enums are used to group related ast nodes together, and can be used for
// matching. However, because there are no public fields, it's possible to
// match only the top level enum: that is the price we pay for increased API
// flexibility
let bin_expr: &ast::BinExpr = match &expr {
ast::Expr::BinExpr(e) => e,
_ => unreachable!(),
};

View file

@ -37,41 +37,72 @@ fn generate_ast(grammar: &Grammar) -> Result<String> {
ast_node.variants.iter().map(|var| format_ident!("{}", var)).collect::<Vec<_>>();
let name = format_ident!("{}", name);
let kinds = if variants.is_empty() { vec![name.clone()] } else { variants.clone() }
.into_iter()
.map(|name| format_ident!("{}", name.to_string().to_shouty_snake_case()))
.collect::<Vec<_>>();
let adt = if variants.is_empty() {
let kind = format_ident!("{}", name.to_string().to_shouty_snake_case());
quote! {
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct #name {
pub(crate) syntax: SyntaxNode,
}
let variants = if variants.is_empty() {
None
impl AstNode for #name {
fn can_cast(kind: SyntaxKind) -> bool {
match kind {
#kind => true,
_ => false,
}
}
fn cast(syntax: SyntaxNode) -> Option<Self> {
if Self::can_cast(syntax.kind()) { Some(Self { syntax }) } else { None }
}
fn syntax(&self) -> &SyntaxNode { &self.syntax }
}
}
} else {
let kind_enum = format_ident!("{}Kind", name);
Some(quote!(
pub enum #kind_enum {
let kinds = variants
.iter()
.map(|name| format_ident!("{}", name.to_string().to_shouty_snake_case()))
.collect::<Vec<_>>();
quote! {
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum #name {
#(#variants(#variants),)*
}
#(
impl From<#variants> for #name {
fn from(node: #variants) -> #name {
#name { syntax: node.syntax }
#name::#variants(node)
}
}
)*
impl #name {
pub fn kind(&self) -> #kind_enum {
let syntax = self.syntax.clone();
match syntax.kind() {
impl AstNode for #name {
fn can_cast(kind: SyntaxKind) -> bool {
match kind {
#(#kinds)|* => true,
_ => false,
}
}
fn cast(syntax: SyntaxNode) -> Option<Self> {
let res = match syntax.kind() {
#(
#kinds =>
#kind_enum::#variants(#variants { syntax }),
#kinds => #name::#variants(#variants { syntax }),
)*
_ => return None,
};
Some(res)
}
fn syntax(&self) -> &SyntaxNode {
match self {
#(
#name::#variants(it) => &it.syntax,
)*
_ => unreachable!(),
}
}
}
))
}
};
let traits = ast_node.traits.iter().map(|trait_name| {
@ -105,25 +136,7 @@ fn generate_ast(grammar: &Grammar) -> Result<String> {
});
quote! {
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct #name {
pub(crate) syntax: SyntaxNode,
}
impl AstNode for #name {
fn can_cast(kind: SyntaxKind) -> bool {
match kind {
#(#kinds)|* => true,
_ => false,
}
}
fn cast(syntax: SyntaxNode) -> Option<Self> {
if Self::can_cast(syntax.kind()) { Some(Self { syntax }) } else { None }
}
fn syntax(&self) -> &SyntaxNode { &self.syntax }
}
#variants
#adt
#(#traits)*