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 <nathan.p.rebours@gmail.com>
  • Loading branch information
NathanReb committed Jan 31, 2024
1 parent 3ee3d52 commit fa75e23
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
unreleased
-------------------

- Fix `Longident.parse` so it properly handles unparenthesized dotted operators
such as `+.` or `*.`. (#111, @rgrinberg, @NathanReb)

- raising an exception does no longer cancel the whole context free phase(#453, @burnleydev1)

- Sort embedded errors that are appended to the AST by location so the compiler
Expand Down
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 -> (
if String.length s < 1 then invalid ();
let open_par = String.index_opt s '(' in
let close_par = String.index_opt s ')' in
match (s.[0], open_par, close_par) with
| ('A' .. 'Z' | 'a' .. 'z' | '_'), None, None -> parse_simple s
| _, None, None -> Lident s (* This is a raw operator, no module path *)
| _, 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
4 changes: 1 addition & 3 deletions test/base/test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,9 @@ let _ = convert_longident ")"
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 fa75e23

Please sign in to comment.