This repository has been archived on 2024-05-26. You can view files and clone it, but cannot push or open issues or pull requests.
trixy/trixy-macros/src/lib.rs

57 lines
1.8 KiB
Rust
Raw Normal View History

/*
* Copyright (C) 2023 The Trinitrix Project <soispha@vhack.eu, antifallobst@systemausfall.org>
*
* This file is part of the Trixy crate for Trinitrix.
*
* Trixy is free software: you can redistribute it and/or modify
* it under the terms of the Lesser GNU General Public License as
* published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* and the Lesser GNU General Public License along with this program.
* If not, see <https://www.gnu.org/licenses/>.
*/
use std::fs;
use proc_macro::TokenStream;
use quote::quote;
use syn::parse_macro_input;
use trixy_lang_parser::parse_trixy_lang;
use crate::config::TrixyConfig;
mod config;
mod generate;
/// This is the heart of Trixy
/// It mainly does one thing:
/// - Generate a tree of modules from the input trixy file
///
#[proc_macro]
pub fn trixy_generate(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as TrixyConfig);
let source_code = fs::read_to_string(input.get_path()).unwrap_or_else(|err| {
panic! {"Can't read file at path: '{}'. The Error is: '{}'", input.get_path().display(), err};
});
let trixy_code = parse_trixy_lang(&source_code).unwrap_or_else(|err| {
panic! {"Parsing of the trixy file failed: \n{}", err}
});
// Build the final rust hosting code
let host_rust_code = generate::host::generate(&trixy_code, &input);
let output = quote! {
#host_rust_code
};
output.into()
}