11// Top-level document rule
2- Document = { SOI ~ Statement * ~ EOI }
2+ Document = { SOI ~ TopLevelStatement * ~ EOI }
33
44// Top-level statements
5- Statement = $ { DocComment* ~ WHITESPACE* ~ StatementKind }
6- StatementKind = ! { ImportStatement | TypeStatement | LetStatement | ExportStatement }
5+ TopLevelStatement = { DocComment* ~ Statement }
6+ Statement = { ImportStatement | TypeStatement | LetStatement | ExportStatement }
77
88// Import statement
99ImportStatement = ${ ImportKeyword ~ DelimitingSpace+ ~ Ident ~ (DelimitingSpace+ ~ WithClause)? ~ DelimitingSpace* ~ Colon ~ DelimitingSpace* ~ ImportType ~ DelimitingSpace* ~ Semicolon }
@@ -14,32 +14,57 @@ PackageName = ${ Ident ~ (":" ~ Ident)+ }
1414PackageVersion = { (ASCII_ALPHANUMERIC | "." | "-" | "+")+ }
1515
1616// Type statement
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 = {
17+ TypeStatement = { InterfaceDecl | WorldDecl | ValueTypeStatement }
18+ InterfaceDecl = ${ InterfaceKeyword ~ DelimitingSpace+ ~ Ident ~ DelimitingSpace* ~ InterfaceBody }
19+ InterfaceBody = !{ OpenBrace ~ InterfaceItem* ~ CloseBrace }
20+ InterfaceItem = { DocComment* ~ InterfaceItemStatement }
21+ InterfaceItemStatement = { UseStatement | ValueTypeStatement | InterfaceExportStatement }
22+ UseStatement = ${ UseKeyword ~ DelimitingSpace+ ~ UseItems ~ DelimitingSpace* ~ Semicolon }
23+ UseItems = !{ UsePath ~ Dot ~ OpenBrace ~ Ident ~ ("," ~ Ident)* ~ ","? ~ CloseBrace }
24+ UsePath = { PackagePath | Ident }
25+ InterfaceExportStatement = { Ident ~ Colon ~ FuncTypeRef ~ Semicolon }
26+ WorldDecl = ${ WorldKeyword ~ DelimitingSpace+ ~ Ident ~ DelimitingSpace* ~ WorldBody }
27+ WorldBody = !{ OpenBrace ~ WorldItem* ~ CloseBrace }
28+ WorldItem = { DocComment* ~ WorldItemStatement }
29+ WorldItemStatement = { UseStatement | ValueTypeStatement | WorldImportStatement | WorldExportStatement | WorldIncludeStatement }
30+ WorldImportStatement = ${ ImportKeyword ~ DelimitingSpace+ ~ WorldItemDecl ~ DelimitingSpace* ~ Semicolon }
31+ WorldExportStatement = ${ ExportKeyword ~ DelimitingSpace+ ~ WorldItemDecl ~ DelimitingSpace* ~ Semicolon }
32+ WorldItemDecl = !{ WorldNamedItem | InterfaceRef }
33+ WorldNamedItem = { Ident ~ Colon ~ ExternType ~ !Slash }
34+ InterfaceRef = { PackagePath | Ident }
35+ ExternType = { FuncType | InlineInterface | Ident }
36+ InlineInterface = { InterfaceKeyword ~ InterfaceBody }
37+ WorldIncludeStatement = ${ IncludeKeyword ~ DelimitingSpace+ ~ WorldRef ~ DelimitingSpace* ~ WorldIncludeWithClause? ~ DelimitingSpace* ~ Semicolon }
38+ WorldIncludeWithClause = !{ WithKeyword ~ OpenBrace ~ WorldIncludeItem ~ ("," ~ WorldIncludeItem)* ~ ","? ~ CloseBrace }
39+ WorldIncludeItem = ${ Ident ~ DelimitingSpace+ ~ AsKeyword ~ DelimitingSpace+ ~ Ident }
40+ WorldRef = { PackagePath | Ident }
41+
42+ // Value type statement
43+ ValueTypeStatement = { ResourceDecl | VariantDecl | RecordDecl | FlagsDecl | EnumDecl | TypeAlias }
44+ ResourceDecl = ${ ResourceKeyword ~ DelimitingSpace+ ~ Ident ~ DelimitingSpace* ~ ResourceBody }
45+ ResourceBody = !{ Semicolon | (OpenBrace ~ (ResourceMethod ~ Semicolon)+ ~ CloseBrace) }
46+ ResourceMethod = { Constructor | Method }
47+ Constructor = { ConstructorKeyword ~ ParamList }
48+ Method = { Ident ~ Colon ~ StaticKeyword? ~ FuncTypeRef }
49+ VariantDecl = ${ VariantKeyword ~ DelimitingSpace+ ~ Ident ~ DelimitingSpace* ~ VariantBody }
50+ VariantBody = !{ OpenBrace ~ VariantCase ~ ("," ~ VariantCase)* ~ ","? ~ CloseBrace }
51+ VariantCase = { Ident ~ VariantType? }
52+ VariantType = { OpenParen ~ Type ~ CloseParen }
53+ RecordDecl = ${ RecordKeyword ~ DelimitingSpace+ ~ Ident ~ DelimitingSpace* ~ RecordBody }
54+ RecordBody = !{ OpenBrace ~ NamedType ~ ("," ~ NamedType)* ~ ","? ~ CloseBrace }
55+ FlagsDecl = ${ FlagsKeyword ~ DelimitingSpace+ ~ Ident ~ DelimitingSpace* ~ FlagsBody }
56+ FlagsBody = !{ OpenBrace ~ Ident ~ ("," ~ Ident)* ~ ","? ~ CloseBrace }
57+ EnumDecl = ${ EnumKeyword ~ DelimitingSpace+ ~ Ident ~ DelimitingSpace* ~ EnumBody }
58+ EnumBody = !{ OpenBrace ~ Ident ~ ("," ~ Ident)* ~ ","? ~ CloseBrace }
59+ TypeAlias = ${ TypeKeyword ~ DelimitingSpace+ ~ Ident ~ DelimitingSpace* ~ Equals ~ DelimitingSpace* ~ TypeAliasKind ~ DelimitingSpace* ~ Semicolon }
60+ TypeAliasKind = !{ FuncType | Type }
61+ FuncTypeRef = { FuncType | Ident }
62+ FuncType = { FuncKeyword ~ ParamList ~ ResultList? }
63+ ParamList = { OpenParen ~ (NamedType ~ ("," ~ NamedType)* ~ ","?)? ~ CloseParen }
64+ ResultList = { Arrow ~ (NamedResultList | Type) }
65+ NamedResultList = { OpenParen ~ (NamedType ~ ("," ~ NamedType)* ~ ","?)? ~ CloseParen }
66+ NamedType = { Ident ~ Colon ~ Type }
67+ Type = {
4368 U8Keyword
4469 | S8Keyword
4570 | U16Keyword
@@ -60,34 +85,13 @@ Type = {
6085 | Borrow
6186 | Ident
6287}
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 }
71- InterfaceBody = !{ OpenBrace ~ InterfaceItem* ~ CloseBrace }
72- InterfaceItem = ${ DocComment* ~ WHITESPACE* ~ InterfaceItemKind }
73- InterfaceItemKind = !{ UseStatement | ValueTypeStatement | InterfaceExportStatement }
74- InterfaceExportStatement = { Ident ~ Colon ~ FuncTypeRef ~ Semicolon }
75- WorldDecl = ${ WorldKeyword ~ DelimitingSpace+ ~ Ident ~ DelimitingSpace* ~ WorldBody }
76- WorldBody = !{ OpenBrace ~ WorldItem* ~ CloseBrace }
77- WorldItem = ${ DocComment* ~ WHITESPACE* ~ WorldItemKind }
78- WorldItemKind = !{ UseStatement | ValueTypeStatement | WorldImportStatement | WorldExportStatement | WorldIncludeStatement }
79- WorldImportStatement = ${ ImportKeyword ~ DelimitingSpace+ ~ WorldItemDecl ~ DelimitingSpace* ~ Semicolon }
80- WorldExportStatement = ${ ExportKeyword ~ DelimitingSpace+ ~ WorldItemDecl ~ DelimitingSpace* ~ Semicolon }
81- WorldItemDecl = !{ WorldNamedItem | InterfaceRef }
82- WorldNamedItem = ${ Ident ~ DelimitingSpace* ~ Colon ~ DelimitingSpace+ ~ ExternType }
83- InterfaceRef = { PackagePath | Ident }
84- ExternType = !{ FuncType | InlineInterface | Ident }
85- InlineInterface = { InterfaceKeyword ~ InterfaceBody }
86-
87- WorldIncludeStatement = ${ IncludeKeyword ~ DelimitingSpace+ ~ WorldRef ~ DelimitingSpace* ~ WorldIncludeWithClause? ~ DelimitingSpace* ~ Semicolon }
88- WorldIncludeWithClause = !{ WithKeyword ~ OpenBrace ~ WorldIncludeItem ~ ("," ~ WorldIncludeItem)* ~ ","? ~ CloseBrace }
89- WorldIncludeItem = ${ Ident ~ DelimitingSpace+ ~ AsKeyword ~ DelimitingSpace+ ~ Ident }
90- WorldRef = { PackagePath | Ident }
88+ Tuple = { TupleKeyword ~ OpenAngle ~ Type ~ ("," ~ Type)* ~ CloseAngle }
89+ List = { ListKeyword ~ OpenAngle ~ Type ~ CloseAngle }
90+ Option = { OptionKeyword ~ OpenAngle ~ Type ~ CloseAngle }
91+ Result = { ResultKeyword ~ SpecifiedResult? }
92+ SpecifiedResult = { OpenAngle ~ OmitType ~ ("," ~ Type)? ~ CloseAngle }
93+ OmitType = { Underscore | Type }
94+ Borrow = { BorrowKeyword ~ OpenAngle ~ Ident ~ CloseAngle }
9195
9296// Let statement
9397LetStatement = ${ LetKeyword ~ DelimitingSpace+ ~ Ident ~ DelimitingSpace* ~ Equals ~ DelimitingSpace* ~ Expr ~ DelimitingSpace* ~ Semicolon }
@@ -109,11 +113,6 @@ PostfixExpr = { AccessExpr | NamedAccessExpr }
109113AccessExpr = ${ Dot ~ Ident }
110114NamedAccessExpr = { OpenBracket ~ String ~ CloseBracket }
111115
112- // Use statement in interface and world definitinos
113- UseStatement = ${ UseKeyword ~ DelimitingSpace+ ~ UseItems ~ DelimitingSpace* ~ Semicolon }
114- UseItems = !{ UsePath ~ Dot ~ OpenBrace ~ Ident ~ ("," ~ Ident)* ~ ","? ~ CloseBrace }
115- UsePath = { PackagePath | Ident }
116-
117116// Identifiers
118117RawIdent = _{ Percent ~ IdentPart ~ (Hyphen ~ IdentPart)* }
119118Ident = @{ RawIdent | !(Keyword ~ !Hyphen) ~ IdentPart ~ (Hyphen ~ IdentPart)* }
0 commit comments