Skip to content

Syntax

opulse.definition.syntax

Tokenizer and parser for definitions and expressions (docs/reference/semantics.md section 8).

Hand-written instead of a generated grammar, because three things depend on the operator set and the configured notation:

  • Operator runs. Adjacent operator characters (&-1, -9⊽⋐) are split against the known symbols. Every segmentation is enumerated; one segmentation is required, several are a syntax error (never a guess).
  • Roles. Whether a symbol is binary, prefix, postfix or a call-form operator (three or more arguments, ⊕(a, b, c)) comes from the operator set, plus the left-hand side of the definition being parsed.
  • Notation. Brackets, variable names, the equals sign and NaN come from :class:~opulse.config.Symbols; base-marked numerals from the registry.

Precedence: postfix binds tighter than prefix, both tighter than binary operators (- a @ is - (a @); unary operators chain). A call-form application delimits itself and is an operand like a number (- ⊕(1, 2, 3) @ is - (⊕(1, 2, 3) @)); a symbol written before a bracket with three or more separated arguments is read as a call, one with fewer as a prefix operator. In definitions all binary operators share one level and associate to the left (generated definitions are fully parenthesised). In expression mode binary operators use the operators' priority (larger binds tighter) and associativity.

Notation dataclass

Everything the tokenizer needs to know about symbols and numerals.

check_symbol

check_symbol(symbol: str) -> None

Words used by the grammar or operands cannot also name operators.

with_role

with_role(symbol: str, role: str) -> Notation

This notation plus one role, without copying the (possibly very large) tables.

role_of

role_of(n_ary: int, unary_position: str | None) -> str

The notation role of an operator shape.

parse_expression

parse_expression(
    text: str,
    notation: Notation,
    expression_mode: bool = True,
    strict: bool = False,
) -> ast.Expr

strict rejects a binary operation used as an operand of another without parentheses.