Module mz_sql_parser::ast::fold

source ·
Expand description

Transformation of an owned AST.

Each method of the Fold trait is a hook that can be overridden to customize the behavior when transforming the corresponding type of node. By default, every method recursively transforms the substructure of the input by invoking the right folder method on each of its fields.

pub trait Fold<T: AstInfo, T2: AstInfo> {
    /* ... */

    fn fold_function(&mut self, node: Function<T>) -> Function<T2> {
        fold_function(self, node)
    }

    /* ... */
}

pub fn fold_function<F, T: AstInfo, T2: AstInfo>(folder: &mut F, node: Function<T>) -> Function<T2>
where
    F: Fold<T, T2> + ?Sized,
{
    Function {
        name: folder.fold_item_name(node.name),
        args: folder.fold_function_args(node.args),
        filter: node.filter.map(|filter| Box::new(folder.fold_expr(*filter))),
        over: node.over.map(|over| folder.fold_window_spec(over)),
        distinct: node.distinct,
   }
}

Of particular note to the fold transformation is its handling of the AST’s generic parameter. The Fold trait is defined so that references to T in the input are replaced by references to T2 in the output. If transformation of T is not required, implement Fold such that T and T2 refer to the same concrete type, and then provide trivial implementations of any methods that fold T’s associated types.

The FoldNode trait is implemented for every node in the AST and can be used to write generic functions that apply a Fold implementation to any node in the AST.

§Implementation notes

This module is automatically generated by the crate’s build script. Changes to the AST will be automatically propagated to the fold transformation.

This approach to AST transformations is inspired by the syn crate. These module docs are directly derived from the syn::fold module docs.

Traits§

Functions§