From c773b29d6d2e24b27ff4f5dc754a5592a28ce895 Mon Sep 17 00:00:00 2001 From: Nick Fitzgerald Date: Wed, 30 May 2018 14:30:40 -0700 Subject: [PATCH] webidl: add `compile` and `compile_file` These will be the functions invoked by crates compiling WebIDL into wasm-bindgen Rust sources inside `build.rs`. --- crates/webidl/Cargo.toml | 1 + crates/webidl/src/lib.rs | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/crates/webidl/Cargo.toml b/crates/webidl/Cargo.toml index 2a53980f..c37b3989 100644 --- a/crates/webidl/Cargo.toml +++ b/crates/webidl/Cargo.toml @@ -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" diff --git a/crates/webidl/src/lib.rs b/crates/webidl/src/lib.rs index 602f61c6..67f206b7 100755 --- a/crates/webidl/src/lib.rs +++ b/crates/webidl/src/lib.rs @@ -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 { 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 { + 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 { + 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<()>; }