mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2025-12-06 11:46:42 +01:00
wip: proc: command (pt.3)
This commit is contained in:
parent
c56b08c24e
commit
751e01a41e
2 changed files with 172 additions and 61 deletions
|
|
@ -1,3 +1,5 @@
|
||||||
|
#![feature(str_as_str)]
|
||||||
|
|
||||||
extern crate proc_macro;
|
extern crate proc_macro;
|
||||||
|
|
||||||
pub(crate) use std::collections::{BTreeMap, BTreeSet};
|
pub(crate) use std::collections::{BTreeMap, BTreeSet};
|
||||||
|
|
@ -27,7 +29,7 @@ mod proc_command;
|
||||||
#[proc_macro_attribute]
|
#[proc_macro_attribute]
|
||||||
pub fn view (meta: TokenStream, item: TokenStream) -> TokenStream {
|
pub fn view (meta: TokenStream, item: TokenStream) -> TokenStream {
|
||||||
use self::proc_view::{ViewDef, ViewMeta, ViewImpl};
|
use self::proc_view::{ViewDef, ViewMeta, ViewImpl};
|
||||||
write_macro(ViewDef(
|
write(ViewDef(
|
||||||
parse_macro_input!(meta as ViewMeta),
|
parse_macro_input!(meta as ViewMeta),
|
||||||
parse_macro_input!(item as ViewImpl),
|
parse_macro_input!(item as ViewImpl),
|
||||||
))
|
))
|
||||||
|
|
@ -36,7 +38,7 @@ pub fn view (meta: TokenStream, item: TokenStream) -> TokenStream {
|
||||||
#[proc_macro_attribute]
|
#[proc_macro_attribute]
|
||||||
pub fn expose (meta: TokenStream, item: TokenStream) -> TokenStream {
|
pub fn expose (meta: TokenStream, item: TokenStream) -> TokenStream {
|
||||||
use self::proc_expose::{ExposeDef, ExposeMeta, ExposeImpl};
|
use self::proc_expose::{ExposeDef, ExposeMeta, ExposeImpl};
|
||||||
write_macro(ExposeDef(
|
write(ExposeDef(
|
||||||
parse_macro_input!(meta as ExposeMeta),
|
parse_macro_input!(meta as ExposeMeta),
|
||||||
parse_macro_input!(item as ExposeImpl),
|
parse_macro_input!(item as ExposeImpl),
|
||||||
))
|
))
|
||||||
|
|
@ -45,14 +47,28 @@ pub fn expose (meta: TokenStream, item: TokenStream) -> TokenStream {
|
||||||
#[proc_macro_attribute]
|
#[proc_macro_attribute]
|
||||||
pub fn command (meta: TokenStream, item: TokenStream) -> TokenStream {
|
pub fn command (meta: TokenStream, item: TokenStream) -> TokenStream {
|
||||||
use self::proc_command::{CommandDef, CommandMeta, CommandImpl};
|
use self::proc_command::{CommandDef, CommandMeta, CommandImpl};
|
||||||
write_macro(CommandDef(
|
write(CommandDef(
|
||||||
parse_macro_input!(meta as CommandMeta),
|
parse_macro_input!(meta as CommandMeta),
|
||||||
parse_macro_input!(item as CommandImpl),
|
parse_macro_input!(item as CommandImpl),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn write_macro <T: ToTokens> (t: T) -> TokenStream {
|
pub(crate) fn write <T: ToTokens> (t: T) -> TokenStream {
|
||||||
let mut out = TokenStream2::new();
|
let mut out = TokenStream2::new();
|
||||||
t.to_tokens(&mut out);
|
t.to_tokens(&mut out);
|
||||||
out.into()
|
out.into()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn write_quote (quote: TokenStream2) -> TokenStream2 {
|
||||||
|
let mut out = TokenStream2::new();
|
||||||
|
for token in quote {
|
||||||
|
out.append(token);
|
||||||
|
}
|
||||||
|
out
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn write_quote_to (out: &mut TokenStream2, quote: TokenStream2) {
|
||||||
|
for token in quote {
|
||||||
|
out.append(token);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,73 +4,79 @@ use crate::*;
|
||||||
pub(crate) struct CommandDef(pub(crate) CommandMeta, pub(crate) CommandImpl);
|
pub(crate) struct CommandDef(pub(crate) CommandMeta, pub(crate) CommandImpl);
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub(crate) struct CommandMeta {
|
pub(crate) struct CommandMeta(Ident);
|
||||||
target: Ident,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub(crate) struct CommandImpl(ItemImpl, BTreeMap<Arc<str>, CommandArm>);
|
pub(crate) struct CommandImpl(ItemImpl, BTreeMap<Arc<str>, CommandArm>);
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
struct CommandVariant(Ident, Vec<FnArg>);
|
struct CommandArm(Ident, Vec<FnArg>, ReturnType);
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
struct CommandArm(Arc<str>, Ident, Vec<FnArg>, ReturnType);
|
struct CommandVariant(Ident, Vec<FnArg>);
|
||||||
|
|
||||||
impl Parse for CommandMeta {
|
impl Parse for CommandMeta {
|
||||||
fn parse (input: ParseStream) -> Result<Self> {
|
fn parse (input: ParseStream) -> Result<Self> {
|
||||||
Ok(Self {
|
Ok(Self(input.parse::<Ident>()?))
|
||||||
target: input.parse::<Ident>()?,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Parse for CommandImpl {
|
impl Parse for CommandImpl {
|
||||||
fn parse (input: ParseStream) -> Result<Self> {
|
fn parse (input: ParseStream) -> Result<Self> {
|
||||||
let block = input.parse::<ItemImpl>()?;
|
let block = input.parse::<ItemImpl>()?;
|
||||||
|
let exposed = Self::collect(&block.items).map_err(|e|input.error(e))?;
|
||||||
|
Ok(Self(block, exposed))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl CommandImpl {
|
||||||
|
fn collect (items: &Vec<ImplItem>)
|
||||||
|
-> std::result::Result<BTreeMap<Arc<str>, CommandArm>, String>
|
||||||
|
{
|
||||||
let mut exposed: BTreeMap<Arc<str>, CommandArm> = Default::default();
|
let mut exposed: BTreeMap<Arc<str>, CommandArm> = Default::default();
|
||||||
for item in block.items.iter() {
|
for item in items.iter() {
|
||||||
if let ImplItem::Fn(ImplItemFn {
|
if let ImplItem::Fn(
|
||||||
sig: Signature { ident, inputs, output, .. }, ..
|
ImplItemFn { sig: Signature { ident, inputs, output, .. }, .. }
|
||||||
}) = item {
|
) = item {
|
||||||
let key: Arc<str> =
|
let key = CommandArm::ident_to_key(&ident);
|
||||||
format!("{}", AsKebabCase(format!("{}", &ident))).into();
|
|
||||||
let variant: Arc<str> =
|
|
||||||
format!("{}", AsUpperCamelCase(format!("{}", &ident))).into();
|
|
||||||
if exposed.contains_key(&key) {
|
if exposed.contains_key(&key) {
|
||||||
return Err(input.error(format!("already defined: {ident}")));
|
return Err(format!("already defined: {ident}"));
|
||||||
}
|
}
|
||||||
exposed.insert(key, CommandArm(
|
exposed.insert(key, CommandArm(
|
||||||
variant,
|
|
||||||
ident.clone(),
|
ident.clone(),
|
||||||
inputs.iter().map(|x|x.clone()).collect(),
|
inputs.iter().map(|x|x.clone()).collect(),
|
||||||
output.clone(),
|
output.clone(),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(Self(block, exposed))
|
Ok(exposed)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ToTokens for CommandDef {
|
impl ToTokens for CommandDef {
|
||||||
fn to_tokens (&self, out: &mut TokenStream2) {
|
fn to_tokens (&self, out: &mut TokenStream2) {
|
||||||
let Self(CommandMeta { target }, CommandImpl(block, exposed)) = self;
|
let Self(CommandMeta(target), CommandImpl(block, exposed)) = self;
|
||||||
let enumeration = &block.self_ty;
|
let enumeration = &block.self_ty;
|
||||||
let definitions = exposed.values().map(|x|CommandVariant(
|
let variants = exposed.values().map(|x|CommandArm::to_enum_variant(x, true, true, false));
|
||||||
x.1.clone(),
|
let matchers = exposed.values().map(CommandArm::to_matcher);
|
||||||
x.2.clone(),
|
let implementations = exposed.values().map(CommandArm::to_implementation);
|
||||||
));
|
write_quote_to(out, quote! {
|
||||||
let implementations = exposed.values().map(|x|CommandArm(
|
/// Generated by [tengri_proc].
|
||||||
x.0.clone(),
|
pub enum #enumeration {
|
||||||
x.1.clone(),
|
#(#variants)*
|
||||||
x.2.clone(),
|
|
||||||
x.3.clone(),
|
|
||||||
));
|
|
||||||
for token in quote! {
|
|
||||||
#block
|
|
||||||
enum #enumeration {
|
|
||||||
#(#definitions)*
|
|
||||||
}
|
}
|
||||||
|
#block
|
||||||
|
/// Generated by [tengri_proc].
|
||||||
|
impl<'a> TryFromDsl<'a, #target> for #enumeration {
|
||||||
|
fn try_from_expr (state: &#target, iter: TokenIter) -> Option<Self> {
|
||||||
|
let mut iter = iter.clone();
|
||||||
|
match iter.next() {
|
||||||
|
#(#matchers)*
|
||||||
|
_ => None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/// Generated by [tengri_proc].
|
||||||
impl Command<#target> for #enumeration {
|
impl Command<#target> for #enumeration {
|
||||||
fn execute (self, state: &mut #target) -> Perhaps<Self> {
|
fn execute (self, state: &mut #target) -> Perhaps<Self> {
|
||||||
match self {
|
match self {
|
||||||
|
|
@ -78,12 +84,107 @@ impl ToTokens for CommandDef {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} {
|
});
|
||||||
out.append(token)
|
if exposed.len() > 0 {
|
||||||
|
//panic!("\n{}", quote! {#out});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl CommandArm {
|
||||||
|
fn to_key (&self) -> Arc<str> {
|
||||||
|
Self::ident_to_key(&self.0)
|
||||||
|
}
|
||||||
|
fn to_enum_variant_ident (&self) -> Ident {
|
||||||
|
Ident::new(&Self::ident_to_enum_variant(&self.0), Span::call_site())
|
||||||
|
}
|
||||||
|
fn ident_to_key (ident: &Ident) -> Arc<str> {
|
||||||
|
format!("{}", AsKebabCase(format!("{ident}"))).into()
|
||||||
|
}
|
||||||
|
fn ident_to_enum_variant (ident: &Ident) -> Arc<str> {
|
||||||
|
format!("{}", AsUpperCamelCase(format!("{ident}"))).into()
|
||||||
|
}
|
||||||
|
fn to_enum_variant (&self, with_types: bool, trailing_comma: bool, with_values: bool) -> TokenStream2 {
|
||||||
|
let mut out = TokenStream2::new();
|
||||||
|
out.append(self.to_enum_variant_ident());
|
||||||
|
let ident = &self.0;
|
||||||
|
if self.1.len() > 2 {
|
||||||
|
out.append(Group::new(Delimiter::Brace, {
|
||||||
|
let mut out = TokenStream2::new();
|
||||||
|
for arg in self.1.iter().skip(2) {
|
||||||
|
if let FnArg::Typed(PatType { attrs, pat, colon_token, ty }) = arg {
|
||||||
|
write_quote_to(&mut out, quote! { #pat });
|
||||||
|
if with_types && with_values {
|
||||||
|
unreachable!();
|
||||||
|
}
|
||||||
|
if with_types {
|
||||||
|
write_quote_to(&mut out, quote! { : #ty });
|
||||||
|
}
|
||||||
|
if with_values {
|
||||||
|
let take_err = LitStr::new(&format!("{}: missing argument \"{}\" ({})",
|
||||||
|
quote!{#ident}, quote!{#pat}, quote!{#ty}), Span::call_site());
|
||||||
|
write_quote_to(&mut out, quote! {
|
||||||
|
: Context::get(state, &iter.next().expect(#take_err).value)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
unreachable!("only typed args should be present at this position")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
out
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
if trailing_comma {
|
||||||
|
out.append(Punct::new(',', Alone));
|
||||||
|
}
|
||||||
|
out
|
||||||
|
}
|
||||||
|
fn to_matcher (&self) -> TokenStream2 {
|
||||||
|
let mut out = TokenStream2::new();
|
||||||
|
let key = LitStr::new(&self.to_key(), Span::call_site());
|
||||||
|
let ident = &self.0;
|
||||||
|
let take_args = self.1.iter().skip(2).map(|arg|{
|
||||||
|
if let FnArg::Typed(PatType { attrs, pat, colon_token, ty }) = arg {
|
||||||
|
let take_err = LitStr::new(&format!("{}: missing argument \"{}\" ({})",
|
||||||
|
quote!{#ident}, quote!{#pat}, quote!{#ty}), Span::call_site());
|
||||||
|
write_quote(quote! {
|
||||||
|
let #pat: #ty = Context::<#ty>::get(
|
||||||
|
state,
|
||||||
|
&iter.next().expect(#take_err).value
|
||||||
|
);
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
unreachable!("only typed args should be present at this position")
|
||||||
|
}
|
||||||
|
}).collect::<Vec<_>>();
|
||||||
|
let variant = Self::ident_to_enum_variant(&self.0);
|
||||||
|
let variant = self.to_enum_variant(false, false, true);
|
||||||
|
write_quote(quote! {
|
||||||
|
Some(::tengri::dsl::Token { value: ::tengri::dsl::Value::Key(#key), .. }) => {
|
||||||
|
let mut iter = iter.clone();
|
||||||
|
//#(#take_args)*
|
||||||
|
//let rest = iter; // TODO
|
||||||
|
Some(Self::#variant)
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
fn to_implementation (&self) -> TokenStream2 {
|
||||||
|
let ident = &self.0;
|
||||||
|
let variant = self.to_enum_variant(false, false, false);
|
||||||
|
let mut give_rest = write_quote(quote! { });
|
||||||
|
let give_args = self.1.iter().skip(2).map(|arg|{
|
||||||
|
if let FnArg::Typed(PatType { attrs, pat, colon_token, ty }) = arg {
|
||||||
|
//let give_err = LitStr::new(&format!("{}: missing value \"{}\" ({})",
|
||||||
|
//quote!{#ident}, quote!{#pat}, quote!{#ty}), Span::call_site());
|
||||||
|
write_quote(quote! { #pat, })
|
||||||
|
} else {
|
||||||
|
unreachable!("only typed args should be present at this position")
|
||||||
|
}
|
||||||
|
}).collect::<Vec<_>>();
|
||||||
|
write_quote(quote! { Self::#variant => self.#ident(state, #(#give_args)* #give_rest), })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl ToTokens for CommandVariant {
|
impl ToTokens for CommandVariant {
|
||||||
fn to_tokens (&self, out: &mut TokenStream2) {
|
fn to_tokens (&self, out: &mut TokenStream2) {
|
||||||
let Self(ident, args) = self;
|
let Self(ident, args) = self;
|
||||||
|
|
@ -92,9 +193,7 @@ impl ToTokens for CommandVariant {
|
||||||
out.append(Group::new(Delimiter::Parenthesis, {
|
out.append(Group::new(Delimiter::Parenthesis, {
|
||||||
let mut out = TokenStream2::new();
|
let mut out = TokenStream2::new();
|
||||||
for arg in args.iter() {
|
for arg in args.iter() {
|
||||||
if let FnArg::Typed(PatType {
|
if let FnArg::Typed(PatType { attrs, pat, colon_token, ty }) = arg {
|
||||||
attrs, pat, colon_token, ty
|
|
||||||
}) = arg {
|
|
||||||
out.append(LitStr::new(
|
out.append(LitStr::new(
|
||||||
&format!("{}", quote! { #ty }),
|
&format!("{}", quote! { #ty }),
|
||||||
Span::call_site()
|
Span::call_site()
|
||||||
|
|
@ -110,34 +209,30 @@ impl ToTokens for CommandVariant {
|
||||||
|
|
||||||
impl ToTokens for CommandArm {
|
impl ToTokens for CommandArm {
|
||||||
fn to_tokens (&self, out: &mut TokenStream2) {
|
fn to_tokens (&self, out: &mut TokenStream2) {
|
||||||
let Self(symbol, ident, args, returnType) = self;
|
let Self(ident, args, returnType) = self;
|
||||||
out.append(Punct::new(':', Joint));
|
for ident in ["tengri", "dsl", "Value", "Sym"].iter() {
|
||||||
out.append(Punct::new(':', Alone));
|
out.append(Punct::new(':', Joint));
|
||||||
out.append(Ident::new("tengri", Span::call_site()));
|
out.append(Punct::new(':', Alone));
|
||||||
out.append(Punct::new(':', Joint));
|
out.append(Ident::new(ident, Span::call_site()));
|
||||||
out.append(Punct::new(':', Alone));
|
}
|
||||||
out.append(Ident::new("dsl", Span::call_site()));
|
|
||||||
out.append(Punct::new(':', Joint));
|
|
||||||
out.append(Punct::new(':', Alone));
|
|
||||||
out.append(Ident::new("Value", Span::call_site()));
|
|
||||||
out.append(Punct::new(':', Joint));
|
|
||||||
out.append(Punct::new(':', Alone));
|
|
||||||
out.append(Ident::new("Sym", Span::call_site()));
|
|
||||||
out.append(Group::new(Delimiter::Parenthesis, {
|
out.append(Group::new(Delimiter::Parenthesis, {
|
||||||
let mut out = TokenStream2::new();
|
let mut out = TokenStream2::new();
|
||||||
|
out.append(self.to_enum_variant_ident());
|
||||||
for arg in args.iter() {
|
for arg in args.iter() {
|
||||||
out.append(LitStr::new(&symbol, Span::call_site()).token());
|
|
||||||
}
|
}
|
||||||
out
|
out
|
||||||
}));
|
}));
|
||||||
out.append(Punct::new('=', Joint));
|
out.append(Punct::new('=', Joint));
|
||||||
out.append(Punct::new('>', Alone));
|
out.append(Punct::new('>', Alone));
|
||||||
out.append(LitStr::new(&format!("{}", ident), Span::call_site()).token());
|
out.append(Ident::new("self", Span::call_site()));
|
||||||
|
out.append(Punct::new('.', Alone));
|
||||||
|
out.append(ident.clone());
|
||||||
out.append(Group::new(Delimiter::Parenthesis, {
|
out.append(Group::new(Delimiter::Parenthesis, {
|
||||||
let mut out = TokenStream2::new();
|
let mut out = TokenStream2::new();
|
||||||
for arg in args.iter() {
|
for arg in args.iter() {
|
||||||
// TODO
|
// TODO
|
||||||
//out.append(LitStr::new(&symbol, Span::call_site()).token());
|
out.append(LitStr::new(&self.to_key(), Span::call_site()).token());
|
||||||
|
out.append(Punct::new(',', Alone));
|
||||||
}
|
}
|
||||||
out
|
out
|
||||||
}));
|
}));
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue