Skip to main content

sqlparser_derive/
lib.rs

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.
17
18//! 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.
23
24use quote::quote;
25use syn::parse_macro_input;
26
27mod dialect;
28mod visit;
29
30/// Implementation of `#[derive(VisitMut)]`
31#[proc_macro_derive(VisitMut, attributes(visit))]
32pub fn derive_visit_mut(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
33    let input = parse_macro_input!(input as syn::DeriveInput);
34    visit::derive_visit(
35        input,
36        &visit::VisitType {
37            visit_trait: quote!(VisitMut),
38            visitor_trait: quote!(VisitorMut),
39            modifier: Some(quote!(mut)),
40        },
41    )
42}
43
44/// Implementation of `#[derive(Visit)]`
45#[proc_macro_derive(Visit, attributes(visit))]
46pub fn derive_visit_immutable(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
47    let input = parse_macro_input!(input as syn::DeriveInput);
48    visit::derive_visit(
49        input,
50        &visit::VisitType {
51            visit_trait: quote!(Visit),
52            visitor_trait: quote!(Visitor),
53            modifier: None,
54        },
55    )
56}
57
58/// Procedural macro for deriving new SQL dialects.
59#[proc_macro]
60pub fn derive_dialect(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
61    let input = parse_macro_input!(input as dialect::DeriveDialectInput);
62    dialect::derive_dialect(input)
63}