mz_sql_parser/ast/
fold.rs

1// Copyright Syn Developers.
2// Copyright Materialize, Inc. and contributors. All rights reserved.
3//
4// This file is derived from the syn project, available at
5// https://github.com/dtolnay/syn. It was incorporated
6// directly into Materialize on January 22, 2021.
7//
8// Licensed under the Apache License, Version 2.0 (the "License");
9// you may not use this file except in compliance with the License.
10// You may obtain a copy of the License in the LICENSE file at the
11// root of this repository, or online at
12//
13//     http://www.apache.org/licenses/LICENSE-2.0
14//
15// Unless required by applicable law or agreed to in writing, software
16// distributed under the License is distributed on an "AS IS" BASIS,
17// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18// See the License for the specific language governing permissions and
19// limitations under the License.
20
21//! Transformation of an owned AST.
22//!
23//! Each method of the [`Fold`] trait is a hook that can be overridden to
24//! customize the behavior when transforming the corresponding type of node. By
25//! default, every method recursively transforms the substructure of the input
26//! by invoking the right folder method on each of its fields.
27//!
28//! ```
29//! # use mz_sql_parser::ast::{Expr, Function, FunctionArgs, WindowSpec, Raw, AstInfo};
30//! #
31//! pub trait Fold<T: AstInfo, T2: AstInfo> {
32//!     /* ... */
33//!
34//!     fn fold_function(&mut self, node: Function<T>) -> Function<T2> {
35//!         fold_function(self, node)
36//!     }
37//!
38//!     /* ... */
39//!     # fn fold_item_name(&mut self, node: <T as AstInfo>::ItemName) -> <T2 as AstInfo>::ItemName;
40//!     # fn fold_function_args(&mut self, node: FunctionArgs<T>) -> FunctionArgs<T2>;
41//!     # fn fold_expr(&mut self, node: Expr<T>) -> Expr<T2>;
42//!     # fn fold_window_spec(&mut self, node: WindowSpec<T>) -> WindowSpec<T2>;
43//! }
44//!
45//! pub fn fold_function<F, T: AstInfo, T2: AstInfo>(folder: &mut F, node: Function<T>) -> Function<T2>
46//! where
47//!     F: Fold<T, T2> + ?Sized,
48//! {
49//!     Function {
50//!         name: folder.fold_item_name(node.name),
51//!         args: folder.fold_function_args(node.args),
52//!         filter: node.filter.map(|filter| Box::new(folder.fold_expr(*filter))),
53//!         over: node.over.map(|over| folder.fold_window_spec(over)),
54//!         distinct: node.distinct,
55//!    }
56//! }
57//! ```
58//!
59//! Of particular note to the fold transformation is its handling of the AST's
60//! generic parameter. The `Fold` trait is defined so that references to `T`
61//! in the input are replaced by references to `T2` in the output. If
62//! transformation of `T` is not required, implement `Fold` such that `T` and
63//! `T2` refer to the same concrete type, and then provide trivial
64//! implementations of any methods that fold `T`'s associated types.
65//!
66//! The [`FoldNode`] trait is implemented for every node in the AST and can be
67//! used to write generic functions that apply a `Fold` implementation to any
68//! node in the AST.
69//!
70//! # Implementation notes
71//!
72//! This module is automatically generated by the crate's build script. Changes
73//! to the AST will be automatically propagated to the fold transformation.
74//!
75//! This approach to AST transformations is inspired by the [`syn`] crate. These
76//! module docs are directly derived from the [`syn::fold`] module docs.
77//!
78//! [`syn`]: https://docs.rs/syn/1.*/syn/index.html
79//! [`syn::fold`]: https://docs.rs/syn/1.*/syn/fold/index.html
80
81#![allow(clippy::all)]
82#![allow(unused_variables)]
83
84use crate::ast::*;
85
86include!(concat!(env!("OUT_DIR"), "/fold.rs"));