diff --git a/README.md b/README.md index 145d611..0db814c 100644 --- a/README.md +++ b/README.md @@ -3,14 +3,10 @@ [![CI Status](https://github.com/mbarbin/loc/workflows/ci/badge.svg)](https://github.com/mbarbin/loc/actions/workflows/ci.yml) [![Coverage Status](https://coveralls.io/repos/github/mbarbin/loc/badge.svg?branch=main)](https://coveralls.io/github/mbarbin/loc?branch=main) -`Loc.it` representing a range of lexing positions from a parsed file. It may be -used to decorate AST nodes built by parsers so as to allow located error -messages during file processing (compilers, interpreters, etc.) +Loc is an OCaml library to manipulate code locations, which are ranges of lexing positions from a parsed file. It may be used to decorate AST nodes built by parsers so as to allow located error messages during file processing (compilers, interpreters, linters, refactor tools, etc.) -It is inspired by dune's `Loc.t`, and uses it under the hood. The type equality -with `dune` is exposed for a better compatibility of packages that uses `loc`. +It is inspired by dune's `Loc.t`, and uses it under the hood. The type equality with `Stdune.Loc.t` is currently not exposed and `Stdune` not mentioned in the interface of `Loc`, with the aim of keeping the signature of `Loc` stable across potential internal changes in `Stdune`. ## Code documentation -The code documentation of the latest release is built with `odoc` and published -to `GitHub` pages [here](https://mbarbin.github.io/loc). +The code documentation of the latest release is built with `odoc` and published to `GitHub` pages [here](https://mbarbin.github.io/loc). diff --git a/dune-project b/dune-project index edb317b..8982737 100644 --- a/dune-project +++ b/dune-project @@ -21,21 +21,8 @@ (depends (ocaml (>= 5.2)) - (ocamlformat - (and - :with-dev-setup - (= 0.26.2))) - (bisect_ppx - (and - :with-dev-setup - (>= 2.8.3))) (fpath (>= 0.7.3)) - (ppx_js_style - (and - :with-dev-setup - (>= v0.17) - (< v0.18))) (sexplib0 (and (>= v0.17) diff --git a/loc.opam b/loc.opam index 0e861e1..719dd38 100644 --- a/loc.opam +++ b/loc.opam @@ -10,10 +10,7 @@ bug-reports: "https://github.com/mbarbin/loc/issues" depends: [ "dune" {>= "3.16"} "ocaml" {>= "5.2"} - "ocamlformat" {with-dev-setup & = "0.26.2"} - "bisect_ppx" {with-dev-setup & >= "2.8.3"} "fpath" {>= "0.7.3"} - "ppx_js_style" {with-dev-setup & >= "v0.17" & < "v0.18"} "sexplib0" {>= "v0.17" & < "v0.18"} "stdune" {>= "3.16"} "odoc" {with-doc} diff --git a/test/test__file_cache.ml b/test/test__file_cache.ml index 5456def..aaf408e 100644 --- a/test/test__file_cache.ml +++ b/test/test__file_cache.ml @@ -1,3 +1,10 @@ +let%expect_test "getters" = + let file_cache = Loc.File_cache.create ~path:(Fpath.v "foo.txt") ~file_contents:"" in + print_endline (Loc.File_cache.path file_cache |> Fpath.to_string); + [%expect {| foo.txt |}]; + () +;; + let%expect_test "negative" = let file_cache = Loc.File_cache.create ~path:(Fpath.v "foo.txt") ~file_contents:"" in require_does_raise [%here] (fun () -> Loc.in_file_line ~file_cache ~line:0); diff --git a/test/test__in_file.ml b/test/test__in_file.ml new file mode 100644 index 0000000..63bf7b4 --- /dev/null +++ b/test/test__in_file.ml @@ -0,0 +1,36 @@ +let loc = Loc.in_file ~path:(Fpath.v "file") + +let%expect_test "to_string" = + print_endline (Loc.to_string loc); + [%expect {| File "file", line 1, characters 0-0: |}]; + print_endline (Loc.to_file_colon_line loc); + [%expect {| file:1 |}]; + () +;; + +let%expect_test "path" = + print_endline (Loc.path loc |> Fpath.to_string); + [%expect {| file |}]; + () +;; + +let%expect_test "start_line" = + print_s [%sexp (Loc.start_line loc : int)]; + [%expect {| 1 |}]; + () +;; + +let%expect_test "is_none" = + print_s [%sexp (Loc.is_none loc : bool)]; + [%expect {| false |}]; + () +;; + +let%expect_test "range" = + print_s [%sexp (Loc.range loc : Loc.Range.t)]; + [%expect {| + ((start 0) + (stop 0)) + |}]; + () +;; diff --git a/test/test__in_file.mli b/test/test__in_file.mli new file mode 100644 index 0000000..e69de29 diff --git a/test/test__position.ml b/test/test__position.ml new file mode 100644 index 0000000..1e18706 --- /dev/null +++ b/test/test__position.ml @@ -0,0 +1,16 @@ +let p1 = [%here] +let p2 = [%here] +let equal_position (a : Lexing.position) (b : Lexing.position) = Stdlib.compare a b = 0 + +let%expect_test "equal" = + let r1 = Loc.create (p1, p2) in + require [%here] (equal_position (Loc.start r1) p1); + [%expect {||}]; + require [%here] (equal_position (Loc.stop r1) p2); + [%expect {||}]; + require [%here] (not (equal_position (Loc.start r1) p2)); + [%expect {||}]; + require [%here] (not (equal_position (Loc.stop r1) p1)); + [%expect {||}]; + () +;; diff --git a/test/test__position.mli b/test/test__position.mli new file mode 100644 index 0000000..e69de29 diff --git a/test/test__to_string.ml b/test/test__to_string.ml new file mode 100644 index 0000000..6a7623d --- /dev/null +++ b/test/test__to_string.ml @@ -0,0 +1,21 @@ +let%expect_test "1 line" = + let loc = + Loc.create + ( { Lexing.pos_fname = "file"; pos_lnum = 1; pos_cnum = 2; pos_bol = 0 } + , { Lexing.pos_fname = "file"; pos_lnum = 1; pos_cnum = 10; pos_bol = 0 } ) + in + print_endline (Loc.to_string loc); + [%expect {| File "file", line 1, characters 2-10: |}]; + () +;; + +let%expect_test "multiple lines" = + let loc = + Loc.create + ( { Lexing.pos_fname = "file"; pos_lnum = 1; pos_cnum = 2; pos_bol = 0 } + , { Lexing.pos_fname = "file"; pos_lnum = 3; pos_cnum = 35; pos_bol = 30 } ) + in + print_endline (Loc.to_string loc); + [%expect {| File "file", lines 1-3, characters 2-35: |}]; + () +;; diff --git a/test/test__to_string.mli b/test/test__to_string.mli new file mode 100644 index 0000000..e69de29