Skip to content

Commit a879adf

Browse files
Visitor for function arguments expressions (apache#7)
1 parent 52482b3 commit a879adf

File tree

1 file changed

+56
-3
lines changed

1 file changed

+56
-3
lines changed

src/ast/visitor.rs

+56-3
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@
1212

1313
//! Recursive visitors for ast Nodes. See [`Visitor`] for more details.
1414
15-
use crate::ast::{Expr, ObjectName, Statement};
15+
use crate::ast::{Expr, FunctionArgExpr, ObjectName, Statement};
1616
use core::ops::ControlFlow;
17-
1817
/// A type that can be visited by a [`Visitor`]. See [`Visitor`] for
1918
/// recursively visiting parsed SQL statements.
2019
///
@@ -199,6 +198,15 @@ pub trait Visitor {
199198
ControlFlow::Continue(())
200199
}
201200

201+
/// Invoked for function arguments that appear in the AST before visiting children
202+
fn pre_visit_function_arg(&mut self, _expr: &FunctionArgExpr) -> ControlFlow<Self::Break> {
203+
ControlFlow::Continue(())
204+
}
205+
206+
/// Invoked for any function arguments that appear in the AST after visiting children
207+
fn post_visit_function_arg(&mut self, _expr: &FunctionArgExpr) -> ControlFlow<Self::Break> {
208+
ControlFlow::Continue(())
209+
}
202210
/// Invoked for any statements that appear in the AST before visiting children
203211
fn pre_visit_statement(&mut self, _statement: &Statement) -> ControlFlow<Self::Break> {
204212
ControlFlow::Continue(())
@@ -277,6 +285,16 @@ pub trait VisitorMut {
277285
ControlFlow::Continue(())
278286
}
279287

288+
/// Invoked for function arguments that appear in the AST before visiting children
289+
fn pre_visit_function_arg(&mut self, _expr: &mut FunctionArgExpr) -> ControlFlow<Self::Break> {
290+
ControlFlow::Continue(())
291+
}
292+
293+
/// Invoked for any function arguments that appear in the AST after visiting children
294+
fn post_visit_function_arg(&mut self, _expr: &mut FunctionArgExpr) -> ControlFlow<Self::Break> {
295+
ControlFlow::Continue(())
296+
}
297+
280298
/// Invoked for any statements that appear in the AST before visiting children
281299
fn pre_visit_statement(&mut self, _statement: &mut Statement) -> ControlFlow<Self::Break> {
282300
ControlFlow::Continue(())
@@ -376,6 +394,42 @@ where
376394
ControlFlow::Continue(())
377395
}
378396

397+
struct FunctionArgExprVisitor<F>(F);
398+
399+
impl<E, F: FnMut(&FunctionArgExpr) -> ControlFlow<E>> Visitor for FunctionArgExprVisitor<F> {
400+
type Break = E;
401+
402+
fn pre_visit_function_arg(&mut self, expr: &FunctionArgExpr) -> ControlFlow<Self::Break> {
403+
self.0(expr)
404+
}
405+
}
406+
407+
impl<E, F: FnMut(&mut FunctionArgExpr) -> ControlFlow<E>> VisitorMut for FunctionArgExprVisitor<F> {
408+
type Break = E;
409+
410+
fn post_visit_function_arg(&mut self, expr: &mut FunctionArgExpr) -> ControlFlow<Self::Break> {
411+
self.0(expr)
412+
}
413+
}
414+
415+
pub fn visit_function_arguments<V, E, F>(v: &V, f: F) -> ControlFlow<E>
416+
where
417+
V: Visit,
418+
F: FnMut(&FunctionArgExpr) -> ControlFlow<E>,
419+
{
420+
let mut visitor = FunctionArgExprVisitor(f);
421+
v.visit(&mut visitor)?;
422+
ControlFlow::Continue(())
423+
}
424+
pub fn visit_function_arguments_mut<V, E, F>(v: &mut V, f: F) -> ControlFlow<E>
425+
where
426+
V: VisitMut,
427+
F: FnMut(&mut FunctionArgExpr) -> ControlFlow<E>,
428+
{
429+
v.visit(&mut FunctionArgExprVisitor(f))?;
430+
ControlFlow::Continue(())
431+
}
432+
379433
struct ExprVisitor<F>(F);
380434

381435
impl<E, F: FnMut(&Expr) -> ControlFlow<E>> Visitor for ExprVisitor<F> {
@@ -432,7 +486,6 @@ where
432486
v.visit(&mut visitor)?;
433487
ControlFlow::Continue(())
434488
}
435-
436489
/// Invokes the provided closure iteratively with a mutable reference to all expressions
437490
/// present in `v`.
438491
///

0 commit comments

Comments
 (0)