Skip to content
This repository has been archived by the owner on Jan 30, 2024. It is now read-only.

Commit

Permalink
Remove tagging in grammar. See msg.
Browse files Browse the repository at this point in the history
Turns out Pest has a bug (see
pest-parser/pest#875) where it fails to detect
left-recursion when tags are used.

If we remove all the tags, then it is able to successfully detect left
recursion better.

This commit removes the tags, and also fixes up one instance of
left-recursion.

We still have the badly left-recursive `bin_expr` to fix up, but that is
left for another commit.
  • Loading branch information
jaybosamiya committed Jun 21, 2023
1 parent 3d507e0 commit e011688
Showing 1 changed file with 41 additions and 44 deletions.
85 changes: 41 additions & 44 deletions src/verus.pest
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,8 @@ macro_rules = {

macro_def = {
attr* ~ visibility? ~
"macro" ~ name ~ #args=token_tree? ~
#body=token_tree
"macro" ~ name ~ token_tree? ~
token_tree
}

module = {
Expand Down Expand Up @@ -376,13 +376,13 @@ const = {
attr* ~ visibility? ~
"default"? ~
"const" ~ (name | "_") ~ ":" ~ type ~
("=" ~ #body=expr)? ~ ";"
("=" ~ expr)? ~ ";"
}

static = {
attr* ~ visibility? ~
"static" ~ "mut"? ~ name ~ ":" ~ type ~
("=" ~ #body=expr)? ~ ";"
("=" ~ expr)? ~ ";"
}

trait = {
Expand Down Expand Up @@ -411,7 +411,7 @@ assoc_item = {
impl = {
attr* ~ visibility? ~
"default"? ~ "unsafe"? ~
"impl" ~ generic_param_list? ~ ("const"? ~ "!"? ~ #trait=type ~ "for")? ~ #self_ty=type ~ where_clause? ~
"impl" ~ generic_param_list? ~ ("const"? ~ "!"? ~ type ~ "for")? ~ type ~ where_clause? ~
assoc_item_list
}

Expand Down Expand Up @@ -442,12 +442,12 @@ generic_param = {

type_param = {
attr* ~ name ~ (":" ~ type_bound_list)? ~
("=" ~ #default_type=type)?
("=" ~ type)?
}

const_param = {
attr* ~ "const" ~ name ~ ":" ~ type ~
("=" ~ #default_val=expr)?
("=" ~ expr)?
}

lifetime_param = {
Expand Down Expand Up @@ -487,7 +487,7 @@ stmt = {

let_stmt = {
attr* ~ "let" ~ "ghost"? ~ "tracked"? ~ pat ~ (":" ~ type)? ~
"=" ~ #initializer=expr ~
"=" ~ expr ~
let_else? ~
";"
}
Expand Down Expand Up @@ -515,6 +515,8 @@ expr = {
| "@"
// index_expr
| "[" ~ expr ~ "]"
// method_call_expr
| "." ~ name_ref ~ generic_arg_list? ~ arg_list
)*
}

Expand All @@ -532,7 +534,6 @@ expr_inner = {
| loop_expr
| macro_call
| match_expr
| method_call_expr
| paren_expr
| path_expr
| prefix_expr
Expand All @@ -556,7 +557,7 @@ macro_expr = {
}

literal = {
attr* ~ #value=(
attr* ~ (
int_number
| float_number
| string
Expand All @@ -577,8 +578,8 @@ path_expr = {
stmt_list = {
"{" ~
attr* ~
#statements=stmt* ~
#tail_expr=expr? ~
stmt* ~
expr? ~
"}"
}

Expand All @@ -591,20 +592,20 @@ block_expr = {
}

prefix_expr = {
attr* ~ #op=("-" | "!" | "*" | "&&&" | "|||") ~ expr
attr* ~ ("-" | "!" | "*" | "&&&" | "|||") ~ expr
}

bin_expr = {
attr* ~
#lhs=expr ~
#op=(
expr ~
(
"||" | "&&"
| "==" | "!=" | "<=" | ">=" | "<" | ">"
| "+" | "*" | "-" | "/" | "%" | "<<" | ">>" | "^" | "|" | "&"
| "=" | "+=" | "/=" | "*=" | "%=" | ">>=" | "<<=" | "-=" | "|=" | "&=" | "^="
| "==>"
) ~
#rhs=expr
expr
}

paren_expr = {
Expand All @@ -619,7 +620,7 @@ array_expr = {
}

tuple_expr = {
attr* ~ "(" ~ attr* ~ #fields=(expr ~ ("," ~ expr)* ~ ","?)? ~ ")"
attr* ~ "(" ~ attr* ~ (expr ~ ("," ~ expr)* ~ ","?)? ~ ")"
}

record_expr = {
Expand All @@ -629,8 +630,8 @@ record_expr = {
record_expr_field_list = {
"{" ~
attr* ~
#fields=(record_expr_field ~ ("," ~ record_expr_field)* ~ ","?)? ~
(".." ~ #spread=expr)? ~
(record_expr_field ~ ("," ~ record_expr_field)* ~ ","?)? ~
(".." ~ expr)? ~
"}"
}

Expand All @@ -639,37 +640,33 @@ record_expr_field = {
}

arg_list = {
"(" ~ #args=(expr ~ ("," ~ expr)* ~ ","?)? ~ ")"
}

method_call_expr = {
attr* ~ #receiver=expr ~ "." ~ name_ref ~ generic_arg_list? ~ arg_list
"(" ~ (expr ~ ("," ~ expr)* ~ ","?)? ~ ")"
}

closure_expr = {
attr* ~ ("for" ~ generic_param_list)? ~
"const"? ~ "static"? ~ "async"? ~ "move" ~ "forall"? ~ "exists"? ~
param_list ~ ret_type? ~ attr* ~
#body=expr
expr
}

if_expr = {
attr* ~ "if" ~ #condition=expr ~ #then_branch=block_expr ~
("else" ~ #else_branch=(if_expr | block_expr))?
attr* ~ "if" ~ expr ~ block_expr ~
("else" ~ (if_expr | block_expr))?
}

loop_expr = {
attr* ~ label? ~ "loop" ~ #loop_body=block_expr
attr* ~ label? ~ "loop" ~ block_expr
}

for_expr = {
attr* ~ label? ~ "for" ~ pat ~ "in" ~ #iterable=expr ~
#loop_body=block_expr
attr* ~ label? ~ "for" ~ pat ~ "in" ~ expr ~
block_expr
}

while_expr = {
attr* ~ label? ~ "while" ~ #condition=expr ~ invariant_clause? ~ decreases_clause? ~
#loop_body=block_expr
attr* ~ label? ~ "while" ~ expr ~ invariant_clause? ~ decreases_clause? ~
block_expr
}

label = {
Expand All @@ -685,7 +682,7 @@ continue_expr = {
}

range_expr = {
attr* ~ #start=expr? ~ #op=(".." | "..=") ~ #end=expr?
attr* ~ expr? ~ (".." | "..=") ~ expr?
}

match_expr = {
Expand All @@ -695,12 +692,12 @@ match_expr = {
match_arm_list = {
"{" ~
attr* ~
#arms=match_arm* ~
match_arm* ~
"}"
}

match_arm = {
attr* ~ pat ~ #guard=match_guard? ~ "=>" ~ expr ~ ","?
attr* ~ pat ~ match_guard? ~ "=>" ~ expr ~ ","?
}

match_guard = {
Expand Down Expand Up @@ -823,7 +820,12 @@ type_bound = {


pat = {
pat_inner ~ ("|" ~ pat_inner)* ~ "|"?
pat_inner ~ (
// range_pat (for 1.. or 1..2)
("..=" | "..") ~ pat?
// or_pat
| ("|" ~ pat_inner)* ~ "|"?
)?
}

pat_inner = {
Expand All @@ -835,7 +837,7 @@ pat_inner = {
| paren_pat
| path_pat
| wildcard_pat
| range_pat
| end_only_range_pat
| record_pat
| ref_pat
| slice_pat
Expand All @@ -856,13 +858,8 @@ wildcard_pat = {
"_"
}

range_pat = {
// 1..
#start=pat ~ #op=(".." | "..=")
// 1..2
| #start=pat ~ #op=(".." | "..=") ~ #end=pat
// ..2
| #op=(".." | "..=") ~ #end=pat
end_only_range_pat = {
("..=" | "..") ~ pat
}

ref_pat = {
Expand Down

0 comments on commit e011688

Please sign in to comment.