import some other helpers from pat's crate

* parse the proc macro argument into a file path
* use a much simpler witx spec by default
This commit is contained in:
Pat Hickey
2020-01-17 15:48:51 -08:00
parent 0d47556cf7
commit 4c7b3e8685
7 changed files with 65 additions and 14 deletions

1
crates/generate/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
target

View File

@@ -12,3 +12,4 @@ witx = { path = "../WASI/tools/witx" }
quote = "1.0"
proc-macro2 = "1.0"
heck = "0.3"
anyhow = "1"

View File

@@ -3,8 +3,6 @@ use proc_macro2::{Delimiter, Group, Literal, TokenStream, TokenTree};
use quote::{format_ident, quote};
use std::convert::TryFrom;
const WITX_PATH: &'static str = "crates/WASI/phases/snapshot/witx/wasi_snapshot_preview1.witx";
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum Mode {
Host,
@@ -21,15 +19,8 @@ impl Mode {
}
}
pub fn gen() -> TokenStream {
pub fn gen(doc: witx::Document) -> TokenStream {
let mut output = TokenStream::new();
let doc = match witx::load(&[&WITX_PATH]) {
Ok(doc) => doc,
Err(e) => {
panic!("error opening file {}: {}", WITX_PATH, e);
}
};
gen_datatypes(&mut output, &doc, Mode::Wasi);
// gen_datatypes(&mut output, &doc, Mode::Wasi32);
// gen_datatypes(&mut output, &doc, Mode::Host);

View File

@@ -1,10 +1,16 @@
extern crate proc_macro;
mod imp;
mod parse;
use proc_macro::TokenStream;
use proc_macro2::TokenStream as TokenStream2;
#[proc_macro]
pub fn from_witx(_args: TokenStream) -> TokenStream {
TokenStream::from(imp::gen())
pub fn from_witx(args: TokenStream) -> TokenStream {
let args = TokenStream2::from(args);
let witx_paths = parse::witx_paths(args).expect("parsing macro arguments");
let doc = witx::load(&witx_paths).expect("loading witx");
let out = imp::gen(doc);
TokenStream::from(out)
}

View File

@@ -0,0 +1,33 @@
use anyhow::{bail, Result};
use proc_macro2::{Literal, TokenStream, TokenTree};
pub fn witx_paths(args: TokenStream) -> Result<Vec<String>> {
let arg_strings = args
.into_iter()
.map(|arg| match arg {
TokenTree::Literal(lit) => string_literal(lit),
_ => bail!("expected string literal, got: {:?}", arg),
})
.collect::<Result<Vec<String>>>()?;
if arg_strings.is_empty() {
bail!("expected at least one argument");
}
Ok(arg_strings)
}
fn string_literal(literal: Literal) -> Result<String> {
let s = literal.to_string();
if !s.starts_with('"') || !s.ends_with('"') {
bail!("string literal must be enclosed in double quotes");
}
let trimmed = s[1..s.len() - 1].to_owned();
if trimmed.contains('"') {
bail!("string literal must not contain quotes");
}
if trimmed.contains('\\') {
bail!("string literal must not contain backslashes");
}
Ok(trimmed)
}

View File

@@ -1,2 +1,8 @@
generate::from_witx!();
pub mod test {
generate::from_witx!("test.witx");
}
/*
pub mod wasi {
generate::from_witx!("crates/WASI/phases/snapshot/witx/wasi_snapshot_preview1.witx");
}
*/

13
test.witx Normal file
View File

@@ -0,0 +1,13 @@
(typename $errno
(enum u32
$dont_want_to
$physically_unable
$picket_line))
(module $foo
(@interface func (export "bar")
(param $name string)
(result $error $errno))
)