From cd2dbc516a6739874ab68d62f205805827416005 Mon Sep 17 00:00:00 2001 From: Soispha Date: Sat, 16 Dec 2023 11:43:12 +0100 Subject: [PATCH] chore(trixy): Rename `language_macros` to `trixy` The motivation behind this change is to comply with the naming scheme: starting crate names with `tri` --- Cargo.lock | 22 ++-- Cargo.toml | 2 +- .../src/command_enum_parsing/mod.rs | 110 ------------------ {language_macros => trixy}/.gitignore | 0 {language_macros => trixy}/Cargo.toml | 2 +- trixy/src/config/mod.rs | 103 ++++++++++++++++ .../src/generate/command_enum/mod.rs | 0 .../lua_functions_to_globals/mod.rs | 0 .../src/generate/lua_wrapper/mod.rs | 0 .../lua_wrapper/rust_wrapper_functions/mod.rs | 0 .../src/generate/mod.rs | 0 {language_macros => trixy}/src/lib.rs | 32 ++--- 12 files changed, 135 insertions(+), 136 deletions(-) delete mode 100644 language_macros/src/command_enum_parsing/mod.rs rename {language_macros => trixy}/.gitignore (100%) rename {language_macros => trixy}/Cargo.toml (90%) create mode 100644 trixy/src/config/mod.rs rename {language_macros => trixy}/src/generate/command_enum/mod.rs (100%) rename {language_macros => trixy}/src/generate/lua_wrapper/lua_functions_to_globals/mod.rs (100%) rename {language_macros => trixy}/src/generate/lua_wrapper/mod.rs (100%) rename {language_macros => trixy}/src/generate/lua_wrapper/rust_wrapper_functions/mod.rs (100%) rename {language_macros => trixy}/src/generate/mod.rs (100%) rename {language_macros => trixy}/src/lib.rs (80%) diff --git a/Cargo.lock b/Cargo.lock index f64c0f2..7b7e8e1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1358,16 +1358,6 @@ dependencies = [ "thiserror", ] -[[package]] -name = "language_macros" -version = "0.1.0" -dependencies = [ - "convert_case", - "proc-macro2", - "quote", - "syn 2.0.41", -] - [[package]] name = "lazy_static" version = "1.4.0" @@ -2797,7 +2787,6 @@ dependencies = [ "directories", "indexmap 2.1.0", "keymaps", - "language_macros", "matrix-sdk", "mlua", "once_cell", @@ -2805,10 +2794,21 @@ dependencies = [ "serde", "tokio", "tokio-util", + "trixy", "tui", "tui-textarea", ] +[[package]] +name = "trixy" +version = "0.1.0" +dependencies = [ + "convert_case", + "proc-macro2", + "quote", + "syn 2.0.41", +] + [[package]] name = "try-lock" version = "0.2.5" diff --git a/Cargo.toml b/Cargo.toml index 68fbea3..d3bd812 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,7 +18,7 @@ matrix-sdk = "0.6" tokio = { version = "1.35", features = ["macros", "rt-multi-thread"] } # lua stuff -language_macros = { path = "./language_macros" } +trixy = { path = "./trixy" } keymaps = { path = "./keymaps", features = ["crossterm"] } mlua = { version = "0.9.2", features = ["lua54", "async", "send", "serialize"] } once_cell = "1.19.0" diff --git a/language_macros/src/command_enum_parsing/mod.rs b/language_macros/src/command_enum_parsing/mod.rs deleted file mode 100644 index c64a5ed..0000000 --- a/language_macros/src/command_enum_parsing/mod.rs +++ /dev/null @@ -1,110 +0,0 @@ -use syn::{braced, parse::Parse, punctuated::Punctuated, token, Attribute, Ident, Token, Type}; - -pub type NamespacePath = Punctuated; - -mod kw { - syn::custom_keyword!(commands); - syn::custom_keyword!(namespace); - syn::custom_keyword!(declare); -} - -#[derive(Debug)] -pub struct DataCommandEnum { - #[allow(dead_code)] - commands_token: kw::commands, - - #[allow(dead_code)] - brace_token: token::Brace, - - pub fields: Punctuated, -} - -#[derive(Debug)] -pub enum Field { - Function(FunctionDeclaration), - Namespace(Namespace), -} -#[derive(Debug)] -pub struct Namespace { - #[allow(dead_code)] - namespace_token: kw::namespace, - - pub path: NamespacePath, - - #[allow(dead_code)] - brace_token: token::Brace, - - pub fields: Punctuated, -} -#[derive(Debug)] -pub struct FunctionDeclaration { - #[allow(dead_code)] - function_token: kw::declare, - - pub name: Ident, - - #[allow(dead_code)] - colon_token: Token![:], - - pub ty: Type, -} - -impl Parse for DataCommandEnum { - fn parse(input: syn::parse::ParseStream) -> syn::Result { - let content; - Ok(DataCommandEnum { - commands_token: input.parse()?, - brace_token: braced!(content in input), - fields: content.parse_terminated(Field::parse, Token![,])?, - }) - } -} -impl Parse for Field { - fn parse(input: syn::parse::ParseStream) -> syn::Result { - let lookahead = input.lookahead1(); - if input.peek(Token![#]) { - // FIXME(@soispha): We ignore doc comments, which should probably be replaced by adding - // them to the output <2023-09-19> - let _output = input.call(Attribute::parse_outer).unwrap_or(vec![]); - let lookahead = input.lookahead1(); - - if lookahead.peek(kw::namespace) { - input.parse().map(Field::Namespace) - } else if lookahead.peek(kw::declare) { - input.parse().map(Field::Function) - } else { - Err(lookahead.error()) - } - } else { - if lookahead.peek(kw::declare) { - input.parse().map(Field::Function) - } else if lookahead.peek(kw::namespace) { - input.parse().map(Field::Namespace) - } else { - Err(lookahead.error()) - } - } - } -} - -impl Parse for FunctionDeclaration { - fn parse(input: syn::parse::ParseStream) -> syn::Result { - Ok(FunctionDeclaration { - function_token: input.parse()?, - name: input.parse()?, - colon_token: input.parse()?, - ty: input.parse()?, - }) - } -} -impl Parse for Namespace { - fn parse(input: syn::parse::ParseStream) -> syn::Result { - let content; - Ok(Namespace { - namespace_token: input.parse()?, - path: NamespacePath::parse_separated_nonempty(input)?, - brace_token: braced!(content in input), - fields: content.parse_terminated(Field::parse, Token![,])?, - }) - } -} diff --git a/language_macros/.gitignore b/trixy/.gitignore similarity index 100% rename from language_macros/.gitignore rename to trixy/.gitignore diff --git a/language_macros/Cargo.toml b/trixy/Cargo.toml similarity index 90% rename from language_macros/Cargo.toml rename to trixy/Cargo.toml index 500e8da..af5db2d 100644 --- a/language_macros/Cargo.toml +++ b/trixy/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "language_macros" +name = "trixy" version = "0.1.0" edition = "2021" diff --git a/trixy/src/config/mod.rs b/trixy/src/config/mod.rs new file mode 100644 index 0000000..5899d6e --- /dev/null +++ b/trixy/src/config/mod.rs @@ -0,0 +1,103 @@ +//! This module is responsible for parsing the config passed to the macro call: +//! For example: +//! ```no_run +//! trixy_generate! { +//! path: ./trintrix_command_interface.tri +//! languages: rust, lua, c +//! } +//! ``` + +use std::path::PathBuf; + +use proc_macro2::Ident; +use syn::{parse::Parse, punctuated::Punctuated, LitStr, Result, Token}; + +mod kw { + syn::custom_keyword!(path); + syn::custom_keyword!(languages); +} + +#[derive(Debug)] +pub enum Language { + Rust, + Lua, + C, +} + +#[derive(Debug)] +struct Languages { + #[allow(dead_code)] + languages: kw::languages, + #[allow(dead_code)] + colon: Token![:], + raw: Punctuated, +} + +#[derive(Debug)] +struct Path { + #[allow(dead_code)] + path: kw::path, + #[allow(dead_code)] + colon: Token![:], + raw: PathBuf, +} + +#[derive(Debug)] +pub struct TrixyConfig { + /// The Path to the base command interface config file + path: Path, + + /// The languages the commands should be exposed in + languages: Languages, +} +impl TrixyConfig { + pub fn get_path(&self) -> PathBuf { + self.path.raw + } +} + +impl Parse for TrixyConfig { + fn parse(input: syn::parse::ParseStream) -> syn::Result { + Ok(Self { + path: input.parse()?, + languages: input.parse()?, + }) + } +} + +impl Parse for Path { + fn parse(input: syn::parse::ParseStream) -> Result { + let path: kw::path = input.parse()?; + let colon: Token![:] = input.parse()?; + let raw = PathBuf::from(input.parse::()?.value()); + Ok(Self { path, colon, raw }) + } +} + +impl Parse for Languages { + fn parse(input: syn::parse::ParseStream) -> syn::Result { + let languages: kw::languages = input.parse()?; + let colon: Token![:] = input.parse()?; + let raw = Punctuated::::parse_separated_nonempty(input)?; + Ok(Self { + languages, + colon, + raw, + }) + } +} + +impl Parse for Language { + fn parse(input: syn::parse::ParseStream) -> Result { + let ident: Ident = input.parse()?; + match &ident.to_string()[..] { + "rust" | "Rust" => Ok(Self::Rust), + "lua" | "Lua" => Ok(Self::Lua), + "c" | "C" => Ok(Self::C), + other => Err(input.error(format!( + "The language: `{}` is not a registered language!", + other + ))), + } + } +} diff --git a/language_macros/src/generate/command_enum/mod.rs b/trixy/src/generate/command_enum/mod.rs similarity index 100% rename from language_macros/src/generate/command_enum/mod.rs rename to trixy/src/generate/command_enum/mod.rs diff --git a/language_macros/src/generate/lua_wrapper/lua_functions_to_globals/mod.rs b/trixy/src/generate/lua_wrapper/lua_functions_to_globals/mod.rs similarity index 100% rename from language_macros/src/generate/lua_wrapper/lua_functions_to_globals/mod.rs rename to trixy/src/generate/lua_wrapper/lua_functions_to_globals/mod.rs diff --git a/language_macros/src/generate/lua_wrapper/mod.rs b/trixy/src/generate/lua_wrapper/mod.rs similarity index 100% rename from language_macros/src/generate/lua_wrapper/mod.rs rename to trixy/src/generate/lua_wrapper/mod.rs diff --git a/language_macros/src/generate/lua_wrapper/rust_wrapper_functions/mod.rs b/trixy/src/generate/lua_wrapper/rust_wrapper_functions/mod.rs similarity index 100% rename from language_macros/src/generate/lua_wrapper/rust_wrapper_functions/mod.rs rename to trixy/src/generate/lua_wrapper/rust_wrapper_functions/mod.rs diff --git a/language_macros/src/generate/mod.rs b/trixy/src/generate/mod.rs similarity index 100% rename from language_macros/src/generate/mod.rs rename to trixy/src/generate/mod.rs diff --git a/language_macros/src/lib.rs b/trixy/src/lib.rs similarity index 80% rename from language_macros/src/lib.rs rename to trixy/src/lib.rs index be1e4b3..21aa639 100644 --- a/language_macros/src/lib.rs +++ b/trixy/src/lib.rs @@ -1,12 +1,16 @@ use command_enum_parsing::DataCommandEnum; +use config::TrixyConfig; use proc_macro::TokenStream; use proc_macro2::TokenStream as TokenStream2; use quote::quote; use syn::parse_macro_input; +use crate::trixy_lang::parse_trixy_lang; -mod generate; mod command_enum_parsing; +mod config; +mod generate; +mod trixy_lang; /// This is the heart of the command api /// It mainly does two things: @@ -74,18 +78,20 @@ mod command_enum_parsing; /// } /// ``` #[proc_macro] -pub fn parse_command_enum(input: TokenStream) -> TokenStream { - let input = parse_macro_input!(input as DataCommandEnum); +pub fn trixy_generate(input: TokenStream) -> TokenStream { + let input = parse_macro_input!(input as TrixyConfig); - // Build the language wrappers - let lua_wrapper: TokenStream2 = generate::lua_wrapper(&input); + let trixy_code = parse_trixy_lang(input.get_path()); + todo!() + // // Build the language wrappers + // let lua_wrapper: TokenStream2 = generate::lua_wrapper(&input); + // + // // Build the final enum + // let command_enum = generate::command_enum(&input); - // Build the final enum - let command_enum = generate::command_enum(&input); - - let output = quote! { - #command_enum - #lua_wrapper - }; - output.into() + // let output = quote! { + // #command_enum + // #lua_wrapper + // }; + // output.into() }