Skip to content

Commit a51b5c3

Browse files
adds setexpr visitor, fixes function arg visitor (apache#8)
1 parent a879adf commit a51b5c3

File tree

3 files changed

+70
-5
lines changed

3 files changed

+70
-5
lines changed

src/ast/mod.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -3403,7 +3403,11 @@ impl fmt::Display for Assignment {
34033403

34043404
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
34053405
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3406-
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3406+
#[cfg_attr(
3407+
feature = "visitor",
3408+
derive(Visit, VisitMut),
3409+
visit(with = "visit_function_arg")
3410+
)]
34073411
pub enum FunctionArgExpr {
34083412
Expr(Expr),
34093413
/// Qualified wildcard, e.g. `alias.*` or `schema.table.*`.

src/ast/query.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,11 @@ impl fmt::Display for Query {
7373
#[allow(clippy::large_enum_variant)]
7474
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
7575
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
76-
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
76+
#[cfg_attr(
77+
feature = "visitor",
78+
derive(Visit, VisitMut),
79+
visit(with = "visit_setexpr")
80+
)]
7781
pub enum SetExpr {
7882
/// Restricted SELECT .. FROM .. HAVING (no ORDER BY or set operations)
7983
Select(Box<Select>),

src/ast/visitor.rs

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

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

202+
/// Invoked for any set expressions that appear in the AST before visiting children
203+
fn pre_visit_setexpr(&mut self, _set_expr: &SetExpr) -> ControlFlow<Self::Break> {
204+
ControlFlow::Continue(())
205+
}
206+
207+
/// Invoked for any set expressions that appear in the AST after visiting children
208+
fn post_visit_setexpr(&mut self, _set_expr: &SetExpr) -> ControlFlow<Self::Break> {
209+
ControlFlow::Continue(())
210+
}
211+
201212
/// Invoked for function arguments that appear in the AST before visiting children
202213
fn pre_visit_function_arg(&mut self, _expr: &FunctionArgExpr) -> ControlFlow<Self::Break> {
203214
ControlFlow::Continue(())
@@ -285,6 +296,16 @@ pub trait VisitorMut {
285296
ControlFlow::Continue(())
286297
}
287298

299+
/// Invoked for any expressions that appear in the AST before visiting children
300+
fn pre_visit_setexpr(&mut self, _set_expr: &mut SetExpr) -> ControlFlow<Self::Break> {
301+
ControlFlow::Continue(())
302+
}
303+
304+
/// Invoked for any expressions that appear in the AST after visiting children
305+
fn post_visit_setexpr(&mut self, _set_expr: &mut SetExpr) -> ControlFlow<Self::Break> {
306+
ControlFlow::Continue(())
307+
}
308+
288309
/// Invoked for function arguments that appear in the AST before visiting children
289310
fn pre_visit_function_arg(&mut self, _expr: &mut FunctionArgExpr) -> ControlFlow<Self::Break> {
290311
ControlFlow::Continue(())
@@ -412,7 +433,7 @@ impl<E, F: FnMut(&mut FunctionArgExpr) -> ControlFlow<E>> VisitorMut for Functio
412433
}
413434
}
414435

415-
pub fn visit_function_arguments<V, E, F>(v: &V, f: F) -> ControlFlow<E>
436+
pub fn visit_function_arg<V, E, F>(v: &V, f: F) -> ControlFlow<E>
416437
where
417438
V: Visit,
418439
F: FnMut(&FunctionArgExpr) -> ControlFlow<E>,
@@ -421,7 +442,7 @@ where
421442
v.visit(&mut visitor)?;
422443
ControlFlow::Continue(())
423444
}
424-
pub fn visit_function_arguments_mut<V, E, F>(v: &mut V, f: F) -> ControlFlow<E>
445+
pub fn visit_function_arg_mut<V, E, F>(v: &mut V, f: F) -> ControlFlow<E>
425446
where
426447
V: VisitMut,
427448
F: FnMut(&mut FunctionArgExpr) -> ControlFlow<E>,
@@ -430,6 +451,42 @@ where
430451
ControlFlow::Continue(())
431452
}
432453

454+
struct SetExprVisitor<F>(F);
455+
456+
impl<E, F: FnMut(&SetExpr) -> ControlFlow<E>> Visitor for SetExprVisitor<F> {
457+
type Break = E;
458+
459+
fn pre_visit_setexpr(&mut self, expr: &SetExpr) -> ControlFlow<Self::Break> {
460+
self.0(expr)
461+
}
462+
}
463+
464+
impl<E, F: FnMut(&mut SetExpr) -> ControlFlow<E>> VisitorMut for SetExprVisitor<F> {
465+
type Break = E;
466+
467+
fn post_visit_setexpr(&mut self, expr: &mut SetExpr) -> ControlFlow<Self::Break> {
468+
self.0(expr)
469+
}
470+
}
471+
472+
pub fn visit_setexpr<V, E, F>(v: &V, f: F) -> ControlFlow<E>
473+
where
474+
V: Visit,
475+
F: FnMut(&SetExpr) -> ControlFlow<E>,
476+
{
477+
let mut visitor = SetExprVisitor(f);
478+
v.visit(&mut visitor)?;
479+
ControlFlow::Continue(())
480+
}
481+
pub fn visit_setexpr_mut<V, E, F>(v: &mut V, f: F) -> ControlFlow<E>
482+
where
483+
V: VisitMut,
484+
F: FnMut(&mut SetExpr) -> ControlFlow<E>,
485+
{
486+
v.visit(&mut SetExprVisitor(f))?;
487+
ControlFlow::Continue(())
488+
}
489+
433490
struct ExprVisitor<F>(F);
434491

435492
impl<E, F: FnMut(&Expr) -> ControlFlow<E>> Visitor for ExprVisitor<F> {

0 commit comments

Comments
 (0)