WeftManual

Language reference

Syntax and primitives

This is the core language accepted by the C compiler. In syntax forms, uppercase words are placeholders and square brackets mark optional parts. Examples in lowercase are literal Weft.

Files, names, and literals

Download the core and page grammar (EBNF)

A source file defines one component. Its header comes first. It needs at least one state field and one action. After the header, declarations can refer to names declared later. Field, view, action, and observer declaration order determines their order in the host interface.

Put each declaration or action statement on one physical line. Blank lines and full lines starting with # after optional whitespace are ignored. A trailing # on a declaration is an unexpected token. Ordinary action indentation is conventional. Page trees use indentation to express structure.

Identifiers start with a lowercase ASCII letter and continue with lowercase letters, digits, or underscores. Use distinct names within each declaration category. The expression names value, true, false, old, shown, enabled, ite, and, or, and not are reserved for named fields, actions, views, and assertions. Labels use double-quoted JSON strings with UTF-8 text and JSON escapes. A NUL is rejected.

Types are u1 through u8. A uN contains unsigned integers from 0 through 2ᴺ − 1. A constant field initializer uses decimal digits. Expression literals also accept 0b binary, 0o octal, and 0x hexadecimal notation and digit separators. Every literal must fit in 0…255. Boolean literals true and false have width one.

state mode: u3 = 5 public
view low "Low bits" = mode & 0b011
view busy "Active" = mode != 0x00

Component declarations

component "TITLE" on OWNER
Choose the owner and human-readable title. A browser page requires the owner name browser. Other owner names work with the native host.
observer PEER
Declare a distinct output recipient. It must differ from the owner. A component may have zero observers.
state FIELD: uN = INITIAL public|private
Introduce a field. INITIAL is a decimal value within the declared width, or external for a typed initializer supplied by the owner.
view NAME "LABEL" = EXPR
Expose a numeric expression at the owner. A view reads state fields. Its inferred width also applies when a claim refers to it.
action NAME "LABEL" [input uN public|private] when PRED
Introduce a guarded event, optionally carrying a typed payload. Close its body with end. The guard must be u1 and can read only state and literals through ordinary expressions.
claim NAME = PRED
Declare a state invariant. This can refer to fields, enabled actions, and shown views.
channel PEER on ACTION only when PRED
Allow a present message to that peer only for the given action and pre-state predicate. At most one channel rule per peer. Peers without this declaration still have their automatic information-flow checks.
protect FIELD from PEER[, PEER ...] [with input ACTION]
Declare one coalition policy for a private field. List distinct declared observers. If present, ACTION must have private input. All listed observers are checked jointly. At most one explicit rule per field.
page "DESCRIPTION" … end · style "SELECTOR" … end
Add an optional browser page and its style blocks. Element names, attributes, bindings, and breakpoints are listed in the page reference.
Page and style grammar

Action statements

set FIELD = EXPR
Assign once per field per action. All right-hand sides read the pre-state plus the optional value payload. Each assignment must fit the destination width.
send EXPR to PEER
Put a value in that observer’s output slot on an enabled step. Zero is a message. Absence is represented separately.
split FIELD_OR_VALUE to LEFT, RIGHT
Use one fresh mask of the source width. The first peer gets the mask and the second gets its XOR with the source. The source is one field or the action payload value. Use two distinct declared recipients.
ensures NAME = PRED
Declare a postcondition for enabled dispatch. Plain fields, shown, and enabled refer to the resulting state. old(FIELD) names a pre-state field and value still names the supplied payload.

An action may contain zero writes or sends. Each recipient has one output slot per action. Two sends to the same peer, a send overlapping a split destination, or a second split are rejected. Other peers can receive independent sends in that action.

Expressions and operator order

Atoms
Use a field identifier, true, false, a numeric literal, or parentheses. value is available inside an action with a payload, including its postconditions.
ite(test, yes, no)
Select a result with a u1 test. Branch widths widen to the larger width. Both branches are pure expressions.
& ^ |
Bitwise AND, XOR, and OR, from tighter to looser binding. Operands widen without losing bits. The result has the larger operand width.
== != < <= > >=
Unsigned comparisons return u1. A single expression cannot chain comparisons. Write (a < b) and (b < c).
not and or
Logical operators require u1 operands and return u1. not binds below comparisons and above and. and binds above or. Write not (mode == 2) when that grouping makes the rule clearer.
shown(VIEW) · enabled(ACTION)
Read the declared view value or the guard bit. These references are allowed in claims, postconditions, and channel conditions. They cannot define a view or action guard recursively.
old(FIELD)
Read the value immediately before the current enabled step. This form takes a field name and is available only in ensures.

From tightest to loosest, ordinary expressions group atoms and calls, &, ^, |, one comparison, not, and, then or. Operators at the same binary bitwise or logical level associate to the left. Parentheses override the grouping.

Literals use the smallest width that represents them, with a minimum of one bit. Assigning a u2 expression to a u4 field is valid. Assigning any u4 expression to a u1 field is rejected, even for wide & 1. Write (wide & 1) != 0 to produce a one-bit predicate. Expression nesting and tree depth are each limited to 1,024.

Choose an expression by context

view selected "Selected" = vote
claim display_matches = shown(selected) == vote

action choose "Set choice" input u2 private when not sealed
  set vote = value
  ensures stored = vote == value
  ensures same_phase = sealed == old(sealed)
end

This excerpt assumes vote is u2 and sealed is u1. The action guard describes whether the control is available before a payload exists. To accept only some payloads in a transition, express the conditional update with ite in the body. The host validates that the payload fits u2 before dispatch.