|
1 | | -# wac |
| 1 | +# WAC grammar |
| 2 | + |
| 3 | +The current WAC grammar: |
| 4 | + |
| 5 | +```ebnf |
| 6 | +document ::= statement* |
| 7 | +statement ::= import-statement |
| 8 | + | type-statement |
| 9 | + | let-statement |
| 10 | + | export-statement |
| 11 | +
|
| 12 | +import-statement ::= 'import' id ('with' string)? ':' import-type ';' |
| 13 | +import-type ::= package-path | func-type | inline-interface | id |
| 14 | +package-path ::= package-name ('/' id)+ ('@' package-version)? |
| 15 | +package-name ::= id (':' id)+ |
| 16 | +package-version ::= <SEMVER> |
| 17 | +
|
| 18 | +type-statement ::= interface-decl | world-decl | type-decl |
| 19 | +interface-decl ::= 'interface' id '{' interface-item* '}' |
| 20 | +interface-item ::= use-type | item-type-decl | interface-export |
| 21 | +use-type ::= 'use' use-path '.' '{' use-items '}' ';' |
| 22 | +use-path ::= package-path | id |
| 23 | +use-items ::= use-item (',' use-item)* ','? |
| 24 | +use-item ::= id ('as' id)? |
| 25 | +interface-export ::= id ':' func-type-ref ';' |
| 26 | +world-decl ::= 'world' id '{' world-item* '}' |
| 27 | +world-item ::= use-type |
| 28 | + | item-type-decl |
| 29 | + | world-import |
| 30 | + | world-export |
| 31 | + | world-include |
| 32 | +world-import ::= 'import' world-item-path ';' |
| 33 | +world-export ::= 'export' world-item-path ';' |
| 34 | +world-item-path ::= named-world-item | package-path | id |
| 35 | +named-world-item ::= id ':' extern-type |
| 36 | +extern-type ::= func-type | inline-interface | id |
| 37 | +inline-interface ::= 'interface' '{' interface-item* '}' |
| 38 | +world-include ::= 'include' world-ref ('with' '{' world-include-items '}')? ';' |
| 39 | +world-include-items ::= world-include-item (',' world-include-item)* ','? |
| 40 | +world-include-item ::= id 'as' id |
| 41 | +world-ref ::= package-path | id |
| 42 | +type-decl ::= variant-decl | record-decl | flags-decl | enum-decl | type-alias |
| 43 | +item-type-decl ::= resource-decl | type-decl |
| 44 | +resource-decl ::= 'resource' id '{' resource-item* '}' |
| 45 | +resource-item ::= constructor | method |
| 46 | +constructor ::= 'constructor' param-list |
| 47 | +method ::= id ':' 'static'? func-type-ref |
| 48 | +variant-decl ::= 'variant' id '{' variant-cases '}' |
| 49 | +variant-cases ::= variant-case (',' variant-case)* ','? |
| 50 | +variant-case ::= id ('(' type ')')? |
| 51 | +record-decl ::= 'record' id '{' fields '}' |
| 52 | +fields ::= named-type (',' named-type)* ','? |
| 53 | +flags-decl ::= 'flags' id '{' ids '}' |
| 54 | +ids ::= id (',' id)* ','? |
| 55 | +enum-decl ::= 'enum' id '{' ids '}' |
| 56 | +type-alias ::= 'type' id '=' (func-type | type) ';' |
| 57 | +func-type-ref ::= func-type | id |
| 58 | +func-type ::= '(' params? ')' ('->' results)? |
| 59 | +params ::= named-type (',' named-type)* ','? |
| 60 | +results ::= type |
| 61 | + | '(' named-type (',' named-type)* ','? ')' |
| 62 | +named-type ::= id ':' type |
| 63 | +type ::= u8 |
| 64 | + | s8 |
| 65 | + | u16 |
| 66 | + | s16 |
| 67 | + | u32 |
| 68 | + | s32 |
| 69 | + | u64 |
| 70 | + | s64 |
| 71 | + | float32 |
| 72 | + | float64 |
| 73 | + | char |
| 74 | + | bool |
| 75 | + | string |
| 76 | + | tuple |
| 77 | + | list |
| 78 | + | option |
| 79 | + | result |
| 80 | + | borrow |
| 81 | + | id |
| 82 | +tuple ::= 'tuple' '<' type (',' type)* ','? '>' |
| 83 | +list ::= 'list' '<' type '>' |
| 84 | +option ::= 'option' '<' type '>' |
| 85 | +result ::= 'result' |
| 86 | + | 'result' '<' type '>' |
| 87 | + | 'result' '<' '_' ',' type '>' |
| 88 | + | 'result' '<' type ',' type '>' |
| 89 | +borrow ::= 'borrow' '<' type '>' |
| 90 | +
|
| 91 | +let-statement ::= 'let' id '=' expr ';' |
| 92 | +expr ::= primary-expr postfix-expr* |
| 93 | +primary-expr ::= new-expr | nested-expr | id |
| 94 | +new-expr ::= 'new' package-name '{' instantiation-args '}' |
| 95 | +instantiation-args ::= instantiation-arg (',' instantiation-arg)* (',' '...'?)? |
| 96 | +instantiation-arg ::= named-instantiation-arg | id |
| 97 | +named-instantiation-arg ::= (id | string) ':' expr |
| 98 | +nested-expr ::= '(' expr ')' |
| 99 | +postfix-expr ::= access-expr | named-access-expr |
| 100 | +access-expr ::= '.' id |
| 101 | +named-access-expr ::= '[' string ']' |
| 102 | +
|
| 103 | +export-statement ::= 'export' expr ('with' string)? ';' |
| 104 | +
|
| 105 | +id ::= '%'?[a-z][a-z0-9]*('-'[a-z][a-z0-9]*)* |
| 106 | +string ::= '"' character-that-is-not-a-double-quote* '"' |
| 107 | +``` |
| 108 | + |
| 109 | +Whitespace (may appear anywhere between tokens): |
| 110 | + |
| 111 | +```ebnf |
| 112 | +whitespace ::= ' ' | '\n' | '\r' | '\t' | comment |
| 113 | +comment ::= '//' character-that-is-not-a-newline* |
| 114 | + | '/*' any-unicode-character* '*/' |
| 115 | +``` |
| 116 | + |
| 117 | +Note: block comments are allowed to be nested. |
0 commit comments