|
| 1 | +# The `*.world` format |
| 2 | + |
| 3 | +This is intended to document the `*.world` format as it currently exists. The goal is to provide an overview to understand what features `world` files give you and how they're structured. This isn't intended to be a formal grammar, although it's expected that one day we'll have a formal grammar for `*.world` files. |
| 4 | + |
| 5 | +Conceptually `world` documents act as a contract between runtimes and components. I.e., hosts implement worlds and components target them. |
| 6 | + |
| 7 | +## Lexical structure |
| 8 | + |
| 9 | +> TODO: Mention lexical additions relative to WIT.md |
| 10 | +
|
| 11 | +## Top-level items |
| 12 | + |
| 13 | +A `world` document is a sequence of items specified at the top level. These items come one after another and it's recommended to separate them with newlines for readability but this isn't required. |
| 14 | + |
| 15 | +Conceretely, the structure of a `world` file is: |
| 16 | + |
| 17 | +``` |
| 18 | +top-level ::= (import | export | extend | use-item | type-item)* |
| 19 | +``` |
| 20 | + |
| 21 | +### Item: `import` |
| 22 | + |
| 23 | +An `import` statement imports an instance, function, or value. |
| 24 | + |
| 25 | +The structure of an import statment is: |
| 26 | + |
| 27 | +``` |
| 28 | +import ::= 'import' id ':' type-use |
| 29 | +``` |
| 30 | + |
| 31 | +Example: |
| 32 | + |
| 33 | +```world |
| 34 | +import backends: { *: "wasi:http/Handler" } |
| 35 | +import console: "wasi:logging/Logger" |
| 36 | +``` |
| 37 | + |
| 38 | +### Item: `export` |
| 39 | + |
| 40 | +An `export` statement exports an instance, function, or value. |
| 41 | + |
| 42 | +The structure of an export statment is: |
| 43 | + |
| 44 | +``` |
| 45 | +export ::= 'export' id ':' type-use |
| 46 | +``` |
| 47 | + |
| 48 | +Example: |
| 49 | + |
| 50 | +```world |
| 51 | +export backends: { +: "wasi:http/Handler" } |
| 52 | +export handler: "wasi:http/Handler" |
| 53 | +``` |
| 54 | + |
| 55 | +### Item: `extends` |
| 56 | + |
| 57 | +An `extends` statement defines a subtype relationship with the referenced profile as the super type. |
| 58 | + |
| 59 | +The structure of an extend statement is: |
| 60 | + |
| 61 | +``` |
| 62 | +extends ::= 'extends' pathlit |
| 63 | +``` |
| 64 | + |
| 65 | +Example: |
| 66 | + |
| 67 | +```world |
| 68 | +// Service.world -- Common service profile |
| 69 | +
|
| 70 | +import console: "wasi:logging/Logger" |
| 71 | +import logs: { *: "wasi:logging/Logger" } |
| 72 | +import config: { *: "wasi:config/Value" } |
| 73 | +import secrets: { *: "wasi:config/Secret" } |
| 74 | +import metrics: { *: "wasi:metrics/Counter" } |
| 75 | +
|
| 76 | +``` |
| 77 | + |
| 78 | +```world |
| 79 | +// http/Service.world -- An HTTP service profile |
| 80 | +
|
| 81 | +extends "wasi:Service" |
| 82 | +export "wasi:http/Handler" |
| 83 | +``` |
| 84 | + |
| 85 | +### Item: `use-item` |
| 86 | + |
| 87 | +> TODO: Does `use` apply to `wit` files? |
| 88 | +
|
| 89 | +### Item: `type-item` |
| 90 | + |
| 91 | +A `type-item` statement declares a new named type in the `world` document. This name can be later referred to when defining `import` and `export` items. |
| 92 | + |
| 93 | +The structure of a type-item statement is: |
| 94 | + |
| 95 | +``` |
| 96 | +type-item ::= 'type' id '=' extern-type |
| 97 | +``` |
| 98 | + |
| 99 | +Example: |
| 100 | + |
| 101 | +```world |
| 102 | +type message = record { x: string } |
| 103 | +type greeter = func(msg: message) -> expected<unit, string> |
| 104 | +``` |
| 105 | + |
| 106 | +> TODO: Discuss semantics of type item |
| 107 | +
|
| 108 | +# Grammar |
| 109 | +``` |
| 110 | + top-level ::= (extend | import | export | use-item | type-item)* |
| 111 | + extends ::= 'extends' worldfile |
| 112 | + import ::= 'import' id ':' type-use |
| 113 | + export ::= 'export' id ':' type-use |
| 114 | + type-item ::= 'type' id '=' extern-type |
| 115 | + type-use ::= id | extern-type |
| 116 | + func-type ::= 'async'? 'func' '(' func-args? ')' func-ret? |
| 117 | + func-args ::= func-arg | func-arg ',' func-args? |
| 118 | + func-arg ::= id ':' val-type |
| 119 | + func-ret ::= '->' val-type |
| 120 | + val-type ::= ty | ... | 'any' |
| 121 | + extern-type ::= instance-type | func-type | val-type |
| 122 | +instance-type ::= '{' export* '}' | '{' ('*' | '+') extern-type '}' | witfile |
| 123 | +``` |
| 124 | + |
| 125 | +> NOTE: `use-def` as defined in [WIT.md](https://github.com/bytecodealliance/wit-bindgen/blob/main/WIT.md#item-use) |
| 126 | +
|
| 127 | +> NOTE: `ty` as defined in [WIT.md](https://github.com/bytecodealliance/wit-bindgen/blob/main/WIT.md#item-ty) |
| 128 | +
|
| 129 | +> TODO: Describe `*` & `+` instance types. |
0 commit comments