Skip to content

Commit 38790f1

Browse files
committed
Specific location printing for dummy locations
Before: Line 0, character -1: the variable title is missing. After: (At unknown location): the variable title is missing.
1 parent c7c090b commit 38790f1

File tree

1 file changed

+26
-16
lines changed

1 file changed

+26
-16
lines changed

lib/mustache.ml

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -180,25 +180,35 @@ let of_string s = parse_lx (Lexing.from_string s)
180180
let pp_loc ppf loc =
181181
let open Lexing in
182182
let fname = loc.loc_start.pos_fname in
183+
let is_dummy_pos pos = pos.pos_lnum < 0 || pos.pos_cnum < 0 in
183184
let extract pos = (pos.pos_lnum, pos.pos_cnum - pos.pos_bol) in
184-
let (start_line, start_col) = extract loc.loc_start in
185-
let (end_line, end_col) = extract loc.loc_end in
185+
let loc_start, loc_end =
186+
let loc_start = loc.loc_start in
187+
let loc_end = loc.loc_end in
188+
let orelse p1 p2 = if not (is_dummy_pos p1) then p1 else p2 in
189+
orelse loc_start loc_end, orelse loc_end loc_start in
186190
let p ppf = Format.fprintf ppf in
187-
let pp_range ppf (start, end_) =
188-
if start = end_ then
189-
p ppf " %d" start
191+
if is_dummy_pos loc_start && is_dummy_pos loc_end then
192+
p ppf "(At unknown location)"
193+
else begin
194+
let (start_line, start_col) = extract loc.loc_start in
195+
let (end_line, end_col) = extract loc.loc_end in
196+
let pp_range ppf (start, end_) =
197+
if start = end_ then
198+
p ppf " %d" start
199+
else
200+
p ppf "s %d-%d" start end_
201+
in
202+
p ppf "@[";
203+
begin if fname <> "" then
204+
p ppf "File %S,@ l" fname
190205
else
191-
p ppf "s %d-%d" start end_
192-
in
193-
p ppf "@[";
194-
begin if fname <> "" then
195-
p ppf "File %S,@ l" fname
196-
else
197-
p ppf "L"
198-
end;
199-
p ppf "ine%a,@ character%a"
200-
pp_range (start_line, end_line)
201-
pp_range (start_col, end_col)
206+
p ppf "L"
207+
end;
208+
p ppf "ine%a,@ character%a"
209+
pp_range (start_line, end_line)
210+
pp_range (start_col, end_col)
211+
end
202212

203213
let pp_template_parse_error ppf ({ loc; kind; } : template_parse_error) =
204214
let p ppf = Format.fprintf ppf in

0 commit comments

Comments
 (0)