mz_sql_parser/ast/
visit_mut.rs

1// Copyright Materialize, Inc. and contributors. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License in the LICENSE file at the
6// root of this repository, or online at
7//
8//     http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16//! Traversal of a mutable AST.
17//!
18//! This module provides a [`VisitMut`] trait that is like the [`Visit`] trait
19//! but operates on a mutable borrow, rather than an immutable borrow, of the
20//! syntax tree.
21//!
22//! Each method of the [`VisitMut`] trait is a hook that can be overridden to
23//! customize the behavior when visiting the corresponding type of node. By
24//! default, every method recursively visits the substructure of the input by
25//! invoking the right visitor method of each of its fields.
26//!
27//! ```
28//! # use mz_sql_parser::ast::{Expr, Function, FunctionArgs, WindowSpec, AstInfo};
29//! #
30//! pub trait VisitMut<'ast, T: AstInfo> {
31//!     /* ... */
32//!
33//!     fn visit_function_mut(&mut self, node: &'ast mut Function<T>) {
34//!         visit_function_mut(self, node);
35//!     }
36//!
37//!     /* ... */
38//!     # fn visit_item_name_mut(&mut self, node: &'ast mut <T as AstInfo>::ItemName);
39//!     # fn visit_function_args_mut(&mut self, node: &'ast mut FunctionArgs<T>);
40//!     # fn visit_expr_mut(&mut self, node: &'ast mut Expr<T>);
41//!     # fn visit_window_spec_mut(&mut self, node: &'ast mut WindowSpec<T>);
42//! }
43//!
44//! pub fn visit_function_mut<'ast, V, T: AstInfo>(visitor: &mut V, node: &'ast mut Function<T>)
45//! where
46//!     V: VisitMut<'ast, T> + ?Sized,
47//! {
48//!     visitor.visit_item_name_mut(&mut node.name);
49//!     visitor.visit_function_args_mut(&mut node.args);
50//!     if let Some(filter) = &mut node.filter {
51//!         visitor.visit_expr_mut(&mut *filter);
52//!     }
53//!     if let Some(over) = &mut node.over {
54//!         visitor.visit_window_spec_mut(over);
55//!     }
56//! }
57//! ```
58//!
59//! [`Visit`]: super::visit::Visit
60//!
61//! # Examples
62//!
63//! This visitor removes parentheses from expressions.
64//!
65//! ```
66//! use std::error::Error;
67//!
68//! use mz_sql_parser::ast::{AstInfo, Expr};
69//! use mz_sql_parser::ast::visit_mut::{self, VisitMut};
70//!
71//! struct RemoveParens;
72//!
73//! impl<'a, T: AstInfo> VisitMut<'a, T> for RemoveParens {
74//!     fn visit_expr_mut(&mut self, expr: &'a mut Expr<T>) {
75//!         visit_mut::visit_expr_mut(self, expr);
76//!         if let Expr::Nested(e) = expr {
77//!             *expr = (**e).clone();
78//!         }
79//!     }
80//! }
81//!
82//! fn main() -> Result<(), Box<dyn Error>> {
83//!     let sql = "(a + ((b))) + c";
84//!     let mut expr = mz_sql_parser::parser::parse_expr(sql.into())?;
85//!     RemoveParens.visit_expr_mut(&mut expr);
86//!     let expected = mz_sql_parser::parser::parse_expr("a + b + c".into())?;
87//!     assert_eq!(expr, expected);
88//!     Ok(())
89//! }
90//! ```
91//!
92//! The [`VisitMutNode`] trait is implemented for every node in the AST and can
93//! be used to write generic functions that apply a `VisitMut` implementation to
94//! any node in the AST.
95//!
96//! # Implementation notes
97//!
98//! This module is automatically generated by the crate's build script. Changes
99//! to the AST will be automatically propagated to the visitor.
100//!
101//! This approach to AST visitors is inspired by the [`syn`] crate. These module
102//! docs are directly derived from the [`syn::visit_mut`] module docs.
103//!
104//! [`syn`]: https://docs.rs/syn/1.*/syn/index.html
105//! [`syn::visit_mut`]: https://docs.rs/syn/1.*/syn/visit_mut/index.html
106
107#![allow(clippy::all)]
108#![allow(unused_variables)]
109
110use crate::ast::*;
111
112include!(concat!(env!("OUT_DIR"), "/visit_mut.rs"));