@@ -128,7 +128,7 @@ type token = {
128
128
location : Location_ .span ;
129
129
}
130
130
131
- type path_prefix = Path_prefix of string
131
+ type path_prefix = Path_prefix of string * Location_ .span
132
132
133
133
(* The string is scanned right-to-left, because we are interested in right-most
134
134
hyphens. The tokens are also returned in right-to-left order, because the
@@ -207,7 +207,10 @@ let tokenize location s : token list * path_prefix option =
207
207
let location = Location_. span [ location; identifier_location ] in
208
208
(kind, location)
209
209
and scan_path started_at tokens =
210
- (tokens, Some (Path_prefix (String. sub s 0 (started_at + 1 ))))
210
+ let location =
211
+ Location_. in_string s ~offset: 0 ~length: (started_at + 1 ) location
212
+ in
213
+ (tokens, Some (Path_prefix (String. sub s 0 (started_at + 1 ), location)))
211
214
in
212
215
213
216
scan_identifier (String. length s) 0 (String. length s - 1 ) []
@@ -221,15 +224,41 @@ let expected ?(expect_paths = false) allowed location =
221
224
let allowed = List. map (Printf. sprintf " '%s-'" ) allowed @ unqualified in
222
225
expected_err (pp_hum_comma_separated Format. pp_print_string) allowed location
223
226
224
- let parse_path p =
227
+ let parse_path whole_path_location p =
225
228
let segs = String. split_on_char '/' p in
229
+ let check segs start =
230
+ let _finish =
231
+ List. fold_left
232
+ (fun offset seg ->
233
+ match seg with
234
+ | "" ->
235
+ let location =
236
+ Location_. in_string p ~offset ~length: 0 whole_path_location
237
+ in
238
+ should_not_be_empty ~what: " Identifier in path reference" location
239
+ |> Error. raise_exception
240
+ | seg -> offset + String. length seg + 1 )
241
+ start segs
242
+ in
243
+ ()
244
+ in
226
245
match segs with
227
- | "." :: segs -> (`TRelativePath , segs)
228
- | "" :: "" :: segs -> (`TCurrentPackage , segs)
229
- | "" :: segs -> (`TAbsolutePath , segs)
230
- | segs -> (`TRelativePath , segs)
231
-
232
- let parse_path_prefix (Path_prefix p ) identifier = parse_path (p ^ identifier)
246
+ | "." :: segs ->
247
+ check segs 2 ;
248
+ (`TRelativePath , segs)
249
+ | "" :: "" :: segs ->
250
+ check segs 2 ;
251
+ (`TCurrentPackage , segs)
252
+ | "" :: segs ->
253
+ check segs 1 ;
254
+ (`TAbsolutePath , segs)
255
+ | segs ->
256
+ check segs 0 ;
257
+ (`TRelativePath , segs)
258
+
259
+ let parse_path_prefix (Path_prefix (p , path_location )) identifier
260
+ prefix_location =
261
+ parse_path (Location_. span [ path_location; prefix_location ]) (p ^ identifier)
233
262
234
263
(* Parse references that do not contain a [/]. Raises errors and warnings. *)
235
264
let parse whole_reference_location s :
@@ -255,7 +284,7 @@ let parse whole_reference_location s :
255
284
| Some p -> (
256
285
match kind with
257
286
| `TUnknown | `TModule ->
258
- `Module_path (parse_path_prefix p identifier)
287
+ `Module_path (parse_path_prefix p identifier location )
259
288
| _ ->
260
289
expected ~expect_paths: true [ " module" ] location
261
290
|> Error. raise_exception))
@@ -287,7 +316,7 @@ let parse whole_reference_location s :
287
316
| Some p -> (
288
317
match kind with
289
318
| `TUnknown | `TModule ->
290
- `Module_path (parse_path_prefix p identifier)
319
+ `Module_path (parse_path_prefix p identifier location )
291
320
| _ ->
292
321
expected ~expect_paths: true [ " module" ] location
293
322
|> Error. raise_exception))
@@ -335,20 +364,25 @@ let parse whole_reference_location s :
335
364
336
365
let label_parent_path kind path_prefix identifier location =
337
366
match kind with
338
- | `TUnknown -> `Any_path (parse_path_prefix path_prefix identifier)
339
- | `TModule -> `Module_path (parse_path_prefix path_prefix identifier)
340
- | `TPage -> `Page_path (parse_path_prefix path_prefix identifier)
367
+ | `TUnknown ->
368
+ `Any_path (parse_path_prefix path_prefix identifier location)
369
+ | `TModule ->
370
+ `Module_path (parse_path_prefix path_prefix identifier location)
371
+ | `TPage -> `Page_path (parse_path_prefix path_prefix identifier location)
341
372
| _ ->
342
373
expected ~expect_paths: true [ " module" ; " page" ] location
343
374
|> Error. raise_exception
344
375
in
345
376
346
377
let any_path kind path_prefix identifier location =
347
378
match kind with
348
- | `TUnknown -> `Any_path (parse_path_prefix path_prefix identifier)
349
- | `TModule -> `Module_path (parse_path_prefix path_prefix identifier)
350
- | `TPage -> `Page_path (parse_path_prefix path_prefix identifier)
351
- | `TAsset -> `Asset_path (parse_path_prefix path_prefix identifier)
379
+ | `TUnknown ->
380
+ `Any_path (parse_path_prefix path_prefix identifier location)
381
+ | `TModule ->
382
+ `Module_path (parse_path_prefix path_prefix identifier location)
383
+ | `TPage -> `Page_path (parse_path_prefix path_prefix identifier location)
384
+ | `TAsset ->
385
+ `Asset_path (parse_path_prefix path_prefix identifier location)
352
386
| _ ->
353
387
expected ~expect_paths: true [ " module" ; " page" ] location
354
388
|> Error. raise_exception
@@ -534,9 +568,9 @@ let parse whole_reference_location s :
534
568
|> Error. raise_exception)
535
569
536
570
(* Parse references that do not contain a [/]. Raises errors and warnings. *)
537
- let parse_asset (* whole_reference_location *) s :
571
+ let parse_asset whole_reference_location s :
538
572
Paths.Reference.Asset. t Error. with_errors_and_warnings =
539
- let path = parse_path s in
573
+ let path = parse_path whole_reference_location s in
540
574
Error. catch_errors_and_warnings (fun () -> `Asset_path path)
541
575
542
576
let read_path_longident location s =
0 commit comments