rust/crates/syntax/src/ast/generated.rs

42 lines
1.0 KiB
Rust
Raw Normal View History

//! This file is actually hand-written, but the submodules are indeed generated.
2020-04-09 18:03:03 +02:00
#[rustfmt::skip]
2020-07-31 15:40:48 +02:00
mod nodes;
2020-04-09 18:03:03 +02:00
#[rustfmt::skip]
2020-07-31 15:40:48 +02:00
mod tokens;
use crate::{
AstNode,
SyntaxKind::{self, *},
SyntaxNode,
};
pub use {nodes::*, tokens::*};
// Stmt is the only nested enum, so it's easier to just hand-write it
impl AstNode for Stmt {
fn can_cast(kind: SyntaxKind) -> bool {
match kind {
LET_STMT | EXPR_STMT => true,
2020-07-31 15:46:12 +02:00
_ => Item::can_cast(kind),
2020-07-31 15:40:48 +02:00
}
}
fn cast(syntax: SyntaxNode) -> Option<Self> {
let res = match syntax.kind() {
LET_STMT => Stmt::LetStmt(LetStmt { syntax }),
EXPR_STMT => Stmt::ExprStmt(ExprStmt { syntax }),
2020-07-31 15:46:12 +02:00
_ => {
let item = Item::cast(syntax)?;
Stmt::Item(item)
}
2020-07-31 15:40:48 +02:00
};
Some(res)
}
fn syntax(&self) -> &SyntaxNode {
match self {
Stmt::LetStmt(it) => &it.syntax,
Stmt::ExprStmt(it) => &it.syntax,
2020-07-31 15:46:12 +02:00
Stmt::Item(it) => it.syntax(),
2020-07-31 15:40:48 +02:00
}
}
}