From 38edf5ce74273f41b3328b12102d7c9ca3684e76 Mon Sep 17 00:00:00 2001 From: Sander Spies Date: Mon, 6 Apr 2020 16:09:38 +0200 Subject: [PATCH] Add support for record expressions of only one field. --- src/passes/1-parser/reasonligo/Parser.mly | 46 ++- .../reasonligo/error.messages.checked-in | 305 ++++++++---------- src/test/contracts/single_record_item.religo | 5 + src/test/integration_tests.ml | 5 + 4 files changed, 171 insertions(+), 190 deletions(-) create mode 100644 src/test/contracts/single_record_item.religo diff --git a/src/passes/1-parser/reasonligo/Parser.mly b/src/passes/1-parser/reasonligo/Parser.mly index 1c6078355..a19ceb203 100644 --- a/src/passes/1-parser/reasonligo/Parser.mly +++ b/src/passes/1-parser/reasonligo/Parser.mly @@ -20,7 +20,6 @@ type 'a record_elements = { type 'a sequence_or_record = PaSequence of 'a sequence_elements | PaRecord of 'a record_elements -| PaSingleExpr of expr let (<@) f g x = f (g x) @@ -912,18 +911,26 @@ expr_with_let_expr: expr { $1 } | let_expr(expr_with_let_expr) { $1 } +more_field_assignments: + "," sep_or_term_list(field_assignment_punning,",") { + let elts, _region = $2 in + $1, elts + } + + sequence_or_record_in: - expr_with_let_expr ";" sep_or_term_list(expr_with_let_expr,";") { - let elts, _region = $3 in - let s_elts = Utils.nsepseq_cons $1 $2 elts - in PaSequence {s_elts; s_terminator=None} + sep_or_term_list(expr_with_let_expr,";") { + let elts, _region = $1 in + PaSequence {s_elts = elts; s_terminator=None} } -| field_assignment "," sep_or_term_list(field_assignment,",") { - let elts, _region = $3 in - let r_elts = Utils.nsepseq_cons $1 $2 elts - in PaRecord {r_elts; r_terminator = None} +| field_assignment more_field_assignments? { + match $2 with + | Some (comma, elts) -> + let r_elts = Utils.nsepseq_cons $1 comma elts in + PaRecord {r_elts; r_terminator = None} + | None -> + PaRecord {r_elts = ($1, []); r_terminator = None} } -| expr_with_let_expr ";"? { PaSingleExpr $1 } sequence_or_record: "{" sequence_or_record_in "}" { @@ -939,18 +946,25 @@ sequence_or_record: let value = {compound; ne_elements = r.r_elts; terminator = r.r_terminator} - in ERecord {region; value} - | PaSingleExpr e -> e } + in ERecord {region; value}} -field_assignment: - field_name { +field_assignment_punning: + (* This can only happen with multiple fields - + one item punning does NOT work in ReasonML *) + field_name { let value = { field_name = $1; assignment = ghost; field_expr = EVar $1 } - in {$1 with value} + in + {$1 with value} } -| field_name ":" expr { +| field_assignment { + $1 +} + +field_assignment: + field_name ":" expr { let start = $1.region in let stop = expr_to_region $3 in let region = cover start stop in diff --git a/src/passes/1-parser/reasonligo/error.messages.checked-in b/src/passes/1-parser/reasonligo/error.messages.checked-in index 19c8fcfa2..c5aec0a5c 100644 --- a/src/passes/1-parser/reasonligo/error.messages.checked-in +++ b/src/passes/1-parser/reasonligo/error.messages.checked-in @@ -1150,11 +1150,130 @@ interactive_expr: LBRACE ELLIPSIS WILD +interactive_expr: LBRACE Ident COLON Bytes COMMA Ident COLON Bytes VBAR +## +## Ends in an error in state: 477. +## +## nsepseq(field_assignment_punning,COMMA) -> field_assignment_punning . [ RBRACE ] +## nsepseq(field_assignment_punning,COMMA) -> field_assignment_punning . COMMA nsepseq(field_assignment_punning,COMMA) [ RBRACE ] +## nseq(__anonymous_0(field_assignment_punning,COMMA)) -> field_assignment_punning . COMMA seq(__anonymous_0(field_assignment_punning,COMMA)) [ RBRACE ] +## +## The known suffix of the stack is as follows: +## field_assignment_punning +## +## WARNING: This example involves spurious reductions. +## This implies that, although the LR(1) items shown above provide an +## accurate view of the past (what has been recognized so far), they +## may provide an INCOMPLETE view of the future (what was expected next). +## In state 155, spurious reduction of production call_expr_level_in -> core_expr +## In state 173, spurious reduction of production option(type_annotation_simple) -> +## In state 174, spurious reduction of production call_expr_level -> call_expr_level_in option(type_annotation_simple) +## In state 175, spurious reduction of production unary_expr_level -> call_expr_level +## In state 128, spurious reduction of production mult_expr_level -> unary_expr_level +## In state 152, spurious reduction of production add_expr_level -> mult_expr_level +## In state 183, spurious reduction of production cat_expr_level -> add_expr_level +## In state 204, spurious reduction of production comp_expr_level -> cat_expr_level +## In state 211, spurious reduction of production conj_expr_level -> comp_expr_level +## In state 218, spurious reduction of production disj_expr_level -> conj_expr_level +## In state 165, spurious reduction of production base_expr(expr) -> disj_expr_level +## In state 222, spurious reduction of production base_cond__open(expr) -> base_expr(expr) +## In state 223, spurious reduction of production expr -> base_cond__open(expr) +## In state 465, spurious reduction of production field_assignment -> Ident COLON expr +## In state 484, spurious reduction of production field_assignment_punning -> field_assignment +## + + + +interactive_expr: LBRACE Ident COLON Bytes COMMA Ident COMMA Ident COLON Bytes VBAR +## +## Ends in an error in state: 481. +## +## nsepseq(field_assignment_punning,COMMA) -> field_assignment_punning . [ RBRACE ] +## nsepseq(field_assignment_punning,COMMA) -> field_assignment_punning . COMMA nsepseq(field_assignment_punning,COMMA) [ RBRACE ] +## seq(__anonymous_0(field_assignment_punning,COMMA)) -> field_assignment_punning . COMMA seq(__anonymous_0(field_assignment_punning,COMMA)) [ RBRACE ] +## +## The known suffix of the stack is as follows: +## field_assignment_punning +## +## WARNING: This example involves spurious reductions. +## This implies that, although the LR(1) items shown above provide an +## accurate view of the past (what has been recognized so far), they +## may provide an INCOMPLETE view of the future (what was expected next). +## In state 155, spurious reduction of production call_expr_level_in -> core_expr +## In state 173, spurious reduction of production option(type_annotation_simple) -> +## In state 174, spurious reduction of production call_expr_level -> call_expr_level_in option(type_annotation_simple) +## In state 175, spurious reduction of production unary_expr_level -> call_expr_level +## In state 128, spurious reduction of production mult_expr_level -> unary_expr_level +## In state 152, spurious reduction of production add_expr_level -> mult_expr_level +## In state 183, spurious reduction of production cat_expr_level -> add_expr_level +## In state 204, spurious reduction of production comp_expr_level -> cat_expr_level +## In state 211, spurious reduction of production conj_expr_level -> comp_expr_level +## In state 218, spurious reduction of production disj_expr_level -> conj_expr_level +## In state 165, spurious reduction of production base_expr(expr) -> disj_expr_level +## In state 222, spurious reduction of production base_cond__open(expr) -> base_expr(expr) +## In state 223, spurious reduction of production expr -> base_cond__open(expr) +## In state 465, spurious reduction of production field_assignment -> Ident COLON expr +## In state 484, spurious reduction of production field_assignment_punning -> field_assignment +## + + + +interactive_expr: LBRACE Ident COLON Bytes COMMA Ident COMMA Ident COMMA WILD +## +## Ends in an error in state: 482. +## +## nsepseq(field_assignment_punning,COMMA) -> field_assignment_punning COMMA . nsepseq(field_assignment_punning,COMMA) [ RBRACE ] +## seq(__anonymous_0(field_assignment_punning,COMMA)) -> field_assignment_punning COMMA . seq(__anonymous_0(field_assignment_punning,COMMA)) [ RBRACE ] +## +## The known suffix of the stack is as follows: +## field_assignment_punning COMMA +## + + + +interactive_expr: LBRACE Ident COLON Bytes COMMA Ident COMMA WILD +## +## Ends in an error in state: 478. +## +## nsepseq(field_assignment_punning,COMMA) -> field_assignment_punning COMMA . nsepseq(field_assignment_punning,COMMA) [ RBRACE ] +## nseq(__anonymous_0(field_assignment_punning,COMMA)) -> field_assignment_punning COMMA . seq(__anonymous_0(field_assignment_punning,COMMA)) [ RBRACE ] +## +## The known suffix of the stack is as follows: +## field_assignment_punning COMMA +## + + + +interactive_expr: LBRACE Ident COLON Bytes COMMA Ident WILD +## +## Ends in an error in state: 473. +## +## field_assignment -> Ident . COLON expr [ RBRACE COMMA ] +## field_assignment_punning -> Ident . [ RBRACE COMMA ] +## +## The known suffix of the stack is as follows: +## Ident +## + + + +interactive_expr: LBRACE Ident COLON Bytes COMMA WILD +## +## Ends in an error in state: 472. +## +## more_field_assignments -> COMMA . sep_or_term_list(field_assignment_punning,COMMA) [ RBRACE ] +## +## The known suffix of the stack is as follows: +## COMMA +## + + + interactive_expr: LBRACE Ident COLON Bytes VBAR ## -## Ends in an error in state: 468. +## Ends in an error in state: 471. ## -## sequence_or_record_in -> field_assignment . COMMA sep_or_term_list(field_assignment,COMMA) [ RBRACE ] +## sequence_or_record_in -> field_assignment . option(more_field_assignments) [ RBRACE ] ## ## The known suffix of the stack is as follows: ## field_assignment @@ -1193,130 +1312,12 @@ interactive_expr: LBRACE Ident COLON VBAR -interactive_expr: LBRACE Ident COMMA Ident COLON Bytes VBAR -## -## Ends in an error in state: 474. -## -## nsepseq(field_assignment,COMMA) -> field_assignment . [ RBRACE ] -## nsepseq(field_assignment,COMMA) -> field_assignment . COMMA nsepseq(field_assignment,COMMA) [ RBRACE ] -## nseq(__anonymous_0(field_assignment,COMMA)) -> field_assignment . COMMA seq(__anonymous_0(field_assignment,COMMA)) [ RBRACE ] -## -## The known suffix of the stack is as follows: -## field_assignment -## -## WARNING: This example involves spurious reductions. -## This implies that, although the LR(1) items shown above provide an -## accurate view of the past (what has been recognized so far), they -## may provide an INCOMPLETE view of the future (what was expected next). -## In state 155, spurious reduction of production call_expr_level_in -> core_expr -## In state 173, spurious reduction of production option(type_annotation_simple) -> -## In state 174, spurious reduction of production call_expr_level -> call_expr_level_in option(type_annotation_simple) -## In state 175, spurious reduction of production unary_expr_level -> call_expr_level -## In state 128, spurious reduction of production mult_expr_level -> unary_expr_level -## In state 152, spurious reduction of production add_expr_level -> mult_expr_level -## In state 183, spurious reduction of production cat_expr_level -> add_expr_level -## In state 204, spurious reduction of production comp_expr_level -> cat_expr_level -## In state 211, spurious reduction of production conj_expr_level -> comp_expr_level -## In state 218, spurious reduction of production disj_expr_level -> conj_expr_level -## In state 165, spurious reduction of production base_expr(expr) -> disj_expr_level -## In state 222, spurious reduction of production base_cond__open(expr) -> base_expr(expr) -## In state 223, spurious reduction of production expr -> base_cond__open(expr) -## In state 465, spurious reduction of production field_assignment -> Ident COLON expr -## - - - -interactive_expr: LBRACE Ident COMMA Ident COMMA Ident COLON Bytes VBAR -## -## Ends in an error in state: 478. -## -## nsepseq(field_assignment,COMMA) -> field_assignment . [ RBRACE ] -## nsepseq(field_assignment,COMMA) -> field_assignment . COMMA nsepseq(field_assignment,COMMA) [ RBRACE ] -## seq(__anonymous_0(field_assignment,COMMA)) -> field_assignment . COMMA seq(__anonymous_0(field_assignment,COMMA)) [ RBRACE ] -## -## The known suffix of the stack is as follows: -## field_assignment -## -## WARNING: This example involves spurious reductions. -## This implies that, although the LR(1) items shown above provide an -## accurate view of the past (what has been recognized so far), they -## may provide an INCOMPLETE view of the future (what was expected next). -## In state 155, spurious reduction of production call_expr_level_in -> core_expr -## In state 173, spurious reduction of production option(type_annotation_simple) -> -## In state 174, spurious reduction of production call_expr_level -> call_expr_level_in option(type_annotation_simple) -## In state 175, spurious reduction of production unary_expr_level -> call_expr_level -## In state 128, spurious reduction of production mult_expr_level -> unary_expr_level -## In state 152, spurious reduction of production add_expr_level -> mult_expr_level -## In state 183, spurious reduction of production cat_expr_level -> add_expr_level -## In state 204, spurious reduction of production comp_expr_level -> cat_expr_level -## In state 211, spurious reduction of production conj_expr_level -> comp_expr_level -## In state 218, spurious reduction of production disj_expr_level -> conj_expr_level -## In state 165, spurious reduction of production base_expr(expr) -> disj_expr_level -## In state 222, spurious reduction of production base_cond__open(expr) -> base_expr(expr) -## In state 223, spurious reduction of production expr -> base_cond__open(expr) -## In state 465, spurious reduction of production field_assignment -> Ident COLON expr -## - - - -interactive_expr: LBRACE Ident COMMA Ident COMMA Ident COMMA WILD -## -## Ends in an error in state: 479. -## -## nsepseq(field_assignment,COMMA) -> field_assignment COMMA . nsepseq(field_assignment,COMMA) [ RBRACE ] -## seq(__anonymous_0(field_assignment,COMMA)) -> field_assignment COMMA . seq(__anonymous_0(field_assignment,COMMA)) [ RBRACE ] -## -## The known suffix of the stack is as follows: -## field_assignment COMMA -## - - - -interactive_expr: LBRACE Ident COMMA Ident COMMA WILD -## -## Ends in an error in state: 475. -## -## nsepseq(field_assignment,COMMA) -> field_assignment COMMA . nsepseq(field_assignment,COMMA) [ RBRACE ] -## nseq(__anonymous_0(field_assignment,COMMA)) -> field_assignment COMMA . seq(__anonymous_0(field_assignment,COMMA)) [ RBRACE ] -## -## The known suffix of the stack is as follows: -## field_assignment COMMA -## - - - -interactive_expr: LBRACE Ident COMMA Ident WILD -## -## Ends in an error in state: 470. -## -## field_assignment -> Ident . [ RBRACE COMMA ] -## field_assignment -> Ident . COLON expr [ RBRACE COMMA ] -## -## The known suffix of the stack is as follows: -## Ident -## - - - -interactive_expr: LBRACE Ident COMMA WILD -## -## Ends in an error in state: 469. -## -## sequence_or_record_in -> field_assignment COMMA . sep_or_term_list(field_assignment,COMMA) [ RBRACE ] -## -## The known suffix of the stack is as follows: -## field_assignment COMMA -## - - - interactive_expr: LBRACE Ident WILD ## ## Ends in an error in state: 463. ## ## common_expr -> Ident . [ TIMES SLASH SEMI RBRACE PLUS Or NE Mod MINUS LT LPAR LE GT GE EQEQ COLON CAT BOOL_OR BOOL_AND ARROW ] -## field_assignment -> Ident . [ COMMA ] -## field_assignment -> Ident . COLON expr [ COMMA ] +## field_assignment -> Ident . COLON expr [ RBRACE COMMA ] ## projection -> Ident . selection [ TIMES SLASH SEMI RBRACE PLUS Or NE Mod MINUS LT LPAR LE GT GE EQEQ COLON CAT BOOL_OR BOOL_AND ARROW ] ## ## The known suffix of the stack is as follows: @@ -1340,20 +1341,7 @@ interactive_expr: LBRACE VBAR interactive_expr: LBRACE WILD SEMI VBAR ## -## Ends in an error in state: 482. -## -## option(SEMI) -> SEMI . [ RBRACE ] -## sequence_or_record_in -> expr_with_let_expr SEMI . sep_or_term_list(expr_with_let_expr,SEMI) [ RBRACE ] -## -## The known suffix of the stack is as follows: -## expr_with_let_expr SEMI -## - - - -interactive_expr: LBRACE WILD SEMI WILD SEMI VBAR -## -## Ends in an error in state: 487. +## Ends in an error in state: 488. ## ## nsepseq(expr_with_let_expr,SEMI) -> expr_with_let_expr SEMI . nsepseq(expr_with_let_expr,SEMI) [ RBRACE ] ## nseq(__anonymous_0(expr_with_let_expr,SEMI)) -> expr_with_let_expr SEMI . seq(__anonymous_0(expr_with_let_expr,SEMI)) [ RBRACE ] @@ -1364,9 +1352,9 @@ interactive_expr: LBRACE WILD SEMI WILD SEMI VBAR -interactive_expr: LBRACE WILD SEMI WILD SEMI WILD SEMI VBAR +interactive_expr: LBRACE WILD SEMI WILD SEMI VBAR ## -## Ends in an error in state: 491. +## Ends in an error in state: 492. ## ## nsepseq(expr_with_let_expr,SEMI) -> expr_with_let_expr SEMI . nsepseq(expr_with_let_expr,SEMI) [ RBRACE ] ## seq(__anonymous_0(expr_with_let_expr,SEMI)) -> expr_with_let_expr SEMI . seq(__anonymous_0(expr_with_let_expr,SEMI)) [ RBRACE ] @@ -1377,9 +1365,9 @@ interactive_expr: LBRACE WILD SEMI WILD SEMI WILD SEMI VBAR -interactive_expr: LBRACE WILD SEMI WILD SEMI WILD VBAR +interactive_expr: LBRACE WILD SEMI WILD VBAR ## -## Ends in an error in state: 490. +## Ends in an error in state: 491. ## ## nsepseq(expr_with_let_expr,SEMI) -> expr_with_let_expr . [ RBRACE ] ## nsepseq(expr_with_let_expr,SEMI) -> expr_with_let_expr . SEMI nsepseq(expr_with_let_expr,SEMI) [ RBRACE ] @@ -1410,9 +1398,9 @@ interactive_expr: LBRACE WILD SEMI WILD SEMI WILD VBAR -interactive_expr: LBRACE WILD SEMI WILD VBAR +interactive_expr: LBRACE WILD VBAR ## -## Ends in an error in state: 486. +## Ends in an error in state: 487. ## ## nsepseq(expr_with_let_expr,SEMI) -> expr_with_let_expr . [ RBRACE ] ## nsepseq(expr_with_let_expr,SEMI) -> expr_with_let_expr . SEMI nsepseq(expr_with_let_expr,SEMI) [ RBRACE ] @@ -1443,38 +1431,6 @@ interactive_expr: LBRACE WILD SEMI WILD VBAR -interactive_expr: LBRACE WILD VBAR -## -## Ends in an error in state: 481. -## -## sequence_or_record_in -> expr_with_let_expr . SEMI sep_or_term_list(expr_with_let_expr,SEMI) [ RBRACE ] -## sequence_or_record_in -> expr_with_let_expr . option(SEMI) [ RBRACE ] -## -## The known suffix of the stack is as follows: -## expr_with_let_expr -## -## WARNING: This example involves spurious reductions. -## This implies that, although the LR(1) items shown above provide an -## accurate view of the past (what has been recognized so far), they -## may provide an INCOMPLETE view of the future (what was expected next). -## In state 155, spurious reduction of production call_expr_level_in -> core_expr -## In state 173, spurious reduction of production option(type_annotation_simple) -> -## In state 174, spurious reduction of production call_expr_level -> call_expr_level_in option(type_annotation_simple) -## In state 175, spurious reduction of production unary_expr_level -> call_expr_level -## In state 128, spurious reduction of production mult_expr_level -> unary_expr_level -## In state 152, spurious reduction of production add_expr_level -> mult_expr_level -## In state 183, spurious reduction of production cat_expr_level -> add_expr_level -## In state 204, spurious reduction of production comp_expr_level -> cat_expr_level -## In state 211, spurious reduction of production conj_expr_level -> comp_expr_level -## In state 218, spurious reduction of production disj_expr_level -> conj_expr_level -## In state 165, spurious reduction of production base_expr(expr) -> disj_expr_level -## In state 222, spurious reduction of production base_cond__open(expr) -> base_expr(expr) -## In state 223, spurious reduction of production expr -> base_cond__open(expr) -## In state 400, spurious reduction of production expr_with_let_expr -> expr -## - - - interactive_expr: LBRACKET VBAR ## ## Ends in an error in state: 91. @@ -4149,4 +4105,5 @@ contract: WILD ## ## - \ No newline at end of file + + diff --git a/src/test/contracts/single_record_item.religo b/src/test/contracts/single_record_item.religo new file mode 100644 index 000000000..9a7216624 --- /dev/null +++ b/src/test/contracts/single_record_item.religo @@ -0,0 +1,5 @@ +type p = {x: int} + +let o = (p: int): p => { + x: p +} \ No newline at end of file diff --git a/src/test/integration_tests.ml b/src/test/integration_tests.ml index c1929f55b..fbda27826 100644 --- a/src/test/integration_tests.ml +++ b/src/test/integration_tests.ml @@ -2325,6 +2325,10 @@ let tuple_list_religo () : unit result = let%bind _ = retype_file "./contracts/tuple_list.religo" in ok () +let single_record_expr_religo () : unit result = + let%bind _ = retype_file "./contracts/single_record_item.religo" in + ok () + let loop_bugs_ligo () : unit result = let%bind program = type_file "./contracts/loop_bugs.ligo" in let input = e_unit () in @@ -2512,4 +2516,5 @@ let main = test_suite "Integration (End to End)" [ test "no semicolon (religo)" no_semicolon_religo ; test "loop_bugs (ligo)" loop_bugs_ligo ; test "tuple_list (religo)" tuple_list_religo ; + test "single_record_expr (religo)" single_record_expr_religo ; ]