-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
217 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
use proc_macro2::TokenStream; | ||
use quote::{quote, ToTokens}; | ||
use syn::parse::ParseStream; | ||
use syn::punctuated::Punctuated; | ||
use syn::Error; | ||
|
||
pub(crate) struct ModuleAttrs { | ||
path: syn::Path, | ||
} | ||
|
||
impl ModuleAttrs { | ||
/// Parse the given parse stream. | ||
pub(crate) fn parse(input: ParseStream) -> Result<Self, Error> { | ||
let path = input.parse::<syn::Path>()?; | ||
let stream = input.parse::<TokenStream>()?; | ||
|
||
if !stream.is_empty() { | ||
return Err(syn::Error::new_spanned(stream, "Unexpected input")); | ||
} | ||
|
||
Ok(Self { path }) | ||
} | ||
} | ||
|
||
pub(crate) struct Module { | ||
attributes: Vec<syn::Attribute>, | ||
docs: syn::ExprArray, | ||
remainder: TokenStream, | ||
} | ||
|
||
impl Module { | ||
/// Parse the given parse stream. | ||
pub(crate) fn parse(input: ParseStream) -> Result<Self, Error> { | ||
let parsed_attributes = input.call(syn::Attribute::parse_outer)?; | ||
|
||
let mut docs = syn::ExprArray { | ||
attrs: Vec::new(), | ||
bracket_token: syn::token::Bracket::default(), | ||
elems: Punctuated::default(), | ||
}; | ||
|
||
let mut attributes = Vec::new(); | ||
|
||
for attr in parsed_attributes { | ||
if attr.path().is_ident("doc") { | ||
if let syn::Meta::NameValue(name_value) = &attr.meta { | ||
docs.elems.push(name_value.value.clone()); | ||
} | ||
} | ||
|
||
attributes.push(attr); | ||
} | ||
|
||
let remainder = input.parse::<TokenStream>()?; | ||
|
||
Ok(Self { | ||
attributes, | ||
docs, | ||
remainder, | ||
}) | ||
} | ||
|
||
/// Expand the function declaration. | ||
pub(crate) fn expand(self, attrs: ModuleAttrs) -> Result<TokenStream, Error> { | ||
let docs = self.docs; | ||
|
||
let item = match attrs.path.leading_colon { | ||
Some(..) => { | ||
let mut it = attrs.path.segments.iter(); | ||
|
||
let Some(krate) = it.next() else { | ||
return Err(Error::new_spanned(&attrs.path, "missing leading segment")); | ||
}; | ||
|
||
let krate = syn::LitStr::new(&krate.ident.to_string(), krate.ident.span()); | ||
let item = build_item(it); | ||
quote!(rune::__private::ItemBuf::with_crate_item(#krate, #item)) | ||
} | ||
None => { | ||
let item = build_item(attrs.path.segments.iter()); | ||
quote!(rune::__private::ItemBuf::from_item(#item)) | ||
} | ||
}; | ||
|
||
let mut stream = TokenStream::new(); | ||
|
||
stream.extend(quote! { | ||
/// Get module metadata. | ||
#[automatically_derived] | ||
#[doc(hidden)] | ||
fn module_meta() -> rune::__private::ModuleMetaData { | ||
rune::__private::ModuleMetaData { | ||
docs: &#docs[..], | ||
item: #item, | ||
} | ||
} | ||
}); | ||
|
||
for attribute in self.attributes { | ||
attribute.to_tokens(&mut stream); | ||
} | ||
|
||
stream.extend(self.remainder); | ||
Ok(stream) | ||
} | ||
} | ||
|
||
fn build_item(it: syn::punctuated::Iter<'_, syn::PathSegment>) -> syn::ExprArray { | ||
let mut item = syn::ExprArray { | ||
attrs: Vec::new(), | ||
bracket_token: syn::token::Bracket::default(), | ||
elems: Punctuated::default(), | ||
}; | ||
|
||
for p in it { | ||
let p = syn::LitStr::new(&p.ident.to_string(), p.ident.span()); | ||
|
||
item.elems.push(syn::Expr::Lit(syn::ExprLit { | ||
attrs: Vec::new(), | ||
lit: syn::Lit::Str(p), | ||
})) | ||
} | ||
item | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.