Grammar

This section contains all the grammar rules from the previous sections collected together.

id :: alpha alphanum+
alpha :: "a".."z" | "A".."Z" | "_"
alphanum :: alpha | digit
digit :: "0".."9"
int :: digit+
float :: digit+ exponent | digit* "." digit+ [ exponent ]
exponent :: ("e" | "E") ["+" | "-"] digit+
str :: <"> (<any character except ", CR or LF> | <"> <">)* <">
     | <'> (<any character except ', CR or LF> | <'> <'>)* <'>
opsym :: "+" | "-" | "*" | "/" | "**" | ":" | "==" | "!=" | "<" | ">" | ">=" | "<="
punct :: "(" | ")" | "[" | "]" | "," | "=" | "+=" | "-=" | "*=" | "/=" | "**=" | "::"
br :: (newline | ";")+
newline :: <CR> <LF> | <LF> | <CR>
whitespace :: " " | <TAB>
comment :: "--" <any character except CR or LF>*
initial-comment :: "#!" <any character except CR or LF>*
utf8-bom :: <EF> <BB> <BF>
main :: [ utf8-bom ] [ initial-comment newline ] [ br] [ encoding-decl ] imports defs
module :: [ utf8-bom ] [ encoding-decl ] module-decl imports defs
module-decl :: "module" module-name br
module-name :: id ("::" id)*
imports :: ("import" module-name ("," module-name)* br)*
encoding-decl :: "encoding" ("utf8" | "ascii" | "latin1") br
defs :: (def br)*
def :: statement | [ "private" ] (var-def | function-def | class-def | interface-def)
var-def :: ("var" | "const") id-list [ "=" expression ]
id-list :: id ("," id)*
function-def :: function-header br block "end"
function-header :: "def" id "(" arg-list ")"
arg-list :: [ var-args ] | argument ("," argument)* [ "," var-args ]
var-args :: "*" id
argument :: id [ "=" single-expression ]
class-def :: "class" id [ "is" gvar ] [ implements ] br (cdef br)* "end"
implements :: "implements" gvar ("," gvar)*
cdef :: [ "private" ] (member-var-def | method-def | getter-def | setter-def)
method-def :: function-def
member-var-def :: var-def
getter-def :: "def" id br block "end"
setter-def :: "def" id "=" id br block "end"
interface-def :: "interface" id [ "is" gvar ] br (bind-def br)* (idef br)* "end"
bind-def :: "bind" gvar
idef :: function-header | "def" id | "def" id "=" id
block :: ((lvar-def | statement) br)*
statement :: expression | assignment-stmt | operator-assignment-stmt
  | if-stmt | switch-stmt | while-stmt | repeat-stmt | for-stmt
  | break-stmt | return-stmt | try-stmt | raise-stmt
assignment-stmt :: expression "=" expression
operator-assignment-stmt :: expression operator-assignment expression
operator-assignment :: "+=" | "-=" | "*=" | "/=" | "**="
lvar-def :: "var" id-list [ "=" expression ]
if-stmt ::
  "if" expression br block
  ( "elif" expression br block )*
  [ "else" br block ]
  "end"
switch-stmt ::
  "switch" expression br
  ( "case" expression-list br block )+
  [ "else" br block ]
  "end"
expression-list :: single-expression ("," single-expression)*
while-stmt :: "while" expression br block "end"
repeat-stmt :: "repeat" br block "until" expression
for-stmt :: "for" id-list "in" expression br block "end"
break-stmt :: "break"
return-stmt :: "return" [ expression ]
try-stmt ::
  "try" br block
  ( ("except" [ id "is" ] gvar br block)+ | "finally" br block )
  "end"
raise-stmt :: "raise" expression
expression :: list-expression | single-expression
single-expression :: simple-expression
  | self-expression
  | simple-name-expression
  | gvar
  | "(" expression ")"
  | binary-operation
  | boolean-operation
  | unary-operation
  | subscript-expression
  | call-expression
  | array-expression
  | tuple-expression
  | member-reference
  | super-reference
  | anonymous-function
  | cast-expression
simple-expression :: int | float | str | "nil"
self-expression :: "self"
simple-name-expression :: id
gvar :: id ("::" id)+ | "::" id
binary-operation :: expression binary-op expression
binary-op :: "+" | "-" | "*" | "/" | "div" | "mod" | "**" | "==" | "!="
  | "<" | "<=" | ">" | ">=" | "is" | "in" | "to" | ":"
boolean-operation :: expression ("and" | "or") expression
unary-operation :: ("-" | "not") expression
subscript-expression :: expression "[" expression "]"
call-expression :: expression "(" args ")"
args :: [ "*" expression ]
  | single-expression ("," single-expression)* [ "," "*" expression ]
array-expression :: "[" comma-list "]"
comma-list :: single-expression | list-expression
list-expression :: single-expression ("," | ("," single-expression)+ [ "," ])
tuple-expression :: "(" ")" | list-expression
member-reference :: expression "." id
super-reference :: "super" "." id
anonymous-function :: "def" "(" arg-list ")" br block "end"
cast-expression :: expression "as" cast-type
cast-type :: gvar | "dynamic"