1#![forbid(unsafe_code)]
2
3extern crate proc_macro;
4use marigold_grammar::marigold_parse;
5use once_cell::sync::Lazy;
6use proc_macro::TokenStream;
7use regex::Regex;
8
9static TOP_LEVEL_BRACKETS: Lazy<Regex> = Lazy::new(|| {
10 Regex::new(
11 r"(?P<function_signature>fn[\s]+[\w]+\(.*\)([\s]+->[\s]+[^{]+))(\{)(?P<function_body>[\s\S]*)(\})"
12).unwrap()
13});
14
15#[proc_macro]
16pub fn marigold(item: TokenStream) -> TokenStream {
17 let s = item.to_string();
18 let cleaned = TOP_LEVEL_BRACKETS.replace_all(
19 s.as_str(),
20 "$function_signature %%%MARIGOLD_FUNCTION_START%%% $function_body %%%MARIGOLD_FUNCTION_END%%%"
21 );
22 format!(
23 "{{\n{}\n}}\n",
24 marigold_parse(&cleaned).expect("marigold parsing error")
25 )
26 .parse()
27 .unwrap()
28}