leo/ast/src/leo.pest
Howard Wu f57a0cc497
Merge pull request #249 from AleoHQ/feature/console
Implement console functions
2020-08-19 00:10:44 -07:00

493 lines
13 KiB
Plaintext

/// Common
// Declared in common/assignee.rs
assignee = { identifier ~ access_assignee* }
// Declared in files/file.rs
file = { SOI ~ NEWLINE* ~ definition* ~ NEWLINE* ~ EOI }
// Declared in definitions/definition.rs
definition = {
definition_annotated
| import
| circuit
| function
| test_function
}
// Declared in definitions/annotated_definition.rs
definition_annotated = { annotation ~ NEWLINE* ~ definition}
// Declared in common/identifier.rs
identifier = @{ ((!protected_name ~ ASCII_ALPHA) | (protected_name ~ (ASCII_ALPHANUMERIC | "_"))) ~ (ASCII_ALPHANUMERIC | "_")* }
protected_name = {
"address"
| "as"
| "circuit"
| "const"
| "console"
| "else"
| "false"
| "field"
| "for"
| "function"
| "group"
| "if"
| "import"
| "in"
| "let"
| "mut"
| "return"
| "static"
| "string"
| "test"
| "true"
}
// Declared in common/line_end.rs
LINE_END = { ";" ~ NEWLINE* }
// Declared in common/mutable.rs
mutable = { "mut " }
// Declared in common/range.rs
range = { expression? ~ ".." ~ expression? }
// Declared in common/range_or_expression.rs
range_or_expression = { range | expression }
// Declared in common/spread.rs
spread = { "..." ~ expression }
// Declared in common/spread_or_expression.rs
spread_or_expression = { spread | expression }
// Declared in common/static_.rs
static_ = { "static " }
// Declared in common/variable_name.rs
variable_name = {mutable? ~ identifier}
variable_name_tuple = _{"(" ~ variable_name ~ ("," ~ variable_name)+ ~ ")"}
// Declared in common/variables.rs
variables = { ( variable_name_tuple | variable_name ) ~ (":" ~ type_ )? }
// Declared in common/declare.rs
declare = { let_ | const_ }
const_ = { "const " }
let_ = { "let " }
/// Operations
// Declared in operations/unary_operation.rs
operation_unary = {
operation_not
| operation_negate
}
operation_not = { "!" }
operation_negate = { "-" }
// Declared in operations/binary_operation.rs
operation_and = { "&&" }
operation_or = { "||" }
operation_eq = { "==" }
operation_ne = { "!=" }
operation_ge = { ">=" }
operation_gt = { ">" }
operation_le = { "<=" }
operation_lt = { "<" }
operation_add = { "+" }
operation_sub = { "- " }
operation_mul = { "*" }
operation_div = { "/" }
operation_pow = { "**" }
operation_compare = _{
operation_eq | operation_ne |
operation_ge | operation_gt |
operation_le | operation_lt
}
operation_binary = _{
operation_compare | operation_and | operation_or |
operation_add | operation_sub | operation_pow | operation_mul | operation_div
}
// Declared in operations/assign_operation.rs
operation_assign = {
assign | operation_add_assign | operation_sub_assign |
operation_mul_assign | operation_div_assign | operation_pow_assign
}
assign = { "=" }
operation_add_assign = { "+=" }
operation_sub_assign = { "-=" }
operation_mul_assign = { "*=" }
operation_div_assign = { "/=" }
operation_pow_assign = { "**=" }
/// Types
// Declared in types/type_.rs
type_ = { type_self | type_tuple | type_array | type_data | type_circuit }
// Declared in types/integer_type.rs
type_integer = {
type_integer_signed
| type_integer_unsigned
}
// Declared in types/unsigned_integer_type.rs
type_u8 = { "u8" }
type_u16 = { "u16" }
type_u32 = { "u32" }
type_u64 = { "u64" }
type_u128 = { "u128" }
type_integer_unsigned = {
type_u8
| type_u16
| type_u32
| type_u64
| type_u128
}
// Declared in types/signed_integer_type.rs
type_i8 = { "i8" }
type_i16 = { "i16" }
type_i32 = { "i32" }
type_i64 = { "i64" }
type_i128 = { "i128" }
type_integer_signed = {
type_i8
| type_i16
| type_i32
| type_i64
| type_i128
}
// Declared in types/field_type.rs
type_field = { "field" }
// Declared in types/group_type.rs
type_group = { "group" }
// Declared in types/boolean_type.rs
type_boolean = { "bool" }
// Declared in types/address_type.rs
type_address = { "address" }
// Declared in types/data_type.rs
type_data = {
type_address
| type_boolean
| type_field
| type_group
| type_integer
}
// Declared in types/self_type.rs
type_self = { "Self" }
// Declared in types/self_type.rs
type_circuit = { identifier }
// Declared in types/array_type.rs
type_array = { type_data ~ ("[" ~ number_positive ~ "]")+ }
type_tuple = { "(" ~ NEWLINE* ~ type_ ~ ("," ~ NEWLINE* ~ type_)+ ~ ","? ~ NEWLINE* ~ ")" }
/// Values
// Declared in values/value.rs
value = {
value_address
| value_boolean
| value_field
| value_group
| value_integer
| value_number // must be last as a catch all
}
// Declared in values/number_value.rs
value_number = { number_negative | number_positive }
// Declared in values/number_negative.rs
number_negative = @{ "-" ~ ASCII_DIGIT+ }
// Declared in values/number_positive.rs
number_positive = @{ ASCII_DIGIT+ }
// Declared in values/integer_value.rs
value_integer = { value_integer_signed | value_integer_unsigned}
// Declared in values/signed_integer_value.rs
value_integer_signed = ${ value_number ~ type_integer_signed }
// Declared in values/unsigned_integer_value.rs
value_integer_unsigned = ${ number_positive ~ type_integer_unsigned }
// Declared in values/boolean_value.rs
value_boolean = { "true" | "false" }
// Declared in values/field_value.rs
value_field = ${ value_number ~ type_field }
// Declared in values/group_value.rs
value_group = ${ group_single_or_tuple ~ type_group }
group_single_or_tuple = {value_number | group_tuple}
group_tuple = !{"(" ~ group_coordinate ~ "," ~ group_coordinate ~ ")"}
// Declared in values/group_coordinate.rs
group_coordinate = {
value_number
| sign_high
| sign_low
| inferred
}
sign_high = @{"+"}
sign_low = @{"-"}
inferred = @{"_"}
// Declared in values/address.rs
address = @{ "aleo1" ~ (LOWERCASE_LETTER | ASCII_DIGIT){58} }
// Declared in values/address_value.rs
value_address = ${ type_address ~ "(" ~ address ~ ")" }
/// Access
// Declared in access/access.rs
access = { access_array | access_tuple | access_call | access_member | access_static_member}
// Declared in access/array_access.rs
access_array = !{ "[" ~ range_or_expression ~ "]" }
// Declared in access/tuple_access.rs
access_tuple = ${ "." ~ number_positive }
// Declared in access/assignee_access.rs
access_assignee = { access_array | access_tuple | access_member }
// Declared in access/call_access.rs
access_call = !{ expression_tuple }
// Declared in access/member_access.rs
access_member = ${ "." ~ identifier }
// Declared in access/static_member_access.rs
access_static_member = ${ "::" ~ identifier }
/// Circuits
// Declared in circuits/circuit_definition.rs
circuit = { "circuit " ~ identifier ~ "{" ~ NEWLINE* ~ circuit_member* ~ NEWLINE* ~ "}" ~ NEWLINE* }
// Declared in circuits/circuit_field.rs
circuit_field = { identifier ~ ":" ~ expression }
// Declared in circuits/circuit_field_definition.rs
circuit_field_definition = { identifier ~ ":" ~ type_ ~ ","?}
// Declared in circuits/circuit_function.rs
circuit_function = { static_? ~ function }
// Declared in circuits/circuit_member.rs
circuit_member = { circuit_function | circuit_field_definition ~ NEWLINE*}
/// Conditionals
expression_conditional = { "if " ~ expression ~ "? " ~ expression ~ ": " ~ expression}
/// Expressions
expression_term = {
value
| ("(" ~ expression ~ ")")
| expression_tuple
| expression_conditional
| expression_array_initializer
| expression_array_inline
| expression_circuit_inline
| expression_unary
| expression_postfix
| identifier
}
// Declared in expressions/expression.rs
expression = { expression_term ~ (operation_binary ~ expression_term)* }
// Declared in expressions/expression_tuple.rs
expression_tuple = { "(" ~ (expression ~ ("," ~ expression)*)? ~ ")" }
// Declared in expressions/array_initializer_expression.rs
expression_array_initializer = { "[" ~ spread_or_expression ~ ";" ~ number_positive ~ "]" }
// Declared in expressions/array_inline_expression.rs
expression_array_inline = { "[" ~ NEWLINE* ~ inline_array_inner ~ NEWLINE* ~ "]"}
inline_array_inner = _{(spread_or_expression ~ ("," ~ NEWLINE* ~ spread_or_expression)*)?}
// Declared in expressions/circuit_inline_expression.rs
expression_circuit_inline = { identifier ~ "{" ~ NEWLINE* ~ circuit_field_list ~ NEWLINE* ~ "}" }
circuit_field_list = _{ (circuit_field ~ ("," ~ NEWLINE* ~ circuit_field)*)? ~ ","? }
// Declared in expressions/unary_expression.rs
expression_unary = { operation_unary ~ expression_term }
// Declared in expressions/postfix_expression.rs
expression_postfix = ${ identifier ~ access+ }
/// Statements
// Declared in statements/statement.rs
statement = {
(statement_return
| statement_conditional
| statement_for
| console_function_call
| statement_definition
| statement_assign
| statement_expression
) ~ NEWLINE*
}
// Declared in statements/assign_statement.rs
statement_assign = { assignee ~ operation_assign ~ expression ~ LINE_END }
// Declared in statements/conditional_statement.rs
statement_conditional = {"if " ~ (expression | "(" ~ expression ~ ")") ~ "{" ~ NEWLINE* ~ statement+ ~ "}" ~ ("else " ~ conditional_nested_or_end_statement)?}
conditional_nested_or_end_statement = { statement_conditional | "{" ~ NEWLINE* ~ statement+ ~ "}"}
// Declared in statements/definition_statement.rs
statement_definition = { declare ~ variables ~ "=" ~ expression ~ LINE_END}
// Declared in statements/expression_statement.rs
statement_expression = { expression ~ LINE_END }
// Declared in statements/for_statement.rs
statement_for = { "for " ~ identifier ~ "in " ~ expression ~ ".." ~ expression ~ "{" ~ NEWLINE* ~ statement+ ~ "}"}
// Declared in statements/return_statement.rs
statement_return = { "return " ~ expression}
/// Functions
// Declared in functions/test_function.rs
test_function = { "test " ~ function }
// Declared in functions/function.rs
function = { "function " ~ identifier ~ input_tuple ~ ("->" ~ type_)? ~ "{" ~ NEWLINE* ~ statement* ~ NEWLINE* ~ "}" ~ NEWLINE* }
// Declared in functions/input/function_input.rs
function_input = { mutable? ~ identifier ~ ":" ~ type_ }
// Declared in functions/input/input_keyword.rs
input_keyword = { "input" }
// Declared in functions/input/input.rs
input = {
function_input
| input_keyword
}
input_tuple = _{ "(" ~ NEWLINE* ~ (input ~ ("," ~ NEWLINE* ~ input)* ~ ","?)? ~ NEWLINE* ~ ")"}
/// Imports
// Declared in imports/import.rs
import = { "import " ~ package ~ LINE_END}
package_name = @{ ((ASCII_ALPHA_LOWER | ASCII_DIGIT) ~ ( "-" ~ (ASCII_ALPHA_LOWER | ASCII_DIGIT))*)+ }
// Declared in imports/package.rs
package = { package_name ~ "." ~ package_access }
// Declared in imports/package_access
package_access = {
multiple_package_access
| star
| package // subpackage
| import_symbol
}
multiple_package_access = _{ "(" ~ NEWLINE* ~ package_access ~ ("," ~ NEWLINE* ~ package_access)* ~ ","? ~ NEWLINE* ~ ")"}
// Declared in imports/star.rs
star = {"*"}
// Declared in imports/import_symbol.rs
import_symbol = { identifier ~ ("as " ~ identifier)? }
/// Utilities
COMMENT = _{ ("/*" ~ (!"*/" ~ ANY)* ~ "*/") | ("//" ~ (!NEWLINE ~ ANY)*) }
WHITESPACE = _{ " " | "\t" ~ (NEWLINE)* } // pest implicit whitespace keyword
/// Console Functions
// Declared in console/console_function_call.rs
console_function_call = ${console_keyword ~ "." ~ console_function ~ LINE_END}
// Declared in console/console_keyword.rs
console_keyword = {"console"}
// Declared in console/console_function.rs
console_function = {
console_assert
| console_debug
| console_error
| console_log
}
// Declared in console/console_assert.rs
console_assert = !{"assert(" ~ expression ~ ")"}
// Declared in console/console_debug.rs
console_debug = !{"debug(" ~ formatted_string? ~ ")"}
// Declared in console/console_error.rs
console_error = !{"error(" ~ formatted_string? ~ ")"}
// Declared in console/console_log.rs
console_log = !{"log(" ~ formatted_string? ~ ")"}
// Declared in console/formatted_string.rs
formatted_string = {
"\""
~ (!"\"" ~ (formatted_container | ANY))*
~ "\""
~ ("," ~ formatted_parameter)*
}
// Declared in console/formatted_container.rs
formatted_container = { "{" ~ "}"}
// Declared in console/formatted_parameter.rs
formatted_parameter = { expression }
/// Annotations
// Declared in annotations/annotation.rs
annotation = ${annotation_symbol ~ annotation_name ~ annotation_arguments}
// Declared in annotations/annotation_symbol.rs
annotation_symbol = ${"@"}
// Declared in annotations/annotation_name.rs
annotation_name = {
context
}
// Declared in annotations/context.rs
context = {"context"}
// Declared in annotations/annotation_argument.rs
annotation_arguments = !{"(" ~ NEWLINE* ~ annotation_argument ~ ("," ~ NEWLINE* ~ annotation_argument)* ~ ","? ~ NEWLINE* ~ ")"}
annotation_argument = @{ (ASCII_ALPHANUMERIC | "_")+ }