Skip to content

Commit 7633e1e

Browse files
committed
Only accept value type declarations in interfaces/worlds.
1 parent 2a755c7 commit 7633e1e

3 files changed

Lines changed: 66 additions & 44 deletions

File tree

crates/wac-parser/src/ast/type.rs

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,28 @@ pub enum TypeStatement<'a> {
2222
Interface(InterfaceDecl<'a>),
2323
/// The statement is for a world declaration.
2424
World(WorldDecl<'a>),
25+
/// The statement is for a value type declaration.
26+
Value(ValueTypeStatement<'a>),
27+
}
28+
29+
impl AstDisplay for TypeStatement<'_> {
30+
fn fmt(&self, f: &mut fmt::Formatter<'_>, indenter: &mut Indenter) -> fmt::Result {
31+
write!(f, "{indenter}")?;
32+
33+
match self {
34+
Self::Interface(interface) => interface.fmt(f, indenter),
35+
Self::World(world) => world.fmt(f, indenter),
36+
Self::Value(value) => value.fmt(f, indenter),
37+
}
38+
}
39+
}
40+
41+
display!(TypeStatement);
42+
43+
/// Represents a value type statement in the AST.
44+
#[derive(Debug, Clone, FromPest)]
45+
#[pest_ast(rule(Rule::ValueTypeStatement))]
46+
pub enum ValueTypeStatement<'a> {
2547
/// The statement is for a resource declaration.
2648
Resource(ResourceDecl<'a>),
2749
/// The statement is for a variant declaration.
@@ -36,13 +58,11 @@ pub enum TypeStatement<'a> {
3658
Alias(TypeAlias<'a>),
3759
}
3860

39-
impl AstDisplay for TypeStatement<'_> {
61+
impl AstDisplay for ValueTypeStatement<'_> {
4062
fn fmt(&self, f: &mut fmt::Formatter<'_>, indenter: &mut Indenter) -> fmt::Result {
4163
write!(f, "{indenter}")?;
4264

4365
match self {
44-
Self::Interface(interface) => interface.fmt(f, indenter),
45-
Self::World(world) => world.fmt(f, indenter),
4666
Self::Resource(resource) => resource.fmt(f, indenter),
4767
Self::Variant(variant) => variant.fmt(f, indenter),
4868
Self::Record(record) => record.fmt(f, indenter),
@@ -53,7 +73,7 @@ impl AstDisplay for TypeStatement<'_> {
5373
}
5474
}
5575

56-
display!(TypeStatement);
76+
display!(ValueTypeStatement);
5777

5878
/// Represents a resource declaration in the AST.
5979
#[derive(Debug, Clone, FromPest)]
@@ -1016,8 +1036,8 @@ display!(InterfaceItem);
10161036
pub enum InterfaceItemKind<'a> {
10171037
/// The item is a use statement.
10181038
Use(Box<UseStatement<'a>>),
1019-
/// The item is a type statement.
1020-
Type(TypeStatement<'a>),
1039+
/// The item is a value type statement.
1040+
Type(ValueTypeStatement<'a>),
10211041
/// The item is an interface export statement.
10221042
Export(InterfaceExportStatement<'a>),
10231043
}
@@ -1221,8 +1241,8 @@ display!(WorldItem);
12211241
pub enum WorldItemKind<'a> {
12221242
/// The item is a use statement.
12231243
Use(Box<UseStatement<'a>>),
1224-
/// The item is a type statement.
1225-
Type(TypeStatement<'a>),
1244+
/// The item is a value type statement.
1245+
Type(ValueTypeStatement<'a>),
12261246
/// The item is a world export statement.
12271247
Import(WorldImportStatement<'a>),
12281248
/// The item is a world export statement.

crates/wac-parser/src/parser.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ mod pest {
2424
Self::Document | Self::Statement | Self::StatementKind => "statement",
2525
Self::ImportStatement => "import statement",
2626
Self::TypeStatement => "type statement",
27+
Self::ValueTypeStatement => "value type statement",
2728
Self::ResourceDecl => "resource declaration",
2829
Self::ResourceBody => "resource body",
2930
Self::VariantDecl => "variant declaration",

crates/wac-parser/wac.pest

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,32 @@ PackageName = ${ Ident ~ (":" ~ Ident)+ }
1414
PackageVersion = { (ASCII_ALPHANUMERIC | "." | "-" | "+")+ }
1515

1616
// Type statement
17-
TypeStatement = !{ InterfaceDecl | WorldDecl | ResourceDecl | VariantDecl | RecordDecl | FlagsDecl | EnumDecl | TypeAlias }
18-
ResourceDecl = ${ ResourceKeyword ~ DelimitingSpace+ ~ Ident ~ DelimitingSpace* ~ ResourceBody }
19-
ResourceBody = !{ Semicolon | (OpenBrace ~ (ResourceMethod ~ Semicolon)+ ~ CloseBrace) }
20-
VariantDecl = ${ VariantKeyword ~ DelimitingSpace+ ~ Ident ~ DelimitingSpace* ~ VariantBody }
21-
VariantBody = !{ OpenBrace ~ VariantCase ~ ("," ~ VariantCase)* ~ ","? ~ CloseBrace }
22-
VariantCase = { Ident ~ VariantType? }
23-
VariantType = { OpenParen ~ Type ~ CloseParen }
24-
RecordDecl = ${ RecordKeyword ~ DelimitingSpace+ ~ Ident ~ DelimitingSpace* ~ RecordBody }
25-
RecordBody = !{ OpenBrace ~ NamedType ~ ("," ~ NamedType)* ~ ","? ~ CloseBrace }
26-
FlagsDecl = ${ FlagsKeyword ~ DelimitingSpace+ ~ Ident ~ DelimitingSpace* ~ FlagsBody }
27-
FlagsBody = !{ OpenBrace ~ Ident ~ ("," ~ Ident)* ~ ","? ~ CloseBrace }
28-
EnumDecl = ${ EnumKeyword ~ DelimitingSpace+ ~ Ident ~ DelimitingSpace* ~ EnumBody }
29-
EnumBody = !{ OpenBrace ~ Ident ~ ("," ~ Ident)* ~ ","? ~ CloseBrace }
30-
TypeAlias = ${ TypeKeyword ~ DelimitingSpace+ ~ Ident ~ DelimitingSpace* ~ Equals ~ DelimitingSpace* ~ TypeAliasKind ~ DelimitingSpace* ~ Semicolon }
31-
TypeAliasKind = !{ FuncType | Type }
32-
ResourceMethod = { Constructor | Method }
33-
Constructor = { ConstructorKeyword ~ ParamList }
34-
Method = { Ident ~ Colon ~ StaticKeyword? ~ FuncTypeRef }
35-
FuncTypeRef = { Ident | FuncType }
36-
FuncType = { FuncKeyword ~ ParamList ~ ResultList? }
37-
ParamList = { OpenParen ~ (NamedType ~ ("," ~ NamedType)* ~ ","?)? ~ CloseParen }
38-
ResultList = { Arrow ~ (NamedResultList | Type) }
39-
NamedResultList = { OpenParen ~ (NamedType ~ ("," ~ NamedType)* ~ ","?)? ~ CloseParen }
40-
NamedType = { Ident ~ Colon ~ Type }
41-
Type = {
17+
TypeStatement = !{ InterfaceDecl | WorldDecl | ValueTypeStatement }
18+
ValueTypeStatement = { ResourceDecl | VariantDecl | RecordDecl | FlagsDecl | EnumDecl | TypeAlias }
19+
ResourceDecl = ${ ResourceKeyword ~ DelimitingSpace+ ~ Ident ~ DelimitingSpace* ~ ResourceBody }
20+
ResourceBody = !{ Semicolon | (OpenBrace ~ (ResourceMethod ~ Semicolon)+ ~ CloseBrace) }
21+
VariantDecl = ${ VariantKeyword ~ DelimitingSpace+ ~ Ident ~ DelimitingSpace* ~ VariantBody }
22+
VariantBody = !{ OpenBrace ~ VariantCase ~ ("," ~ VariantCase)* ~ ","? ~ CloseBrace }
23+
VariantCase = { Ident ~ VariantType? }
24+
VariantType = { OpenParen ~ Type ~ CloseParen }
25+
RecordDecl = ${ RecordKeyword ~ DelimitingSpace+ ~ Ident ~ DelimitingSpace* ~ RecordBody }
26+
RecordBody = !{ OpenBrace ~ NamedType ~ ("," ~ NamedType)* ~ ","? ~ CloseBrace }
27+
FlagsDecl = ${ FlagsKeyword ~ DelimitingSpace+ ~ Ident ~ DelimitingSpace* ~ FlagsBody }
28+
FlagsBody = !{ OpenBrace ~ Ident ~ ("," ~ Ident)* ~ ","? ~ CloseBrace }
29+
EnumDecl = ${ EnumKeyword ~ DelimitingSpace+ ~ Ident ~ DelimitingSpace* ~ EnumBody }
30+
EnumBody = !{ OpenBrace ~ Ident ~ ("," ~ Ident)* ~ ","? ~ CloseBrace }
31+
TypeAlias = ${ TypeKeyword ~ DelimitingSpace+ ~ Ident ~ DelimitingSpace* ~ Equals ~ DelimitingSpace* ~ TypeAliasKind ~ DelimitingSpace* ~ Semicolon }
32+
TypeAliasKind = !{ FuncType | Type }
33+
ResourceMethod = { Constructor | Method }
34+
Constructor = { ConstructorKeyword ~ ParamList }
35+
Method = { Ident ~ Colon ~ StaticKeyword? ~ FuncTypeRef }
36+
FuncTypeRef = { Ident | FuncType }
37+
FuncType = { FuncKeyword ~ ParamList ~ ResultList? }
38+
ParamList = { OpenParen ~ (NamedType ~ ("," ~ NamedType)* ~ ","?)? ~ CloseParen }
39+
ResultList = { Arrow ~ (NamedResultList | Type) }
40+
NamedResultList = { OpenParen ~ (NamedType ~ ("," ~ NamedType)* ~ ","?)? ~ CloseParen }
41+
NamedType = { Ident ~ Colon ~ Type }
42+
Type = {
4243
U8Keyword
4344
| S8Keyword
4445
| U16Keyword
@@ -59,23 +60,23 @@ Type = {
5960
| Borrow
6061
| Ident
6162
}
62-
Tuple = { TupleKeyword ~ OpenAngle ~ Type ~ ("," ~ Type)* ~ CloseAngle }
63-
List = { ListKeyword ~ OpenAngle ~ Type ~ CloseAngle }
64-
Option = { OptionKeyword ~ OpenAngle ~ Type ~ CloseAngle }
65-
Result = { ResultKeyword ~ SpecifiedResult? }
66-
SpecifiedResult = { OpenAngle ~ OmitType ~ ("," ~ Type)? ~ CloseAngle }
67-
OmitType = { Underscore | Type }
68-
Borrow = { BorrowKeyword ~ OpenAngle ~ Ident ~ CloseAngle }
69-
InterfaceDecl = ${ InterfaceKeyword ~ DelimitingSpace+ ~ Ident ~ DelimitingSpace* ~ InterfaceBody }
63+
Tuple = { TupleKeyword ~ OpenAngle ~ Type ~ ("," ~ Type)* ~ CloseAngle }
64+
List = { ListKeyword ~ OpenAngle ~ Type ~ CloseAngle }
65+
Option = { OptionKeyword ~ OpenAngle ~ Type ~ CloseAngle }
66+
Result = { ResultKeyword ~ SpecifiedResult? }
67+
SpecifiedResult = { OpenAngle ~ OmitType ~ ("," ~ Type)? ~ CloseAngle }
68+
OmitType = { Underscore | Type }
69+
Borrow = { BorrowKeyword ~ OpenAngle ~ Ident ~ CloseAngle }
70+
InterfaceDecl = ${ InterfaceKeyword ~ DelimitingSpace+ ~ Ident ~ DelimitingSpace* ~ InterfaceBody }
7071
InterfaceBody = !{ OpenBrace ~ InterfaceItem* ~ CloseBrace }
7172
InterfaceItem = ${ DocComment* ~ WHITESPACE* ~ InterfaceItemKind }
72-
InterfaceItemKind = !{ UseStatement | TypeStatement | InterfaceExportStatement }
73+
InterfaceItemKind = !{ UseStatement | ValueTypeStatement | InterfaceExportStatement }
7374
InterfaceExportStatement = { Ident ~ Colon ~ FuncTypeRef ~ Semicolon }
7475
// TODO: support world include statements
75-
WorldDecl = ${ WorldKeyword ~ DelimitingSpace+ ~ Ident ~ DelimitingSpace* ~ WorldBody }
76+
WorldDecl = ${ WorldKeyword ~ DelimitingSpace+ ~ Ident ~ DelimitingSpace* ~ WorldBody }
7677
WorldBody = !{ OpenBrace ~ WorldItem* ~ CloseBrace }
7778
WorldItem = ${ DocComment* ~ WHITESPACE* ~ WorldItemKind }
78-
WorldItemKind = !{ UseStatement | TypeStatement | WorldImportStatement | WorldExportStatement }
79+
WorldItemKind = !{ UseStatement | ValueTypeStatement | WorldImportStatement | WorldExportStatement }
7980
WorldImportStatement = ${ ImportKeyword ~ DelimitingSpace+ ~ WorldItemDecl ~ DelimitingSpace* ~ Semicolon }
8081
WorldExportStatement = ${ ExportKeyword ~ DelimitingSpace+ ~ WorldItemDecl ~ DelimitingSpace* ~ Semicolon }
8182
WorldItemDecl = !{ WorldNamedItem | InterfaceRef }

0 commit comments

Comments
 (0)