Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unnecessary surrounding parentheses for immediate objects #10441

Merged
merged 3 commits into from
Jun 5, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 11 additions & 4 deletions boot/menhir/menhirLib.ml
Expand Up @@ -1509,8 +1509,7 @@ module Make (T : TABLE) = struct

(* In the legacy strategy, we call [reduce] instead of [announce_reduce],
apparently in an attempt to hide the reduction steps performed during
error handling. This seems inconsistent, as the default reduction steps
are still announced. In the simplified strategy, all reductions are
error handling. In the simplified strategy, all reductions steps are
announced. *)

match strategy with
Expand Down Expand Up @@ -1546,7 +1545,15 @@ module Make (T : TABLE) = struct
else begin

(* The stack is nonempty. Pop a cell, updating the current state
with that found in the popped cell, and try again. *)
to the state [cell.state] found in the popped cell, and continue
error handling there. *)

(* I note that if the new state [cell.state] has a default reduction,
then it is ignored. It is unclear whether this is intentional. It
could be a good thing, as it avoids a scenario where the parser
diverges by repeatedly popping, performing a default reduction of
an epsilon production, popping, etc. Still, the question of whether
to obey default reductions while error handling seems obscure. *)

let env = { env with
stack = next;
Expand Down Expand Up @@ -3785,5 +3792,5 @@ module MakeEngineTable (T : TableFormat.TABLES) = struct
end
end
module StaticVersion = struct
let require_20201216 = ()
let require_20210419 = ()
end
2 changes: 1 addition & 1 deletion boot/menhir/menhirLib.mli
Expand Up @@ -1803,5 +1803,5 @@ module MakeEngineTable
and type nonterminal = int
end
module StaticVersion : sig
val require_20201216: unit
val require_20210419: unit
end
5,606 changes: 2,780 additions & 2,826 deletions boot/menhir/parser.ml

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions parsing/parser.mly
Expand Up @@ -824,7 +824,7 @@ The precedences must be listed from low to high.
%nonassoc below_DOT
%nonassoc DOT DOTOP
/* Finally, the first tokens of simple_expr are above everything else. */
%nonassoc BACKQUOTE BANG BEGIN CHAR FALSE FLOAT INT
%nonassoc BACKQUOTE BANG BEGIN CHAR FALSE FLOAT INT OBJECT
LBRACE LBRACELESS LBRACKET LBRACKETBAR LIDENT LPAREN
NEW PREFIXOP STRING TRUE UIDENT
LBRACKETPERCENT QUOTED_STRING_EXPR
Expand Down Expand Up @@ -2314,10 +2314,6 @@ expr:
{ Pexp_assert $3, $2 }
| LAZY ext_attributes simple_expr %prec below_HASH
{ Pexp_lazy $3, $2 }
| OBJECT ext_attributes class_structure END
{ Pexp_object $3, $2 }
| OBJECT ext_attributes class_structure error
{ unclosed "object" $loc($1) "end" $loc($4) }
;
%inline expr_:
| simple_expr nonempty_llist(labeled_simple_expr)
Expand Down Expand Up @@ -2370,6 +2366,10 @@ simple_expr:
{ Pexp_constraint (ghexp ~loc:$sloc (Pexp_pack $4), $6), $3 }
| LPAREN MODULE ext_attributes module_expr COLON error
{ unclosed "(" $loc($1) ")" $loc($6) }
| OBJECT ext_attributes class_structure END
{ Pexp_object $3, $2 }
| OBJECT ext_attributes class_structure error
{ unclosed "object" $loc($1) "end" $loc($4) }
;
%inline simple_expr_:
| mkrhs(val_longident)
Expand Down
208 changes: 208 additions & 0 deletions testsuite/tests/parsetree/locations_test.compilers.reference
Expand Up @@ -1100,4 +1100,212 @@ Ptop_def
]

