use std::marker::PhantomData;
use rowan::{TextRange, TextSize, WalkEvent};
use crate::kind::{CppKind, CppSyntaxKind, CppTokenKind};
use crate::kind::{CppSyntaxElementChildren, CppSyntaxNodeChildren};
use crate::lexer::CppTokenData;
use crate::syntax::{CppSyntaxId, CppSyntaxNode, CppSyntaxToken};
pub use super::cpp::*;
pub use super::token::*;
pub trait CppAstNode {
fn syntax(&self) -> &CppSyntaxNode;
fn can_cast(kind: CppSyntaxKind) -> bool
where
Self: Sized;
fn cast(syntax: CppSyntaxNode) -> Option<Self>
where
Self: Sized;
fn child<N: CppAstNode>(&self) -> Option<N> {
self.syntax().children().find_map(N::cast)
}
fn token<N: CppAstToken>(&self) -> Option<N> {
self.syntax()
.children_with_tokens()
.find_map(|it| it.into_token().and_then(N::cast))
}
fn token_by_kind(&self, kind: CppTokenKind) -> Option<super::token::CppGeneralToken> {
let token = self
.syntax()
.children_with_tokens()
.filter_map(|it| it.into_token())
.find(|it| it.kind() == CppKind::Token(kind))?;
super::token::CppGeneralToken::cast(token)
}
fn children<N: CppAstNode>(&self) -> CppAstChildren<N> {
CppAstChildren::new(self.syntax())
}
fn tokens<N: CppAstToken>(&self) -> CppAstTokenChildren<N> {
CppAstTokenChildren::new(self.syntax())
}
fn descendants<N: CppAstNode>(&self) -> impl Iterator<Item = N> {
self.syntax().descendants().filter_map(N::cast)
}
fn walk_descendants<N: CppAstNode>(&self) -> impl Iterator<Item = WalkEvent<N>> {
self.syntax().preorder().filter_map(|event| match event {
WalkEvent::Enter(node) => N::cast(node).map(WalkEvent::Enter),
WalkEvent::Leave(node) => N::cast(node).map(WalkEvent::Leave),
})
}
fn ancestors<N: CppAstNode>(&self) -> impl Iterator<Item = N> {
self.syntax().ancestors().filter_map(N::cast)
}
fn get_root(&self) -> CppSyntaxNode {
let syntax = self.syntax();
if syntax.kind() == CppKind::Syntax(CppSyntaxKind::TranslationUnit) {
syntax.clone()
} else {
syntax.ancestors().last().unwrap_or_else(|| syntax.clone())
}
}
fn get_parent<N: CppAstNode>(&self) -> Option<N> {
self.syntax().parent().and_then(N::cast)
}
fn get_position(&self) -> TextSize {
self.syntax().text_range().start()
}
fn get_range(&self) -> TextRange {
self.syntax().text_range()
}
fn get_self_or_ancestor<N: CppAstNode>(&self) -> Option<N> {
N::cast(self.syntax().clone()).or_else(|| self.ancestors().next())
}
fn get_syntax_id(&self) -> CppSyntaxId {
CppSyntaxId::from_node(self.syntax())
}
fn dump(&self) -> String {
format!("{:#?}", self.syntax())
}
}
#[derive(Debug, Clone)]
pub struct CppAstChildren<N> {
inner: CppSyntaxNodeChildren,
ph: PhantomData<N>,
}
impl<N> CppAstChildren<N> {
pub fn new(parent: &CppSyntaxNode) -> CppAstChildren<N> {
CppAstChildren {
inner: parent.children(),
ph: PhantomData,
}
}
}
impl<N: CppAstNode> Iterator for CppAstChildren<N> {
type Item = N;
fn next(&mut self) -> Option<N> {
self.inner.find_map(N::cast)
}
}
pub trait CppAstToken {
fn syntax(&self) -> &CppSyntaxToken;
fn can_cast(kind: CppTokenKind) -> bool
where
Self: Sized;
fn cast(syntax: CppSyntaxToken) -> Option<Self>
where
Self: Sized;
fn get_token_kind(&self) -> CppTokenKind {
self.syntax().kind().into()
}
fn get_token_data(&self) -> CppTokenData {
CppTokenData::new(
self.get_token_kind(),
source_range(self.syntax().text_range()),
)
}
fn get_position(&self) -> TextSize {
self.syntax().text_range().start()
}
fn get_range(&self) -> TextRange {
self.syntax().text_range()
}
fn get_syntax_id(&self) -> CppSyntaxId {
CppSyntaxId::from_token(self.syntax())
}
fn get_text(&self) -> &str {
self.syntax().text()
}
fn get_parent<N: CppAstNode>(&self) -> Option<N> {
self.syntax().parent().and_then(N::cast)
}
fn ancestors<N: CppAstNode>(&self) -> impl Iterator<Item = N> {
self.syntax().parent_ancestors().filter_map(N::cast)
}
fn dump(&self) -> String {
format!("{:#?}", self.syntax())
}
}
#[derive(Debug, Clone)]
pub struct CppAstTokenChildren<N> {
inner: CppSyntaxElementChildren,
ph: PhantomData<N>,
}
impl<N> CppAstTokenChildren<N> {
pub fn new(parent: &CppSyntaxNode) -> CppAstTokenChildren<N> {
CppAstTokenChildren {
inner: parent.children_with_tokens(),
ph: PhantomData,
}
}
}
impl<N: CppAstToken> Iterator for CppAstTokenChildren<N> {
type Item = N;
fn next(&mut self) -> Option<N> {
self.inner.find_map(|it| it.into_token().and_then(N::cast))
}
}
pub fn node_kind_in(kind: CppSyntaxKind, kinds: &[CppSyntaxKind]) -> bool {
kinds.contains(&kind)
}
pub const TRIVIA_KINDS: &[CppTokenKind] = &[
CppTokenKind::Whitespace,
CppTokenKind::Newline,
CppTokenKind::LineContinuation,
CppTokenKind::LineComment,
CppTokenKind::BlockComment,
];
pub fn is_trivia(kind: CppTokenKind) -> bool {
TRIVIA_KINDS.contains(&kind)
}
pub fn significant_children(node: &CppSyntaxNode) -> impl Iterator<Item = CppSyntaxToken> {
node.children_with_tokens()
.filter_map(|it| it.into_token())
.filter(|token| !is_trivia(token.kind().into()))
}
pub fn token_at(node: &CppSyntaxNode, offset: usize) -> Option<CppSyntaxToken> {
if offset > usize::from(node.text_range().end()) {
return None;
}
let at = TextSize::from(u32::try_from(offset).ok()?);
node.token_at_offset(at).right_biased()
}
pub fn tokens_range(tokens: &[CppSyntaxToken]) -> Option<TextRange> {
let first = tokens.first()?.text_range();
let last = tokens.last()?.text_range();
Some(TextRange::new(first.start(), last.end()))
}
pub fn source_range(range: TextRange) -> crate::text::SourceRange {
crate::text::SourceRange::new(
u32::from(range.start()) as usize,
u32::from(range.end() - range.start()) as usize,
)
}
pub fn first_child_of_kind(node: &CppSyntaxNode, kinds: &[CppSyntaxKind]) -> Option<CppSyntaxNode> {
node.children()
.find(|child| kinds.contains(&CppSyntaxKind::from(child.kind())))
}
pub fn first_significant_token(node: &CppSyntaxNode) -> Option<CppSyntaxToken> {
significant_children(node).next()
}
pub fn subtree_text(node: &CppSyntaxNode) -> String {
let mut out = String::new();
collect_subtree_text(node, &mut out);
out
}
fn collect_subtree_text(node: &CppSyntaxNode, out: &mut String) {
for element in node.children_with_tokens() {
match element {
rowan::NodeOrToken::Node(child) => collect_subtree_text(&child, out),
rowan::NodeOrToken::Token(token) => {
if !is_trivia(token.kind().into()) {
out.push_str(token.text());
}
}
}
}
}
pub fn subtree_text_spaced(node: &CppSyntaxNode) -> String {
let raw = node.text().to_string();
raw.split_whitespace().collect::<Vec<_>>().join(" ")
}