DEVELOPMENT EDITION · 769442725406
Vibe syntax
On this page

Vibe grammar and precedence

This chapter gives Vibe's grammar. The parser reads Python's shapes; the rules after each group of productions, and the other chapters, say which of them Vibe gives a meaning, and the rest are rejected with a diagnostic.

In this notation, quoted text is literal, [...] is optional, {...} means repetition, | separates alternatives, and NAME, INT, REAL, and STRING are lexical tokens. NEWLINE, INDENT, and DEDENT are the logical layout tokens from the lexical rules. Lists generally allow a trailing comma where shown. It is a readable transcription of the parser's productions.

Modules and statements

module          ::= { statement } EOF
statement       ::= compound | simple_line
simple_line     ::= small { ";" small } [";"] NEWLINE
suite           ::= ":" simple_line
                  | ":" NEWLINE INDENT statement { statement } DEDENT

small           ::= "pass" | "break" | "continue"
                  | "return" [test_list]
                  | "raise" [expression]
                  | "del" expression { "," expression }
                  | "assert" expression ["," expression]
                  | ("global" | "nonlocal") NAME { "," NAME }
                  | "import" dotted_name ["as" NAME]
                  | "from" dotted_name "import" import_names
                  | "type" NAME "=" expression
                  | expression_statement
import_names    ::= import_name { "," import_name } [","]
                  | "(" import_name { "," import_name } [","] ")"
import_name     ::= (NAME | "*") ["as" NAME]
dotted_name     ::= NAME { "." NAME }

expression_statement ::= test_list
                       | test_list ":" expression ["=" test_list]
                       | test_list augop test_list
                       | test_list "=" test_list { "=" test_list }
augop           ::= "+=" | "-=" | "*=" | "/=" | "//=" | "%=" | "**="
                  | "&=" | "|=" | "^=" | "<<=" | ">>="

compound        ::= if_stmt | while_stmt | for_stmt | try_stmt
                  | match_stmt | function_def | class_def | decorated
if_stmt         ::= "if" named_expression suite
                    { "elif" named_expression suite } ["else" suite]
while_stmt      ::= "while" named_expression suite ["else" suite]
for_stmt        ::= "for" target_list "in" test_list suite ["else" suite]
target_list     ::= bit_or { "," bit_or }
try_stmt        ::= "try" suite { "except" [expression] ["as" NAME] suite }
                    ["else" suite] ["finally" suite]
match_stmt      ::= "match" test_list ":" NEWLINE INDENT
                    case_clause { case_clause } DEDENT
case_clause     ::= "case" or_expression ["if" expression] suite
decorated       ::= ("@" expression NEWLINE)+ (function_def | class_def)

Imports are absolute, one unit per import statement; see import forms. del frees an owner (sharing).

Functions and classes

function_def    ::= "def" NAME [type_parameters]
                    "(" [parameters] ")" ["->" expression] suite
parameters      ::= parameter { "," parameter } [","]
parameter       ::= (NAME | "/" | "*") [":" expression] ["=" expression]
type_parameters ::= "[" type_parameter { "," type_parameter } [","] "]"
type_parameter  ::= NAME [":" NAME]
class_def       ::= "class" NAME [type_parameters]
                    ["(" [arguments] ")"] suite

Every parameter needs an annotation (except a method's self), and every function needs -> T or -> None. The / and * markers are B0401: every Vibe parameter can be passed by position or by name. *args and **kwargs are not parameters. A type parameter's bound is a single name. The class families say which classes Vibe declares.

Expressions and displays

test_list       ::= test_item { "," test_item } [","]
test_item       ::= "*" bit_or | named_expression
named_expression ::= NAME ":=" expression | expression
expression      ::= "lambda" [NAME { "," NAME }] ":" expression
                  | or_expression ["if" or_expression "else" expression]
or_expression   ::= and_expression { "or" and_expression }
and_expression  ::= not_expression { "and" not_expression }
not_expression  ::= "not" not_expression | comparison
comparison      ::= bit_or { comparator bit_or }
comparator      ::= "<" | ">" | "==" | ">=" | "<=" | "!="
                  | "in" | "not in" | "is" | "is not"
bit_or          ::= bit_xor { "|" bit_xor }
bit_xor         ::= bit_and { "^" bit_and }
bit_and         ::= shift { "&" shift }
shift           ::= arithmetic { ("<<" | ">>") arithmetic }
arithmetic      ::= term { ("+" | "-") term }
term            ::= factor { ("*" | "/" | "//" | "%" | "@") factor }
factor          ::= ("+" | "-" | "~") factor | power
power           ::= primary ["**" factor]
primary         ::= atom { "(" [arguments] ")"
                         | "[" subscript { "," subscript } [","] "]"
                         | "." NAME }
arguments       ::= argument { "," argument } [","]
argument        ::= NAME "=" expression | expression [comprehension+]
subscript       ::= expression | [expression] ":" [expression] [":" [expression]]
comprehension   ::= "for" target_list "in" or_expression
                    { "if" or_expression }
atom            ::= NAME | INT | REAL | STRING+ | formatted_string
                  | "True" | "False" | "None" | "..."
                  | "(" ")"
                  | "(" named_expression comprehension+ ")"
                  | "(" named_expression { "," expression } [","] ")"
                  | "[" [expression { "," expression } [","]] "]"
                  | "[" expression comprehension+ "]"
                  | "{" "}"
                  | "{" expression ":" expression
                      { "," expression ":" expression } [","] "}"
                  | "{" expression { "," expression } [","] "}"

A starred item (*rest) is an assignment's target, and a walrus (n := e) is taken in a condition and a comprehension's filter (statements); a slice's step is B0401. A comprehension has one for clause. A lambda is an iterator stage's argument (takewhile(lambda y: ..., source)). Generator expressions are consumed by sum, all, any and iterator stages where they are written. is and is not compare with None. Dictionary and set displays initialize typed declarations.

Operator precedence

Rows run from tightest binding to loosest. Operators in a row associate left to right unless stated otherwise.

Level Forms Associativity / restrictions
Primary call, [], .name Left to right
Power ** Right associative; tighter than a unary sign on its left
Unary arithmetic +x, -x, ~x Right to left
Multiplicative *, /, //, %, @ / gives a float; @ is the matrix product
Additive +, - Numeric operations of the operands' type
Shifts <<, >> Left to right
Bitwise and & Supported operand types required
Bitwise xor ^ Supported operand types required
Bitwise or | Also joins classes in a union and alternatives in a pattern
Comparisons < <= > >= == != in not in is is not Comparisons chain
Boolean not not Boolean operand
Boolean and and Short-circuit Boolean result
Boolean or or Short-circuit Boolean result
Conditional a if condition else b Right associative
Lambda lambda name: expression An iterator stage's argument

Consequently, -2 ** 2 is -(2 ** 2) and 2 ** 3 ** 2 is 2 ** (3 ** 2). Parenthesize arithmetic in mixed conditions when it improves clarity; grouping does not waive overflow, range, division, or index obligations.

Source evidence: parser productions and semantic lowering.