val x : int = 42
Ptop_def
[
structure_item (//toplevel//[3,56+0]..[3,56+31])
Pstr_value Nonrec
[
<def>
pattern (//toplevel//[3,56+4]..[3,56+5])
Ppat_var "x" (//toplevel//[3,56+4]..[3,56+5])
expression (//toplevel//[3,56+8]..[3,56+31])
Pexp_object
class_structure
pattern (//toplevel//[3,56+14]..[3,56+14]) ghost
Ppat_any
[
class_field (//toplevel//[3,56+15]..[3,56+27])
Pcf_method Public
"f" (//toplevel//[3,56+22]..[3,56+23])
Concrete Fresh
expression (//toplevel//[3,56+26]..[3,56+27]) ghost
Pexp_poly
expression (//toplevel//[3,56+26]..[3,56+27])
Pexp_constant PConst_int (1,None)
None
]
]
]

val x : < f : int > = <obj>
Ptop_def
[
structure_item (//toplevel//[1,0+0]..[1,0+35])
Pstr_value Nonrec
[
<def>
pattern (//toplevel//[1,0+4]..[1,0+5])
Ppat_var "x" (//toplevel//[1,0+4]..[1,0+5])
expression (//toplevel//[1,0+8]..[1,0+35])
Pexp_send "f"
expression (//toplevel//[1,0+8]..[1,0+31])
Pexp_object
class_structure
pattern (//toplevel//[1,0+14]..[1,0+14]) ghost
Ppat_any
[
class_field (//toplevel//[1,0+15]..[1,0+27])
Pcf_method Public
"f" (//toplevel//[1,0+22]..[1,0+23])
Concrete Fresh
expression (//toplevel//[1,0+26]..[1,0+27]) ghost
Pexp_poly
expression (//toplevel//[1,0+26]..[1,0+27])
Pexp_constant PConst_int (1,None)
None
]
]
]

val x : int = 1
Ptop_def
[
structure_item (//toplevel//[1,0+0]..[1,0+36])
Pstr_value Nonrec
[
<def>
pattern (//toplevel//[1,0+4]..[1,0+5])
Ppat_var "x" (//toplevel//[1,0+4]..[1,0+5])
expression (//toplevel//[1,0+8]..[1,0+36])
Pexp_construct "Some" (//toplevel//[1,0+8]..[1,0+12])
Some
expression (//toplevel//[1,0+13]..[1,0+36])
Pexp_object
class_structure
pattern (//toplevel//[1,0+19]..[1,0+19]) ghost
Ppat_any
[
class_field (//toplevel//[1,0+20]..[1,0+32])
Pcf_method Public
"f" (//toplevel//[1,0+27]..[1,0+28])
Concrete Fresh
expression (//toplevel//[1,0+31]..[1,0+32]) ghost
Pexp_poly
expression (//toplevel//[1,0+31]..[1,0+32])
Pexp_constant PConst_int (1,None)
None
]
]
]

val x : < f : int > option = Some <obj>
Ptop_def
[
structure_item (//toplevel//[1,0+0]..[1,0+40])
Pstr_value Nonrec
[
<def>
pattern (//toplevel//[1,0+4]..[1,0+5])
Ppat_var "x" (//toplevel//[1,0+4]..[1,0+5])
expression (//toplevel//[1,0+8]..[1,0+40])
Pexp_construct "Some" (//toplevel//[1,0+8]..[1,0+12])
Some
expression (//toplevel//[1,0+13]..[1,0+40])
Pexp_send "f"
expression (//toplevel//[1,0+13]..[1,0+36])
Pexp_object
class_structure
pattern (//toplevel//[1,0+19]..[1,0+19]) ghost
Ppat_any
[
class_field (//toplevel//[1,0+20]..[1,0+32])
Pcf_method Public
"f" (//toplevel//[1,0+27]..[1,0+28])
Concrete Fresh
expression (//toplevel//[1,0+31]..[1,0+32]) ghost
Pexp_poly
expression (//toplevel//[1,0+31]..[1,0+32])
Pexp_constant PConst_int (1,None)
None
]
]
]

val x : int option = Some 1
Ptop_def
[
structure_item (//toplevel//[2,1+0]..[5,76+12])
Pstr_eval
expression (//toplevel//[2,1+0]..[5,76+12])
Pexp_let Nonrec
[
<def>
pattern (//toplevel//[2,1+4]..[2,1+5])
Ppat_var "f" (//toplevel//[2,1+4]..[2,1+5])
expression (//toplevel//[2,1+6]..[2,1+15]) ghost
Pexp_fun
Nolabel
None
pattern (//toplevel//[2,1+6]..[2,1+7])
Ppat_var "x" (//toplevel//[2,1+6]..[2,1+7])
expression (//toplevel//[2,1+8]..[2,1+15]) ghost
Pexp_fun
Nolabel
None
pattern (//toplevel//[2,1+8]..[2,1+9])
Ppat_var "y" (//toplevel//[2,1+8]..[2,1+9])
expression (//toplevel//[2,1+10]..[2,1+15]) ghost
Pexp_fun
Nolabel
None
pattern (//toplevel//[2,1+10]..[2,1+11])
Ppat_var "z" (//toplevel//[2,1+10]..[2,1+11])
expression (//toplevel//[2,1+14]..[2,1+15])
Pexp_ident "x" (//toplevel//[2,1+14]..[2,1+15])
]
expression (//toplevel//[3,20+0]..[5,76+12])
Pexp_apply
expression (//toplevel//[3,20+0]..[3,20+1])
Pexp_ident "f" (//toplevel//[3,20+0]..[3,20+1])
[
<arg>
Nolabel
expression (//toplevel//[3,20+2]..[3,20+25])
Pexp_object
class_structure
pattern (//toplevel//[3,20+8]..[3,20+8]) ghost
Ppat_any
[
class_field (//toplevel//[3,20+9]..[3,20+21])
Pcf_method Public
"f" (//toplevel//[3,20+16]..[3,20+17])
Concrete Fresh
expression (//toplevel//[3,20+20]..[3,20+21]) ghost
Pexp_poly
expression (//toplevel//[3,20+20]..[3,20+21])
Pexp_constant PConst_int (1,None)
None
]
<arg>
Nolabel
expression (//toplevel//[4,46+2]..[4,46+29])
Pexp_send "f"
expression (//toplevel//[4,46+2]..[4,46+25])
Pexp_object
class_structure
pattern (//toplevel//[4,46+8]..[4,46+8]) ghost
Ppat_any
[
class_field (//toplevel//[4,46+9]..[4,46+21])
Pcf_method Public
"f" (//toplevel//[4,46+16]..[4,46+17])
Concrete Fresh
expression (//toplevel//[4,46+20]..[4,46+21]) ghost
Pexp_poly
expression (//toplevel//[4,46+20]..[4,46+21])
Pexp_constant PConst_int (1,None)
None
]
<arg>
Nolabel
expression (//toplevel//[5,76+2]..[5,76+12])
Pexp_object
class_structure
pattern (//toplevel//[5,76+8]..[5,76+8]) ghost
Ppat_any
[]
]
]

- : < f : int > = <obj>

12 changes: 12 additions & 0 deletions testsuite/tests/parsetree/locations_test.ml
Expand Up @@ -110,3 +110,15 @@ let x =
42
(** Another docstring attached to x. *)
;;

(* No surrounding parentheses for immediate objects *)
let x = object method f = 1 end;;
let x = object method f = 1 end # f;;
let x = Some object method f = 1 end;;
let x = Some object method f = 1 end # f;;

let f x y z = x in
f object method f = 1 end
object method f = 1 end # f
object end
;;
12 changes: 12 additions & 0 deletions testsuite/tests/parsetree/source.ml
Expand Up @@ -7425,3 +7425,15 @@ module M = struct
let q =
let%foo x and y and z in (x,y,z)
end

(* No surrounding parentheses for immediate objects *)
let x = object method f = 1 end;;
let x = object method f = 1 end # f;;
let x = Some object method f = 1 end;;
let x = Some object method f = 1 end # f;;

let f x y z = x in
f object method f = 1 end
object method f = 1 end # f
object end
;;