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:
1
crates/generate/.gitignore
vendored
Normal file
1
crates/generate/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
target
|
||||
@@ -12,3 +12,4 @@ witx = { path = "../WASI/tools/witx" }
|
||||
quote = "1.0"
|
||||
proc-macro2 = "1.0"
|
||||
heck = "0.3"
|
||||
anyhow = "1"
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
33
crates/generate/src/parse.rs
Normal file
33
crates/generate/src/parse.rs
Normal 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)
|
||||
}
|
||||
Reference in New Issue
Block a user