rutile/docs/grammar.abnf
2025-01-31 21:47:11 -03:00

75 lines
2.2 KiB
ABNF

; Rutile's grammar formal definition in ABNF
; NOTES:
; - terminal strings are by default case sensitive
; - alternatives are ordered
function-kind = "proc" / "iter"
proc-decl = function-kind ident ["*"] "(" [proc-decl-args] ")"
[":" ident] stmt-block
proc-decl-args = symbol-type-pair *("," symbol-type-pair)
symbol-type-pair = ident ":" ident
ident-type-expr = ident [":" ident] ["=" expr]
symbol-decl = ("var" / "let" / "const") ident-type-expr ; "Variable" declaration
function-call = ident-qualified "(" [funcargs] ")"
funcargs = expr-list
array-literal = "[" expr-list "]"
; See `OperatorTable` at compiler/parse.c for the precedences
binary-operator = "+" / "-" / "*" / "/" / "<" / "<=" / ">" / ">=" / "==" / "!="
unary-operator = "-"
ident-or-call = ident / function-call
atom = number
/ array-literal
/ string-literal
/ ident-or-call
/ "(" expr ")"
unary = [unary-operator] atom
expr = unary [binary-operator expr]
expr-list = expr *("," expr) [","] ; Allows a trailling comma
pragma-directive = ident *("," ident) [","]
pragma = "#" "[" pragma-directive "]"
decorated-decl = pragma (proc-decl / symbol-decl)
simple-comment = "//" *OCTET
block-comment = "/*" *OCTET "*/"
; Statements
while-stmt = "while" expr stmt-block
elif-else = ["elif" expr stmt-block] ["else" stmt-block]
if-expr = "if" expr stmt-block elif-else
break-expr = "break" [expr]
return-stmt = "return" [expr]
yield-stmt = "yield" [expr]
discard-stmt = "discard" expr
loop-control = "next" / break-expr
stmt-block = "{" *stmt "}"
stmt = proc-decl
/ symbol-decl
/ expr
/ loop-control
/ if-expr
/ while-stmt
/ return-stmt
/ yield-stmt
/ discard-stmt
/ stmt-block
ident = (ALPHA / "_") *(ALPHA / DIGIT / "_" / "?" / "!")
; Identifier accounting for namespacing: std:io.print, aaa:bbb:ccc
ident-qualified = (ALPHA / "_") *(ALPHA / DIGIT / "_" / "?" / "!" / ":" / ".")
OCTDIGIT = %x30-37
number = 1*DIGIT ; Decimal
/ "0b" 1*("0" / "1") ; Binary
/ "0o" 1*OCTDIGIT ; Octal
/ "0x" 1*HEXDIGIT ; Hexadecimal
escape = "\" ("\" / DQUOTE / "0" / "a" / "b" / "n" / "r" / "t" / ("x" 1*2HEXDIGIT))
character = OCTET / escape
string-literal = DQUOTE *character DQUOTE