From c2f8d6811bc5537b77855f5780e6f059149b5f93 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Fri, 6 Dec 2019 20:21:46 -0600 Subject: [PATCH] fix(sexp): set location properly when parsing a file --- src/sexp/CCSexp.ml | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/sexp/CCSexp.ml b/src/sexp/CCSexp.ml index a533ebfe..a0c12257 100644 --- a/src/sexp/CCSexp.ml +++ b/src/sexp/CCSexp.ml @@ -241,16 +241,24 @@ module Make(Sexp : SEXP) = struct | Yield x -> Result.Ok x | Fail s -> Result.Error s - let parse_chan ic : sexp or_error = + let set_file_ ?file buf = + let open Lexing in + match file with + | Some s -> buf.lex_start_p <- {buf.lex_start_p with pos_fname=s} + | None -> () + + let parse_chan_ ?file ic : sexp or_error = let buf = Lexing.from_channel ic in + set_file_ ?file buf; let d = Decoder.of_lexbuf buf in match Decoder.next d with | End -> Result.Error "unexpected end of file" | Yield x -> Result.Ok x | Fail e -> Result.Error e - let parse_chan_list ic = + let parse_chan_list_ ?file ic = let buf = Lexing.from_channel ic in + set_file_ ?file buf; let d = Decoder.of_lexbuf buf in let rec iter acc = match Decoder.next d with | End -> Result.Ok (List.rev acc) @@ -259,6 +267,9 @@ module Make(Sexp : SEXP) = struct in iter [] + let parse_chan ic = parse_chan_ ic + let parse_chan_list ic = parse_chan_list_ ic + let parse_chan_gen ic = let buf = Lexing.from_channel ic in let d = Decoder.of_lexbuf buf in @@ -267,9 +278,9 @@ module Make(Sexp : SEXP) = struct | Fail e -> Some (Result.Error e) | Yield x -> Some (Result.Ok x) - let parse_file filename = _with_in filename parse_chan + let parse_file filename = _with_in filename (parse_chan_ ~file:filename) - let parse_file_list filename = _with_in filename parse_chan_list + let parse_file_list filename = _with_in filename (parse_chan_list_ ~file:filename) end type t = [