Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion LANGUAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ ids ::= id (',' id)* ','?
enum-decl ::= 'enum' id '{' ids '}'
type-alias ::= 'type' id '=' (func-type | type) ';'
func-type-ref ::= func-type | id
func-type ::= 'func' '(' params? ')' ('->' results)?
func-type ::= 'async'? 'func' '(' params? ')' ('->' results)?
params ::= named-type (',' named-type)* ','?
results ::= type
| '(' named-type (',' named-type)* ','? ')'
Expand All @@ -508,6 +508,9 @@ type ::= u8
| option
| result
| borrow
| stream
| future
| error-context
| id
tuple ::= 'tuple' '<' type (',' type)* ','? '>'
list ::= 'list' '<' type '>'
Expand All @@ -517,6 +520,9 @@ result ::= 'result'
| 'result' '<' '_' ',' type '>'
| 'result' '<' type ',' type '>'
borrow ::= 'borrow' '<' type '>'
stream ::= 'stream' | 'stream' '<' type '>'
future ::= 'future' | 'future' '<' type '>'
error-context ::= 'error-context'

let-statement ::= 'let' id '=' expr ';'
expr ::= primary-expr postfix-expr*
Expand Down
24 changes: 24 additions & 0 deletions crates/wac-parser/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,30 @@ where
}
}

/// Consumes the current keyword token, parses an optional `<T>` payload, then
/// builds the type using it's constructor.
pub fn parse_optional_payload<'a>(
lexer: &mut Lexer<'a>,
ctor: impl FnOnce(Option<Box<Type<'a>>>, SourceSpan) -> Type<'a>,
) -> ParseResult<Type<'a>> {
let span = lexer.next().unwrap().1;
let ty = parse_optional(lexer, Token::OpenAngle, |lexer| {
let ty = Box::new(Parse::parse(lexer)?);
let close = parse_token(lexer, Token::CloseAngle)?;
Ok((ty, close))
})?;
Ok(match ty {
Some((ty, close)) => ctor(
Some(ty),
SourceSpan::new(
span.offset().into(),
(close.offset() + close.len()) - span.offset(),
),
),
None => ctor(None, span),
})
}

