1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements. See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership. The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License. You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied. See the License for the
15// specific language governing permissions and limitations
16// under the License.
1718//! Procedural macros for sqlparser.
19//!
20//! This crate provides:
21//! - [`Visit`] and [`VisitMut`] derive macros for AST traversal.
22//! - [`derive_dialect!`] macro for creating custom SQL dialects.
2324use quote::quote;
25use syn::parse_macro_input;
2627mod dialect;
28mod visit;
2930/// Implementation of `#[derive(VisitMut)]`
31#[proc_macro_derive(VisitMut, attributes(visit))]
32pub fn derive_visit_mut(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
33let input = match ::syn::parse::<syn::DeriveInput>(input) {
::syn::__private::Ok(data) => data,
::syn::__private::Err(err) => {
return ::syn::__private::TokenStream::from(err.to_compile_error());
}
}parse_macro_input!(input as syn::DeriveInput);
34 visit::derive_visit(
35input,
36&visit::VisitType {
37 visit_trait: {
let mut _s = ::quote::__private::TokenStream::new();
::quote::__private::push_ident(&mut _s, "VisitMut");
_s
}quote!(VisitMut),
38 visitor_trait: {
let mut _s = ::quote::__private::TokenStream::new();
::quote::__private::push_ident(&mut _s, "VisitorMut");
_s
}quote!(VisitorMut),
39 modifier: Some({
let mut _s = ::quote::__private::TokenStream::new();
::quote::__private::push_ident(&mut _s, "mut");
_s
}quote!(mut)),
40 },
41 )
42}
4344/// Implementation of `#[derive(Visit)]`
45#[proc_macro_derive(Visit, attributes(visit))]
46pub fn derive_visit_immutable(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
47let input = match ::syn::parse::<syn::DeriveInput>(input) {
::syn::__private::Ok(data) => data,
::syn::__private::Err(err) => {
return ::syn::__private::TokenStream::from(err.to_compile_error());
}
}parse_macro_input!(input as syn::DeriveInput);
48 visit::derive_visit(
49input,
50&visit::VisitType {
51 visit_trait: {
let mut _s = ::quote::__private::TokenStream::new();
::quote::__private::push_ident(&mut _s, "Visit");
_s
}quote!(Visit),
52 visitor_trait: {
let mut _s = ::quote::__private::TokenStream::new();
::quote::__private::push_ident(&mut _s, "Visitor");
_s
}quote!(Visitor),
53 modifier: None,
54 },
55 )
56}
5758/// Procedural macro for deriving new SQL dialects.
59#[proc_macro]
60pub fn derive_dialect(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
61let input = match ::syn::parse::<dialect::DeriveDialectInput>(input) {
::syn::__private::Ok(data) => data,
::syn::__private::Err(err) => {
return ::syn::__private::TokenStream::from(err.to_compile_error());
}
}parse_macro_input!(input as dialect::DeriveDialectInput);
62 dialect::derive_dialect(input)
63}