component |
|
|
component.rs |
Types and support for parsing the component model text format. |
526 |
core |
|
|
core.rs |
Types and support for parsing the core wasm text format. |
586 |
encode.rs |
|
1842 |
error.rs |
|
5884 |
gensym.rs |
|
428 |
lexer.rs |
Definition of a lexer for the WebAssembly text format.
This module provides a [`Lexer`][] type which is an iterate over the raw
tokens of a WebAssembly text file. A [`Lexer`][] accounts for every single
byte in a WebAssembly text field, returning tokens even for comments and
whitespace. Typically you'll ignore comments and whitespace, however.
If you'd like to iterate over the tokens in a file you can do so via:
```
# fn foo() -> Result<(), wast::Error> {
use wast::lexer::Lexer;
let wat = "(module (func $foo))";
for token in Lexer::new(wat).iter(0) {
println!("{:?}", token?);
}
# Ok(())
# }
```
Note that you'll typically not use this module but will rather use
[`ParseBuffer`](crate::parser::ParseBuffer) instead.
[`Lexer`]: crate::lexer::Lexer |
56414 |
lib.rs |
A crate for low-level parsing of the WebAssembly text formats: WAT and WAST.
This crate is intended to be a low-level detail of the `wat` crate,
providing a low-level parsing API for parsing WebAssembly text format
structures. The API provided by this crate is very similar to
[`syn`](https://docs.rs/syn) and provides the ability to write customized
parsers which may be an extension to the core WebAssembly text format. For
more documentation see the [`parser`] module.
# High-level Overview
This crate provides a few major pieces of functionality
* [`lexer`] - this is a raw lexer for the wasm text format. This is not
customizable, but if you'd like to iterate over raw tokens this is the
module for you. You likely won't use this much.
* [`parser`] - this is the workhorse of this crate. The [`parser`] module
provides the [`Parse`][] trait primarily and utilities
around working with a [`Parser`](`parser::Parser`) to parse streams of
tokens.
* [`Module`](crate::core::Module) - this contains an Abstract Syntax Tree
(AST) of the WebAssembly Text format (WAT) as well as the unofficial WAST
format. This also has a [`Module::encode`](crate::core::Module::encode)
method to emit a module in its binary form.
# Stability and WebAssembly Features
This crate provides support for many in-progress WebAssembly features such
as reference types, multi-value, etc. Be sure to check out the documentation
of the [`wast` crate](https://docs.rs/wast) for policy information on crate
stability vs WebAssembly Features. The tl;dr; version is that this crate
will issue semver-non-breaking releases which will break the parsing of the
text format. This crate, unlike `wast`, is expected to have numerous Rust
public API changes, all of which will be accompanied with a semver-breaking
release.
# Compile-time Cargo features
This crate has a `wasm-module` feature which is turned on by default which
includes all necessary support to parse full WebAssembly modules. If you
don't need this (for example you're parsing your own s-expression format)
then this feature can be disabled.
This crate also has an off-by-default `dwarf` feature which enables using
[`core::EncodeOptions::dwarf`] to embed DWARF debugging information in generated
binaries.
[`Parse`]: parser::Parse
[`LexError`]: lexer::LexError |
18327 |
names.rs |
|
1841 |
parser.rs |
Traits for parsing the WebAssembly Text format
This module contains the traits, abstractions, and utilities needed to
define custom parsers for WebAssembly text format items. This module exposes
a recursive descent parsing strategy and centers around the [`Parse`] trait
for defining new fragments of WebAssembly text syntax.
The top-level [`parse`] function can be used to fully parse AST fragments:
```
use wast::Wat;
use wast::parser::{self, ParseBuffer};
# fn foo() -> Result<(), wast::Error> {
let wat = "(module (func))";
let buf = ParseBuffer::new(wat)?;
let module = parser::parse::<Wat>(&buf)?;
# Ok(())
# }
```
and you can also define your own new syntax with the [`Parse`] trait:
```
use wast::kw;
use wast::core::{Import, Func};
use wast::parser::{Parser, Parse, Result};
// Fields of a WebAssembly which only allow imports and functions, and all
// imports must come before all the functions
struct OnlyImportsAndFunctions<'a> {
imports: Vec<Import<'a>>,
functions: Vec<Func<'a>>,
}
impl<'a> Parse<'a> for OnlyImportsAndFunctions<'a> {
fn parse(parser: Parser<'a>) -> Result<Self> {
// While the second token is `import` (the first is `(`, so we care
// about the second) we parse an `ast::ModuleImport` inside of
// parentheses. The `parens` function here ensures that what we
// parse inside of it is surrounded by `(` and `)`.
let mut imports = Vec::new();
while parser.peek2::<kw::import>()? {
let import = parser.parens(|p| p.parse())?;
imports.push(import);
}
// Afterwards we assume everything else is a function. Note that
// `parse` here is a generic function and type inference figures out
// that we're parsing functions here and imports above.
let mut functions = Vec::new();
while !parser.is_empty() {
let func = parser.parens(|p| p.parse())?;
functions.push(func);
}
Ok(OnlyImportsAndFunctions { imports, functions })
}
}
```
This module is heavily inspired by [`syn`](https://docs.rs/syn) so you can
likely also draw inspiration from the excellent examples in the `syn` crate. |
51617 |
token.rs |
Common tokens that implement the [`Parse`] trait which are otherwise not
associated specifically with the wasm text format per se (useful in other
contexts too perhaps). |
23458 |
wast.rs |
|
14276 |
wat.rs |
|
2273 |