Skip to content

Commit

Permalink
Make set-expression a statement in grammar
Browse files Browse the repository at this point in the history
Create special cases for colon: expr
  • Loading branch information
tjammer committed Sep 29, 2024
1 parent 4656295 commit 1e6860a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 24 deletions.
14 changes: 9 additions & 5 deletions lib/parser.mly
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@
%token Or

%nonassoc Below_expr
%nonassoc Below_Ampersand

%nonassoc Type_application

Expand Down Expand Up @@ -116,6 +115,8 @@ stmt_no_ident:
{ Rec($loc, (func true) :: (List.map (fun f -> f true) tail)) }
| expr = expr_no_ident { Expr ($loc, expr) }
| Use; path = use_path { Use ($loc(path), path) }
| Ampersand; expr = expr; Equal; newval = expr
{ Expr($loc, Set ($loc, ($loc(expr), expr), newval)) }

stmt:
| stmt = stmt_no_ident { stmt }
Expand All @@ -129,7 +130,7 @@ func:

func_colon:
| name = func_name; params = parens(param_decl); attr = loption(capture_copies);
return_annot = option(return_annot); Colon; body = expr
return_annot = option(return_annot); Colon; body = expr_or_set
{ fun is_rec ->
let body = [Expr ($loc(body), body)] in
($loc, { name; params; return_annot; body; attr; is_rec }) }
Expand Down Expand Up @@ -324,14 +325,17 @@ expr_no_ident:
{ Record_update ($loc, record, items) }
| Match; expr = passed(expr); Lcurly; option(Hbar); clauses = clauses; Rcurly
{ Match (($startpos, $endpos(expr)), fst expr, snd expr, clauses) }
| Ampersand; expr = expr; Equal; newval = expr; %prec Below_Ampersand
{ Set ($loc, ($loc(expr), expr), newval) }
| id = Path_id; expr = expr; %prec Path { Local_use ($loc, id, expr) }

expr:
| ident = ident { Var ident }
| expr = expr_no_ident { expr }

expr_or_set:
| expr = expr { expr }
| Ampersand; expr = expr; Equal; newval = expr
{ Set ($loc, ($loc(expr), expr), newval) }

%inline dot_callee:
| callee = ident { Var callee }
| callee = path_ident { callee }
Expand Down Expand Up @@ -359,7 +363,7 @@ path_ident:
{ List.fold_right (fun path expr -> Local_use ($loc, path, expr)) paths (Var callee) }

clause:
| pattern = match_pattern; Colon; expr = expr { $loc, pattern, expr }
| pattern = match_pattern; Colon; expr = expr_or_set { $loc, pattern, expr }

clauses:
| clause = clause { [ clause ] }
Expand Down
54 changes: 35 additions & 19 deletions lib/syntax_errors.messages
Original file line number Diff line number Diff line change
Expand Up @@ -626,11 +626,7 @@ prog: False Lpar Exclamation With

<YOUR SYNTAX ERROR MESSAGE HERE>

prog: Ampersand With

<YOUR SYNTAX ERROR MESSAGE HERE>

prog: Ampersand False Wildcard
prog: False Lpar Exclamation False Wildcard

<YOUR SYNTAX ERROR MESSAGE HERE>

Expand Down Expand Up @@ -754,18 +750,6 @@ prog: False Dot Ident Wildcard

<YOUR SYNTAX ERROR MESSAGE HERE>

prog: Ampersand False Equal With

<YOUR SYNTAX ERROR MESSAGE HERE>

prog: Ampersand False Equal False Wildcard

<YOUR SYNTAX ERROR MESSAGE HERE>

prog: False Lpar Exclamation False Wildcard

<YOUR SYNTAX ERROR MESSAGE HERE>

prog: False Lpar Ampersand With

<YOUR SYNTAX ERROR MESSAGE HERE>
Expand Down Expand Up @@ -826,7 +810,39 @@ prog: Fun Ident Lpar Rpar Colon With

<YOUR SYNTAX ERROR MESSAGE HERE>

prog: Fun Ident Lpar Rpar Colon False Wildcard
prog: Match False Lcurly Ident Colon Ampersand With

<YOUR SYNTAX ERROR MESSAGE HERE>

prog: Match False Lcurly Ident Colon Ampersand False Wildcard

<YOUR SYNTAX ERROR MESSAGE HERE>

prog: Match False Lcurly Ident Colon Ampersand False Equal With

<YOUR SYNTAX ERROR MESSAGE HERE>

prog: Match False Lcurly Ident Colon Ampersand False Equal False Wildcard

<YOUR SYNTAX ERROR MESSAGE HERE>

prog: Match False Lcurly Ident Colon False Wildcard

<YOUR SYNTAX ERROR MESSAGE HERE>

prog: Ampersand With

<YOUR SYNTAX ERROR MESSAGE HERE>

prog: Ampersand False Wildcard

<YOUR SYNTAX ERROR MESSAGE HERE>

prog: Ampersand False Equal With

<YOUR SYNTAX ERROR MESSAGE HERE>

prog: Ampersand False Equal False Wildcard

<YOUR SYNTAX ERROR MESSAGE HERE>

Expand Down Expand Up @@ -990,7 +1006,7 @@ prog: Match False Lcurly Ident Colon With

<YOUR SYNTAX ERROR MESSAGE HERE>

prog: Match False Lcurly Ident Colon False Wildcard
prog: Match False Lcurly Ident Colon False Eof

<YOUR SYNTAX ERROR MESSAGE HERE>

Expand Down

0 comments on commit 1e6860a

Please sign in to comment.