pub trait Parse<'a>: Sized {
// Required method
fn parse(parser: Parser<'a>) -> Result<Self>;
}
Expand description
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
core
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 convention, so it’s not necessarily required for
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::core::{Import, Func};
use wast::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§
Sourcefn parse(parser: Parser<'a>) -> Result<Self>
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.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.
Implementations on Foreign Types§
Source§impl<'a> Parse<'a> for Option<NameAnnotation<'a>>
impl<'a> Parse<'a> for Option<NameAnnotation<'a>>
Source§impl<'a> Parse<'a> for Vec<ComponentTypeDecl<'a>>
Available on crate feature component-model
only.
impl<'a> Parse<'a> for Vec<ComponentTypeDecl<'a>>
component-model
only.Source§impl<'a> Parse<'a> for Vec<InstanceTypeDecl<'a>>
Available on crate feature component-model
only.
impl<'a> Parse<'a> for Vec<InstanceTypeDecl<'a>>
component-model
only.Source§impl<'a> Parse<'a> for Vec<ModuleTypeDecl<'a>>
Available on crate feature component-model
only.
impl<'a> Parse<'a> for Vec<ModuleTypeDecl<'a>>
component-model
only.Source§impl<'a> Parse<'a> for Vec<ComponentExport<'a>>
Available on crate feature component-model
only.
impl<'a> Parse<'a> for Vec<ComponentExport<'a>>
component-model
only.Source§impl<'a> Parse<'a> for Vec<CoreInstanceExport<'a>>
Available on crate feature component-model
only.
impl<'a> Parse<'a> for Vec<CoreInstanceExport<'a>>
component-model
only.Source§impl<'a> Parse<'a> for Vec<CoreInstantiationArg<'a>>
Available on crate feature component-model
only.
impl<'a> Parse<'a> for Vec<CoreInstantiationArg<'a>>
component-model
only.Source§impl<'a> Parse<'a> for Vec<InstantiationArg<'a>>
Available on crate feature component-model
only.
impl<'a> Parse<'a> for Vec<InstantiationArg<'a>>
component-model
only.Implementors§
impl<'a> Parse<'a> for CanonOpt<'a>
component-model
only.impl<'a> Parse<'a> for ComponentExportAliasKind
component-model
only.impl<'a> Parse<'a> for ComponentExportKind<'a>
component-model
only.impl<'a> Parse<'a> for ComponentField<'a>
component-model
only.impl<'a> Parse<'a> for ComponentOuterAliasKind
component-model
only.impl<'a> Parse<'a> for ComponentTypeDecl<'a>
component-model
only.impl<'a> Parse<'a> for ComponentValType<'a>
component-model
only.impl<'a> Parse<'a> for CoreFuncKind<'a>
component-model
only.impl<'a> Parse<'a> for CoreInstanceKind<'a>
component-model
only.impl<'a> Parse<'a> for CoreInstantiationArgKind<'a>
component-model
only.impl<'a> Parse<'a> for CoreTypeDef<'a>
component-model
only.impl<'a> Parse<'a> for FuncKind<'a>
component-model
only.impl<'a> Parse<'a> for InstanceKind<'a>
component-model
only.impl<'a> Parse<'a> for InstanceTypeDecl<'a>
component-model
only.impl<'a> Parse<'a> for InstantiationArgKind<'a>
component-model
only.impl<'a> Parse<'a> for ModuleTypeDecl<'a>
component-model
only.impl<'a> Parse<'a> for PrimitiveValType
component-model
only.impl<'a> Parse<'a> for Refinement<'a>
component-model
only.impl<'a> Parse<'a> for TypeBounds<'a>
component-model
only.impl<'a> Parse<'a> for wast::component::TypeDef<'a>
component-model
only.impl<'a> Parse<'a> for WastVal<'a>
component-model
only.impl<'a> Parse<'a> for AbstractHeapType
impl<'a> Parse<'a> for wast::core::Custom<'a>
impl<'a> Parse<'a> for CustomPlace
impl<'a> Parse<'a> for CustomPlaceAnchor
impl<'a> Parse<'a> for DataVal<'a>
impl<'a> Parse<'a> for ExportKind
impl<'a> Parse<'a> for HeapType<'a>
impl<'a> Parse<'a> for InnerTypeKind<'a>
impl<'a> Parse<'a> for Instruction<'a>
impl<'a> Parse<'a> for ModuleField<'a>
impl<'a> Parse<'a> for Ordering
impl<'a> Parse<'a> for StorageType<'a>
impl<'a> Parse<'a> for TagType<'a>
impl<'a> Parse<'a> for V128Const
impl<'a> Parse<'a> for V128Pattern
impl<'a> Parse<'a> for ValType<'a>
impl<'a> Parse<'a> for WastArgCore<'a>
impl<'a> Parse<'a> for WastRetCore<'a>
impl<'a> Parse<'a> for QuoteWat<'a>
impl<'a> Parse<'a> for WastArg<'a>
impl<'a> Parse<'a> for WastDirective<'a>
impl<'a> Parse<'a> for WastExecute<'a>
impl<'a> Parse<'a> for WastRet<'a>
impl<'a> Parse<'a> for Wat<'a>
impl<'a> Parse<'a> for Index<'a>
impl<'a> Parse<'a> for custom
impl<'a> Parse<'a> for dylink_0
impl<'a> Parse<'a> for metadata_code_branch_hint
impl<'a> Parse<'a> for name
impl<'a> Parse<'a> for producers
impl<'a> Parse<'a> for Alias<'a>
component-model
only.impl<'a> Parse<'a> for CanonLift<'a>
component-model
only.impl<'a> Parse<'a> for CanonLower<'a>
component-model
only.impl<'a> Parse<'a> for CanonResourceDrop<'a>
component-model
only.impl<'a> Parse<'a> for CanonResourceNew<'a>
component-model
only.impl<'a> Parse<'a> for CanonResourceRep<'a>
component-model
only.impl<'a> Parse<'a> for CanonThreadHwConcurrency
component-model
only.impl<'a> Parse<'a> for CanonThreadSpawn<'a>
component-model
only.impl<'a> Parse<'a> for CanonicalFunc<'a>
component-model
only.impl<'a> Parse<'a> for Component<'a>
component-model
only.impl<'a> Parse<'a> for ComponentExport<'a>
component-model
only.impl<'a> Parse<'a> for ComponentExportType<'a>
component-model
only.impl<'a> Parse<'a> for ComponentExternName<'a>
component-model
only.impl<'a> Parse<'a> for ComponentFunctionParam<'a>
component-model
only.impl<'a> Parse<'a> for ComponentFunctionResult<'a>
component-model
only.impl<'a> Parse<'a> for ComponentFunctionType<'a>
component-model
only.impl<'a> Parse<'a> for ComponentImport<'a>
component-model
only.impl<'a> Parse<'a> for ComponentType<'a>
component-model
only.impl<'a> Parse<'a> for ComponentValTypeUse<'a>
component-model
only.impl<'a> Parse<'a> for CoreFunc<'a>
component-model
only.impl<'a> Parse<'a> for CoreInstance<'a>
component-model
only.impl<'a> Parse<'a> for CoreInstanceExport<'a>
component-model
only.impl<'a> Parse<'a> for CoreInstantiationArg<'a>
component-model
only.impl<'a> Parse<'a> for CoreModule<'a>
component-model
only.impl<'a> Parse<'a> for CoreType<'a>
component-model
only.impl<'a> Parse<'a> for wast::component::Custom<'a>
component-model
only.impl<'a> Parse<'a> for Enum<'a>
component-model
only.impl<'a> Parse<'a> for Flags<'a>
component-model
only.impl<'a> Parse<'a> for wast::component::Func<'a>
component-model
only.impl<'a> Parse<'a> for InlineComponentValType<'a>
component-model
only.impl<'a> Parse<'a> for wast::component::InlineExport<'a>
component-model
only.impl<'a> Parse<'a> for wast::component::InlineImport<'a>
component-model
only.impl<'a> Parse<'a> for Instance<'a>
component-model
only.impl<'a> Parse<'a> for InstanceType<'a>
component-model
only.impl<'a> Parse<'a> for InstantiationArg<'a>
component-model
only.impl<'a> Parse<'a> for wast::component::ItemSig<'a>
component-model
only.impl<'a> Parse<'a> for ItemSigNoName<'a>
component-model
only.impl<'a> Parse<'a> for List<'a>
component-model
only.impl<'a> Parse<'a> for ModuleType<'a>
component-model
only.impl<'a> Parse<'a> for NestedComponent<'a>
component-model
only.impl<'a> Parse<'a> for OptionType<'a>
component-model
only.impl<'a> Parse<'a> for Record<'a>
component-model
only.impl<'a> Parse<'a> for RecordField<'a>
component-model
only.impl<'a> Parse<'a> for ResourceType<'a>
component-model
only.impl<'a> Parse<'a> for ResultType<'a>
component-model
only.impl<'a> Parse<'a> for Start<'a>
component-model
only.impl<'a> Parse<'a> for Tuple<'a>
component-model
only.impl<'a> Parse<'a> for Variant<'a>
component-model
only.impl<'a> Parse<'a> for VariantCase<'a>
component-model
only.impl<'a> Parse<'a> for ArrayCopy<'a>
impl<'a> Parse<'a> for ArrayFill<'a>
impl<'a> Parse<'a> for ArrayInit<'a>
impl<'a> Parse<'a> for ArrayNewData<'a>
impl<'a> Parse<'a> for ArrayNewElem<'a>
impl<'a> Parse<'a> for ArrayNewFixed<'a>
impl<'a> Parse<'a> for ArrayType<'a>
impl<'a> Parse<'a> for BlockType<'a>
impl<'a> Parse<'a> for BrOnCast<'a>
impl<'a> Parse<'a> for BrOnCastFail<'a>
impl<'a> Parse<'a> for BrTableIndices<'a>
impl<'a> Parse<'a> for CallIndirect<'a>
impl<'a> Parse<'a> for ContBind<'a>
impl<'a> Parse<'a> for ContType<'a>
impl<'a> Parse<'a> for Data<'a>
impl<'a> Parse<'a> for Dylink0<'a>
impl<'a> Parse<'a> for Elem<'a>
impl<'a> Parse<'a> for Export<'a>
impl<'a> Parse<'a> for ExportType<'a>
impl<'a> Parse<'a> for Expression<'a>
impl<'a> Parse<'a> for wast::core::Func<'a>
impl<'a> Parse<'a> for FunctionType<'a>
impl<'a> Parse<'a> for FunctionTypeNoNames<'a>
impl<'a> Parse<'a> for Global<'a>
impl<'a> Parse<'a> for GlobalType<'a>
impl<'a> Parse<'a> for I8x16Shuffle
impl<'a> Parse<'a> for Import<'a>
impl<'a> Parse<'a> for wast::core::InlineExport<'a>
impl<'a> Parse<'a> for wast::core::InlineImport<'a>
impl<'a> Parse<'a> for wast::core::ItemSig<'a>
impl<'a> Parse<'a> for LaneArg
impl<'a> Parse<'a> for Limits
impl<'a> Parse<'a> for LocalParser<'a>
impl<'a> Parse<'a> for Memory<'a>
impl<'a> Parse<'a> for MemoryArg<'a>
impl<'a> Parse<'a> for MemoryCopy<'a>
impl<'a> Parse<'a> for MemoryInit<'a>
impl<'a> Parse<'a> for MemoryType
impl<'a> Parse<'a> for Module<'a>
impl<'a> Parse<'a> for Producers<'a>
impl<'a> Parse<'a> for RawCustomSection<'a>
impl<'a> Parse<'a> for Rec<'a>
impl<'a> Parse<'a> for RefCast<'a>
impl<'a> Parse<'a> for RefTest<'a>
impl<'a> Parse<'a> for RefType<'a>
impl<'a> Parse<'a> for Resume<'a>
impl<'a> Parse<'a> for ResumeTable<'a>
impl<'a> Parse<'a> for ResumeThrow<'a>
impl<'a> Parse<'a> for SelectTypes<'a>
impl<'a> Parse<'a> for StructAccess<'a>
impl<'a> Parse<'a> for StructType<'a>
impl<'a> Parse<'a> for Switch<'a>
impl<'a> Parse<'a> for Table<'a>
impl<'a> Parse<'a> for TableArg<'a>
impl<'a> Parse<'a> for TableCopy<'a>
impl<'a> Parse<'a> for TableInit<'a>
impl<'a> Parse<'a> for TableType<'a>
impl<'a> Parse<'a> for Tag<'a>
impl<'a> Parse<'a> for TryTable<'a>
impl<'a> Parse<'a> for Type<'a>
impl<'a> Parse<'a> for wast::core::TypeDef<'a>
impl<'a> Parse<'a> for acq_rel
impl<'a> Parse<'a> for after
impl<'a> Parse<'a> for alias
impl<'a> Parse<'a> for any
impl<'a> Parse<'a> for anyref
impl<'a> Parse<'a> for arg
impl<'a> Parse<'a> for array
impl<'a> Parse<'a> for arrayref
impl<'a> Parse<'a> for assert_exception
impl<'a> Parse<'a> for assert_exhaustion
impl<'a> Parse<'a> for assert_invalid
impl<'a> Parse<'a> for assert_malformed
impl<'a> Parse<'a> for assert_return
impl<'a> Parse<'a> for assert_suspension
impl<'a> Parse<'a> for assert_trap
impl<'a> Parse<'a> for assert_unlinkable
impl<'a> Parse<'a> for before
impl<'a> Parse<'a> for binary
impl<'a> Parse<'a> for block
impl<'a> Parse<'a> for bool_
impl<'a> Parse<'a> for borrow
impl<'a> Parse<'a> for canon
impl<'a> Parse<'a> for case
impl<'a> Parse<'a> for catch
impl<'a> Parse<'a> for catch_all
impl<'a> Parse<'a> for catch_all_ref
impl<'a> Parse<'a> for catch_ref
impl<'a> Parse<'a> for char
impl<'a> Parse<'a> for code
impl<'a> Parse<'a> for component
impl<'a> Parse<'a> for cont
impl<'a> Parse<'a> for contref
impl<'a> Parse<'a> for core
impl<'a> Parse<'a> for data
impl<'a> Parse<'a> for declare
impl<'a> Parse<'a> for definition
impl<'a> Parse<'a> for delegate
impl<'a> Parse<'a> for do
impl<'a> Parse<'a> for dtor
impl<'a> Parse<'a> for elem
impl<'a> Parse<'a> for else
impl<'a> Parse<'a> for end
impl<'a> Parse<'a> for enum_
impl<'a> Parse<'a> for eq
impl<'a> Parse<'a> for eqref
impl<'a> Parse<'a> for error
impl<'a> Parse<'a> for exn
impl<'a> Parse<'a> for exnref
impl<'a> Parse<'a> for export
impl<'a> Parse<'a> for export_info
impl<'a> Parse<'a> for extern
impl<'a> Parse<'a> for externref
impl<'a> Parse<'a> for f32
impl<'a> Parse<'a> for f32x4
impl<'a> Parse<'a> for f64
impl<'a> Parse<'a> for f64x2
impl<'a> Parse<'a> for false_
impl<'a> Parse<'a> for field
impl<'a> Parse<'a> for final
impl<'a> Parse<'a> for first
impl<'a> Parse<'a> for flags
impl<'a> Parse<'a> for float32
impl<'a> Parse<'a> for float64
impl<'a> Parse<'a> for func
impl<'a> Parse<'a> for funcref
impl<'a> Parse<'a> for get
impl<'a> Parse<'a> for global
impl<'a> Parse<'a> for i8
impl<'a> Parse<'a> for i8x16
impl<'a> Parse<'a> for i16
impl<'a> Parse<'a> for i16x8
impl<'a> Parse<'a> for i31
impl<'a> Parse<'a> for i31ref
impl<'a> Parse<'a> for i32
impl<'a> Parse<'a> for i32x4
impl<'a> Parse<'a> for i64
impl<'a> Parse<'a> for i64x2
impl<'a> Parse<'a> for if
impl<'a> Parse<'a> for import
impl<'a> Parse<'a> for import_info
impl<'a> Parse<'a> for instance
impl<'a> Parse<'a> for instantiate
impl<'a> Parse<'a> for interface
impl<'a> Parse<'a> for invoke
impl<'a> Parse<'a> for item
impl<'a> Parse<'a> for language
impl<'a> Parse<'a> for last
impl<'a> Parse<'a> for lift
impl<'a> Parse<'a> for list
impl<'a> Parse<'a> for local
impl<'a> Parse<'a> for loop
impl<'a> Parse<'a> for lower
impl<'a> Parse<'a> for mem_info
impl<'a> Parse<'a> for memory
impl<'a> Parse<'a> for module
impl<'a> Parse<'a> for modulecode
impl<'a> Parse<'a> for mut
impl<'a> Parse<'a> for nan_arithmetic
impl<'a> Parse<'a> for nan_canonical
impl<'a> Parse<'a> for needed
impl<'a> Parse<'a> for nocont
impl<'a> Parse<'a> for noexn
impl<'a> Parse<'a> for noextern
impl<'a> Parse<'a> for nofunc
impl<'a> Parse<'a> for none
impl<'a> Parse<'a> for null
impl<'a> Parse<'a> for nullcontref
impl<'a> Parse<'a> for nullexnref
impl<'a> Parse<'a> for nullexternref
impl<'a> Parse<'a> for nullfuncref
impl<'a> Parse<'a> for nullref
impl<'a> Parse<'a> for offset
impl<'a> Parse<'a> for on
impl<'a> Parse<'a> for option
impl<'a> Parse<'a> for outer
impl<'a> Parse<'a> for own
impl<'a> Parse<'a> for pagesize
impl<'a> Parse<'a> for param
impl<'a> Parse<'a> for parent
impl<'a> Parse<'a> for passive
impl<'a> Parse<'a> for post_return
impl<'a> Parse<'a> for processed_by
impl<'a> Parse<'a> for quote
impl<'a> Parse<'a> for realloc
impl<'a> Parse<'a> for rec
impl<'a> Parse<'a> for record
impl<'a> Parse<'a> for ref
impl<'a> Parse<'a> for ref_func
impl<'a> Parse<'a> for ref_null
impl<'a> Parse<'a> for refines
impl<'a> Parse<'a> for register
impl<'a> Parse<'a> for rep
impl<'a> Parse<'a> for resource
impl<'a> Parse<'a> for resource_drop
impl<'a> Parse<'a> for resource_new
impl<'a> Parse<'a> for resource_rep
impl<'a> Parse<'a> for result
impl<'a> Parse<'a> for s8
impl<'a> Parse<'a> for s16
impl<'a> Parse<'a> for s32
impl<'a> Parse<'a> for s64
impl<'a> Parse<'a> for sdk
impl<'a> Parse<'a> for seq_cst
impl<'a> Parse<'a> for start
impl<'a> Parse<'a> for string
impl<'a> Parse<'a> for string_latin1_utf16
impl<'a> Parse<'a> for string_utf8
impl<'a> Parse<'a> for string_utf16
impl<'a> Parse<'a> for struct
impl<'a> Parse<'a> for structref
impl<'a> Parse<'a> for sub
impl<'a> Parse<'a> for switch
impl<'a> Parse<'a> for table
impl<'a> Parse<'a> for tag
impl<'a> Parse<'a> for then
impl<'a> Parse<'a> for thread
impl<'a> Parse<'a> for thread_hw_concurrency
impl<'a> Parse<'a> for thread_spawn
impl<'a> Parse<'a> for true_
impl<'a> Parse<'a> for try
impl<'a> Parse<'a> for tuple
impl<'a> Parse<'a> for type
impl<'a> Parse<'a> for u8
impl<'a> Parse<'a> for u16
impl<'a> Parse<'a> for u32
impl<'a> Parse<'a> for u64
impl<'a> Parse<'a> for v128
impl<'a> Parse<'a> for value
impl<'a> Parse<'a> for variant
impl<'a> Parse<'a> for wait
impl<'a> Parse<'a> for with
impl<'a> Parse<'a> for Wast<'a>
impl<'a> Parse<'a> for WastInvoke<'a>
impl<'a> Parse<'a> for WastThread<'a>
impl<'a> Parse<'a> for F32
impl<'a> Parse<'a> for F64
impl<'a> Parse<'a> for Id<'a>
impl<'a> Parse<'a> for NameAnnotation<'a>
impl<'a, K> Parse<'a> for IndexOrCoreRef<'a, K>
component-model
only.impl<'a, K> Parse<'a> for IndexOrRef<'a, K>
component-model
only.impl<'a, K: Parse<'a>> Parse<'a> for CoreItemRef<'a, K>
component-model
only.impl<'a, K: Parse<'a>> Parse<'a> for wast::component::ItemRef<'a, K>
component-model
only.impl<'a, K: Parse<'a>> Parse<'a> for wast::token::ItemRef<'a, K>
impl<'a, T> Parse<'a> for NanPattern<T>where
T: Parse<'a>,
impl<'a, T> Parse<'a> for Ordered<T>where
T: Parse<'a>,
impl<'a, T: Parse<'a>> Parse<'a> for ComponentTypeUse<'a, T>
component-model
only.impl<'a, T: Parse<'a>> Parse<'a> for CoreTypeUse<'a, T>
component-model
only.impl<'a, T: Peek + Parse<'a>> Parse<'a> for TypeUse<'a, T>
impl<'a, const CORE: bool> Parse<'a> for InlineExportAlias<'a, CORE>
component-model
only.