Skip to content

Commit

Permalink
Support trailing commas in literals
Browse files Browse the repository at this point in the history
  • Loading branch information
tjammer committed Feb 29, 2024
1 parent b2b692e commit e954040
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 29 deletions.
32 changes: 20 additions & 12 deletions lib/parser.mly
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func_name:
| infix = infix_no_inline { $loc(infix), infix }

typedef:
| Type; name = decl_typename; Equal; Lbrac; labels = separated_nonempty_list(Comma, record_item_decl); Rbrac
| Type; name = decl_typename; Equal; Lbrac; labels = separated_nonempty_trailing_list(Comma, record_item_decl, Rbrac)
{ Trecord ({name; labels = Array.of_list labels}) }
| Type; name = decl_typename; Equal; spec = type_spec { Talias (name, spec) }
| Type; name = decl_typename; Equal; ctors = separated_nonempty_list(Hbar, ctor)
Expand Down Expand Up @@ -265,22 +265,22 @@ param_pattern:
| Lpar; tups = tup_tups(param_pattern); Rpar; Ampersand { let loc, tups = tups in Ptup (loc, tups, Dmut) }
| Lpar; tups = tup_tups(param_pattern); Rpar; Exclamation { let loc, tups = tups in Ptup (loc, tups, Dmove) }

let tup_pattern(x) :=
tup_pattern(x):
| tups = tup_tups(x); { let loc, tups = tups in Ptup (loc, tups, Dnorm) }

let tup_tups(x) :=
tup_tups(x):
| head = with_loc(x); Comma; tail = separated_nonempty_list(Comma, with_loc(x));
{ $loc, head :: tail }

let record_pattern(x) :=
| Lbrac; items = separated_nonempty_list(Comma, record_item_pattern(x)); Rbrac;
record_pattern(x):
| Lbrac; items = separated_nonempty_trailing_list(Comma, record_item_pattern(x), Rbrac);
{ Precord ($loc, items, Dnorm) }

let record_item_pattern(x) :=
record_item_pattern(x):
| ident = ident; Equal; pat = x; { ident, Some pat }
| ident = ident; { ident, None }

let with_loc(x) :=
with_loc(x):
| pat = x; { $loc, pat }

ident:
Expand Down Expand Up @@ -328,12 +328,12 @@ expr:
{ Lambda ($loc, params, attr, return_annot, body) }
| Fun; param = only_one_param; attr = loption(capture_copies); Colon; body = block
{ Lambda ($loc, [param], attr, None, body) }
| Lbrac; items = separated_nonempty_list(Comma, record_item); Rbrac
| Lbrac; items = separated_nonempty_trailing_list(Comma, record_item, Rbrac)
{ Record ($loc, items) }
| Lpar; tuple = tuple; Rpar { Tuple ($loc, tuple) }
| Lpar; expr = expr; Rpar { expr }
| upcases = upcases { upcases }
| Lbrac; record = expr; With; items = separated_nonempty_list(Comma, record_item); Rbrac
| Lbrac; record = expr; With; items = separated_nonempty_trailing_list(Comma, record_item, Rbrac)
{ Record_update ($loc, record, items) }
| Do; Colon; block = block { Do_block block }
| aexpr = expr; Pipe_tail; pipeable = expr
Expand Down Expand Up @@ -401,10 +401,18 @@ bool:
| False { false }

array_lit:
| Lbrack; exprs = separated_list(Comma, expr); Rbrack { exprs }
| Lbrack; exprs = separated_trailing_list(Comma, expr, Rbrack) { exprs }

separated_nonempty_trailing_list(sep, item, terminator):
| x = item; option(sep); terminator { [ x ] }
| x = item; sep; xs = separated_nonempty_trailing_list(sep, item, terminator) { x :: xs }

separated_trailing_list(sep, item, terminator):
| terminator { [] }
| lst = separated_nonempty_trailing_list(sep, item, terminator) { lst }

fixed_array_lit:
| Hashtag_brack; items = separated_nonempty_list(Comma, expr); Rbrack { Fixed_array items }
| Hashtag_brack; items = separated_nonempty_trailing_list(Comma, expr, Rbrack) { Fixed_array items }
| num = Hashnum_brack; item = expr; Rbrack { Fixed_array_num (num, item) }

call_arg:
Expand Down Expand Up @@ -460,7 +468,7 @@ infix_no_inline:
| And { And }
| Or { Or }

