Skip to content

Commit c7c090b

Browse files
committed
mustache binary: include the input filename in locations
1 parent 9df6265 commit c7c090b

File tree

3 files changed

+51
-31
lines changed

3 files changed

+51
-31
lines changed

bin/mustache_cli.ml

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,15 @@ module Mustache = struct
33
include With_locations
44
end
55

6-
let apply_mustache json_data template_data =
6+
let apply_mustache ~json_data ~template_filename ~template_data =
77
let env = Ezjsonm.from_string json_data
88
and tmpl =
9-
try Mustache.of_string template_data
9+
let lexbuf = Lexing.from_string template_data in
10+
let () =
11+
let open Lexing in
12+
lexbuf.lex_curr_p <- { lexbuf.lex_curr_p with pos_fname = template_filename };
13+
in
14+
try Mustache.parse_lx lexbuf
1015
with Mustache.Template_parse_error err ->
1116
Format.eprintf "Template parse error:@\n%a@."
1217
Mustache.pp_template_parse_error err;
@@ -27,10 +32,10 @@ let load_file f =
2732
(Bytes.to_string s)
2833

2934
let run json_filename template_filename =
30-
let j = load_file json_filename
31-
and t = load_file template_filename
35+
let json_data = load_file json_filename
36+
and template_data = load_file template_filename
3237
in
33-
apply_mustache j t
38+
apply_mustache ~json_data ~template_filename ~template_data
3439

3540
let usage () =
3641
print_endline "Usage: mustache-cli json_filename template_filename"

bin/test/errors/parsing-errors.t

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,70 +5,72 @@ Delimiter problems:
55
$ echo "{{foo" > $PROBLEM
66
$ mustache foo.json $PROBLEM
77
Template parse error:
8-
Line 2, character 0: syntax error.
8+
File "no-closing-mustache.mustache", line 2, character 0: syntax error.
99
[3]
1010
1111
$ PROBLEM=one-closing-mustache.mustache
1212
$ echo "{{foo}" > $PROBLEM
1313
$ mustache foo.json $PROBLEM
1414
Template parse error:
15-
Lines 1-2, characters 6-0: syntax error.
15+
File "one-closing-mustache.mustache", lines 1-2, characters 6-0:
16+
syntax error.
1617
[3]
1718
1819
$ PROBLEM=eof-before-variable.mustache
1920
$ echo "{{" > $PROBLEM
2021
$ mustache foo.json $PROBLEM
2122
Template parse error:
22-
Line 2, character 0: ident expected.
23+
File "eof-before-variable.mustache", line 2, character 0: ident expected.
2324
[3]
2425
2526
$ PROBLEM=eof-before-section.mustache
2627
$ echo "{{#" > $PROBLEM
2728
$ mustache foo.json $PROBLEM
2829
Template parse error:
29-
Line 2, character 0: ident expected.
30+
File "eof-before-section.mustache", line 2, character 0: ident expected.
3031
[3]
3132
3233
$ PROBLEM=eof-before-section-end.mustache
3334
$ echo "{{#foo}} {{.}} {{/" > $PROBLEM
3435
$ mustache foo.json $PROBLEM
3536
Template parse error:
36-
Line 2, character 0: ident expected.
37+
File "eof-before-section-end.mustache", line 2, character 0: ident expected.
3738
[3]
3839
3940
$ PROBLEM=eof-before-inverted-section.mustache
4041
$ echo "{{^" > $PROBLEM
4142
$ mustache foo.json $PROBLEM
4243
Template parse error:
43-
Line 2, character 0: ident expected.
44+
File "eof-before-inverted-section.mustache", line 2, character 0:
45+
ident expected.
4446
[3]
4547
4648
$ PROBLEM=eof-before-unescape.mustache
4749
$ echo "{{{" > $PROBLEM
4850
$ mustache foo.json $PROBLEM
4951
Template parse error:
50-
Line 2, character 0: ident expected.
52+
File "eof-before-unescape.mustache", line 2, character 0: ident expected.
5153
[3]
5254
5355
$ PROBLEM=eof-before-unescape.mustache
5456
$ echo "{{&" > $PROBLEM
5557
$ mustache foo.json $PROBLEM
5658
Template parse error:
57-
Line 2, character 0: ident expected.
59+
File "eof-before-unescape.mustache", line 2, character 0: ident expected.
5860
[3]
5961
6062
$ PROBLEM=eof-before-partial.mustache
6163
$ echo "{{>" > $PROBLEM
6264
$ mustache foo.json $PROBLEM
6365
Template parse error:
64-
Line 2, character 0: ident expected.
66+
File "eof-before-partial.mustache", line 2, character 0: ident expected.
6567
[3]
6668
6769
$ PROBLEM=eof-in-comment.mustache
6870
$ echo "{{! non-terminated comment" > $PROBLEM
6971
$ mustache foo.json $PROBLEM
7072
Template parse error:
71-
Line 2, character 0: non-terminated comment.
73+
File "eof-in-comment.mustache", line 2, character 0: non-terminated comment.
7274
[3]
7375
7476
@@ -78,14 +80,14 @@ Mismatches between opening and closing mustaches:
7880
$ echo "{{ foo }}}" > $PROBLEM
7981
$ mustache foo.json $PROBLEM
8082
Template parse error:
81-
Lines 1-2, characters 10-0: syntax error.
83+
File "two-three.mustache", lines 1-2, characters 10-0: syntax error.
8284
[3]
8385
8486
$ PROBLEM=three-two.mustache
8587
$ echo "{{{ foo }}" > $PROBLEM
8688
$ mustache foo.json $PROBLEM
8789
Template parse error:
88-
Lines 1-2, characters 10-0: syntax error.
90+
File "three-two.mustache", lines 1-2, characters 10-0: syntax error.
8991
[3]
9092
9193
@@ -95,19 +97,21 @@ Mismatch between section-start and section-end:
9597
$ echo "{{#foo}} {{.}} {{/bar}}" > $PROBLEM
9698
$ mustache foo.json $PROBLEM
9799
Template parse error:
98-
Lines 1-2, characters 23-0: Mismatched section 'foo' with 'bar'.
100+
File "foo-bar.mustache", lines 1-2, characters 23-0:
101+
Mismatched section 'foo' with 'bar'.
99102
[3]
100103
101104
$ PROBLEM=foo-not-closed.mustache
102105
$ echo "{{#foo}} {{.}} {{foo}}" > $PROBLEM
103106
$ mustache foo.json $PROBLEM
104107
Template parse error:
105-
Line 2, character 0: syntax error.
108+
File "foo-not-closed.mustache", line 2, character 0: syntax error.
106109
[3]
107110
108111
$ PROBLEM=wrong-nesting.mustache
109112
$ echo "{{#bar}} {{#foo}} {{.}} {{/bar}} {{/foo}}" > $PROBLEM
110113
$ mustache foo.json $PROBLEM
111114
Template parse error:
112-
Lines 1-2, characters 41-0: Mismatched section 'foo' with 'bar'.
115+
File "wrong-nesting.mustache", lines 1-2, characters 41-0:
116+
Mismatched section 'foo' with 'bar'.
113117
[3]

bin/test/errors/render-errors.t/run.t

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,58 +28,68 @@ Invalid variable name:
2828

2929
$ mustache reference.json missing-variable.mustache
3030
Template render error:
31-
Line 14, characters 40-46: the variable 'na' is missing.
31+
File "missing-variable.mustache", line 14, characters 40-46:
32+
the variable 'na' is missing.
3233
[2]
3334

3435
$ mustache missing-variable.json reference.mustache
3536
Template render error:
36-
Line 5, characters 4-12: the variable 'data' is missing.
37+
File "reference.mustache", line 5, characters 4-12:
38+
the variable 'data' is missing.
3739
[2]
3840

3941
Invalid section name:
4042

4143
$ mustache reference.json missing-section.mustache
4244
Template render error:
43-
Line 14, characters 0-55: the section 'na' is missing.
45+
File "missing-section.mustache", line 14, characters 0-55:
46+
the section 'na' is missing.
4447
[2]
4548

4649
$ mustache missing-section.json reference.mustache
4750
Template render error:
48-
Lines 9-12, characters 0-10: the section 'group' is missing.
51+
File "reference.mustache", lines 9-12, characters 0-10:
52+
the section 'group' is missing.
4953
[2]
5054

5155
Error in a dotted path foo.bar (one case for the first component, the other in the second).
5256

5357
$ mustache reference.json invalid-dotted-name-1.mustache
5458
Template render error:
55-
Line 10, characters 2-15: the variable 'gro' is missing.
59+
File "invalid-dotted-name-1.mustache", line 10, characters 2-15:
60+
the variable 'gro' is missing.
5661
[2]
5762

5863
$ mustache invalid-dotted-name-1.json reference.mustache
5964
Template render error:
60-
Lines 9-12, characters 0-10: the section 'group' is missing.
65+
File "reference.mustache", lines 9-12, characters 0-10:
66+
the section 'group' is missing.
6167
[2]
6268

6369
$ mustache reference.json invalid-dotted-name-2.mustache
6470
Template render error:
65-
Line 10, characters 2-15: the variable 'group.fir' is missing.
71+
File "invalid-dotted-name-2.mustache", line 10, characters 2-15:
72+
the variable 'group.fir' is missing.
6673
[2]
6774

6875
$ mustache invalid-dotted-name-2.json reference.mustache
6976
Template render error:
70-
Line 10, characters 2-17: the variable 'group.first' is missing.
77+
File "reference.mustache", line 10, characters 2-17:
78+
the variable 'group.first' is missing.
7179
[2]
7280

7381
Non-scalar used as a scalar:
7482

7583
$ mustache reference.json non-scalar.mustache
7684
Template render error:
77-
Line 4, characters 0-8: the value of 'list' is not a valid scalar.
85+
File "non-scalar.mustache", line 4, characters 0-8:
86+
the value of 'list' is not a valid scalar.
7887
[2]
7988

8089
$ mustache non-scalar.json reference.mustache
8190
Template render error:
82-
Line 1, characters 7-16: the value of 'title' is not a valid scalar.
91+
File "reference.mustache", line 1, characters 7-16:
92+
the value of 'title' is not a valid scalar.
8393
[2]
8494

8595
Missing partial (currently the CLI does not support any partial anyway):
@@ -89,5 +99,6 @@ in better `ls` output).
8999

90100
$ mustache reference.json z-missing-partial.mustache
91101
Template render error:
92-
Line 11, characters 2-13: the partial 'second' is missing.
102+
File "z-missing-partial.mustache", line 11, characters 2-13:
103+
the partial 'second' is missing.
93104
[2]

0 commit comments

Comments
 (0)