Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't lex the entire input immediately #1110

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions crates/wasm-compose/tests/compositions/defs/composed.wat
Expand Up @@ -31,6 +31,9 @@
(memory (;0;) 0)
(export "memory" (memory 0))
(export "cabi_realloc" (func 2))
(@producers
(processed-by "wit-component" "0.11.0")
)
)
(core module (;1;)
(type (;0;) (func (param i32)))
Expand All @@ -48,13 +51,19 @@
(export "0" (func $indirect-wasi:cli-base/environment-get-environment))
(export "1" (func $indirect-wasi:cli-base/environment-get-arguments))
(export "$imports" (table 0))
(@producers
(processed-by "wit-component" "0.11.0")
)
)
(core module (;2;)
(type (;0;) (func (param i32)))
(import "" "0" (func (;0;) (type 0)))
(import "" "1" (func (;1;) (type 0)))
(import "" "$imports" (table (;0;) 2 2 funcref))
(elem (;0;) (i32.const 0) func 0 1)
(@producers
(processed-by "wit-component" "0.11.0")
)
)
(core instance (;0;) (instantiate 1))
(alias core export 0 "0" (core func (;0;)))
Expand All @@ -74,6 +83,9 @@
(core func (;3;) (canon lower (func 0) (memory 0) (realloc 2) string-encoding=utf8))
(alias export 0 "get-arguments" (func (;1;)))
(core func (;4;) (canon lower (func 1) (memory 0) (realloc 2) string-encoding=utf8))
(@producers
(processed-by "wit-component" "0.11.0")
)
(core instance (;3;)
(export "$imports" (table 0))
(export "0" (func 3))
Expand Down
30 changes: 15 additions & 15 deletions crates/wast/src/component/alias.rs
Expand Up @@ -79,15 +79,15 @@ impl<'a> Parse<'a> for Alias<'a> {

let mut l = parser.lookahead1();

let (target, id, name) = if l.peek::<kw::outer>() {
let (target, id, name) = if l.peek::<kw::outer>()? {
parser.parse::<kw::outer>()?;
let outer = parser.parse()?;
let index = parser.parse()?;
let (kind, id, name) =
parser.parens(|parser| Ok((parser.parse()?, parser.parse()?, parser.parse()?)))?;

(AliasTarget::Outer { outer, index, kind }, id, name)
} else if l.peek::<kw::export>() {
} else if l.peek::<kw::export>()? {
parser.parse::<kw::export>()?;
let instance = parser.parse()?;
let export_name = parser.parse()?;
Expand All @@ -103,7 +103,7 @@ impl<'a> Parse<'a> for Alias<'a> {
id,
name,
)
} else if l.peek::<kw::core>() {
} else if l.peek::<kw::core>()? {
parser.parse::<kw::core>()?;
parser.parse::<kw::export>()?;
let instance = parser.parse()?;
Expand Down Expand Up @@ -155,28 +155,28 @@ pub enum ComponentExportAliasKind {
impl<'a> Parse<'a> for ComponentExportAliasKind {
fn parse(parser: Parser<'a>) -> Result<Self> {
let mut l = parser.lookahead1();
if l.peek::<kw::core>() {
if l.peek::<kw::core>()? {
parser.parse::<kw::core>()?;
let mut l = parser.lookahead1();
if l.peek::<kw::module>() {
if l.peek::<kw::module>()? {
parser.parse::<kw::module>()?;
Ok(Self::CoreModule)
} else {
Err(l.error())
}
} else if l.peek::<kw::func>() {
} else if l.peek::<kw::func>()? {
parser.parse::<kw::func>()?;
Ok(Self::Func)
} else if l.peek::<kw::value>() {
} else if l.peek::<kw::value>()? {
parser.parse::<kw::value>()?;
Ok(Self::Value)
} else if l.peek::<kw::r#type>() {
} else if l.peek::<kw::r#type>()? {
parser.parse::<kw::r#type>()?;
Ok(Self::Type)
} else if l.peek::<kw::component>() {
} else if l.peek::<kw::component>()? {
parser.parse::<kw::component>()?;
Ok(Self::Component)
} else if l.peek::<kw::instance>() {
} else if l.peek::<kw::instance>()? {
parser.parse::<kw::instance>()?;
Ok(Self::Instance)
} else {
Expand All @@ -201,22 +201,22 @@ pub enum ComponentOuterAliasKind {
impl<'a> Parse<'a> for ComponentOuterAliasKind {
fn parse(parser: Parser<'a>) -> Result<Self> {
let mut l = parser.lookahead1();
if l.peek::<kw::core>() {
if l.peek::<kw::core>()? {
parser.parse::<kw::core>()?;
let mut l = parser.lookahead1();
if l.peek::<kw::module>() {
if l.peek::<kw::module>()? {
parser.parse::<kw::module>()?;
Ok(Self::CoreModule)
} else if l.peek::<kw::r#type>() {
} else if l.peek::<kw::r#type>()? {
parser.parse::<kw::r#type>()?;
Ok(Self::CoreType)
} else {
Err(l.error())
}
} else if l.peek::<kw::r#type>() {
} else if l.peek::<kw::r#type>()? {
parser.parse::<kw::r#type>()?;
Ok(Self::Type)
} else if l.peek::<kw::component>() {
} else if l.peek::<kw::component>()? {
parser.parse::<kw::component>()?;
Ok(Self::Component)
} else {
Expand Down
15 changes: 14 additions & 1 deletion crates/wast/src/component/binary.rs
@@ -1,6 +1,6 @@
use crate::component::*;
use crate::core;
use crate::token::{Id, Index, NameAnnotation};
use crate::token::{Id, Index, NameAnnotation, Span};
use wasm_encoder::{
CanonicalFunctionSection, ComponentAliasSection, ComponentDefinedTypeEncoder,
ComponentExportSection, ComponentImportSection, ComponentInstanceSection, ComponentNameSection,
Expand Down Expand Up @@ -43,6 +43,7 @@ fn encode_fields(
ComponentField::Import(i) => e.encode_import(i),
ComponentField::Export(ex) => e.encode_export(ex),
ComponentField::Custom(c) => e.encode_custom(c),
ComponentField::Producers(c) => e.encode_producers(c),
}
}

Expand Down Expand Up @@ -179,6 +180,18 @@ impl<'a> Encoder<'a> {
self.component.section(custom);
}

fn encode_producers(&mut self, custom: &core::Producers) {
use crate::encode::Encode;

let mut data = Vec::new();
custom.encode(&mut data);
self.encode_custom(&Custom {
name: "producers",
span: Span::from_offset(0),
data: vec![&data],
})
}

fn encode_core_module(&mut self, module: &CoreModule<'a>) {
// Flush any in-progress section before encoding the module
self.flush(None);
Expand Down
41 changes: 24 additions & 17 deletions crates/wast/src/component/component.rs
@@ -1,5 +1,6 @@
use crate::annotation;
use crate::component::*;
use crate::core::Producers;
use crate::kw;
use crate::parser::{Parse, Parser, Result};
use crate::token::Index;
Expand Down Expand Up @@ -108,12 +109,14 @@ impl<'a> Component<'a> {
impl<'a> Parse<'a> for Component<'a> {
fn parse(parser: Parser<'a>) -> Result<Self> {
let _r = parser.register_annotation("custom");
let _r = parser.register_annotation("producers");
let _r = parser.register_annotation("name");

let span = parser.parse::<kw::component>()?.0;
let id = parser.parse()?;
let name = parser.parse()?;

let kind = if parser.peek::<kw::binary>() {
let kind = if parser.peek::<kw::binary>()? {
parser.parse::<kw::binary>()?;
let mut data = Vec::new();
while !parser.is_empty() {
Expand Down Expand Up @@ -150,6 +153,7 @@ pub enum ComponentField<'a> {
Import(ComponentImport<'a>),
Export(ComponentExport<'a>),
Custom(Custom<'a>),
Producers(Producers<'a>),
}

impl<'a> ComponentField<'a> {
Expand All @@ -164,47 +168,50 @@ impl<'a> ComponentField<'a> {

impl<'a> Parse<'a> for ComponentField<'a> {
fn parse(parser: Parser<'a>) -> Result<Self> {
if parser.peek::<kw::core>() {
if parser.peek2::<kw::module>() {
if parser.peek::<kw::core>()? {
if parser.peek2::<kw::module>()? {
return Ok(Self::CoreModule(parser.parse()?));
}
if parser.peek2::<kw::instance>() {
if parser.peek2::<kw::instance>()? {
return Ok(Self::CoreInstance(parser.parse()?));
}
if parser.peek2::<kw::r#type>() {
if parser.peek2::<kw::r#type>()? {
return Ok(Self::CoreType(parser.parse()?));
}
if parser.peek2::<kw::func>() {
if parser.peek2::<kw::func>()? {
return Ok(Self::CoreFunc(parser.parse()?));
}
} else {
if parser.peek::<kw::component>() {
if parser.peek::<kw::component>()? {
return Ok(Self::Component(parser.parse()?));
}
if parser.peek::<kw::instance>() {
if parser.peek::<kw::instance>()? {
return Ok(Self::Instance(parser.parse()?));
}
if parser.peek::<kw::alias>() {
if parser.peek::<kw::alias>()? {
return Ok(Self::Alias(parser.parse()?));
}
if parser.peek::<kw::r#type>() {
if parser.peek::<kw::r#type>()? {
return Ok(Self::Type(Type::parse_maybe_with_inline_exports(parser)?));
}
if parser.peek::<kw::import>() {
if parser.peek::<kw::import>()? {
return Ok(Self::Import(parser.parse()?));
}
if parser.peek::<kw::func>() {
if parser.peek::<kw::func>()? {
return Ok(Self::Func(parser.parse()?));
}
if parser.peek::<kw::export>() {
if parser.peek::<kw::export>()? {
return Ok(Self::Export(parser.parse()?));
}
if parser.peek::<kw::start>() {
if parser.peek::<kw::start>()? {
return Ok(Self::Start(parser.parse()?));
}
if parser.peek::<annotation::custom>() {
if parser.peek::<annotation::custom>()? {
return Ok(Self::Custom(parser.parse()?));
}
if parser.peek::<annotation::producers>()? {
return Ok(Self::Producers(parser.parse()?));
}
}
Err(parser.error("expected valid component field"))
}
Expand All @@ -226,12 +233,12 @@ impl<'a> Parse<'a> for Start<'a> {
parser.parse::<kw::start>()?;
let func = parser.parse()?;
let mut args = Vec::new();
while !parser.is_empty() && !parser.peek2::<kw::result>() {
while !parser.is_empty() && !parser.peek2::<kw::result>()? {
args.push(parser.parens(|parser| parser.parse())?);
}

let mut results = Vec::new();
while !parser.is_empty() && parser.peek2::<kw::result>() {
while !parser.is_empty() && parser.peek2::<kw::result>()? {
results.push(parser.parens(|parser| {
parser.parse::<kw::result>()?;
parser.parens(|parser| {
Expand Down
5 changes: 4 additions & 1 deletion crates/wast/src/component/expand.rs
Expand Up @@ -127,7 +127,10 @@ impl<'a> Expander<'a> {
}
None
}
ComponentField::Start(_) | ComponentField::Alias(_) | ComponentField::Custom(_) => None,
ComponentField::Start(_)
| ComponentField::Alias(_)
| ComponentField::Custom(_)
| ComponentField::Producers(_) => None,
};

if let Some(expanded) = expanded {
Expand Down