leo/docs/grammar/abnf-grammar.txt

452 lines
13 KiB
Plaintext
Raw Normal View History

2022-02-02 21:07:27 +03:00
; Copyright (C) 2019-2022 Aleo Systems Inc.
; This file is part of the Leo library.
2021-03-23 19:11:42 +03:00
; The Leo library is free software: you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation, either version 3 of the License, or
; (at your option) any later version.
2021-03-23 19:11:42 +03:00
; The Leo library is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
; GNU General Public License for more details.
2021-03-23 19:11:42 +03:00
; You should have received a copy of the GNU General Public License
; along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
2021-03-23 19:11:42 +03:00
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Lexical Grammar
; ---------------
ascii = %x0-7F
safe-nonascii = %x80-2029 / %x202F-2065 / %x2070-D7FF / %xE000-10FFFF
; excludes bidi embeddings/overrides/isolates
; and excludes high/low surrogates
character = ascii / safe-nonascii
2021-03-23 19:11:42 +03:00
horizontal-tab = %x9 ; <HT>
2021-03-23 19:11:42 +03:00
line-feed = %xA ; <LF>
2021-03-23 19:11:42 +03:00
carriage-return = %xD ; <CR>
2021-03-23 19:11:42 +03:00
space = %x20 ; <SP>
2021-03-23 19:11:42 +03:00
double-quote = %x22 ; "
2021-03-23 19:11:42 +03:00
single-quote = %x27 ; '
line-terminator = line-feed / carriage-return / carriage-return line-feed
2021-03-23 19:11:42 +03:00
whitespace = space / horizontal-tab / line-terminator
not-line-feed-or-carriage-return =
%x0-9 / %xB-C / %xE-7F / safe-nonascii
; anything but <LF> or <CR>
2021-03-23 19:11:42 +03:00
not-star-or-line-feed-or-carriage-return =
%x0-9/ %xB-C / %xD-29 / %x2B-7F / safe-nonascii
; anything but * or <LF> or <CR>
not-star-or-slash-or-line-feed-or-carriage-return =
%x0-9 / %xB-C / %xD-29 / %x2B-2E / %x30-7F / safe-nonascii
; anything but * or / or <LF> or <CR>
2021-03-23 19:11:42 +03:00
not-double-quote-or-backslash-or-line-feed-or-carriage-return =
%x0-9 / %xB-C / %xD-21 / %x23-5B / %x5D-7F / safe-nonascii
; anything but " or \ or <LF> or <CR>
2021-03-23 19:11:42 +03:00
comment = block-comment / end-of-line-comment
block-comment = "/*" rest-of-block-comment
rest-of-block-comment =
"*" rest-of-block-comment-after-star
/ not-star-or-line-feed-or-carriage-return rest-of-block-comment
/ line-terminator rest-of-block-comment
2021-03-23 19:11:42 +03:00
rest-of-block-comment-after-star =
"/"
/ "*" rest-of-block-comment-after-star
/ not-star-or-slash-or-line-feed-or-carriage-return rest-of-block-comment
/ line-terminator rest-of-block-comment
2021-03-23 19:11:42 +03:00
2022-02-02 21:07:27 +03:00
end-of-line-comment = "//" *not-line-feed-or-carriage-return
2021-03-23 19:11:42 +03:00
keyword = %s"address"
/ %s"bool"
/ %s"circuit"
2021-03-23 19:11:42 +03:00
/ %s"console"
/ %s"const"
/ %s"constant"
2021-03-23 19:11:42 +03:00
/ %s"else"
/ %s"field"
/ %s"for"
/ %s"function"
/ %s"group"
/ %s"i8"
/ %s"i16"
/ %s"i32"
/ %s"i64"
/ %s"i128"
/ %s"if"
/ %s"in"
/ %s"let"
2022-04-07 23:07:37 +03:00
/ %s"public"
2021-03-23 19:11:42 +03:00
/ %s"return"
2022-05-14 07:32:56 +03:00
/ %s"scalar"
2022-06-08 16:46:54 +03:00
/ %s"string"
2021-03-23 19:11:42 +03:00
/ %s"u8"
/ %s"u16"
/ %s"u32"
/ %s"u64"
/ %s"u128"
uppercase-letter = %x41-5A ; A-Z
lowercase-letter = %x61-7A ; a-z
letter = uppercase-letter / lowercase-letter
decimal-digit = %x30-39 ; 0-9
octal-digit = %x30-37 ; 0-7
hexadecimal-digit = decimal-digit / "a" / "b" / "c" / "d" / "e" / "f"
identifier = letter *( letter / decimal-digit / "_" )
; but not a keyword or a boolean literal or aleo1...
2021-03-23 19:11:42 +03:00
numeral = 1*decimal-digit
2021-03-23 19:11:42 +03:00
unsigned-literal = numeral ( %s"u8" / %s"u16" / %s"u32" / %s"u64" / %s"u128" )
2021-03-23 19:11:42 +03:00
signed-literal = numeral ( %s"i8" / %s"i16" / %s"i32" / %s"i64" / %s"i128" )
2021-03-23 19:11:42 +03:00
field-literal = numeral %s"field"
2021-03-23 19:11:42 +03:00
product-group-literal = numeral %s"group"
2021-03-23 19:11:42 +03:00
2022-05-14 07:32:56 +03:00
scalar-literal = numeral %s"scalar"
2021-03-23 19:11:42 +03:00
boolean-literal = %s"true" / %s"false"
address-literal = %s"aleo1" 58( lowercase-letter / decimal-digit )
2021-03-23 19:11:42 +03:00
single-quote-escape = "\" single-quote ; \'
double-quote-escape = "\" double-quote ; \"
backslash-escape = "\\"
line-feed-escape = %s"\n"
carriage-return-escape = %s"\r"
horizontal-tab-escape = %s"\t"
null-character-escape = "\0"
simple-character-escape = single-quote-escape
/ double-quote-escape
/ backslash-escape
/ line-feed-escape
/ carriage-return-escape
/ horizontal-tab-escape
/ null-character-escape
ascii-character-escape = %s"\x" octal-digit hexadecimal-digit
unicode-character-escape = %s"\u{" 1*6hexadecimal-digit "}"
2021-07-21 00:52:35 +03:00
string-literal = double-quote *string-literal-element double-quote
string-literal-element =
not-double-quote-or-backslash-or-line-feed-or-carriage-return
/ simple-character-escape
/ ascii-character-escape
/ unicode-character-escape
integer-literal = unsigned-literal
/ signed-literal
numeric-literal = integer-literal
/ field-literal
/ product-group-literal
2022-05-14 07:32:56 +03:00
/ scalar-literal
atomic-literal = numeric-literal
/ boolean-literal
/ address-literal
/ string-literal
2021-03-23 19:11:42 +03:00
symbol = "!" / "&&" / "||"
2021-03-23 19:11:42 +03:00
/ "==" / "!="
/ "<" / "<=" / ">" / ">="
/ "+" / "-" / "*" / "/" / "**"
/ "="
2021-03-23 19:11:42 +03:00
/ "(" / ")"
/ "[" / "]"
2021-03-23 19:11:42 +03:00
/ "{" / "}"
/ "," / "." / ".." / ";" / ":" / "::" / "?"
2021-03-24 18:40:26 +03:00
/ "->" / "_"
/ %s")group"
2021-03-23 19:11:42 +03:00
token = keyword
/ identifier
/ atomic-literal
/ numeral
2021-03-23 19:11:42 +03:00
/ symbol
lexeme = token / comment / whitespace
2021-03-23 19:11:42 +03:00
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Syntactic Grammar
; -----------------
unsigned-type = %s"u8" / %s"u16" / %s"u32" / %s"u64" / %s"u128"
signed-type = %s"i8" / %s"i16" / %s"i32" / %s"i64" / %s"i128"
integer-type = unsigned-type / signed-type
field-type = %s"field"
group-type = %s"group"
2022-05-14 07:32:56 +03:00
scalar-type = %s"scalar"
arithmetic-type = integer-type / field-type / group-type / scalar-type
2021-03-23 19:11:42 +03:00
boolean-type = %s"bool"
address-type = %s"address"
2022-06-02 06:39:27 +03:00
string-type = %s"string"
primitive-type = boolean-type / arithmetic-type / address-type / string-type
2021-03-23 19:11:42 +03:00
named-type = primitive-type / identifier
type = named-type
group-coordinate = ( [ "-" ] numeral ) / "+" / "-" / "_"
affine-group-literal = "(" group-coordinate "," group-coordinate %s")group"
literal = atomic-literal / affine-group-literal
group-literal = product-group-literal / affine-group-literal
primary-expression = literal
/ variable-or-free-constant
/ associated-constant
2021-03-23 19:11:42 +03:00
/ "(" expression ")"
/ free-function-call
/ static-function-call
/ circuit-expression
variable-or-free-constant = identifier
associated-constant = named-type "::" identifier
free-function-call = identifier function-arguments
static-function-call = named-type "::" identifier function-arguments
2021-03-23 19:11:42 +03:00
function-arguments = "(" [ expression *( "," expression ) [ "," ] ] ")"
2021-03-23 19:11:42 +03:00
circuit-expression = identifier "{" circuit-component-initializer
*( "," circuit-component-initializer )
[ "," ] "}"
circuit-component-initializer = identifier
/ identifier ":" expression
postfix-expression = primary-expression
/ circuit-component-expression
/ operator-call
circuit-component-expression = postfix-expression "." identifier
operator-call = unary-operator-call / binary-operator-call
unary-operator-call = postfix-expression "." identifier "(" ")"
binary-operator-call =
postfix-expression "." identifier "(" expression [ "," ] ")"
2021-03-23 19:11:42 +03:00
unary-expression = postfix-expression
2021-03-23 19:11:42 +03:00
/ "!" unary-expression
/ "-" unary-expression
exponential-expression = unary-expression
/ unary-expression "**" exponential-expression
2021-03-23 19:11:42 +03:00
multiplicative-expression = exponential-expression
/ multiplicative-expression "*" exponential-expression
/ multiplicative-expression "/" exponential-expression
additive-expression = multiplicative-expression
/ additive-expression "+" multiplicative-expression
/ additive-expression "-" multiplicative-expression
shift-expression = additive-expression
/ shift-expression "<<" additive-expression
/ shift-expression ">>" additive-expression
bitwise-and-expression = shift-expression
/ bitwise-and-expression "&" shift-expression
bitwise-inclusive-or-expression =
bitwise-and-expression
/ bitwise-inclusive-or-expression "|" bitwise-and-expression
bitwise-exclusive-or-expression =
bitwise-inclusive-or-expression
/ bitwise-exclusive-or-expression "^" bitwise-inclusive-or-expression
ordering-expression =
bitwise-exclusive-or-expression
/ bitwise-exclusive-or-expression "<" bitwise-exclusive-or-expression
/ bitwise-exclusive-or-expression ">" bitwise-exclusive-or-expression
/ bitwise-exclusive-or-expression "<=" bitwise-exclusive-or-expression
/ bitwise-exclusive-or-expression ">=" bitwise-exclusive-or-expression
2021-03-23 19:11:42 +03:00
equality-expression = ordering-expression
2021-07-01 23:40:29 +03:00
/ ordering-expression "==" ordering-expression
/ ordering-expression "!=" ordering-expression
2021-03-23 19:11:42 +03:00
boolean-and-expression = equality-expression
/ boolean-and-expression "&&" equality-expression
2021-03-23 19:11:42 +03:00
boolean-or-expression = boolean-and-expression
/ boolean-or-expression "||" boolean-and-expression
2021-03-23 19:11:42 +03:00
binary-expression = boolean-or-expression
conditional-expression = binary-expression
/ binary-expression "?" expression ":" expression
2021-03-23 19:11:42 +03:00
expression = conditional-expression
statement = return-statement
2021-04-28 19:41:42 +03:00
/ variable-declaration
/ constant-declaration
2021-03-23 19:11:42 +03:00
/ conditional-statement
/ loop-statement
/ assignment-statement
/ console-statement
/ block
block = "{" *statement "}"
return-statement = %s"return" expression ";"
2021-03-23 19:11:42 +03:00
variable-declaration = %s"let" identifier ":" type "=" expression ";"
2021-04-28 19:41:42 +03:00
constant-declaration = %s"const" identifier ":" type "=" expression ";"
2021-03-23 19:11:42 +03:00
branch = %s"if" expression block
conditional-statement = branch
/ branch %s"else" block
/ branch %s"else" conditional-statement
loop-statement = %s"for" identifier ":" type
%s"in" expression ".." expression
block
2021-03-23 19:11:42 +03:00
assignment-operator = "="
2021-03-23 19:11:42 +03:00
assignment-statement = expression assignment-operator expression ";"
console-statement = %s"console" "." console-call ";"
2021-03-23 19:11:42 +03:00
console-call = assert-call
/ print-call
assert-call = %s"assert" "(" expression ")"
2022-02-26 00:45:49 +03:00
print-function = %s"error" / %s"log"
2021-03-23 19:11:42 +03:00
print-arguments = "(" string-literal *( "," expression ) [ "," ] ")"
2021-03-23 19:11:42 +03:00
print-call = print-function print-arguments
function-declaration = %s"function" identifier
"(" [ function-parameters ] ")" "->" type
2021-03-23 19:11:42 +03:00
block
function-parameters = function-parameter *( "," function-parameter ) [ "," ]
2022-04-30 08:01:54 +03:00
function-parameter = [ %s"public" / %s"constant" / %s"const" ]
identifier ":" type
circuit-declaration = %s"circuit" "{" circuit-component-declaration
*( "," circuit-component-declaration )
[ "," ] "}"
circuit-component-declaration = identifier ":" type
declaration = function-declaration
/ circuit-declaration
2021-03-23 19:11:42 +03:00
file = *declaration
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Format String Grammar
; ---------------------
not-brace = %x0-7A / %x7C / %x7E-10FFFF ; anything but { or }
format-string-container = "{}"
format-string-open-brace = "{{"
format-string-close-brace = "}}"
format-string-element = not-brace
/ format-string-container
/ format-string-open-brace
/ format-string-close-brace
format-string = *format-string-element
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Input Grammar
; -------------
2022-06-01 18:48:36 +03:00
input-type = primitive-type
input-expression = expression
input-item = identifier ":" input-type "=" input-expression ";"
input-title = "[" ( %s"public" / %s"private" / %s"constant" / %s"const" ) "]"
input-section = input-title *input-item
input-file = *input-section
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Output Grammar
; --------------
output-expression = expression
output-item = output-expression ";"
output-title = "[" %s"output" "]"
output-section = output-title output-item
output-file = output-section