Source file model_parser.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
open Core
open Ppxlib
open Angstrom
open Angstrom.Let_syntax
open Model

module Locations : sig
  type t

  val create : Location.t -> string -> t
  val location : t -> start:int -> end_:int -> Location.t

  (** The 1-indexed source line number containing [offset]. *)
  val line_number_of_offset : t -> int -> int

  (** Render multiple annotated code snippets. Annotations on the same line are merged
      into a single snippet block with multiple caret rows (sorted by column). Annotations
      on different lines get separate snippet blocks, rendered in list order. *)
  val format_code_snippets
    :  t
    -> snippets:(int * int * string) list
    -> ?hint:string
    -> unit
    -> string
end = struct
  type t =
    { lines : position Int.Map.t
    ; text : string
    }

  let position ~lines i =
    let i', pos = Map.closest_key lines `Less_or_equal_to i |> Option.value_exn in
    { pos with pos_cnum = pos.pos_cnum - i' + i }
  ;;

  let create { loc_start; _ } text =
    let lines = Int.Map.empty in
    let lines = Map.set lines ~key:0 ~data:loc_start in
    let lines =
      String.foldi text ~init:lines ~f:(fun i lines c ->
        if Char.( <> ) c '\n'
        then lines
        else (
          let pos =
            { loc_start with
              pos_cnum = loc_start.pos_cnum + i + 1
            ; pos_bol = loc_start.pos_cnum + i + 1
            ; pos_lnum = loc_start.pos_lnum + Map.length lines
            }
          in
          Map.set lines ~key:(i + 1) ~data:pos))
    in
    { lines; text }
  ;;

  let position { lines; text } i =
    assert (0 <= i && i <= String.length text);
    position ~lines i
  ;;

  let location t ~start ~end_ =
    let loc_start = position t start
    and loc_end = position t end_ in
    { Location.loc_start; loc_end; loc_ghost = false }
  ;;

  (** [line_starts] returns the list of [(line_start_offset, line_number)] pairs for all
      lines in the text, sorted by offset. *)
  let line_starts { lines; _ } =
    Map.to_alist lines |> List.map ~f:(fun (offset, pos) -> offset, pos.pos_lnum)
  ;;

  (** [line_of_offset] returns [(line_start_offset, line_number)] given a byte offset in
      the text. *)
  let line_of_offset { lines; _ } offset =
    let () =
      if offset < 0
      then
        Location.raise_errorf
          ~loc:{ loc_start = [%here]; loc_end = [%here]; loc_ghost = true }
          "Somehow received negative offset while computing line number for error. This \
           is an error in [ppx_html], please contact the maintainers."
    in
    match Map.closest_key lines `Less_or_equal_to offset with
    | Some (line_start, pos) -> line_start, pos.pos_lnum
    | None -> 0, 1
  ;;

  let line_number_of_offset t offset = snd (line_of_offset t offset)

  (** [line_text] extracts the text of a single line starting at [substring_start_offset]
      until the next following newline character or EOF. *)
  let line_text { lines; text } substring_start_offset =
    let line_end =
      match Map.closest_key lines `Greater_than substring_start_offset with
      | Some (next_offset, _) ->
        (* [next_offset] points to the char after '\n', so the line ends at
           [next_offset - 1] (the '\n' itself). We exclude the '\n'. *)
        next_offset - 1
      | None -> String.length text
    in
    String.sub text ~pos:substring_start_offset ~len:(line_end - substring_start_offset)
  ;;

  let format_code_snippets t ~snippets ?hint () =
    List.iter snippets ~f:(fun (start, end_, _msg) ->
      if not (0 <= start && start <= end_ && end_ <= String.length t.text)
      then
        Location.raise_errorf
          ~loc:{ loc_start = [%here]; loc_end = [%here]; loc_ghost = true }
          "Snippet offsets [start=%d, end_=%d, length=%d] do not satisfy [0 <= start <= \
           end_ <= length]. This is an error in [ppx_html], please contact the \
           maintainers."
          start
          end_
          (String.length t.text));
    (* Group consecutive annotations that share a line. We walk the list in order, and
       whenever two adjacent annotations land on the same source line we merge them into
       the same group.

       Each group is rendered as a single snippet block. *)
    let groups =
      (* We can assume that if snippets are within the same line, then they are contiguous
         with other snippets on the same line *)
      List.fold snippets ~init:Int.Map.empty ~f:(fun acc (start, end_, msg) ->
        let _, line_num = line_of_offset t start in
        Map.update acc line_num ~f:(function
          | None -> Reversed_list.[ start, end_, msg ]
          | Some prev -> (start, end_, msg) :: prev))
    in
    let all_line_starts = line_starts t in
    let buf = Buffer.create (String.length t.text) in
    let open struct
      type t =
        { snippets : (int * int * string) list
        ; relevant_lines : (int * string) list
        }
    end in
    let groups =
      Map.mapi groups ~f:(fun ~key:error_line_num ~data:snippets ->
        let snippets = Reversed_list.rev snippets in
        let relevant_lines =
          let context_radius = 1 in
          let min_line = error_line_num - context_radius in
          let max_line = error_line_num + context_radius in
          List.filter all_line_starts ~f:(fun (_, lnum) ->
            lnum >= min_line && lnum <= max_line)
          |> List.map ~f:(fun (line_start_offset, lnum) ->
            let line = line_text t line_start_offset in
            lnum, line)
        in
        (* Sort snippets by column so the rightmost renders first. We print snippets
           rightmost to left to avoid snippets blocking subsequent snippets. *)
        let snippets =
          List.sort snippets ~compare:(Comparable.reverse [%compare__local: int * _ * _])
        in
        { relevant_lines; snippets })
    in
    (* Padding so line numbers are aligned properly *)
    let num_digits_in_highest_lnum =
      let max_lnum =
        Map.fold groups ~init:0 ~f:(fun ~key:_ ~data:{ relevant_lines; _ } acc ->
          List.fold relevant_lines ~init:acc ~f:(fun acc (lnum, _) -> Int.max acc lnum))
      in
      String.length (Int.to_string max_lnum)
    in
    let pad = String.make num_digits_in_highest_lnum ' ' in
    (* Yes in theory this is inefficient, but errors have a maximum of 2 groups *)
    Map.iteri groups ~f:(fun ~key:error_line_num ~data:{ snippets; relevant_lines } ->
      let ( (* Print a buffer line betwen the error message and the context text *) ) =
        Buffer.add_string buf [%string "%{pad} |\n"]
      in
      List.iter relevant_lines ~f:(fun (lnum, line) ->
        let ( (* Print out the line number + line text *) ) =
          let lnum_str = Int.to_string lnum in
          let padding_for_current_line =
            let num_spaces_for_current_line =
              num_digits_in_highest_lnum - String.length lnum_str
            in
            String.make num_spaces_for_current_line ' '
          in
          Buffer.add_string
            buf
            [%string "%{padding_for_current_line}%{lnum_str} | %{line}\n"]
        in
        if lnum = error_line_num
        then (
          let error_line_start, _ =
            List.find_exn all_line_starts ~f:(fun (_, lnum) -> lnum = error_line_num)
          in
          List.iter snippets ~f:(fun (caret_start, caret_end, msg) ->
            let col_start = caret_start - error_line_start in
            let col_end =
              if caret_end > caret_start
              then caret_end - error_line_start
              else col_start + 1
            in
            let line_len = String.length line in
            let col_end = Int.min col_end line_len in
            let col_start = Int.min col_start line_len in
            let num_carets = Int.max 1 (col_end - col_start) in
            let leading_spaces = String.make col_start ' ' in
            let carets = String.make num_carets '^' in
            Buffer.add_string buf [%string "%{pad} | %{leading_spaces}%{carets} %{msg}\n"])));
      (* End with another buffer line before the hint *)
      Buffer.add_string buf [%string "%{pad} |\n"]);
    (match hint with
     | None -> ()
     | Some hint -> Buffer.add_string buf [%string "%{pad} | Hint: %{hint}\n"]);
    Buffer.contents buf
  ;;
end

module Error = struct
  module Position = struct
    type t = Lexing.position =
      { pos_fname : string
      ; pos_lnum : int
      ; pos_bol : int
      ; pos_cnum : int
      }
    [@@deriving sexp]
  end

  module Location = struct
    include Location

    type t = Location.t =
      { loc_start : Position.t
      ; loc_end : Position.t
      ; loc_ghost : bool
      }
    [@@deriving sexp]
  end

  module T = struct
    type t =
      { message : string
      ; location : Location.t option
      ; snippet : string option [@sexp.option]
      }
    [@@deriving sexp]
  end

  include T
  include Sexpable.To_stringable (T)

  let of_string str =
    try of_string str with
    | _ -> { location = None; message = str; snippet = None }
  ;;

  let raise ~loc { location; message; snippet } =
    let message =
      match snippet with
      | None -> message
      | Some snippet -> [%string "%{message}\n%{snippet}"]
    in
    Location.raise_errorf ~loc:(Option.value location ~default:loc) "%s" message
  ;;
end

let ocaml_expression ({ txt; loc } : string Loc.t) =
  (* borrowed from [ppx_string] *)
  let lexbuf = Lexing.from_string txt in
  lexbuf.lex_abs_pos <- loc.loc_start.pos_cnum;
  lexbuf.lex_curr_p <- loc.loc_start;
  Parse.expression lexbuf
;;

let with_loc' ~locs f =
  let%map start = pos
  and f
  and end_ = pos in
  let loc = Locations.location locs ~start ~end_ in
  f loc
;;

let with_loc ~locs txt =
  with_loc'
    ~locs
    (let%map txt in
     fun loc -> { txt; loc })
;;

let fail_but_we_know_location ~exn ~loc message =
  let message =
    assert (String.is_empty (Format.flush_str_formatter ()));
    Format.fprintf Format.str_formatter "%s\n" message;
    Location.report_exception Format.str_formatter exn;
    Format.flush_str_formatter ()
  in
  fail (Error.to_string { location = Some loc; message; snippet = None })
;;

(** [fail] creates an error with the provided parameters.

    [snippets] get printed as a source location with a caret pointing to the exact span *)
let fail ?start ?end_ ~locs ?exn ?hint ?snippets message =
  let%bind pos in
  let start = Option.value start ~default:pos in
  let end_ = Option.value end_ ~default:pos in
  let snippets = Option.value snippets ~default:[ start, end_, "" ] in
  let location = Locations.location locs ~start ~end_ in
  let message =
    match exn with
    | None -> message
    | Some exn ->
      assert (String.is_empty (Format.flush_str_formatter ()));
      Format.fprintf Format.str_formatter "%s\n" message;
      Location.report_exception Format.str_formatter exn;
      Format.flush_str_formatter ()
  in
  let formatted_snippet = Locations.format_code_snippets locs ~snippets ?hint () in
  fail
    (Error.to_string
       { location = Some location; message; snippet = Some formatted_snippet })
;;

let string s =
  let%map (_ : string) = string s in
  ()
;;

let char c =
  let%map (_ : char) = char c in
  ()
;;

let option s = option None (s >>| Option.some)

let scan_through_ocaml_expression_until_unclosed_curly_brace ~locs =
  let%bind start_pos = pos in
  let%bind code =
    scan_string "" (fun curr_string c ->
      (* NOTE: This code is semi-complex/ugly. What it does is adding support for being
         able to handle tricky edge cases like:

         [%html {|<div>%{Vdom.Node.text "}"}</div>|}]

         Solely reading a single character at a time results in the curly brace inside of
         the interpolated ocaml closing the entire segment despite it being inside of an
         escaped string.

         The way this code works is that it solely calls the OCaml tokenizer until it
         seems a string with an unclosed curly brace, at which point it stops scanning the
         string. *)
      let curr_string = curr_string ^ Char.to_string c in
      let tokens_of_string = Ocaml_parsing.string_tokens curr_string in
      match tokens_of_string with
      | Error _ -> Some curr_string
      | Ok tokens ->
        let has_unclosed_curlys =
          List.fold tokens ~init:0 ~f:(fun parens -> function
            | LBRACE -> parens + 1
            | RBRACE -> parens - 1
            | _ -> parens)
          < 0
        in
        (match has_unclosed_curlys with
         | true -> None
         | false -> Some curr_string))
  in
  match%bind peek_char with
  | Some '}' -> return code
  | None | Some _ ->
    let error_pos = start_pos + String.length (String.rstrip code) in
    fail
      ~start:error_pos
      ~end_:error_pos
      ~locs
      ~snippets:[ error_pos, error_pos, "curly brace expected here" ]
      "Missing curly brace for interpolated OCaml"
;;

let identify_case_if_string_is_peeked ~string ~case =
  match%bind peek_string (String.length string) with
  | peeked when String.equal peeked string -> return case
  | _ -> Angstrom.fail "If you ever see this error message, this is a ppx_html bug"
;;

let identify_case_if_next_char_matches ~test ~case =
  match%bind peek_char with
  | Some peeked when test peeked -> return case
  | _ -> Angstrom.fail "If you ever see this error message, this is a ppx_html bug"
;;

let ( => ) string case = identify_case_if_string_is_peeked ~string ~case
let ( >=> ) test case = identify_case_if_next_char_matches ~test ~case

let parse_intepolation_kind : Model.Interpolation_kind.t t =
  choice
    [ string "%{" *> return Model.Interpolation_kind.Normal
    ; string "?{" *> return Model.Interpolation_kind.Option
    ; string "*{" *> return Model.Interpolation_kind.List
    ; string "#{" *> return Model.Interpolation_kind.String
    ]
;;

type interpolation =
  { expr : Model.Expr.t
  ; interpolation_kind : Model.Interpolation_kind.t
  }

let parse_expr_common ~locs : interpolation Angstrom.t =
  with_loc'
    ~locs
    (let%bind interpolation_kind = parse_intepolation_kind
     and start = pos
     and code = scan_through_ocaml_expression_until_unclosed_curly_brace ~locs
     and end_ = pos
     and () = string "}" in
     let interpolation_content_loc = Locations.location locs ~start ~end_ in
     let code, to_t =
       match Ocaml_parsing.rsplit_on_hash code with
       | None -> code, None
       | Some (code, to_t) ->
         let to_t =
           { txt = to_t
           ; loc = Locations.location locs ~start:(end_ - String.length to_t - 1) ~end_
           }
           |> ocaml_expression
           |> function
           | { pexp_desc = Pexp_construct (t, None); _ } ->
             { t with txt = Longident.name t.txt }
           | { pexp_loc; _ } ->
             Location.raise_errorf ~loc:pexp_loc "Expected a module identifier (e.g. Foo)"
         in
         code, Some to_t
     in
     let code =
       { txt = "(" ^ code ^ ")"; loc = Locations.location locs ~start:(start - 1) ~end_ }
     in
     let%map (expr : Ppxlib.expression) =
       (* borrowed from [ppx_string] *)
       match ocaml_expression code with
       | expr -> return expr
       | exception (Syntaxerr.Error error as exn) ->
         fail_but_we_know_location
           ~exn
           ~loc:(Syntaxerr.location_of_error error)
           "Failed to parse OCaml expression inside of HTML."
       | exception exn ->
         fail
           ~start
           ~end_
           ~locs
           ~exn
           ~snippets:[ start, end_, "expression here" ]
           "Failed to parse OCaml expression"
     in
     let expr = { expr with pexp_loc = interpolation_content_loc } in
     let string_relative_location = { String_relative_location.start; end_ = end_ - 1 } in
     fun loc ->
       let expr =
         { Expr.expr; code; to_t; loc; string_relative_location; escape_kind = Escaped }
       in
       { expr; interpolation_kind })
;;

let skip_while1 f = skip f *> skip_while f
let take_span_while1 ~locs f = with_loc ~locs (take_while1 f)
let is_capitalized s = (not (String.is_empty s)) && Char.is_uppercase s.[0]

let parse_name ~allow_hyphens ~allow_dots ~expected ~locs : string Loc.t Angstrom.t =
  take_span_while1 ~locs (function
    | '_' -> true
    | c ->
      Char.is_alphanum c
      || Char.equal c '\''
      || (allow_hyphens && Char.equal c '-')
      (* NOTE: We want to allow dots for the "component" syntax:

         e.g. <Foo.f></> *)
      || (allow_dots && Char.equal c '.'))
  <|> (peek_char
       >>= fun c ->
       let but =
         match c with
         | None -> ""
         | Some c ->
           let found =
             match Char.is_whitespace c with
             | true -> "whitespace. No whitespace is allowed here."
             | false -> [%string "'%{c#Char}'"]
           in
           [%string {|, but instead found %{found}|}]
       in
       fail ~locs [%string {|Expected a valid %{expected}%{but}.  |}])
;;

let parse_ocaml_name = parse_name ~allow_hyphens:false
let parse_attr_name = parse_name ~allow_hyphens:true ~allow_dots:false

let parse_html_token ~locs : string Loc.t Angstrom.t =
  take_span_while1 ~locs (function
    | '_' | ':' | '-' | '.' -> true
    | c -> Char.is_alphanum c)
;;

let only_normal_interpolation_allowed ~start ~end_ ~locs ~interpolation_kind expr =
  match interpolation_kind with
  | Interpolation_kind.Normal -> return expr
  | (Option | List | String) as kind ->
    let normal_interpolation = "%{}" in
    fail
      ~start
      ~end_
      ~locs
      ~snippets:[ start, end_, "invalid interpolation here" ]
      [%string
        {|%{kind#Interpolation_kind} interpolation is not allowed here, only %{normal_interpolation} interpolation is allowed in this context.|}]
;;

let parse_quote_expr ~locs =
  let%bind start = pos
  and { expr; interpolation_kind } = parse_expr_common ~locs
  and end_ = pos in
  only_normal_interpolation_allowed
    ~start
    ~end_
    ~locs
    ~interpolation_kind
    (Model.Quote.Elt.Expr expr)
;;

let interpolation_case =
  choice
    [ "%{" => `Expression; "?{" => `Expression; "*{" => `Expression; "#{" => `Expression ]
;;

let argument_case = choice [ "~" => `Argument `Tilde; "?" => `Argument `Question_mark ]

let parse_quote ~locs : Model.Quote.t Angstrom.t =
  let quoted =
    match%bind
      choice
        [ interpolation_case
        ; Char.is_whitespace >=> `Whitespace_literal
        ; return `Literal
        ]
    with
    | `Expression -> parse_quote_expr ~locs
    | `Whitespace_literal ->
      let%map str = with_loc ~locs (take_while1 Char.is_whitespace) in
      Model.Quote.Elt.Literal str
    | `Literal ->
      let%map str =
        with_loc
          ~locs
          (consumed
             (skip_many1
                (match%bind any_char with
                 | '"' -> fail ~locs "End of string"
                 | '\\' ->
                   (* escape... *)
                   let%map _ = any_char in
                   ()
                 | '%' ->
                   let%bind c = peek_char_fail in
                   if Char.( = ) c '{'
                   then fail ~locs "Expected code block"
                   else return ()
                 | c ->
                   if Char.is_whitespace c then fail ~locs "Whitespace" else return ()))
           >>| Scanf.unescaped)
      in
      Model.Quote.Elt.Literal str
  in
  with_loc
    ~locs
    (match%bind choice [ "\"" => `Quoted; return `Html_token ] with
     | `Quoted ->
       let%map () = string "\""
       and t = many quoted
       and () = string "\"" in
       t
     | `Html_token ->
       let%map t = parse_html_token ~locs in
       [ Model.Quote.Elt.Literal t ])
;;

let parse_attr_value_expr ~locs =
  let%bind start = pos
  and { expr; interpolation_kind } = parse_expr_common ~locs
  and end_ = pos in
  only_normal_interpolation_allowed
    ~start
    ~end_
    ~locs
    ~interpolation_kind
    (Model.Attr.Value.Expr expr)
;;

let parse_value ~locs : Model.Attr.Value.t Angstrom.t =
  match%bind choice [ interpolation_case; return `Literal ] with
  | `Expression -> parse_attr_value_expr ~locs
  | `Literal ->
    let%map str = parse_quote ~locs in
    Model.Attr.Value.Literal str
;;

let parse_tag_expr ~locs =
  let%bind start = pos
  and { expr; interpolation_kind } = parse_expr_common ~locs
  and end_ = pos in
  only_normal_interpolation_allowed
    ~start
    ~end_
    ~locs
    ~interpolation_kind
    (Model.Tag.Expr expr)
;;

let parse_tag ~locs : Model.Tag.t Angstrom.t =
  match%bind choice [ interpolation_case; ">" => `Fragment; return `Literal ] with
  | `Expression -> parse_tag_expr ~locs
  | `Fragment ->
    let%map { txt = (); loc } = with_loc ~locs (return ()) in
    Model.Tag.Fragment loc
  | `Literal ->
    let%map start = pos
    and atom = parse_ocaml_name ~expected:"HTML tag" ~locs ~allow_dots:true
    and end_ = pos in
    (match String.mem atom.txt '.' || is_capitalized atom.txt with
     | false -> Model.Tag.Literal (Literal atom)
     | true ->
       let name = { atom with txt = Ppxlib.Longident.parse atom.txt }
       and string_relative_location = { String_relative_location.start; end_ } in
       Model.Tag.Literal (Component { name; string_relative_location; code = atom }))
;;

let skip_opt_ws = skip_while Char.is_whitespace
let skip_req_ws = skip_while1 Char.is_whitespace

let skip_comment =
  string "<!--"
  *> scan_string
       Reversed_list.[]
       (fun prev c ->
         let prev = Reversed_list.(c :: prev) in
         match prev with
         | '>' :: '-' :: '-' :: _ -> None
         | _ -> Some prev)
  *> string ">"
  *> return ()
;;

let collapse_prefix_and_trailing_ws s =
  (* NOTE: This "collapses" whitespace so that it remains in-sync with the spec defined
     in:

     https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model/Whitespace

     this does not "collapse" across adjacent whitespace between elements sadly.

     The "collapsing" whitespace part is optional. Another valid way of addressing this
     would be to solely leave the whitespace in, but this would have some runtime cost in
     addition to making the string harder to read during tests... This instead moves the
     cost to build time (at ppx expansion time).
  *)
  match (not (String.is_empty s)) && String.for_all s ~f:Char.is_whitespace with
  | true -> " "
  | false ->
    let ws_prefix = String.take_while s ~f:Char.is_whitespace in
    let ws_suffix =
      String.take_while (String.rev s) ~f:Char.is_whitespace |> String.rev
    in
    let s =
      match ws_prefix with
      | "" -> s
      | _ -> " " ^ String.chop_prefix_exn ~prefix:ws_prefix s
    in
    let s =
      match ws_suffix with
      | "" -> s
      | _ -> String.chop_suffix_exn ~suffix:ws_suffix s ^ " "
    in
    s
;;

module Collapse_ws = struct
  (* [Collapse_ws] uses buffers to build strings because otherwise we'd have to either use
     a [char Reversed_list.t], which would have to be reversed and then concatenated, or a
     string, which takes [n] time to append each char to. *)
  type t =
    | Leading of (Buffer.t * bool)
    | Content of
        { content : Buffer.t
        ; trailing_whitespace : Buffer.t * bool
        }

  let join_whitespace
    ((ws, seen_newline) : Buffer.t * bool)
    (c : [ `Newline | `Whitespace of char ])
    =
    match seen_newline, c with
    | true, (`Newline | `Whitespace _) | false, `Newline ->
      (* If we've already seen a newline, we ignore all whitespace since this will either
         be trimmed or turned into a single space *)
      Buffer.reset ws;
      ws, true
    | false, `Whitespace c ->
      Buffer.add_char ws c;
      ws, false
  ;;

  let append_char t c =
    let c =
      match c with
      | '\n' | '\r' -> `Newline
      | (' ' | '\t') as c -> `Whitespace c
      | other -> `Content other
    in
    match t, c with
    | Leading leading, ((`Newline | `Whitespace _) as c) ->
      Leading (join_whitespace leading c)
    | Content ({ trailing_whitespace = ws; _ } as t), ((`Newline | `Whitespace _) as c) ->
      Content { t with trailing_whitespace = join_whitespace ws c }
    | Leading (buffer, true), `Content c ->
      let ( (* Reset the buffer since we've seen a newline which means we need to trim the
               leading whitespace *) )
        =
        Buffer.reset buffer
      in
      Buffer.add_char buffer c;
      Content { content = buffer; trailing_whitespace = Buffer.create 16, false }
    | Leading (buffer, false), `Content c ->
      let ( (* We will be reusing the leading whitespace buffer here since we haven't seen
               a newline, which means we want to preserve the whitespace. *) )
        =
        Buffer.add_char buffer c
      in
      Content { content = buffer; trailing_whitespace = Buffer.create 16, false }
    | Content { content; trailing_whitespace = ws, seen_trailing_newline; _ }, `Content c
      ->
      let ( (* Handle the whitespace first since it's the whitespace that has been
               trailing [content].

               In the middle of text content, whitespace containing a newline will be
               translated into a single space, while whitespace that does NOT contain a
               newline is preserved
               https://typescriptlang.org/play/?#code/DwEwlgbgfAUABHAhguAjFG4GN5xAgUxmAHpxog
            *) )
        =
        let () =
          match seen_trailing_newline with
          | true -> Buffer.add_char content ' '
          | false -> Buffer.add_buffer content ws
        in
        Buffer.reset ws
      in
      Buffer.add_char content c;
      Content { content; trailing_whitespace = ws, false }
  ;;

  let finalize = function
    | Leading (_, true) ->
      (* This is whitespace containing a newline. Trim all whitespace, which returns the
         empty string *)
      ""
    | Leading (ws, false) ->
      (* A single line of whitespace without newlines. We return the whitespace directly *)
      Buffer.contents ws
    | Content { content; trailing_whitespace = ws, seen_trailing_newline } ->
      let ( (* Update the contents buffer with the trailing whitespace buffer if no
               newline has been seen *) )
        =
        if not seen_trailing_newline then Buffer.add_buffer content ws;
        Buffer.reset ws
      in
      Buffer.contents content
  ;;
end

(** [collapse_ws] will:
    - In the middle of text content, whitespace containing a newline will be translated
      into a single space, whereas whitespace that does NOT contain a newline is preserved
    - For leading and trailing whitespace, whitespace containing a newline will be
      trimmed, whereas whitespace that does NOT contain a newline is preserved

    This is done to replicate what the TypeScript compiler does with whitespaces for jsx. *)
let collapse_ws s =
  let max_chars = String.length s in
  String.fold
    s
    ~init:(Collapse_ws.Leading (Buffer.create max_chars, false))
    ~f:Collapse_ws.append_char
  |> Collapse_ws.finalize
;;

module Processed_node = struct
  type t =
    | Expression of
        { expr : Expr.t
        ; interpolation_kind : Interpolation_kind.t
        }
    | Element of Element.t
    | Text of string loc

  let finalize ~(whitespace_behavior : [ `Jsx | `Collapse_leading_trailing ]) = function
    | Expression { expr; interpolation_kind } -> Node.Expr { expr; interpolation_kind }
    | Text { txt; loc } ->
      let previous = collapse_prefix_and_trailing_ws txt in
      let txt =
        match whitespace_behavior with
        | `Jsx -> collapse_ws txt
        | `Collapse_leading_trailing ->
          (* This is the old behavior, so we just pass [previous] *)
          previous
      in
      Node.Text { txt; loc }
    | Element element -> Node.Element element
  ;;
end

let many_nodes ~parse_node =
  fix (fun (many_nodes : Processed_node.t list t) ->
    match%bind
      choice
        [ interpolation_case
        ; "<!--" => `Comment
        ; "</" => `Closing
        ; "<" => `Element
        ; (match%bind peek_char with
           | None -> return `End
           | _ -> Angstrom.fail "If you see this error, it is a ppx_html bug.")
        ; return `Text
        ]
    with
    | `Closing | `End -> return []
    | `Expression | `Element | `Text ->
      let%bind current_node = parse_node
      and remaining = many_nodes in
      return (current_node :: remaining)
    | `Comment ->
      let%bind () = skip_comment in
      many_nodes)
;;

let fail_with_closing_tag ~(tag : Tag.t) ~locs =
  let element =
    match tag with
    | Literal (Literal { txt; _ } | Component { name = _; code = { txt; _ }; _ }) ->
      [%string {|element "%{txt}"|}]
    | Expr _ -> "element"
    | Fragment _ -> "fragment"
  in
  peek_char
  >>= function
  | None ->
    let%bind pos in
    fail
      ~locs
      ~snippets:[ pos, pos, "missing '>' here" ]
      [%string
        {|Expected closing '>' to terminate %{element}, but found end of ppx_html expression|}]
  | Some c ->
    let hint =
      match c with
      | '-' ->
        (* Should have errored out before this, but adding for completeness *)
        "ppx_html doesn't allow dashes in HTML tags"
      | ':' -> "attributes use '=' instead of ':' when assigning to a value"
      | _ -> ""
    in
    let%bind pos in
    fail
      ~locs
      ~hint
      ~snippets:[ pos, pos, "expected '>' here" ]
      [%string {|Expected closing '>' to terminate %{element}, but found '%{c#Char}'|}]
;;

let parse_attr_expr ~locs =
  let%map { expr; interpolation_kind } = parse_expr_common ~locs in
  Model.Attr.Expr { expr; interpolation_kind }
;;

let rec parse_attr ~parse_node ~locs : Model.Attr.t Angstrom.t =
  with_loc'
    ~locs
    (match%bind choice [ interpolation_case; argument_case; return `Attr_name ] with
     | `Attr_name ->
       let%bind name = parse_attr_name ~expected:"attribute name" ~locs in
       let%bind value =
         match%bind choice [ "=" => `Equal; return `No_equal ] with
         | `Equal ->
           let%map () = char '='
           and value = parse_value ~locs in
           Some value
         | `No_equal -> return None
       in
       let ret loc = Model.Attr.Attr { name; value; loc } in
       return ret
     | `Argument sigil ->
       let error_message =
         "Expected an OCaml interpolation (e.g. %{}) or HTML element (e.g. (<></>))"
       in
       let%map sigil =
         match sigil with
         | `Tilde -> string "~" *> return Model.Attr.Sigil.Tilde
         | `Question_mark -> string "?" *> return Model.Attr.Sigil.Question_mark
       and name = parse_attr_name ~expected:"argument name" ~locs
       and argument =
         match%bind
           choice [ ":" => `With_payload; "=" => `Equal_typo; return `Punned ]
         with
         | `Punned -> return None
         | `Equal_typo ->
           let%bind pos in
           fail
             ~locs
             ~snippets:[ pos, pos, "expected ':' here" ]
             ~hint:
               "named OCaml function arguments must be prefixed with '~' or '?' and use \
                ':' instead of '=' when providing a value"
             "Expected ':' to provide an argument value, but found '='"
         | `With_payload ->
           let%bind () = string ":" *> return () in
           (match%bind
              choice
                [ "%{" => `Interpolation
                ; "(" => `Element
                ; "<" => `Fail_element_missing_parens
                ; "{" => `Fail_missing_percent
                ; Char.is_alpha >=> `Fail_missing_interpolation
                ; return `Unknown
                ]
            with
            | `Unknown ->
              let%bind pos in
              fail ~locs ~snippets:[ pos, pos, "invalid argument here" ] error_message
            | `Interpolation ->
              let%bind start = pos
              and { expr; interpolation_kind } = parse_expr_common ~locs
              and end_ = pos in
              only_normal_interpolation_allowed
                ~start
                ~end_
                ~locs
                ~interpolation_kind
                (Some (Attr.Argument.Expr expr))
            | `Element ->
              let%bind () = char '('
              and () = skip_opt_ws
              and element = parse_element ~parse_node ~locs
              and () = skip_opt_ws
              and () = char ')' in
              return (Some (Attr.Argument.Element element))
            | `Fail_element_missing_parens ->
              let%bind pos in
              fail
                ~locs
                ~snippets:[ pos, pos, "invalid argument here" ]
                "ppx_html expressions must be wrapped in parentheses when used as \
                 arguments"
            | `Fail_missing_percent ->
              let percent_interpolation = "%{...}" in
              let%bind pos in
              fail
                ~locs
                ~snippets:[ pos, pos, "invalid argument here" ]
                ~hint:
                  [%string
                    "Did you mean to write %{percent_interpolation} instead of {...}? \
                     The percent sign is required."]
                error_message
            | `Fail_missing_interpolation ->
              let percent_interpolation = "%{...}" in
              let%bind pos in
              fail
                ~locs
                ~snippets:[ pos, pos, "invalid argument here" ]
                ~hint:
                  [%string
                    "Did you mean to write %{percent_interpolation}? Bare identifiers \
                     are not allowed here, wrap the expression in \
                     %{percent_interpolation}."]
                error_message)
       in
       let ret loc = Model.Attr.Argument { name; argument; loc; sigil } in
       ret
     | `Expression ->
       let%map expr = parse_attr_expr ~locs in
       fun _ -> expr)

and parse_element ~(parse_node : Processed_node.t t) ~locs : Element.t Angstrom.t =
  with_loc'
    ~locs
    (let%bind { txt = tag, attrs, closed, open_string_relative_location; loc = open_loc } =
       with_loc
         ~locs
         (let%bind start = pos in
          let%bind () = char '<'
          and tag = parse_tag ~locs in
          let%map attrs = many_attrs ~parse_node ~locs ~tag
          and () = skip_opt_ws
          and closed = option (char '/' *> skip_opt_ws) >>| Option.is_some
          and end_ = pos
          and () = char '>' in
          tag, attrs, closed, { String_relative_location.start; end_ })
     in
     let error_message =
       lazy
         (let tag =
            match tag with
            | Model.Tag.Literal
                ( Literal { txt; _ }
                | Component { name = _; code = { txt; _ }; string_relative_location = _ }
                  ) -> txt
            | Model.Tag.Expr { expr; _ } -> Pprintast.string_of_expression expr
            | Model.Tag.Fragment _ -> "fragment (<></>)"
          in
          [%string {|Expected closing tag for '%{tag}'|}])
     in
     let%map inner, closing_tag =
       if closed
       then return (None, None)
       else (
         let%bind inner = many_nodes ~parse_node
         and close_start = pos in
         let inner =
           let whitespace_behavior =
             match tag with
             | Literal (Literal { txt = "script" | "style"; _ }) ->
               (* For script and style tags, we want to preserve the whitespace so that we
                  don't mangle the javascript/css into something unusable. This is a
                  different behavior from how typescript handles script/style tags in JSX *)
               `Collapse_leading_trailing
             | _ -> `Jsx
           in
           List.map inner ~f:(Processed_node.finalize ~whitespace_behavior)
         in
         let fail_closing_tag ?extra_snippet error_message =
           let start = open_string_relative_location.start in
           (* We add 1 here because end_ is actually end_ - 1 for substring reasons *)
           let end_ = open_string_relative_location.end_ + 1 in
           let snippets = Option.to_list extra_snippet in
           fail
             ~start
             ~end_
             ~snippets:((start, end_, "opening tag found here") :: snippets)
             ~locs
             error_message
         in
         let%bind error_pos = pos in
         let%bind `Has_closing =
           let fail_has_closing =
             let error_message = [%string "%{force error_message}, found EOF instead"] in
             let extra_snippet =
               let opening_tag_line =
                 Locations.line_number_of_offset locs open_string_relative_location.start
               in
               let eof_line = Locations.line_number_of_offset locs error_pos in
               (* If the file immediately ends on the same line, we don't print the second
                  caret to avoid clutter *)
               if opening_tag_line = eof_line
               then None
               else Some (error_pos, error_pos, "closing tag expected here")
             in
             fail_closing_tag ?extra_snippet error_message
           in
           match%bind peek_string 2 <|> fail_has_closing with
           | "</" -> return `Has_closing
           | _ -> fail_has_closing
         in
         let%bind () =
           let%map () = char '<'
           and () = char '/' in
           ()
         and () = skip_opt_ws
         and close_tag =
           option (parse_ocaml_name ~expected:"HTML tag" ~locs ~allow_dots:true)
         and () = skip_opt_ws
         and close_end = pos
         and () = char '>' <|> fail_with_closing_tag ~tag ~locs in
         let open struct
           (* NOTE: This is a bit complex. There are roughly three scenarios:

              - "Normal" tags, (e.g. <div></div>): The closing tag _must_ match the
                opening tag.

              - "Interpolated" tags/Fragments (e.g. <></> or <%[{F}]></>): The closing tag
                _must_ be empty.

              - "Component" tags (e.g. <Foo.f></> or <Foo.f></Foo.f>): The closing tag may
                be empty or must match the opening tag.
           *)
           type expected_closing_tag =
             | Empty
             | Empty_or_string of string
             | String of string
         end in
         let expected_closing_tag =
           match tag with
           | Tag.Literal (Literal tag) -> String tag.txt
           | Literal (Component { name; string_relative_location = _; code = _ }) ->
             Empty_or_string (Ppxlib.Longident.name name.txt)
           | Expr _ -> Empty
           | Fragment _ -> Empty
         in
         let ok =
           lazy
             (let loc =
                { String_relative_location.start = close_start; end_ = close_end }
              in
              let is_fragment_like =
                match close_tag with
                | Some { txt = ""; loc = _ } | None -> true
                | _ -> false
              in
              return (Some inner, Some { Closing_tag.loc; is_fragment_like }))
         in
         let fail message =
           fail_closing_tag (* We add one cause close_end is actually the end - 1 *)
             ~extra_snippet:(close_start, close_end + 1, "invalid closing tag here")
             message
         in
         match expected_closing_tag with
         | Empty ->
           (match close_tag with
            | None -> force ok
            | Some wrong ->
              fail
                [%string "Expected an empty (</>) closing tag, but got </%{wrong.txt}>."])
         | String expected ->
           (match close_tag with
            | None ->
              fail
                [%string "Expected </%{expected}>, but got an empty closing tag (</>)."]
            | Some { txt = got; loc = _ } ->
              if String.equal got expected
              then force ok
              else
                fail [%string "Expected closing tag </%{expected}>, but got </%{got}>."])
         | Empty_or_string expected ->
           (match close_tag with
            | None -> force ok
            | Some { txt = got; loc = _ } ->
              if String.equal got expected
              then force ok
              else
                fail [%string "Expected closing tag </%{expected}>, but got </%{got}>."]))
     in
     fun loc ->
       { Element.tag
       ; attrs
       ; inner
       ; loc
       ; open_loc
       ; open_string_relative_location
       ; closing_tag
       })

and many_attrs ~parse_node ~tag ~locs =
  fix (fun many_attrs ->
    match%bind choice [ ">" => `End; "/>" => `End; return `Must_have_whitespace ] with
    | `End -> return []
    | `Must_have_whitespace ->
      let%bind () = skip_req_ws <|> fail_with_closing_tag ~tag ~locs in
      (match%bind choice [ ">" => `End; "/>" => `End; return `Maybe_attr ] with
       | `End -> return []
       | `Maybe_attr ->
         let%map curr = parse_attr ~parse_node ~locs
         and rem = many_attrs in
         curr :: rem))
;;

let string_until_interpolation_or_segment =
  let maybe_take s = s => `Take s in
  fix (fun string_until_interpolation_or_segment ->
    match%bind
      choice
        [ maybe_take "%%{"
        ; "%{" => `Finish
        ; "?{" => `Finish
        ; "*{" => `Finish
        ; "#{" => `Finish
        ; "<" => `Finish
        ; return `Consume
        ]
    with
    | `Finish -> return ()
    | `Take s -> string s *> string_until_interpolation_or_segment
    | `Consume ->
      (match%bind peek_char with
       | None -> return ()
       | Some c -> char c *> string_until_interpolation_or_segment))
