change proc macro argument parsing to use syn

This commit is contained in:
Pat Hickey
2020-01-30 14:29:53 -08:00
parent e2079c085d
commit 0ba8e73184
4 changed files with 41 additions and 9 deletions

View File

@@ -14,3 +14,4 @@ quote = "1.0"
proc-macro2 = "1.0" proc-macro2 = "1.0"
heck = "0.3" heck = "0.3"
anyhow = "1" anyhow = "1"
syn = { version = "1.0", features = ["full"] }

View File

@@ -1,7 +1,37 @@
use anyhow::{bail, Result}; use std::path::PathBuf;
use proc_macro2::{Literal, TokenStream, TokenTree};
pub fn witx_paths(args: TokenStream) -> Result<Vec<String>> { use syn::{
bracketed,
parse::{Parse, ParseStream},
punctuated::Punctuated,
token, LitStr, Result, Token,
};
pub struct Config {
_bracket_token: token::Bracket,
path_lits: Punctuated<LitStr, Token![,]>,
}
impl Config {
pub fn witx_paths(&self) -> Vec<PathBuf> {
self.path_lits
.iter()
.map(|lit| PathBuf::from(lit.value()))
.collect()
}
}
impl Parse for Config {
fn parse(input: ParseStream) -> Result<Self> {
let content;
Ok(Config {
_bracket_token: bracketed!(content in input),
path_lits: content.parse_terminated(Parse::parse)?,
})
}
}
/*
let arg_strings = args let arg_strings = args
.into_iter() .into_iter()
.map(|arg| match arg { .map(|arg| match arg {
@@ -31,3 +61,4 @@ fn string_literal(literal: Literal) -> Result<String> {
} }
Ok(trimmed) Ok(trimmed)
} }
*/

View File

@@ -1,15 +1,16 @@
extern crate proc_macro; extern crate proc_macro;
mod config;
mod funcs; mod funcs;
mod module_trait; mod module_trait;
mod names; mod names;
mod parse;
mod types; mod types;
use proc_macro::TokenStream; use proc_macro::TokenStream;
use proc_macro2::TokenStream as TokenStream2;
use quote::quote; use quote::quote;
use syn::parse_macro_input;
use config::Config;
use funcs::define_func; use funcs::define_func;
use module_trait::define_module_trait; use module_trait::define_module_trait;
use names::Names; use names::Names;
@@ -17,12 +18,11 @@ use types::define_datatype;
#[proc_macro] #[proc_macro]
pub fn from_witx(args: TokenStream) -> TokenStream { pub fn from_witx(args: TokenStream) -> TokenStream {
let args = TokenStream2::from(args); let config = parse_macro_input!(args as Config);
let witx_paths = parse::witx_paths(args).expect("parsing macro arguments");
let names = Names::new(); // TODO parse the names from the invocation of the macro, or from a file? let names = Names::new(); // TODO parse the names from the invocation of the macro, or from a file?
let doc = witx::load(&witx_paths).expect("loading witx"); let doc = witx::load(&config.witx_paths()).expect("loading witx");
let types = doc.typenames().map(|t| define_datatype(&names, &t)); let types = doc.typenames().map(|t| define_datatype(&names, &t));

View File

@@ -1,6 +1,6 @@
pub mod test { pub mod test {
// FIXME: parameterize macro on what ctx type is used here // FIXME: parameterize macro on what ctx type is used here
generate::from_witx!("test.witx"); generate::from_witx!(["test.witx"]);
pub struct WasiCtx { pub struct WasiCtx {
guest_errors: Vec<::memory::GuestError>, guest_errors: Vec<::memory::GuestError>,