remove visitor module
This commit is contained in:
@@ -1,7 +1,5 @@
|
||||
//! FIXME: write short doc here
|
||||
|
||||
pub mod visit;
|
||||
|
||||
use std::ops::RangeInclusive;
|
||||
|
||||
use itertools::Itertools;
|
||||
|
||||
@@ -1,112 +0,0 @@
|
||||
//! FIXME: write short doc here
|
||||
|
||||
use crate::{AstNode, SyntaxNode};
|
||||
|
||||
use std::marker::PhantomData;
|
||||
|
||||
pub fn visitor<'a, T>() -> impl Visitor<'a, Output = T> {
|
||||
EmptyVisitor { ph: PhantomData }
|
||||
}
|
||||
|
||||
pub fn visitor_ctx<'a, T, C>(ctx: C) -> impl VisitorCtx<'a, Output = T, Ctx = C> {
|
||||
EmptyVisitorCtx { ph: PhantomData, ctx }
|
||||
}
|
||||
|
||||
pub trait Visitor<'a>: Sized {
|
||||
type Output;
|
||||
fn accept(self, node: &'a SyntaxNode) -> Option<Self::Output>;
|
||||
fn visit<N, F>(self, f: F) -> Vis<Self, N, F>
|
||||
where
|
||||
N: AstNode + 'a,
|
||||
F: FnOnce(N) -> Self::Output,
|
||||
{
|
||||
Vis { inner: self, f, ph: PhantomData }
|
||||
}
|
||||
}
|
||||
|
||||
pub trait VisitorCtx<'a>: Sized {
|
||||
type Output;
|
||||
type Ctx;
|
||||
fn accept(self, node: &'a SyntaxNode) -> Result<Self::Output, Self::Ctx>;
|
||||
fn visit<N, F>(self, f: F) -> VisCtx<Self, N, F>
|
||||
where
|
||||
N: AstNode + 'a,
|
||||
F: FnOnce(N, Self::Ctx) -> Self::Output,
|
||||
{
|
||||
VisCtx { 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: &'a SyntaxNode) -> Option<T> {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct EmptyVisitorCtx<T, C> {
|
||||
ctx: C,
|
||||
ph: PhantomData<fn() -> T>,
|
||||
}
|
||||
|
||||
impl<'a, T, C> VisitorCtx<'a> for EmptyVisitorCtx<T, C> {
|
||||
type Output = T;
|
||||
type Ctx = C;
|
||||
|
||||
fn accept(self, _node: &'a SyntaxNode) -> Result<T, C> {
|
||||
Err(self.ctx)
|
||||
}
|
||||
}
|
||||
|
||||
#[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,
|
||||
F: FnOnce(N) -> <V as Visitor<'a>>::Output,
|
||||
{
|
||||
type Output = <V as Visitor<'a>>::Output;
|
||||
|
||||
fn accept(self, node: &'a SyntaxNode) -> Option<Self::Output> {
|
||||
let Vis { inner, f, .. } = self;
|
||||
inner.accept(node).or_else(|| N::cast(node.clone()).map(f))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct VisCtx<V, N, F> {
|
||||
inner: V,
|
||||
f: F,
|
||||
ph: PhantomData<fn(N)>,
|
||||
}
|
||||
|
||||
impl<'a, V, N, F> VisitorCtx<'a> for VisCtx<V, N, F>
|
||||
where
|
||||
V: VisitorCtx<'a>,
|
||||
N: AstNode + 'a,
|
||||
F: FnOnce(N, <V as VisitorCtx<'a>>::Ctx) -> <V as VisitorCtx<'a>>::Output,
|
||||
{
|
||||
type Output = <V as VisitorCtx<'a>>::Output;
|
||||
type Ctx = <V as VisitorCtx<'a>>::Ctx;
|
||||
|
||||
fn accept(self, node: &'a SyntaxNode) -> Result<Self::Output, Self::Ctx> {
|
||||
let VisCtx { inner, f, .. } = self;
|
||||
inner.accept(node).or_else(|ctx| match N::cast(node.clone()) {
|
||||
None => Err(ctx),
|
||||
Some(node) => Ok(f(node, ctx)),
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -160,6 +160,17 @@ impl SourceFile {
|
||||
}
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! match_ast {
|
||||
(match $node:ident {
|
||||
$( ast::$ast:ident($it:ident) => $res:block, )*
|
||||
_ => $catch_all:expr,
|
||||
}) => {{
|
||||
$( if let Some($it) = ast::$ast::cast($node.clone()) $res else )*
|
||||
{ $catch_all }
|
||||
}};
|
||||
}
|
||||
|
||||
/// This test does not assert anything and instead just shows off the crate's
|
||||
/// API.
|
||||
#[test]
|
||||
@@ -294,8 +305,7 @@ fn api_walkthrough() {
|
||||
// To recursively process the tree, there are three approaches:
|
||||
// 1. explicitly call getter methods on AST nodes.
|
||||
// 2. use descendants and `AstNode::cast`.
|
||||
// 3. use descendants and the visitor.
|
||||
// 4. use descendants and `match_ast!`.
|
||||
// 3. use descendants and `match_ast!`.
|
||||
//
|
||||
// Here's how the first one looks like:
|
||||
let exprs_cast: Vec<String> = file
|
||||
@@ -305,29 +315,18 @@ fn api_walkthrough() {
|
||||
.map(|expr| expr.syntax().text().to_string())
|
||||
.collect();
|
||||
|
||||
// An alternative is to use a visitor. The visitor does not do traversal
|
||||
// automatically (so it's more akin to a generic lambda) and is constructed
|
||||
// from closures. This seems more flexible than a single generated visitor
|
||||
// trait.
|
||||
use algo::visit::{visitor, Visitor};
|
||||
// An alternative is to use a macro.
|
||||
let mut exprs_visit = Vec::new();
|
||||
for node in file.syntax().descendants() {
|
||||
if let Some(result) =
|
||||
visitor().visit::<ast::Expr, _>(|expr| expr.syntax().text().to_string()).accept(&node)
|
||||
{
|
||||
exprs_visit.push(result);
|
||||
match_ast! {
|
||||
match node {
|
||||
ast::Expr(it) => {
|
||||
let res = it.syntax().text().to_string();
|
||||
exprs_visit.push(res);
|
||||
},
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
}
|
||||
assert_eq!(exprs_cast, exprs_visit);
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! match_ast {
|
||||
(match $node:ident {
|
||||
$( ast::$ast:ident($it:ident) => $res:block, )*
|
||||
_ => $catch_all:expr,
|
||||
}) => {{
|
||||
$( if let Some($it) = ast::$ast::cast($node.clone()) $res else )*
|
||||
{ $catch_all }
|
||||
}};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user