;;

let parse_text ~locs : string loc t =
  with_loc
    ~locs
    (consumed string_until_interpolation_or_segment
     >>| fun s ->
     if String.for_all s ~f:Char.is_whitespace && String.mem s '\n'
     then ""
     else String.substr_replace_all ~pattern:"%%" ~with_:"%" s)
;;

let parse_node_expr ~locs =
  let%map { expr; interpolation_kind } = parse_expr_common ~locs in
  Processed_node.Expression { expr; interpolation_kind }
;;

let parse_node ~locs : Processed_node.t Angstrom.t =
  fix (fun parse_node ->
    match%bind choice [ interpolation_case; "<" => `Element; return `Text ] with
    | `Expression -> parse_node_expr ~locs
    | `Element ->
      let%map.Angstrom element = parse_element ~parse_node ~locs in
      Processed_node.Element element
    | `Text ->
      let%map.Angstrom text = parse_text ~locs in
      Processed_node.Text text)
;;

let filter_empty_text_nodes nodes =
  List.filter nodes ~f:(function
    | Node.Text { txt = ""; _ } -> false
    | _ -> true)
;;

let map_nodes =
  object
    inherit Model.Traverse.map as super

    method! element ({ inner; _ } as element) =
      let inner =
        let%map.Option inner in
        filter_empty_text_nodes inner
      in
      let element = { element with inner } in
      super#element element
  end
;;

let map_filter_empty_text_nodes nodes =
  filter_empty_text_nodes nodes |> List.map ~f:map_nodes#node
;;

let parse ~filter_empty_text_nodes ~locs =
  let%bind nodes = many_nodes ~parse_node:(parse_node ~locs) in
  let nodes =
    (* Top-level nodes should all be treated as [jsx] as by definition we cannot be inside
       a [script] tag since this is the top level *)
    List.map nodes ~f:(Processed_node.finalize ~whitespace_behavior:`Jsx)
  in
  let nodes =
    (* We're filtering at the top level so that we can retrieve the empty text nodes if
       necessary. This is useful for tree smashes, as removing the empty text nodes
       actually removes some information about the code that is crucial to generating
       correct treesmashes that deal with whitespace *)
    match filter_empty_text_nodes with
    | true -> map_filter_empty_text_nodes nodes
    | false -> nodes
  in
  match%bind peek_char with
  | None -> return nodes
  | Some _ ->
    (* NOTE: Here is unparsed input. Parsing will fail, but we still do things here to
       provide better error messages. I _think_ that this only happens with
       <div></div></div> though I am unsure it's __ONLY__ in that case hence the `Unknown
       case. *)
    (match%bind choice [ "</" => `Unopened_tag; return `Unknown ] with
     | `Unopened_tag ->
       let%bind pos in
       fail
         ~snippets:[ pos, pos, "closing tag here" ]
         ~locs
         "This closing tag was never opened."
     | `Unknown ->
       fail ~locs "Unparsed input. Please report this bug to ppx_html maintainers.")
;;

let of_string ?(filter_empty_text_nodes = true) ~loc str =
  let locs = Locations.create loc str in
  let parse = parse ~filter_empty_text_nodes ~locs in
  match
    Angstrom.parse_string
      ~consume:All
      (let%bind result = parse in
       let%bind () = end_of_input in
       return result)
      str
  with
  | Ok model -> model
  | Error message ->
    let error =
      let last =
        String.lsplit2 ~on:':' message |> Option.value_map ~f:snd ~default:message
      in
      try Sexp.of_string last |> Error.t_of_sexp with
      | _ -> Error.of_string last
    in
    Error.raise ~loc error
;;

module Private = struct
  let collapse_prefix_and_trailing_ws = collapse_prefix_and_trailing_ws
  let collapse_ws = collapse_ws
end