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

Add tests for parsing longident's with . #111

Merged
merged 4 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
22 changes: 22 additions & 0 deletions test/base/test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,28 @@ let _ = convert_longident ")"
Exception: Invalid_argument "Ppxlib.Longident.parse: \")\"".
|}]

let _ = convert_longident "+."
[%%expect{|
- : string * longident = ("( +. )", Ppxlib.Longident.Lident "+.")
|}]

let _ = convert_longident "(+.)"
[%%expect{|
- : string * longident = ("( +. )", Ppxlib.Longident.Lident "+.")
|}]

let _ = convert_longident "Foo.(+.)"
[%%expect{|
- : string * longident =
("Foo.( +. )", Ppxlib.Longident.Ldot (Ppxlib.Longident.Lident "Foo", "+."))
|}]

let _ = convert_longident "Foo.( *. )"
[%%expect{|
- : string * longident =
("Foo.( *. )", Ppxlib.Longident.Ldot (Ppxlib.Longident.Lident "Foo", "*."))
|}]

let _ = Ppxlib.Code_path.(file_path @@ top_level ~file_path:"dir/main.ml")
[%%expect{|
- : string = "dir/main.ml"
Expand Down
Loading