autocorrect_derive/
lib.rs1#[macro_use]
2extern crate quote;
3extern crate proc_macro;
4
5use quote::format_ident;
6
7use proc_macro::TokenStream;
8use syn::{parse_macro_input, DeriveInput};
9
10#[proc_macro_derive(GrammarParser, attributes(name))]
11pub fn derive_grammar_parser(input: TokenStream) -> TokenStream {
12 let input = parse_macro_input!(input as DeriveInput);
13
14 let struct_name = input.ident.to_string();
15 let name = struct_name.replace("Parser", "").to_lowercase();
16
17 let struct_name = format_ident!("{}", struct_name);
18 let format_fn = format_ident!("format_{}", name);
19 let lint_fn = format_ident!("lint_{}", name);
20
21 quote! {
22 #[allow(dead_code)]
23 pub fn #format_fn(text: &str) -> FormatResult {{
24 let pairs = #struct_name::parse(Rule::item, text);
25 let text = code::FormatResult::new(text);
26 code::format_pairs(text, pairs)
27 }}
28
29 #[allow(dead_code)]
30 pub fn #lint_fn(text: &str) -> LintResult {{
31 let pairs = #struct_name::parse(Rule::item, text);
32 let text = code::LintResult::new(text);
33 code::format_pairs(text, pairs)
34 }}
35 }
36 .into()
37}