Skip to content

Commit 663ac33

Browse files
committed
fix the location of section-mismatch error positions
1 parent 750f7f0 commit 663ac33

File tree

4 files changed

+21
-19
lines changed

4 files changed

+21
-19
lines changed

bin/test/errors/parsing-errors.t

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ Mismatch between section-start and section-end:
9696
$ echo "{{#foo}} {{.}} {{/bar}}" > $PROBLEM
9797
$ mustache foo.json $PROBLEM
9898
Template parse error:
99-
File "foo-bar.mustache", lines 1-2, characters 23-0:
99+
File "foo-bar.mustache", line 1, characters 0-23:
100100
Section mismatch: {{#foo}} is closed by {{/bar}}.
101101
[3]
102102
@@ -111,7 +111,7 @@ Mismatch between section-start and section-end:
111111
$ echo "{{#bar}} {{#foo}} {{.}} {{/bar}} {{/foo}}" > $PROBLEM
112112
$ mustache foo.json $PROBLEM
113113
Template parse error:
114-
File "wrong-nesting.mustache", lines 1-2, characters 41-0:
114+
File "wrong-nesting.mustache", line 1, characters 9-32:
115115
Section mismatch: {{#foo}} is closed by {{/bar}}.
116116
[3]
117117

lib/mustache.ml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,10 @@ and template_parse_error_kind =
157157
exception Template_parse_error of template_parse_error
158158

159159
let parse_lx (lexbuf: Lexing.lexbuf) : Locs.t =
160-
let raise_err lexbuf kind =
161-
let loc =
162-
let open Lexing in
163-
{ loc_start = lexbuf.lex_start_p; loc_end = lexbuf.lex_curr_p } in
160+
let loc_of lexbuf =
161+
let open Lexing in
162+
{ loc_start = lexbuf.lex_start_p; loc_end = lexbuf.lex_curr_p } in
163+
let raise_err loc kind =
164164
raise (Template_parse_error { loc; kind })
165165
in
166166
try
@@ -169,11 +169,11 @@ let parse_lx (lexbuf: Lexing.lexbuf) : Locs.t =
169169
Mustache_lexer.(handle_standalone mustache lexbuf)
170170
with
171171
| Mustache_lexer.Error msg ->
172-
raise_err lexbuf (Lexing msg)
172+
raise_err (loc_of lexbuf) (Lexing msg)
173173
| Mustache_parser.Error ->
174-
raise_err lexbuf Parsing
175-
| Mismatched_section { start_name; end_name } ->
176-
raise_err lexbuf (Mismatched_section { start_name; end_name })
174+
raise_err (loc_of lexbuf) Parsing
175+
| Mismatched_section { loc; start_name; end_name } ->
176+
raise_err loc (Mismatched_section { start_name; end_name })
177177

178178
let of_string s = parse_lx (Lexing.from_string s)
179179

lib/mustache_parser.mly

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,17 @@
2424
open Mustache_types
2525
open Mustache_types.Locs
2626
27-
let parse_section start_name end_name contents =
27+
let mkloc (start_pos, end_pos) =
28+
{ loc_start = start_pos;
29+
loc_end = end_pos }
30+
31+
let parse_section loc start_name end_name contents =
2832
if start_name <> end_name then
29-
raise (Mismatched_section { start_name; end_name });
33+
raise (Mismatched_section { loc = mkloc loc; start_name; end_name });
3034
{ contents; name = start_name }
3135

32-
let with_loc (startpos, endpos) desc =
33-
let loc =
34-
{ loc_start = startpos;
35-
loc_end = endpos } in
36-
{ loc; desc }
36+
let with_loc loc desc =
37+
{ loc = mkloc loc; desc }
3738
%}
3839

3940
%token EOF
@@ -57,13 +58,13 @@ section:
5758
e = mustache_expr
5859
se = CLOSE_SECTION {
5960
with_loc $sloc
60-
(Inverted_section (parse_section ss se e))
61+
(Inverted_section (parse_section $sloc ss se e))
6162
}
6263
| ss = OPEN_SECTION
6364
e = mustache_expr
6465
se = CLOSE_SECTION {
6566
with_loc $sloc
66-
(Section (parse_section ss se e))
67+
(Section (parse_section $sloc ss se e))
6768
}
6869

6970
mustache_element:

lib/mustache_types.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ end
8585
(* this exception is used internally in the parser,
8686
never exposed to users *)
8787
exception Mismatched_section of {
88+
loc: loc;
8889
start_name: dotted_name;
8990
end_name: dotted_name;
9091
}

0 commit comments

Comments
 (0)