Skip to content

Commit

Permalink
Fix Longident parsing of unparenthesized dotted operators
Browse files Browse the repository at this point in the history
Signed-off-by: Nathan Rebours <[email protected]>
  • Loading branch information
NathanReb committed Jan 31, 2024
1 parent 3ee3d52 commit 8b378e7
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
12 changes: 8 additions & 4 deletions src/longident.ml
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,14 @@ let parse s =
let invalid () =
invalid_arg (Printf.sprintf "Ppxlib.Longident.parse: %S" s)
in
match (String.index_opt s '(', String.rindex_opt s ')') with
| None, None -> parse_simple s
| None, _ | _, None -> invalid ()
| Some l, Some r -> (
let open_par = String.index_opt s '(' in
let close_par = String.index_opt s ')' in
let first_char_is_normal = is_normal_ident_char s.[0] in
match (first_char_is_normal, open_par, close_par) with
| false, None, None -> Lident s (* This is a raw operator, no module path *)
| true, None, None -> parse_simple s
| _, None, _ | _, _, None -> invalid ()
| _, Some l, Some r -> (
if Int.(r <> String.length s - 1) then invalid ();
let group =
if Int.(r = l + 1) then "()"
Expand Down
3 changes: 1 addition & 2 deletions test/base/test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,7 @@ Exception: Invalid_argument "Ppxlib.Longident.parse: \")\"".
(* FIXME this is a bug *)
let _ = convert_longident "+."
[%%expect{|
- : string * longident =
("( + ).", Ppxlib.Longident.Ldot (Ppxlib.Longident.Lident "+", ""))
- : string * longident = ("( +. )", Ppxlib.Longident.Lident "+.")
|}]

let _ = convert_longident "(+.)"
Expand Down

0 comments on commit 8b378e7

Please sign in to comment.