webidl: add compile and compile_file

These will be the functions invoked by crates compiling WebIDL into wasm-bindgen
Rust sources inside `build.rs`.
This commit is contained in:
Nick Fitzgerald 2018-05-30 14:30:40 -07:00
parent a30ccd7c18
commit c773b29d6d
2 changed files with 23 additions and 0 deletions

View File

@ -13,6 +13,7 @@ wasm-bindgen-backend = { version = "=0.2.11", path = "../backend", features = ["
[dependencies]
failure = "0.1"
proc-macro2 = "0.4"
quote = '0.6'
syn = { version = '0.14', features = ['full'] }
wasm-bindgen-backend = { version = "=0.2.11", path = "../backend" }
webidl = "0.6.0"

View File

@ -10,12 +10,14 @@ emitted for the types and methods described in the WebIDL.
extern crate failure;
extern crate proc_macro2;
extern crate quote;
extern crate syn;
extern crate wasm_bindgen_backend as backend;
extern crate webidl;
use failure::ResultExt;
use proc_macro2::Ident;
use quote::ToTokens;
use std::fs;
use std::io::{self, Read};
use std::path::Path;
@ -43,6 +45,26 @@ pub fn parse(webidl_source: &str) -> Result<backend::ast::Program> {
Ok(program)
}
/// Compile the given WebIDL file into Rust source text containing
/// `wasm-bindgen` bindings to the things described in the WebIDL.
pub fn compile_file(webidl_path: &Path) -> Result<String> {
let ast = parse_file(webidl_path)?;
Ok(compile_ast(&ast))
}
/// Compile the given WebIDL source text into Rust source text containing
/// `wasm-bindgen` bindings to the things described in the WebIDL.
pub fn compile(webidl_source: &str) -> Result<String> {
let ast = parse(webidl_source)?;
Ok(compile_ast(&ast))
}
fn compile_ast(ast: &backend::ast::Program) -> String {
let mut tokens = proc_macro2::TokenStream::new();
ast.to_tokens(&mut tokens);
tokens.to_string()
}
trait WebidlParse {
fn webidl_parse(&self, program: &mut backend::ast::Program) -> Result<()>;
}