feat: Add `pre` and `post` visit events for `Literal` (#7629)

pull/24376/head
Stuart Carnie 2023-04-25 07:15:44 +10:00 committed by GitHub
parent ebf0b34523
commit 06bc2d6272
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 79 additions and 4 deletions

View File

@ -9,10 +9,14 @@ expression: "visit_statement!(\"DELETE WHERE 'foo bar' =~ /foo/\")"
- pre_visit_conditional_binary
- pre_visit_conditional_expression
- pre_visit_expr
- pre_visit_literal
- post_visit_literal
- post_visit_expr
- post_visit_conditional_expression
- pre_visit_conditional_expression
- pre_visit_expr
- pre_visit_literal
- post_visit_literal
- post_visit_expr
- post_visit_conditional_expression
- post_visit_conditional_binary

View File

@ -65,6 +65,8 @@ expression: "visit_statement!(r#\"SELECT value FROM (SELECT usage FROM cpu WHERE
- post_visit_conditional_expression
- pre_visit_conditional_expression
- pre_visit_expr
- pre_visit_literal
- post_visit_literal
- post_visit_expr
- post_visit_conditional_expression
- post_visit_conditional_binary
@ -79,6 +81,8 @@ expression: "visit_statement!(r#\"SELECT value FROM (SELECT usage FROM cpu WHERE
- post_visit_conditional_expression
- pre_visit_conditional_expression
- pre_visit_expr
- pre_visit_literal
- post_visit_literal
- post_visit_expr
- post_visit_conditional_expression
- post_visit_conditional_binary
@ -90,6 +94,8 @@ expression: "visit_statement!(r#\"SELECT value FROM (SELECT usage FROM cpu WHERE
- pre_visit_select_dimension
- pre_visit_select_time_dimension
- pre_visit_expr
- pre_visit_literal
- post_visit_literal
- post_visit_expr
- post_visit_select_time_dimension
- post_visit_select_dimension

View File

@ -15,6 +15,8 @@ expression: "visit_statement!(\"SHOW MEASUREMENTS WHERE host = 'west'\")"
- post_visit_conditional_expression
- pre_visit_conditional_expression
- pre_visit_expr
- pre_visit_literal
- post_visit_literal
- post_visit_expr
- post_visit_conditional_expression
- post_visit_conditional_binary

View File

@ -23,6 +23,8 @@ expression: "visit_statement!(\"SHOW MEASUREMENTS ON * WITH MEASUREMENT =~ /foo/
- post_visit_conditional_expression
- pre_visit_conditional_expression
- pre_visit_expr
- pre_visit_literal
- post_visit_literal
- post_visit_expr
- post_visit_conditional_expression
- post_visit_conditional_binary

View File

@ -9,10 +9,14 @@ expression: "visit_statement!(\"DELETE WHERE 'foo bar' =~ /foo/\")"
- pre_visit_conditional_binary
- pre_visit_conditional_expression
- pre_visit_expr
- pre_visit_literal
- post_visit_literal
- post_visit_expr
- post_visit_conditional_expression
- pre_visit_conditional_expression
- pre_visit_expr
- pre_visit_literal
- post_visit_literal
- post_visit_expr
- post_visit_conditional_expression
- post_visit_conditional_binary

View File

@ -65,6 +65,8 @@ expression: "visit_statement!(r#\"SELECT value FROM (SELECT usage FROM cpu WHERE
- post_visit_conditional_expression
- pre_visit_conditional_expression
- pre_visit_expr
- pre_visit_literal
- post_visit_literal
- post_visit_expr
- post_visit_conditional_expression
- post_visit_conditional_binary
@ -79,6 +81,8 @@ expression: "visit_statement!(r#\"SELECT value FROM (SELECT usage FROM cpu WHERE
- post_visit_conditional_expression
- pre_visit_conditional_expression
- pre_visit_expr
- pre_visit_literal
- post_visit_literal
- post_visit_expr
- post_visit_conditional_expression
- post_visit_conditional_binary
@ -90,6 +94,8 @@ expression: "visit_statement!(r#\"SELECT value FROM (SELECT usage FROM cpu WHERE
- pre_visit_select_dimension
- pre_visit_select_time_dimension
- pre_visit_expr
- pre_visit_literal
- post_visit_literal
- post_visit_expr
- post_visit_select_time_dimension
- post_visit_select_dimension

View File

@ -15,6 +15,8 @@ expression: "visit_statement!(\"SHOW MEASUREMENTS WHERE host = 'west'\")"
- post_visit_conditional_expression
- pre_visit_conditional_expression
- pre_visit_expr
- pre_visit_literal
- post_visit_literal
- post_visit_expr
- post_visit_conditional_expression
- post_visit_conditional_binary

View File

@ -23,6 +23,8 @@ expression: "visit_statement!(\"SHOW MEASUREMENTS ON * WITH MEASUREMENT =~ /foo/
- post_visit_conditional_expression
- pre_visit_conditional_expression
- pre_visit_expr
- pre_visit_literal
- post_visit_literal
- post_visit_expr
- post_visit_conditional_expression
- post_visit_conditional_binary

View File

@ -35,6 +35,7 @@ use crate::explain::ExplainStatement;
use crate::expression::arithmetic::Expr;
use crate::expression::conditional::ConditionalExpression;
use crate::expression::{Binary, Call, ConditionalBinary, VarRef};
use crate::literal::Literal;
use crate::select::{
Dimension, Field, FieldList, FillClause, FromMeasurementClause, GroupByClause,
MeasurementSelection, SLimitClause, SOffsetClause, SelectStatement, TimeDimension,
@ -585,6 +586,16 @@ pub trait Visitor: Sized {
fn post_visit_conditional_binary(self, _n: &ConditionalBinary) -> Result<Self, Self::Error> {
Ok(self)
}
/// Invoked before any children of a literal are visited.
fn pre_visit_literal(self, _n: &Literal) -> Result<Recursion<Self>, Self::Error> {
Ok(Continue(self))
}
/// Invoked after a literal is visited.
fn post_visit_literal(self, _n: &Literal) -> Result<Self, Self::Error> {
Ok(self)
}
}
/// Trait for types that can be visited by [`Visitor`]
@ -1242,18 +1253,28 @@ impl Visitable for Expr {
Self::Binary(expr) => expr.accept(visitor),
Self::Nested(expr) => expr.accept(visitor),
Self::VarRef(expr) => expr.accept(visitor),
Self::Literal(expr) => expr.accept(visitor),
// We explicitly list out each enumeration, to ensure
// we revisit if new items are added to the Expr enumeration.
Self::BindParameter(_) | Self::Literal(_) | Self::Wildcard(_) | Self::Distinct(_) => {
Ok(visitor)
}
Self::BindParameter(_) | Self::Wildcard(_) | Self::Distinct(_) => Ok(visitor),
}?;
visitor.post_visit_expr(self)
}
}
impl Visitable for Literal {
fn accept<V: Visitor>(&self, visitor: V) -> Result<V, V::Error> {
let visitor = match visitor.pre_visit_literal(self)? {
Continue(visitor) => visitor,
Stop(visitor) => return Ok(visitor),
};
visitor.post_visit_literal(self)
}
}
impl Visitable for OnClause {
fn accept<V: Visitor>(&self, visitor: V) -> Result<V, V::Error> {
let visitor = match visitor.pre_visit_on_clause(self)? {
@ -1331,6 +1352,7 @@ mod test {
use crate::expression::arithmetic::Expr;
use crate::expression::conditional::ConditionalExpression;
use crate::expression::{Binary, Call, ConditionalBinary, VarRef};
use crate::literal::Literal;
use crate::select::{
Dimension, Field, FieldList, FillClause, FromMeasurementClause, GroupByClause,
MeasurementSelection, SLimitClause, SOffsetClause, SelectStatement, TimeDimension,
@ -1427,6 +1449,7 @@ mod test {
trace_visit!(call, Call);
trace_visit!(expr_binary, Binary);
trace_visit!(conditional_binary, ConditionalBinary);
trace_visit!(literal, Literal);
}
macro_rules! visit_statement {

View File

@ -35,6 +35,7 @@ use crate::explain::ExplainStatement;
use crate::expression::arithmetic::Expr;
use crate::expression::conditional::ConditionalExpression;
use crate::expression::{Binary, Call, ConditionalBinary, VarRef};
use crate::literal::Literal;
use crate::select::{
Dimension, Field, FieldList, FillClause, FromMeasurementClause, GroupByClause,
MeasurementSelection, SLimitClause, SOffsetClause, SelectStatement, TimeDimension,
@ -616,6 +617,16 @@ pub trait VisitorMut: Sized {
) -> Result<(), Self::Error> {
Ok(())
}
/// Invoked before any children of a literal are visited.
fn pre_visit_literal(&mut self, _n: &mut Literal) -> Result<Recursion, Self::Error> {
Ok(Continue)
}
/// Invoked after a literal is visited.
fn post_visit_literal(&mut self, _n: &mut Literal) -> Result<(), Self::Error> {
Ok(())
}
}
/// Trait for types that can be visited by [`VisitorMut`]
@ -1185,16 +1196,27 @@ impl VisitableMut for Expr {
Self::Binary(expr) => expr.accept(visitor)?,
Self::Nested(expr) => expr.accept(visitor)?,
Self::VarRef(expr) => expr.accept(visitor)?,
Self::Literal(expr) => expr.accept(visitor)?,
// We explicitly list out each enumeration, to ensure
// we revisit if new items are added to the Expr enumeration.
Self::BindParameter(_) | Self::Literal(_) | Self::Wildcard(_) | Self::Distinct(_) => {}
Self::BindParameter(_) | Self::Wildcard(_) | Self::Distinct(_) => {}
};
visitor.post_visit_expr(self)
}
}
impl VisitableMut for Literal {
fn accept<V: VisitorMut>(&mut self, visitor: &mut V) -> Result<(), V::Error> {
if let Stop = visitor.pre_visit_literal(self)? {
return Ok(());
};
visitor.post_visit_literal(self)
}
}
impl VisitableMut for OnClause {
fn accept<V: VisitorMut>(&mut self, visitor: &mut V) -> Result<(), V::Error> {
if let Stop = visitor.pre_visit_on_clause(self)? {
@ -1267,6 +1289,7 @@ mod test {
use crate::expression::arithmetic::Expr;
use crate::expression::conditional::ConditionalExpression;
use crate::expression::{Binary, Call, ConditionalBinary, VarRef};
use crate::literal::Literal;
use crate::parse_statements;
use crate::select::{
Dimension, Field, FieldList, FillClause, FromMeasurementClause, GroupByClause,
@ -1362,6 +1385,7 @@ mod test {
trace_visit!(call, Call);
trace_visit!(expr_binary, Binary);
trace_visit!(conditional_binary, ConditionalBinary);
trace_visit!(literal, Literal);
}
macro_rules! visit_statement {