Skip to content

Commit

Permalink
Switch to the Rust wast crate for parsing *.witx (WebAssembly#124)
Browse files Browse the repository at this point in the history
This commit switches to the recently-published `wast` crate to parse
`*.witx` file. The `wast` crate is intended to be a way to easily write
recursive descent and composable parsers. It includes built-in parsers
for all of `*.wat` and `*.wast` syntax, but only a few limited ones were
used for `*.witx`, otherwise it's just the idioms that were used for the
`Parse` trait and such!

The hope here is that this can consolidate lexing/handling of
s-expressions and simplify the parsing strategy as well. Eventually it's
intended that `wast` is flexible enough to have built-in parsers for
interface types as well!

The crate was refactored slightly in implementing this change,
prominently:

* The `validate` pass is not baked into the `src/toplevel.rs` parsing.
* The `src/toplevel.rs` parsing was simplified slightly to have only one
  recursive function for parsing includes.
* All `*Syntax` data types now have a `'a` lifetime parameter and are
  consumed immediately on the stack frame while parsing.
* `Location` is no longer manufactured while parsing, but rather it's
  created on-demand while the ast is being constructed. This required a
  small refactoring to have a `DocValidationScope` which carries
  metadata about the current file that was parsed to manufacture
  `Location`.
  • Loading branch information
alexcrichton authored and Pat Hickey committed Oct 22, 2019
1 parent be294c8 commit 44ecbe9
Show file tree
Hide file tree
Showing 9 changed files with 608 additions and 1,276 deletions.
1 change: 1 addition & 0 deletions tools/witx/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ path = "src/main.rs"

[dependencies]
clap = "2"
wast = "3.0.1"
failure = "0.1"
12 changes: 12 additions & 0 deletions tools/witx/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@ pub trait WitxIo {
fn canonicalize(&self, path: &Path) -> Result<PathBuf, WitxError>;
}

impl<T: WitxIo + ?Sized> WitxIo for &'_ T {
fn fgets(&self, path: &Path) -> Result<String, WitxError> {
T::fgets(self, path)
}
fn fget_line(&self, path: &Path, line_num: usize) -> Result<String, WitxError> {
T::fget_line(self, path, line_num)
}
fn canonicalize(&self, path: &Path) -> Result<PathBuf, WitxError> {
T::canonicalize(self, path)
}
}

pub struct Filesystem;

impl WitxIo for Filesystem {
Expand Down
354 changes: 0 additions & 354 deletions tools/witx/src/lexer.rs

This file was deleted.

Loading

0 comments on commit 44ecbe9

Please sign in to comment.