Skip to main content

static_automata_macros/
lib.rs

1/// Macros for the [`static-automata`](https://crates.io/crates/static-automata)
2/// library. This is not supposed to be used directly.
3use proc_macro::TokenStream;
4use proc_macro_error::{abort, proc_macro_error};
5use quote::ToTokens;
6
7mod validate;
8
9/// Derive grammar validation functions.
10///
11/// This derive macro generates the two functions `validate_bytes` and
12/// `validate_str` using the provided automaton function.
13///
14/// The two generated functions are `const` compatible.
15#[proc_macro_derive(Validate, attributes(automaton))]
16#[proc_macro_error]
17pub fn derive_validate(input: TokenStream) -> TokenStream {
18	let item = syn::parse_macro_input!(input as syn::DeriveInput);
19	match validate::derive(item) {
20		Ok(tokens) => tokens.into(),
21		Err(e) => {
22			let span = e.span();
23			abort!(span, e)
24		}
25	}
26}
27
28/// Grammar module annotation.
29///
30/// This attribute is used to annotate a `mod foo {}` module with information
31/// about the grammar it represents. The macro itself doesn't generate any code,
32/// it will just replace it with an external module declaration `mod foo;`.
33/// It is used as a marker for the Command Line Interface to pick up and
34/// generate the external module file.
35#[proc_macro_attribute]
36#[proc_macro_error]
37pub fn grammar(_attr: TokenStream, input: TokenStream) -> TokenStream {
38	let mut item = syn::parse_macro_input!(input as syn::ItemMod);
39	item.content = None;
40	item.into_token_stream().into()
41}