From 59eda906b07577e98c2c15a528209d62f23fccbe Mon Sep 17 00:00:00 2001 From: Rudi Grinberg Date: Mon, 23 Apr 2018 23:47:55 +0700 Subject: [PATCH] Convert locs to sexps Done in a crappy way for now because the converters haven't yet made it to Usexp --- src/loc.ml | 22 ++++++++++++++++++++++ src/loc.mli | 2 ++ 2 files changed, 24 insertions(+) diff --git a/src/loc.ml b/src/loc.ml index 96450d27..633c294c 100644 --- a/src/loc.ml +++ b/src/loc.ml @@ -5,6 +5,28 @@ type t = Usexp.Loc.t = ; stop : Lexing.position } +(* TODO get rid of all this stuff once this parsing code moves to Usexp and + there will be no circular dependency *) +let int n = Usexp.Atom (Usexp.Atom.of_int n) +let string = Usexp.atom_or_quoted_string +let record l = + let open Usexp in + List (List.map l ~f:(fun (n, v) -> List [Atom(Atom.of_string n); v])) + +let sexp_of_position_no_file (p : Lexing.position) = + record + [ "pos_lnum", int p.pos_lnum + ; "pos_bol", int p.pos_bol + ; "pos_cnum", int p.pos_cnum + ] + +let sexp_of_t t = + record (* TODO handle when pos_fname differs *) + [ "pos_fname", string t.start.pos_fname + ; "start", sexp_of_position_no_file t.start + ; "stop", sexp_of_position_no_file t.stop + ] + let of_lexbuf lb = { start = Lexing.lexeme_start_p lb ; stop = Lexing.lexeme_end_p lb diff --git a/src/loc.mli b/src/loc.mli index d47e0f40..7a0bea74 100644 --- a/src/loc.mli +++ b/src/loc.mli @@ -3,6 +3,8 @@ type t = Usexp.Loc.t = ; stop : Lexing.position } +val sexp_of_t : t -> Usexp.t + val of_lexbuf : Lexing.lexbuf -> t exception Error of t * string