mod generated;
mod node_ext;
mod nodes;
mod support;
mod token_ext;
mod traits;
use std::marker::PhantomData;
use crate::syntax_node::{SyntaxNode, SyntaxNodeChildren, SyntaxToken};
use squawk_parser::SyntaxKind;
pub use self::{
generated::tokens::*,
node_ext::{BinOp, LitKind, PostfixOp},
nodes::*,
traits::{HasCreateTable, HasParamList, HasWithClause, NameLike},
};
pub trait AstNode {
fn can_cast(kind: SyntaxKind) -> bool
where
Self: Sized;
fn cast(syntax: SyntaxNode) -> Option<Self>
where
Self: Sized;
fn syntax(&self) -> &SyntaxNode;
fn clone_for_update(&self) -> Self
where
Self: Sized,
{
Self::cast(self.syntax().clone_for_update()).unwrap()
}
fn clone_subtree(&self) -> Self
where
Self: Sized,
{
Self::cast(self.syntax().clone_subtree()).unwrap()
}
}
pub trait AstToken {
fn can_cast(token: SyntaxKind) -> bool
where
Self: Sized;
fn cast(syntax: SyntaxToken) -> Option<Self>
where
Self: Sized;
fn syntax(&self) -> &SyntaxToken;
fn text(&self) -> &str {
self.syntax().text()
}
}
#[derive(Debug, Clone)]
pub struct AstChildren<N> {
inner: SyntaxNodeChildren,
ph: PhantomData<N>,
}
impl<N> AstChildren<N> {
fn new(parent: &SyntaxNode) -> Self {
AstChildren {
inner: parent.children(),
ph: PhantomData,
}
}
}
impl<N: AstNode> Iterator for AstChildren<N> {
type Item = N;
fn next(&mut self) -> Option<N> {
self.inner.find_map(N::cast)
}
}