/// Used to look ahead one token in the lexer.
///
/// The lookahead stores up to 10 attempted tokens.
Expand Down
20 changes: 20 additions & 0 deletions crates/wac-parser/src/ast/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ impl<'a, W: Write> DocumentPrinter<'a, W> {

/// Prints the given function type.
pub fn func_type(&mut self, ty: &FuncType) -> std::fmt::Result {
if ty.is_async {
write!(self.writer, "async ")?;
}
write!(self.writer, "func(")?;
self.named_types(&ty.params)?;
write!(self.writer, ")")?;
Expand Down Expand Up @@ -212,6 +215,23 @@ impl<'a, W: Write> DocumentPrinter<'a, W> {
Type::Borrow(id, _) => {
write!(self.writer, "borrow<{id}>", id = self.source(id.span))
}
Type::Stream(ty, _) => match ty {
Some(ty) => {
write!(self.writer, "stream<")?;
self.ty(ty)?;
write!(self.writer, ">")
}
None => write!(self.writer, "stream"),
},
Type::Future(ty, _) => match ty {
Some(ty) => {
write!(self.writer, "future<")?;
self.ty(ty)?;
write!(self.writer, ">")
}
None => write!(self.writer, "future"),
},
Type::ErrorContext(_) => write!(self.writer, "error-context"),
Type::Ident(id) => write!(self.writer, "{id}", id = self.source(id.span)),
}
}
Expand Down
136 changes: 129 additions & 7 deletions crates/wac-parser/src/ast/type.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::{
parse_delimited, parse_optional, parse_token, DocComment, Error, Ident, Lookahead, PackagePath,
Parse, ParseResult, Peek,
parse_delimited, parse_optional, parse_optional_payload, parse_token, DocComment, Error, Ident,
Lookahead, PackagePath, Parse, ParseResult, Peek,
};
use crate::lexer::{Lexer, Token};
use miette::SourceSpan;
Expand Down Expand Up @@ -482,7 +482,7 @@ pub enum FuncTypeRef<'a> {
impl<'a> Parse<'a> for FuncTypeRef<'a> {
fn parse(lexer: &mut Lexer<'a>) -> ParseResult<Self> {
let mut lookahead = Lookahead::new(lexer);
if lookahead.peek(Token::FuncKeyword) {
if lookahead.peek(Token::AsyncKeyword) || lookahead.peek(Token::FuncKeyword) {
Ok(Self::Func(Parse::parse(lexer)?))
} else if Ident::peek(&mut lookahead) {
Ok(Self::Ident(Parse::parse(lexer)?))
Expand All @@ -496,6 +496,8 @@ impl<'a> Parse<'a> for FuncTypeRef<'a> {
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct FuncType<'a> {
/// Whether the function is async.
pub is_async: bool,
/// The parameters of the function.
pub params: Vec<NamedType<'a>>,
/// The results of the function.
Expand All @@ -504,20 +506,25 @@ pub struct FuncType<'a> {

impl<'a> Parse<'a> for FuncType<'a> {
fn parse(lexer: &mut Lexer<'a>) -> ParseResult<Self> {
let is_async = parse_optional(lexer, Token::AsyncKeyword, |_| Ok(()))?.is_some();
parse_token(lexer, Token::FuncKeyword)?;
parse_token(lexer, Token::OpenParen)?;
let params = parse_delimited(lexer, Token::CloseParen, true)?;
parse_token(lexer, Token::CloseParen)?;
let results =
parse_optional(lexer, Token::Arrow, Parse::parse)?.unwrap_or(ResultList::Empty);

Ok(Self { params, results })
Ok(Self {
is_async,
params,
results,
})
}
}

impl Peek for FuncType<'_> {
fn peek(lookahead: &mut Lookahead) -> bool {
lookahead.peek(Token::FuncKeyword)
lookahead.peek(Token::AsyncKeyword) || lookahead.peek(Token::FuncKeyword)
}
}

Expand Down Expand Up @@ -604,7 +611,7 @@ pub enum TypeAliasKind<'a> {
impl<'a> Parse<'a> for TypeAliasKind<'a> {
fn parse(lexer: &mut Lexer<'a>) -> ParseResult<Self> {
let mut lookahead = Lookahead::new(lexer);
if lookahead.peek(Token::FuncKeyword) {
if lookahead.peek(Token::AsyncKeyword) || lookahead.peek(Token::FuncKeyword) {
Ok(Self::Func(Parse::parse(lexer)?))
} else if Type::peek(&mut lookahead) {
Ok(Self::Type(Parse::parse(lexer)?))
Expand Down Expand Up @@ -661,6 +668,12 @@ pub enum Type<'a> {
},
/// A borrow type.
Borrow(Ident<'a>, SourceSpan),
/// A stream type.
Stream(Option<Box<Type<'a>>>, SourceSpan),
/// A future type.
Future(Option<Box<Type<'a>>>, SourceSpan),
/// An error-context type.
ErrorContext(SourceSpan),
/// An identifier to a value type.
Ident(Ident<'a>),
}
Expand All @@ -686,7 +699,10 @@ impl Type<'_> {
| Self::List(_, span)
| Self::Option(_, span)
| Self::Result { span, .. }
| Self::Borrow(_, span) => *span,
| Self::Borrow(_, span)
| Self::Stream(_, span)
| Self::Future(_, span)
| Self::ErrorContext(span) => *span,
Self::Ident(ident) => ident.span,
}
}
Expand Down Expand Up @@ -817,6 +833,12 @@ impl<'a> Parse<'a> for Type<'a> {
(close.offset() + close.len()) - span.offset(),
),
))
} else if lookahead.peek(Token::StreamKeyword) {
parse_optional_payload(lexer, Self::Stream)
} else if lookahead.peek(Token::FutureKeyword) {
parse_optional_payload(lexer, Self::Future)
} else if lookahead.peek(Token::ErrorContextKeyword) {
Ok(Self::ErrorContext(lexer.next().unwrap().1))
} else if Ident::peek(&mut lookahead) {
Ok(Self::Ident(Parse::parse(lexer)?))
} else {
Expand Down Expand Up @@ -845,6 +867,9 @@ impl Peek for Type<'_> {
|| lookahead.peek(Token::OptionKeyword)
|| lookahead.peek(Token::ResultKeyword)
|| lookahead.peek(Token::BorrowKeyword)
|| lookahead.peek(Token::StreamKeyword)
|| lookahead.peek(Token::FutureKeyword)
|| lookahead.peek(Token::ErrorContextKeyword)
|| Ident::peek(lookahead)
}
}
Expand Down Expand Up @@ -1561,6 +1586,103 @@ enum foo {
.unwrap();
}

