holium_macro_support/
lib.rs

1//! The `macro-support` is responsible for the logic coordination behind the `holium_bindgen` macro
2
3extern crate proc_macro2;
4extern crate quote;
5extern crate syn;
6#[macro_use]
7extern crate holium_backend as backend;
8
9use crate::parser::MacroParse;
10use backend::{Diagnostic, TryToTokens};
11use proc_macro2::TokenStream;
12
13mod parser;
14
15/// Takes the parsed input from a `#[holium_bindgen]` macro and returns the generated bindings
16pub fn expand(input: TokenStream) -> Result<TokenStream, Diagnostic> {
17    let item = syn::parse2::<syn::Item>(input)?;
18
19    let mut tokens = proc_macro2::TokenStream::new();
20    let mut program = backend::ast::Program::default();
21
22    // First step is to parse the `TokenStream` to copy source tokens & generate custom AST structures
23    // for the codegen step
24    item.macro_parse(&mut program, &mut tokens)?;
25
26    // Second step is to generate code custom tokens based on custom AST structures & append it to
27    // the `TokenStream`
28    program.try_to_tokens(&mut tokens)?;
29
30    Ok(tokens)
31}