From d70b93a252a79889d510b22f59e8bd29beaf4b86 Mon Sep 17 00:00:00 2001 From: Gabriel Scherer Date: Sun, 15 Sep 2013 11:36:47 +0000 Subject: [PATCH] ocamlbuild: a short location printing implementation git-svn-id: http://caml.inria.fr/svn/ocaml/trunk@14146 f963ae5c-01c2-4b8c-9fe0-0dff7051ff02 --- ocamlbuild/loc.ml | 30 ++++++++++++++++++++++++++++++ ocamlbuild/loc.mli | 6 ++++++ ocamlbuild/ocamlbuild_pack.mlpack | 1 + 3 files changed, 37 insertions(+) create mode 100644 ocamlbuild/loc.ml create mode 100644 ocamlbuild/loc.mli diff --git a/ocamlbuild/loc.ml b/ocamlbuild/loc.ml new file mode 100644 index 000000000..2bf3900e8 --- /dev/null +++ b/ocamlbuild/loc.ml @@ -0,0 +1,30 @@ +(* it's not worth adding a dependency on parsing/location.ml(i) or + compilerlibs just to support location printing, so we re-implement + that here *) + +open Lexing + +type location = position * position + +let file loc = loc.pos_fname +let line loc = loc.pos_lnum +let char loc = loc.pos_cnum - loc.pos_bol + +let print_loc ppf (start, end_) = + let open Format in + let print one_or_two ppf (start_num, end_num) = + if one_or_two then fprintf ppf " %d" start_num + else fprintf ppf "s %d-%d" start_num end_num in + fprintf ppf "File %S, line%a, character%a:@." + (file start) + (print (line start = line end_)) + (line start, line end_) + (print (line start = line end_ && char start = char end_)) + (char start, char end_) + +let of_lexbuf lexbuf = + (lexbuf.lex_start_p, lexbuf.lex_curr_p) + +let print_loc_option ppf = function + | None -> () + | Some loc -> print_loc ppf loc diff --git a/ocamlbuild/loc.mli b/ocamlbuild/loc.mli new file mode 100644 index 000000000..9ed842ef2 --- /dev/null +++ b/ocamlbuild/loc.mli @@ -0,0 +1,6 @@ +type location = Lexing.position * Lexing.position + +val print_loc : Format.formatter -> location -> unit +val print_loc_option : Format.formatter -> location option -> unit + +val of_lexbuf : Lexing.lexbuf -> location diff --git a/ocamlbuild/ocamlbuild_pack.mlpack b/ocamlbuild/ocamlbuild_pack.mlpack index 09dc4e026..83f1065f4 100644 --- a/ocamlbuild/ocamlbuild_pack.mlpack +++ b/ocamlbuild/ocamlbuild_pack.mlpack @@ -1,3 +1,4 @@ +Loc Log My_unix My_std