#[test]
fn stream_future_error_context_roundtrip() {
// stream (no payload)
roundtrip(
"package foo:bar; type a = stream;",
"package foo:bar;\n\ntype a = stream;\n",
)
.unwrap();

// stream<u8>
roundtrip(
"package foo:bar; type b = stream<u8>;",
"package foo:bar;\n\ntype b = stream<u8>;\n",
)
.unwrap();

// future (no payload)
roundtrip(
"package foo:bar; type c = future;",
"package foo:bar;\n\ntype c = future;\n",
)
.unwrap();

// future<string>
roundtrip(
"package foo:bar; type d = future<string>;",
"package foo:bar;\n\ntype d = future<string>;\n",
)
.unwrap();

// error-context
roundtrip(
"package foo:bar; type e = error-context;",
"package foo:bar;\n\ntype e = error-context;\n",
)
.unwrap();

// nested: stream<stream<u8>>
roundtrip(
"package foo:bar; type f = stream<stream<u8>>;",
"package foo:bar;\n\ntype f = stream<stream<u8>>;\n",
)
.unwrap();

// future<option<u32>>
roundtrip(
"package foo:bar; type g = future<option<u32>>;",
"package foo:bar;\n\ntype g = future<option<u32>>;\n",
)
.unwrap();
}

#[test]
fn async_func_roundtrip() {
// async func type alias
roundtrip(
"package foo:bar; type x = async func() -> string;",
"package foo:bar;\n\ntype x = async func() -> string;\n",
)
.unwrap();

// async func with params
roundtrip(
"package foo:bar; type y = async func(a: u32, b: string) -> stream<u8>;",
"package foo:bar;\n\ntype y = async func(a: u32, b: string) -> stream<u8>;\n",
)
.unwrap();

// async func with no results
roundtrip(
"package foo:bar; type z = async func();",
"package foo:bar;\n\ntype z = async func();\n",
)
.unwrap();

// sync func still works (no async prefix)
roundtrip(
"package foo:bar; type w = func() -> u32;",
"package foo:bar;\n\ntype w = func() -> u32;\n",
)
.unwrap();

// async func in interface export
roundtrip(
"package foo:bar; interface i { do-work: async func(input: string) -> future<u32>; }",
"package foo:bar;\n\ninterface i {\n do-work: async func(input: string) -> future<u32>;\n}\n",
)
.unwrap();

// async func in world import/export
roundtrip(
"package foo:bar; world w { import run: async func(); export process: async func(data: list<u8>) -> stream<u8>; }",
"package foo:bar;\n\nworld w {\n import run: async func();\n\n export process: async func(data: list<u8>) -> stream<u8>;\n}\n",
)
.unwrap();
}

#[test]
fn interface_roundtrip() {
roundtrip(
Expand Down
24 changes: 24 additions & 0 deletions crates/wac-parser/src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,18 @@ pub enum Token {
/// The `targets` keyword.
#[token("targets")]
TargetsKeyword,
/// The `async` keyword.
#[token("async")]
AsyncKeyword,
/// The `stream` keyword.
#[token("stream")]
StreamKeyword,
/// The `future` keyword.
#[token("future")]
FutureKeyword,
/// The `error-context` keyword.
#[token("error-context")]
ErrorContextKeyword,

/// The `;` symbol.
#[token(";")]
Expand Down Expand Up @@ -342,6 +354,10 @@ impl fmt::Display for Token {
Token::AsKeyword => write!(f, "`as` keyword"),
Token::PackageKeyword => write!(f, "`package` keyword"),
Token::TargetsKeyword => write!(f, "`targets` keyword"),
Token::AsyncKeyword => write!(f, "`async` keyword"),
Token::StreamKeyword => write!(f, "`stream` keyword"),
Token::FutureKeyword => write!(f, "`future` keyword"),
Token::ErrorContextKeyword => write!(f, "`error-context` keyword"),
Token::Semicolon => write!(f, "`;`"),
Token::OpenBrace => write!(f, "`{{`"),
Token::CloseBrace => write!(f, "`}}`"),
Expand Down Expand Up @@ -833,6 +849,10 @@ include
as
package
targets
async
stream
future
error-context
"#,
&[
(Ok(Token::ImportKeyword), "import", 1..7),
Expand Down Expand Up @@ -874,6 +894,10 @@ targets
(Ok(Token::AsKeyword), "as", 207..209),
(Ok(Token::PackageKeyword), "package", 210..217),
(Ok(Token::TargetsKeyword), "targets", 218..225),
(Ok(Token::AsyncKeyword), "async", 226..231),
(Ok(Token::StreamKeyword), "stream", 232..238),
(Ok(Token::FutureKeyword), "future", 239..245),
(Ok(Token::ErrorContextKeyword), "error-context", 246..259),
],
);
}
Expand Down
Loading
Loading