[][src]Trait wast::parser::Parse

pub trait Parse<'a>: Sized {
    fn parse(parser: Parser<'a>) -> Result<Self>;
}

A trait for parsing a fragment of syntax in a recursive descent fashion.

The Parse trait is main abstraction you'll be working with when defining custom parser or custom syntax for your WebAssembly text format (or when using the official format items). Almost all items in the ast module implement the Parse trait, and you'll commonly use this with:

  • The top-level parse function to parse an entire input.
  • The intermediate Parser::parse function to parse an item out of an input stream and then parse remaining items.

Implementation of Parse take a Parser as input and will mutate the parser as they parse syntax. Once a token is consume it cannot be "un-consumed". Utilities such as Parser::peek and Parser::lookahead1 can be used to determine what to parse next.

When to parse ( and )?

Conventionally types are not responsible for parsing their own ( and ) tokens which surround the type. For example WebAssembly imports look like:

(import "foo" "bar" (func (type 0)))

but the Import type parser looks like:

impl<'a> Parse<'a> for Import<'a> {
    fn parse(parser: Parser<'a>) -> Result<Self> {
        parser.parse::<kw::import>()?;
        // ...
    }
}

It is assumed here that the ( and ) tokens which surround an import statement in the WebAssembly text format are parsed by the parent item parsing Import.

Note that this is just a a convention, so it's not necessarily required of all types. It's recommended that your types stick to this convention where possible to avoid nested calls to Parser::parens or accidentally trying to parse too many parenthesis.

Examples

Let's say you want to define your own WebAssembly text format which only contains imports and functions. You also require all imports to be listed before all functions. An example Parse implementation might look like:

use wast::{Import, Func, kw};
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 })
    }
}

Required methods

