mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-08-23 03:44:23 +00:00
visitor
This commit is contained in:
parent
d5119133fc
commit
7afd84febc
11 changed files with 211 additions and 86 deletions
|
@ -1,4 +1,5 @@
|
|||
pub mod walk;
|
||||
pub mod visit;
|
||||
|
||||
use {SyntaxNodeRef, TextUnit, TextRange};
|
||||
|
||||
|
|
52
crates/libsyntax2/src/algo/visit.rs
Normal file
52
crates/libsyntax2/src/algo/visit.rs
Normal file
|
@ -0,0 +1,52 @@
|
|||
use std::marker::PhantomData;
|
||||
use {SyntaxNodeRef, AstNode, SyntaxRoot};
|
||||
|
||||
|
||||
pub fn visitor<'a, T>() -> impl Visitor<'a, Output=T> {
|
||||
EmptyVisitor { ph: PhantomData }
|
||||
}
|
||||
|
||||
pub trait Visitor<'a>: Sized {
|
||||
type Output;
|
||||
fn accept(self, node: SyntaxNodeRef<'a>) -> Option<Self::Output>;
|
||||
fn visit<N, F>(self, f: F) -> Vis<Self, N, F>
|
||||
where N: AstNode<&'a SyntaxRoot>,
|
||||
F: FnOnce(N) -> Self::Output,
|
||||
{
|
||||
Vis { inner: self, f, ph: PhantomData }
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct EmptyVisitor<T> {
|
||||
ph: PhantomData<fn() -> T>
|
||||
}
|
||||
|
||||
impl<'a, T> Visitor<'a> for EmptyVisitor<T> {
|
||||
type Output = T;
|
||||
|
||||
fn accept(self, _node: SyntaxNodeRef<'a>) -> Option<T> {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Vis<V, N, F> {
|
||||
inner: V,
|
||||
f: F,
|
||||
ph: PhantomData<fn(N)>,
|
||||
}
|
||||
|
||||
impl<'a, V, N, F> Visitor<'a> for Vis<V, N, F>
|
||||
where
|
||||
V: Visitor<'a>,
|
||||
N: AstNode<&'a SyntaxRoot>,
|
||||
F: FnOnce(N) -> <V as Visitor<'a>>::Output,
|
||||
{
|
||||
type Output = <V as Visitor<'a>>::Output;
|
||||
|
||||
fn accept(self, node: SyntaxNodeRef<'a>) -> Option<Self::Output> {
|
||||
let Vis { inner, f, .. } = self;
|
||||
inner.accept(node).or_else(|| N::cast(node).map(f))
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue