@@ -6,14 +6,15 @@ let mustache_from_file file =
6
6
let chan = open_in file in
7
7
let lex = Lexing. from_channel chan in
8
8
Location. init lex file ;
9
- let t = Mustache. parse_lx lex in
9
+ let t = Mustache.With_locations. parse_lx lex in
10
10
close_in chan ;
11
11
t
12
12
13
- let mustache_from_string ~loc string =
13
+ let mustache_from_string ~lexloc string =
14
14
let lex = Lexing. from_string string in
15
- lex.Lexing. lex_curr_p < - loc ;
16
- Mustache. parse_lx lex
15
+ lex.Lexing. lex_start_p < - lexloc ;
16
+ lex.Lexing. lex_curr_p < - lexloc ;
17
+ Mustache.With_locations. parse_lx lex
17
18
18
19
let antiquot_pcdata ~loc ~lang var =
19
20
let pcdata = Ppx_common. make ~loc lang " pcdata" in
70
71
71
72
module Template = struct
72
73
73
- type t = desc list
74
+ type t = desc Location .loc list
74
75
and desc =
75
76
| Markup of string
76
77
| Pcdata of string
@@ -82,17 +83,23 @@ module Template = struct
82
83
contents : t ;
83
84
}
84
85
86
+ let mkloc {Mustache.With_locations. loc_start ; loc_end } txt =
87
+ let loc = {Location. loc_ghost = true ; loc_start ; loc_end} in
88
+ [{Location. loc ; txt}]
89
+
85
90
let rec of_mustache resolve =
86
- Mustache. fold
87
- ~string: (fun x -> [ Markup x] )
91
+ Mustache.With_locations. fold
92
+ ~string: (fun ~ loc x -> mkloc loc @@ Markup x)
88
93
~section:
89
- (fun ~inverted name contents -> [Section { inverted ; name ; contents }])
90
- ~escaped: (fun x -> [Pcdata x])
91
- ~unescaped: (fun x -> [Expr x])
94
+ (fun ~loc ~inverted name contents ->
95
+ mkloc loc @@ Section { inverted ; name ; contents})
96
+ ~escaped: (fun ~loc x -> mkloc loc @@ Pcdata x)
97
+ ~unescaped: (fun ~loc x -> mkloc loc @@ Expr x)
92
98
~partial:
93
- (fun s -> of_mustache resolve @@ mustache_from_file @@ resolve s)
94
- ~comment: (fun _ -> [] )
95
- ~concat: List. concat
99
+ (fun ~loc :_ s ->
100
+ of_mustache resolve @@ mustache_from_file @@ resolve s)
101
+ ~comment: (fun ~loc :_ _ -> [] )
102
+ ~concat: (fun ~loc :_ l -> List. concat l)
96
103
97
104
let bindings ~env ~sec_env ~id =
98
105
let f s b b' = match b, b' with
@@ -108,9 +115,9 @@ module Template = struct
108
115
in
109
116
Exp. let_ Asttypes. Nonrecursive @@ Var.Env. fold make_binding env []
110
117
111
- let rec desc_to_expr ~loc ~ lang env t =
118
+ let rec desc_to_expr ~lang env { Location. txt; loc} =
112
119
Ast_helper. default_loc := loc ;
113
- match (t : desc ) with
120
+ match (txt : desc ) with
114
121
| Markup s -> env, AC. str s
115
122
| Pcdata s ->
116
123
Var. add env s Var , antiquot_pcdata ~loc ~lang s
@@ -131,7 +138,7 @@ module Template = struct
131
138
132
139
and to_expr ~simplify ~loc ~lang env l =
133
140
let f (env , acc ) t =
134
- let env, expr = desc_to_expr ~loc ~ lang env t in
141
+ let env, expr = desc_to_expr ~lang env t in
135
142
env, expr::acc
136
143
in
137
144
let env, l = List. fold_left f (env, [] ) l in
@@ -157,9 +164,9 @@ let expr_of_mustache ~loc ~lang t =
157
164
in
158
165
Template. make_function env e
159
166
160
- let expr_of_string ~loc ~lang s =
167
+ let expr_of_string ~loc ~lang ~ lexloc s =
161
168
expr_of_mustache ~loc ~lang @@
162
- mustache_from_string ~loc: loc.loc_start s
169
+ mustache_from_string ~lexloc s
163
170
164
171
165
172
(* * Mappers *)
@@ -169,39 +176,45 @@ open Parsetree
169
176
let error loc =
170
177
Ppx_common. error loc " Invalid payload for [%%template]."
171
178
172
- let extract_str loc str = match AC. get_str str with
179
+ let extract_str loc str =
180
+ match AC. get_str_with_quotation_delimiter str with
173
181
| None -> error loc
174
- | Some s -> s
182
+ | Some ( s , quot ) -> ( Ppx_tyxml.Loc. string_start quot loc, s)
175
183
176
184
let expr mapper e =
177
- let loc = e.pexp_loc in
185
+ let sloc = e.pexp_loc in
178
186
match e.pexp_desc with
179
187
| Pexp_extension ({ txt = ("template" | "tyxml.template" )} , payload ) ->
180
188
begin match payload with
181
189
| PStr [[% stri let [% p? var] = [% e? str] in [% e? e]]] ->
182
- let s = extract_str loc str in
183
- Exp. let_ Asttypes. Nonrecursive
184
- [Vb. mk var @@ expr_of_string ~loc: str.pexp_loc ~lang: Html s]
190
+ let loc = str.pexp_loc in
191
+ let lexloc, s = extract_str loc str in
192
+ Exp. let_ ~loc: sloc Asttypes. Nonrecursive
193
+ [Vb. mk ~loc: sloc var @@
194
+ expr_of_string ~loc ~lang: Html ~lexloc s]
185
195
e
186
196
187
197
| PStr [{pstr_desc = Pstr_eval (str, _)}] ->
188
- let s = extract_str loc str in
189
- expr_of_string ~loc: str.pexp_loc ~lang: Html s
198
+ let loc = str.pexp_loc in
199
+ let lexloc, s = extract_str loc str in
200
+ expr_of_string ~loc ~lang: Html ~lexloc s
190
201
191
- | _ -> error loc
202
+ | _ -> error sloc
192
203
end
193
204
| _ -> Ast_mapper. default_mapper.expr mapper e
194
205
195
206
let structure_item mapper stri =
196
- let loc = stri.pstr_loc in
207
+ let sloc = stri.pstr_loc in
197
208
match stri.pstr_desc with
198
209
| Pstr_extension (({ txt = ("template" | "tyxml.template" )} , payload ), _ ) ->
199
210
begin match payload with
200
- | PStr [[% stri let [% p? var] = [% e? str]]] ->
201
- let s = extract_str loc str in
202
- Str. value Asttypes. Nonrecursive
203
- [Vb. mk var @@ expr_of_string ~loc: str.pexp_loc ~lang: Html s]
204
- | _ -> error loc
211
+ | PStr [([% stri let [% p? var] = [% e? str]] as decl)] ->
212
+ let loc = str.pexp_loc in
213
+ let lexloc, s = extract_str loc str in
214
+ Str. value ~loc: decl.pstr_loc Asttypes. Nonrecursive
215
+ [Vb. mk ~loc: decl.pstr_loc var @@
216
+ expr_of_string ~loc ~lang: Html ~lexloc s]
217
+ | _ -> error sloc
205
218
end
206
219
| _ -> Ast_mapper. default_mapper.structure_item mapper stri
207
220
0 commit comments