fn parse(parser: Parser<'a>) -> Result<Self>

Attempts to parse Self from parser, returning an error if it could not be parsed.

This method will mutate the state of parser after attempting to parse an instance of Self. If an error happens then it is likely fatal and there is no guarantee of how many tokens have been consumed from parser.

As recommended in the documentation of Parse, implementations of this function should not start out by parsing ( and ) tokens, but rather parents calling recursive parsers should parse the ( and ) tokens for their child item that's being parsed.

Errors

This function will return an error if Self could not be parsed. Note that creating an Error is not exactly a cheap operation, so Error is typically fatal and propagated all the way back to the top parse call site.

Loading content...

Implementations on Foreign Types

impl<'a> Parse<'a> for u8[src]

impl<'a> Parse<'a> for u16[src]

impl<'a> Parse<'a> for u32[src]

impl<'a> Parse<'a> for u64[src]

impl<'a> Parse<'a> for i8[src]

impl<'a> Parse<'a> for i16[src]

impl<'a> Parse<'a> for i32[src]

impl<'a> Parse<'a> for i64[src]

impl<'a> Parse<'a> for &'a [u8][src]

impl<'a> Parse<'a> for &'a str[src]

impl<'_> Parse<'_> for String[src]

impl<'a, T: Peek + Parse<'a>> Parse<'a> for Option<T>[src]

Loading content...

Implementors

impl<'a> Parse<'a> for Index<'a>[src]

impl<'a> Parse<'a> for Instruction<'a>[src]

impl<'a> Parse<'a> for ModuleField<'a>[src]

impl<'a> Parse<'a> for QuoteModule<'a>[src]

impl<'a> Parse<'a> for TableElemType[src]

impl<'a> Parse<'a> for V128Const[src]

impl<'a> Parse<'a> for ValType[src]

impl<'a> Parse<'a> for WastDirective<'a>[src]

impl<'a> Parse<'a> for WastExecute<'a>[src]

impl<'a> Parse<'a> for anyfunc[src]

impl<'a> Parse<'a> for anyref[src]

impl<'a> Parse<'a> for assert_exhaustion[src]

impl<'a> Parse<'a> for assert_invalid[src]

impl<'a> Parse<'a> for assert_malformed[src]

impl<'a> Parse<'a> for assert_return[src]

impl<'a> Parse<'a> for assert_return_arithmetic_nan[src]

impl<'a> Parse<'a> for assert_return_canonical_nan[src]

impl<'a> Parse<'a> for assert_return_func[src]

impl<'a> Parse<'a> for assert_trap[src]

impl<'a> Parse<'a> for assert_unlinkable[src]

impl<'a> Parse<'a> for binary[src]

impl<'a> Parse<'a> for block[src]

impl<'a> Parse<'a> for data[src]

impl<'a> Parse<'a> for elem[src]

impl<'a> Parse<'a> for else[src]

impl<'a> Parse<'a> for end[src]

impl<'a> Parse<'a> for export[src]

impl<'a> Parse<'a> for f32[src]

impl<'a> Parse<'a> for f32x4[src]

impl<'a> Parse<'a> for f64[src]

impl<'a> Parse<'a> for f64x2[src]

impl<'a> Parse<'a> for func[src]

impl<'a> Parse<'a> for funcref[src]

impl<'a> Parse<'a> for get[src]

impl<'a> Parse<'a> for global[src]

impl<'a> Parse<'a> for i16x8[src]

impl<'a> Parse<'a> for i32[src]

impl<'a> Parse<'a> for i32x4[src]

impl<'a> Parse<'a> for i64[src]

impl<'a> Parse<'a> for i64x2[src]

impl<'a> Parse<'a> for i8x16[src]

impl<'a> Parse<'a> for if[src]

impl<'a> Parse<'a> for import[src]

impl<'a> Parse<'a> for invoke[src]

impl<'a> Parse<'a> for local[src]

impl<'a> Parse<'a> for loop[src]

impl<'a> Parse<'a> for memory[src]

impl<'a> Parse<'a> for module[src]

impl<'a> Parse<'a> for mut[src]

impl<'a> Parse<'a> for offset[src]

impl<'a> Parse<'a> for param[src]

impl<'a> Parse<'a> for passive[src]

impl<'a> Parse<'a> for quote[src]

impl<'a> Parse<'a> for ref_func[src]

impl<'a> Parse<'a> for ref_null[src]

impl<'a> Parse<'a> for register[src]

impl<'a> Parse<'a> for result[src]

impl<'a> Parse<'a> for shared[src]

impl<'a> Parse<'a> for start[src]

impl<'a> Parse<'a> for table[src]

impl<'a> Parse<'a> for then[src]

impl<'a> Parse<'a> for type[src]

impl<'a> Parse<'a> for v128[src]

impl<'a> Parse<'a> for BlockType<'a>[src]

impl<'a> Parse<'a> for BrTableIndices<'a>[src]

impl<'a> Parse<'a> for CallIndirect<'a>[src]

impl<'a> Parse<'a> for Data<'a>[src]

impl<'a> Parse<'a> for Elem<'a>[src]

impl<'a> Parse<'a> for Export<'a>[src]

impl<'a> Parse<'a> for Expression<'a>[src]

impl<'a> Parse<'a> for Float32[src]

impl<'a> Parse<'a> for Float64[src]

impl<'a> Parse<'a> for Func<'a>[src]

impl<'a> Parse<'a> for FunctionType<'a>[src]

impl<'a> Parse<'a> for Global<'a>[src]

impl<'a> Parse<'a> for GlobalType[src]

impl<'a> Parse<'a> for Id<'a>[src]

impl<'a> Parse<'a> for Import<'a>[src]

impl<'a> Parse<'a> for InlineExport<'a>[src]

impl<'a> Parse<'a> for Limits[src]

impl<'a> Parse<'a> for Memory<'a>[src]

impl<'a> Parse<'a> for MemoryInit<'a>[src]

impl<'a> Parse<'a> for MemoryType[src]

impl<'a> Parse<'a> for Module<'a>[src]

impl<'a> Parse<'a> for Table<'a>[src]

impl<'a> Parse<'a> for TableInit<'a>[src]

impl<'a> Parse<'a> for TableType[src]

impl<'a> Parse<'a> for Type<'a>[src]

impl<'a> Parse<'a> for TypeUse<'a>[src]

impl<'a> Parse<'a> for V8x16Shuffle[src]

impl<'a> Parse<'a> for Wast<'a>[src]

impl<'a> Parse<'a> for WastInvoke<'a>[src]

impl<'a> Parse<'a> for Wat<'a>[src]

Loading content...