let parens(x) :=
parens(x):
| Lpar; items = separated_list(Comma, x); Rpar; { items }

type_spec:
Expand Down
22 changes: 5 additions & 17 deletions lib/syntax_errors.messages
Original file line number Diff line number Diff line change
Expand Up @@ -582,10 +582,6 @@ prog: Ctor Lpar With

<YOUR SYNTAX ERROR MESSAGE HERE>

prog: Ctor Lpar Ident Comma Ident Rbrack

<YOUR SYNTAX ERROR MESSAGE HERE>

prog: Ctor Lpar Ident With

<YOUR SYNTAX ERROR MESSAGE HERE>
Expand All @@ -594,11 +590,11 @@ prog: Lpar Ident Comma With

<YOUR SYNTAX ERROR MESSAGE HERE>

prog: Hashtag_brack Ident With
prog: Fmt Lpar Ident With

<YOUR SYNTAX ERROR MESSAGE HERE>

prog: Hashtag_brack Ident Comma With
prog: Fmt Lpar Ident Comma With

<YOUR SYNTAX ERROR MESSAGE HERE>

Expand Down Expand Up @@ -814,15 +810,15 @@ prog: Ident With

<YOUR SYNTAX ERROR MESSAGE HERE>

prog: Fmt Lpar Ident Rbrack
prog: Hashnum_brack Ident With

<YOUR SYNTAX ERROR MESSAGE HERE>

prog: Hashnum_brack Ident With
prog: Hashtag_brack Ident With

<YOUR SYNTAX ERROR MESSAGE HERE>

prog: Hashtag_brack Ident Rpar
prog: Hashtag_brack Ident Comma With

<YOUR SYNTAX ERROR MESSAGE HERE>

Expand Down Expand Up @@ -890,14 +886,6 @@ prog: Lbrac Ident With With

<YOUR SYNTAX ERROR MESSAGE HERE>

prog: Lbrack Ident Rpar

<YOUR SYNTAX ERROR MESSAGE HERE>

prog: Lpar Ident Comma Ident Rbrack

<YOUR SYNTAX ERROR MESSAGE HERE>

prog: Lpar Ident With

<YOUR SYNTAX ERROR MESSAGE HERE>
Expand Down
8 changes: 8 additions & 0 deletions test/typing.ml
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ let test_record_false () =
test_exn "Unbound field z on record t"
"type t = {x : int, y : int}\n{x = 2, z = 2}"

let test_record_trailing () =
test "t" "type t = { x : int, y : int }\n{x = 2, y = 2,}"

let test_record_choose () =
test "t1"
"type t1 = { x : int, y : int }\n\
Expand Down Expand Up @@ -395,6 +398,7 @@ fun get_item(slot):
get_item|}

let test_array_lit () = test "array(int)" "[0, 1]"
let test_array_lit_trailing () = test "array(int)" "[0, 1,]"

let test_array_var () = test "array(int)" {|let a = [0, 1]
a|}
Expand Down Expand Up @@ -1219,6 +1223,7 @@ let test_functor_check_concrete () =
but found (_) -> [make/t('a)]" (check_sig_test "int")
let test_farray_lit () = test "unit" "let arr = #[1, 2, 3]"
let test_farray_lit_trailing () = test "unit" "let arr = #[1, 2, 3,]"
let test_farray_nested_lit () =
test "unit" "let arr = #[#[1, 2, 3], #[3, 4, 5]]"
Expand Down Expand Up @@ -1281,6 +1286,7 @@ let () =
[
case "clear" test_record_clear;
case "false" test_record_false;
case "trailing" test_record_trailing;
case "choose" test_record_choose;
case "reorder" test_record_reorder;
case "create if" test_record_create_if;
Expand Down Expand Up @@ -1365,6 +1371,7 @@ let () =
( "array",
[
case "literal" test_array_lit;
case "literal trailing" test_array_lit_trailing;
case "var" test_array_var;
case "weak" test_array_weak;
case "different_types" test_array_different_types;
Expand Down Expand Up @@ -1683,6 +1690,7 @@ do:
( "fixed-size array",
[
case "lit" test_farray_lit;
case "lit trailing" test_farray_lit_trailing;
case "nested lit" test_farray_nested_lit;
case "generalize / instantiate" test_farray_inference;
] );
Expand Down

0 comments on commit e954040

Please sign in to comment.