|
| 1 | +WHITESPACE = _{ " " | "\t" | "\n" | "\r" } |
| 2 | + |
| 3 | +// Program structure |
| 4 | +program = { SOI ~ constant_declaration* ~ function+ ~ EOI } |
| 5 | + |
| 6 | +// Constants |
| 7 | +constant_declaration = { "const" ~ identifier ~ "=" ~ expression ~ ";" } |
| 8 | + |
| 9 | +// Functions |
| 10 | +function = { "fn" ~ identifier ~ "(" ~ parameter_list? ~ ")" ~ return_count? ~ "{" ~ statement* ~ "}" } |
| 11 | +parameter_list = { parameter ~ ("," ~ parameter)* } |
| 12 | +parameter = { (const_keyword)? ~ identifier } |
| 13 | +const_keyword = { "const" } |
| 14 | +return_count = { "->" ~ number } |
| 15 | + |
| 16 | +// Statements |
| 17 | +statement = { |
| 18 | + single_assignment | |
| 19 | + array_assign | |
| 20 | + if_statement | |
| 21 | + for_statement | |
| 22 | + return_statement | |
| 23 | + break_statement | |
| 24 | + continue_statement | |
| 25 | + function_call | |
| 26 | + assert_eq_statement | |
| 27 | + assert_not_eq_statement |
| 28 | +} |
| 29 | + |
| 30 | +return_statement = { "return" ~ (tuple_expression)? ~ ";" } |
| 31 | + |
| 32 | +break_statement = { "break" ~ ";" } |
| 33 | +continue_statement = { "continue" ~ ";" } |
| 34 | + |
| 35 | +single_assignment = { identifier ~ "=" ~ expression ~ ";" } |
| 36 | + |
| 37 | +array_assign = { identifier ~ "[" ~ expression ~ "]" ~ "=" ~ expression ~ ";" } |
| 38 | + |
| 39 | +if_statement = { "if" ~ condition ~ "{" ~ statement* ~ "}" ~ else_clause? } |
| 40 | + |
| 41 | +condition = {condition_eq | condition_diff} |
| 42 | +condition_eq = { expression ~ "==" ~ expression } |
| 43 | +condition_diff = { expression ~ "!=" ~ expression } |
| 44 | + |
| 45 | +else_clause = { "else" ~ "{" ~ statement* ~ "}" } |
| 46 | + |
| 47 | +for_statement = { "for" ~ identifier ~ "in" ~ expression ~ ".." ~ expression ~ unroll_clause? ~ "{" ~ statement* ~ "}" } |
| 48 | +unroll_clause = { "unroll" } |
| 49 | + |
| 50 | +function_call = { function_res? ~ identifier ~ "(" ~ tuple_expression? ~ ")" ~ ";" } |
| 51 | +function_res = { var_list ~ "=" } |
| 52 | +var_list = { identifier ~ ("," ~ identifier)* } |
| 53 | + |
| 54 | +assert_eq_statement = { "assert" ~ expression ~ "==" ~ expression ~ ";" } |
| 55 | +assert_not_eq_statement = { "assert" ~ expression ~ "!=" ~ expression ~ ";" } |
| 56 | + |
| 57 | +// Expressions |
| 58 | +tuple_expression = { expression ~ ("," ~ expression)* } |
| 59 | +expression = { add_expr } |
| 60 | +add_expr = { sub_expr ~ ("+" ~ sub_expr)* } |
| 61 | +sub_expr = { mul_expr ~ ("-" ~ mul_expr)* } |
| 62 | +mul_expr = { div_expr ~ ("*" ~ div_expr)* } |
| 63 | +div_expr = { exp_expr ~ ("/" ~ exp_expr)* } |
| 64 | +exp_expr = { primary ~ ("**" ~ primary)* } |
| 65 | +primary = { |
| 66 | + "(" ~ expression ~ ")" | |
| 67 | + array_access_expr | |
| 68 | + var_or_constant |
| 69 | +} |
| 70 | +array_access_expr = { identifier ~ "[" ~ expression ~ "]" } |
| 71 | + |
| 72 | +// Basic elements |
| 73 | +var_or_constant = { constant_value | identifier } |
| 74 | +constant_value = { number | "public_input_start" } |
| 75 | + |
| 76 | +// Lexical elements |
| 77 | +identifier = @{ (ASCII_ALPHA | "_") ~ (ASCII_ALPHANUMERIC | "_")* } |
| 78 | +number = @{ ASCII_DIGIT+ } |
0 commit comments