-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathparser.ml
More file actions
2248 lines (2079 loc) · 53.1 KB
/
parser.ml
File metadata and controls
2248 lines (2079 loc) · 53.1 KB
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
open Ast
open Compat
module Sub : sig
type t
val of_string : string -> t
val to_string : t -> string
val offset : int -> t -> t
val lexbuf : t -> Lexing.lexbuf
val contains : string -> t -> bool
val print : Format.formatter -> t -> unit
val head : ?rev:unit -> t -> char option
val tail : ?rev:unit -> t -> t
val heads : int -> t -> char list
val tails : int -> t -> t
val for_all : (char -> bool) -> t -> bool
val exists : (char -> bool) -> t -> bool
val is_empty : t -> bool
val length : t -> int
val sub : len:int -> t -> t
end = struct
type t =
{ base : string
; off : int
; len : int
}
let of_string base = { base; off = 0; len = String.length base }
let to_string { base; off; len } = String.sub base off len
let print ppf s = Format.fprintf ppf "%S" (to_string s)
let length { len; _ } = len
let offset n { base; off; len } =
if n < 0 then invalid_arg "offset";
let rec loop n base off len =
if n = 0 || len = 0 then
{ base; off; len }
else
match base.[off] with
| '\t' ->
let ts = ((off + 4) / 4 * 4) - off in
let b = Buffer.create len in
Buffer.add_substring b base 0 off;
for _ = 1 to ts do
Buffer.add_char b ' '
done;
Buffer.add_substring b base (off + 1) (len - 1);
loop n (Buffer.contents b) off (len + ts - 1)
| _ -> loop (n - 1) base (off + 1) (len - 1)
in
loop n base off len
let lexbuf s = Lexing.from_string (to_string s)
let contains s1 { base; off; len } =
let rec loop off =
if off + String.length s1 > len then
false
else
s1 = String.sub base off (String.length s1) || loop (off + 1)
in
loop off
let head ?rev s =
match (rev, s) with
| _, { len = 0; _ } -> None
| None, { base; off; _ } -> Some base.[off]
| Some (), { base; off; len } -> Some base.[off + len - 1]
let tail ?rev s =
match (rev, s) with
| _, { len = 0; _ } -> s
| None, { base; off; len } -> { base; off = succ off; len = pred len }
| Some (), { base; off; len } -> { base; off; len = pred len }
let heads n s =
if n < 0 then invalid_arg "heads";
let rec loop n s =
if n = 0 || length s = 0 then
[]
else
match head s with
| Some c -> c :: loop (pred n) (tail s)
| None -> []
in
loop n s
let tails n s =
if n < 0 then invalid_arg "tails";
let rec loop n s =
if n = 0 then
s
else
loop (pred n) (tail s)
in
loop n s
let is_empty s = length s = 0
let exists f s =
let rec loop s i =
if i >= s.len then
false
else if f s.base.[s.off + i] then
true
else
loop s (succ i)
in
loop s 0
let for_all f s = not (exists (fun c -> not (f c)) s)
let sub ~len s =
if len > s.len then invalid_arg "sub";
{ s with len }
end
exception Fail
module P : sig
type state
type 'a t = state -> 'a
val of_string : string -> state
val peek : char option t
val peek_exn : char t
val pos : state -> int
val range : state -> int -> int -> string
val set_pos : state -> int -> unit
val junk : unit t
val char : char -> unit t
val next : char t
val ( ||| ) : 'a t -> 'a t -> 'a t
val ws : unit t
val sp : unit t
val ws1 : unit t
val ( >>> ) : unit t -> 'a t -> 'a t
val ( <<< ) : 'a t -> unit t -> 'a t
val protect : 'a t -> 'a t
val peek_before : char -> state -> char
val peek_after : char -> state -> char
val pair : 'a t -> 'b t -> ('a * 'b) t
end = struct
type state =
{ str : string
; mutable pos : int
}
let of_string str = { str; pos = 0 }
type 'a t = state -> 'a
let char c st =
if st.pos >= String.length st.str then raise Fail;
if st.str.[st.pos] <> c then raise Fail;
st.pos <- st.pos + 1
let next st =
if st.pos >= String.length st.str then
raise Fail
else
let c = st.str.[st.pos] in
st.pos <- st.pos + 1;
c
let peek_exn st =
if st.pos >= String.length st.str then
raise Fail
else
st.str.[st.pos]
let peek st =
if st.pos >= String.length st.str then
None
else
Some st.str.[st.pos]
let peek_before c st =
if st.pos = 0 then
c
else
st.str.[st.pos - 1]
let peek_after c st =
if st.pos + 1 >= String.length st.str then
c
else
st.str.[st.pos + 1]
let pos st = st.pos
let range st pos n = String.sub st.str pos n
let set_pos st pos = st.pos <- pos
let junk st = if st.pos < String.length st.str then st.pos <- st.pos + 1
let protect p st =
let off = pos st in
try p st with
| e ->
set_pos st off;
raise e
let ( ||| ) p1 p2 st =
try protect p1 st with
| Fail -> p2 st
let ws st =
let rec loop () =
match peek_exn st with
| ' '
| '\t'
| '\010' .. '\013' ->
junk st;
loop ()
| _ -> ()
in
try loop () with
| Fail -> ()
let sp st =
let rec loop () =
match peek_exn st with
| ' '
| '\t' ->
junk st;
loop ()
| _ -> ()
in
try loop () with
| Fail -> ()
let ws1 st =
match peek_exn st with
| ' '
| '\t'
| '\010' .. '\013' ->
junk st;
ws st
| _ -> raise Fail
let ( >>> ) p q st =
p st;
q st
let ( <<< ) p q st =
let x = p st in
q st;
x
let pair p q st =
let x = p st in
let y = q st in
(x, y)
end
type html_kind =
| Hcontains of string list
| Hblank
type code_block_kind =
| Tilde
| Backtick
type t =
| Lempty
| Lblockquote of Sub.t
| Lthematic_break
| Latx_heading of int * string * attributes
| Lsetext_heading of int * int
| Lfenced_code of int * int * code_block_kind * (string * string) * attributes
| Lindented_code of Sub.t
| Lhtml of bool * html_kind
| Llist_item of list_type * int * Sub.t
| Lparagraph
| Ldef_list of int * string
let sp3 s =
match Sub.head s with
| Some ' ' -> (
let s = Sub.tail s in
match Sub.head s with
| Some ' ' -> (
let s = Sub.tail s in
match Sub.head s with
| Some ' ' -> (3, Sub.tail s)
| Some _
| None ->
(2, s))
| Some _
| None ->
(1, s))
| Some _
| None ->
(0, s)
let ( ||| ) p1 p2 s =
try p1 s with
| Fail -> p2 s
let rec ws ?rev s =
match Sub.head ?rev s with
| Some (' ' | '\t' | '\010' .. '\013') -> ws ?rev (Sub.tail ?rev s)
| None
| Some _ ->
s
let is_empty s = Sub.is_empty (ws s)
let thematic_break s =
match Sub.head s with
| Some (('*' | '_' | '-') as c) ->
let rec loop n s =
match Sub.head s with
| Some c1 when c = c1 -> loop (succ n) (Sub.tail s)
| Some (' ' | '\t' | '\010' .. '\013') -> loop n (Sub.tail s)
| Some _ -> raise Fail
| None ->
if n < 3 then raise Fail;
Lthematic_break
in
loop 1 (Sub.tail s)
| Some _
| None ->
raise Fail
let setext_heading s =
match Sub.head s with
| Some (('-' | '=') as c) ->
let rec loop n s =
match Sub.head s with
| Some c1 when c = c1 -> loop (succ n) (Sub.tail s)
| Some _
| None ->
if not (Sub.is_empty (ws s)) then raise Fail;
if c = '-' && n = 1 then raise Fail;
(* can be interpreted as an empty list item *)
Lsetext_heading
( (if c = '-' then
2
else
1)
, n )
in
loop 1 (Sub.tail s)
| Some _
| None ->
raise Fail
let is_punct = function
| '!'
| '"'
| '#'
| '$'
| '%'
| '&'
| '\''
| '('
| ')'
| '*'
| '+'
| ','
| '-'
| '.'
| '/'
| ':'
| ';'
| '<'
| '='
| '>'
| '?'
| '@'
| '['
| '\\'
| ']'
| '^'
| '_'
| '`'
| '{'
| '|'
| '}'
| '~' ->
true
| _ -> false
let parse_attributes = function
| None -> []
| Some s -> (
let attributes = String.split_on_char ' ' s in
let f (id, classes, acc) s =
if s = "" then
(id, classes, acc)
else
match s.[0] with
| '#' -> (Some (String.sub s 1 (String.length s - 1)), classes, acc)
| '.' -> (id, String.sub s 1 (String.length s - 1) :: classes, acc)
| _ -> (
let attr = String.split_on_char '=' s in
match attr with
| [] -> (id, classes, acc)
| h :: t -> (id, classes, (h, String.concat "=" t) :: acc))
in
let id, classes, acc = List.fold_left f (None, [], []) attributes in
let acc = List.rev acc in
let acc =
if classes <> [] then
("class", String.concat " " (List.rev classes)) :: acc
else
acc
in
match id with
| Some id -> ("id", id) :: acc
| None -> acc)
let attribute_string s =
let buf = Buffer.create 64 in
let rec loop s =
match Sub.head s with
| None -> (Sub.of_string (Buffer.contents buf), None)
| Some ('\\' as c) -> (
let s = Sub.tail s in
match Sub.head s with
| Some c when is_punct c ->
Buffer.add_char buf c;
loop (Sub.tail s)
| Some _
| None ->
Buffer.add_char buf c;
loop s)
| Some '{' ->
let buf' = Buffer.create 64 in
let rec loop' s =
match Sub.head s with
| Some '}' -> (
let s = Sub.tail s in
match Sub.head s with
| Some _ ->
Buffer.add_char buf '{';
Buffer.add_buffer buf buf';
Buffer.add_char buf '}';
loop s
| None ->
( Sub.of_string (Buffer.contents buf)
, Some (Buffer.contents buf') ))
| None ->
Buffer.add_char buf '{';
Buffer.add_buffer buf buf';
(Sub.of_string (Buffer.contents buf), None)
| Some '{' ->
Buffer.add_char buf '{';
Buffer.add_buffer buf buf';
Buffer.reset buf';
loop' (Sub.tail s)
| Some c ->
Buffer.add_char buf' c;
loop' (Sub.tail s)
in
loop' (Sub.tail s)
| Some c ->
Buffer.add_char buf c;
loop (Sub.tail s)
in
let s', a = loop (ws s) in
(s', parse_attributes a)
let atx_heading s =
let rec loop n s =
if n > 6 then raise Fail;
match Sub.head s with
| Some '#' -> loop (succ n) (Sub.tail s)
| Some (' ' | '\t' | '\010' .. '\013') ->
let s, a =
match Sub.head ~rev:() s with
| Some '}' -> attribute_string s
| _ -> (s, [])
in
let s = ws ~rev:() (ws s) in
let rec loop t =
match Sub.head ~rev:() t with
| Some '#' -> loop (Sub.tail ~rev:() t)
| Some (' ' | '\t' | '\010' .. '\013')
| None ->
ws ~rev:() t
| Some _ -> s
in
Latx_heading (n, Sub.to_string (ws (loop s)), a)
| Some _ -> raise Fail
| None -> Latx_heading (n, Sub.to_string s, [])
in
loop 0 s
let entity s =
match Sub.heads 2 s with
| '#' :: ('x' | 'X') :: _ ->
let rec loop m n s =
if m > 8 then raise Fail;
match Sub.head s with
| Some ('a' .. 'f' as c) ->
loop
(succ m)
((n * 16) + Char.code c - Char.code 'a' + 10)
(Sub.tail s)
| Some ('A' .. 'F' as c) ->
loop
(succ m)
((n * 16) + Char.code c - Char.code 'A' + 10)
(Sub.tail s)
| Some ('0' .. '9' as c) ->
loop (succ m) ((n * 16) + Char.code c - Char.code '0') (Sub.tail s)
| Some ';' ->
if m = 0 then raise Fail;
let u =
if n = 0 || not (Uchar.is_valid n) then
Uchar.rep
else
Uchar.of_int n
in
([ u ], Sub.tail s)
| Some _
| None ->
raise Fail
in
loop 0 0 (Sub.tails 2 s)
| '#' :: _ ->
let rec loop m n s =
if m > 8 then raise Fail;
match Sub.head s with
| Some ('0' .. '9' as c) ->
loop (succ m) ((n * 10) + Char.code c - Char.code '0') (Sub.tail s)
| Some ';' ->
if m = 0 then raise Fail;
let u =
if n = 0 || not (Uchar.is_valid n) then
Uchar.rep
else
Uchar.of_int n
in
([ u ], Sub.tail s)
| Some _
| None ->
raise Fail
in
loop 0 0 (Sub.tail s)
| ('a' .. 'z' | 'A' .. 'Z') :: _ ->
let rec loop len t =
match Sub.head t with
| Some ('a' .. 'z' | 'A' .. 'Z' | '0' .. '9') ->
loop (succ len) (Sub.tail t)
| Some ';' -> (
let name = Sub.to_string (Sub.sub ~len s) in
match Entities.f name with
| [] -> raise Fail
| cps -> (cps, Sub.tail t))
| Some _
| None ->
raise Fail
in
loop 1 (Sub.tail s)
| _ -> raise Fail
let info_string c s =
let buf = Buffer.create 17 in
let s, a =
match Sub.head ~rev:() s with
| Some '}' -> attribute_string s
| _ -> (s, [])
in
let s = ws ~rev:() (ws s) in
let rec loop s =
match Sub.head s with
| Some (' ' | '\t' | '\010' .. '\013')
| None ->
if
c = '`'
&& Sub.exists
(function
| '`' -> true
| _ -> false)
s
then
raise Fail;
((Buffer.contents buf, Sub.to_string (ws s)), a)
| Some '`' when c = '`' -> raise Fail
| Some ('\\' as c) -> (
let s = Sub.tail s in
match Sub.head s with
| Some c when is_punct c ->
Buffer.add_char buf c;
loop (Sub.tail s)
| Some _
| None ->
Buffer.add_char buf c;
loop s)
| Some ('&' as c) -> (
let s = Sub.tail s in
match entity s with
| ul, s ->
List.iter (Buffer.add_utf_8_uchar buf) ul;
loop s
| exception Fail ->
Buffer.add_char buf c;
loop s)
| Some c ->
Buffer.add_char buf c;
loop (Sub.tail s)
in
loop (ws s)
let fenced_code ind s =
match Sub.head s with
| Some (('`' | '~') as c) ->
let rec loop n s =
match Sub.head s with
| Some c1 when c = c1 -> loop (succ n) (Sub.tail s)
| Some _
| None ->
if n < 3 then raise Fail;
let s, a = info_string c s in
let c =
if c = '`' then
Backtick
else
Tilde
in
Lfenced_code (ind, n, c, s, a)
in
loop 1 (Sub.tail s)
| Some _
| None ->
raise Fail
let indent s =
let rec loop n s =
match Sub.head s with
| Some ' ' -> loop (n + 1) (Sub.tail s)
| Some '\t' -> loop (n + 4) (Sub.tail s)
| Some _
| None ->
n
in
loop 0 s
let unordered_list_item ind s =
match Sub.head s with
| Some (('+' | '-' | '*') as c) ->
let s = Sub.tail s in
if is_empty s then
Llist_item (Bullet c, 2 + ind, s)
else
let n = indent s in
if n = 0 then raise Fail;
let n =
if n <= 4 then
n
else
1
in
Llist_item (Bullet c, n + 1 + ind, Sub.offset n s)
| Some _
| None ->
raise Fail
let ordered_list_item ind s =
let rec loop n m s =
match Sub.head s with
| Some ('0' .. '9' as c) ->
if n >= 9 then raise Fail;
loop (succ n) ((m * 10) + Char.code c - Char.code '0') (Sub.tail s)
| Some (('.' | ')') as c) ->
let s = Sub.tail s in
if is_empty s then
Llist_item (Ordered (m, c), n + 1 + ind, s)
else
let ind' = indent s in
if ind' = 0 then raise Fail;
let ind' =
if ind' <= 4 then
ind'
else
1
in
Llist_item (Ordered (m, c), n + ind + ind' + 1, Sub.offset ind' s)
| Some _
| None ->
raise Fail
in
loop 0 0 s
let tag_name s0 =
match Sub.head s0 with
| Some ('a' .. 'z' | 'A' .. 'Z') ->
let rec loop len s =
match Sub.head s with
| Some ('a' .. 'z' | 'A' .. 'Z' | '0' .. '9' | '-') ->
loop (succ len) (Sub.tail s)
| Some _
| None ->
(Sub.to_string (Sub.sub s0 ~len), s)
in
loop 1 (Sub.tail s0)
| Some _
| None ->
raise Fail
let known_tags =
[ "address"
; "aside"
; "base"
; "basefont"
; "blockquote"
; "body"
; "caption"
; "center"
; "col"
; "colgroup"
; "dd"
; "details"
; "dialog"
; "dir"
; "div"
; "dl"
; "dt"
; "fieldset"
; "figcaption"
; "figure"
; "footer"
; "form"
; "frame"
; "frameset"
; "h1"
; "h2"
; "h3"
; "h4"
; "h5"
; "h6"
; "head"
; "header"
; "hr"
; "html"
; "iframe"
; "legend"
; "li"
; "link"
; "main"
; "menu"
; "menuitem"
; "meta"
; "nav"
; "noframes"
; "ol"
; "optgroup"
; "option"
; "p"
; "param"
; "section"
; "source"
; "summary"
; "table"
; "tbody"
; "td"
; "tfoot"
; "th"
; "thead"
; "title"
; "tr"
; "track"
; "ul"
]
let special_tags = [ "script"; "pre"; "style" ]
let known_tag s =
let s = String.lowercase_ascii s in
List.mem s known_tags
let special_tag s =
let s = String.lowercase_ascii s in
List.mem s special_tags
let closing_tag s =
let s = ws s in
match Sub.head s with
| Some '>' ->
if not (is_empty (Sub.tail s)) then raise Fail;
Lhtml (false, Hblank)
| Some _
| None ->
raise Fail
let special_tag tag s =
if not (special_tag tag) then raise Fail;
match Sub.head s with
| Some (' ' | '\t' | '\010' .. '\013' | '>')
| None ->
Lhtml (true, Hcontains [ "</script>"; "</pre>"; "</style>" ])
| Some _ -> raise Fail
let known_tag tag s =
if not (known_tag tag) then raise Fail;
match Sub.heads 2 s with
| (' ' | '\t' | '\010' .. '\013') :: _
| []
| '>' :: _
| '/' :: '>' :: _ ->
Lhtml (true, Hblank)
| _ -> raise Fail
let ws1 s =
match Sub.head s with
| Some (' ' | '\t' | '\010' .. '\013') -> ws s
| Some _
| None ->
raise Fail
let attribute_name s =
match Sub.head s with
| Some ('a' .. 'z' | 'A' .. 'Z' | '_' | ':') ->
let rec loop s =
match Sub.head s with
| Some ('a' .. 'z' | 'A' .. 'Z' | '_' | '.' | ':' | '0' .. '9') ->
loop (Sub.tail s)
| Some _
| None ->
s
in
loop s
| Some _
| None ->
raise Fail
let attribute_value s =
match Sub.head s with
| Some (('\'' | '"') as c) ->
let rec loop s =
match Sub.head s with
| Some c1 when c = c1 -> Sub.tail s
| Some _ -> loop (Sub.tail s)
| None -> raise Fail
in
loop (Sub.tail s)
| Some _ ->
let rec loop first s =
match Sub.head s with
| Some
(' ' | '\t' | '\010' .. '\013' | '"' | '\'' | '=' | '<' | '>' | '`')
| None ->
if first then raise Fail;
s
| Some _ -> loop false (Sub.tail s)
in
loop true s
| None -> raise Fail
let attribute s =
let s = ws1 s in
let s = attribute_name s in
let s = ws s in
match Sub.head s with
| Some '=' ->
let s = ws (Sub.tail s) in
attribute_value s
| Some _
| None ->
s
let attributes s =
let rec loop s =
match attribute s with
| s -> loop s
| exception Fail -> s
in
loop s
let open_tag s =
let s = attributes s in
let s = ws s in
let n =
match Sub.heads 2 s with
| '/' :: '>' :: _ -> 2
| '>' :: _ -> 1
| _ -> raise Fail
in
if not (is_empty (Sub.tails n s)) then raise Fail;
Lhtml (false, Hblank)
let raw_html s =
match Sub.heads 10 s with
| '<' :: '?' :: _ -> Lhtml (true, Hcontains [ "?>" ])
| '<' :: '!' :: '-' :: '-' :: _ -> Lhtml (true, Hcontains [ "-->" ])
| '<' :: '!' :: '[' :: 'C' :: 'D' :: 'A' :: 'T' :: 'A' :: '[' :: _ ->
Lhtml (true, Hcontains [ "]]>" ])
| '<' :: '!' :: _ -> Lhtml (true, Hcontains [ ">" ])
| '<' :: '/' :: _ ->
let tag, s = tag_name (Sub.tails 2 s) in
(known_tag tag ||| closing_tag) s
| '<' :: _ ->
let tag, s = tag_name (Sub.tails 1 s) in
(special_tag tag ||| known_tag tag ||| open_tag) s
| _ -> raise Fail
let blank s =
if not (is_empty s) then raise Fail;
Lempty
let tag_string s =
let buf = Buffer.create 17 in
let s, a =
match Sub.head ~rev:() s with
| Some '}' -> attribute_string s
| _ -> (s, [])
in
let s = ws ~rev:() (ws s) in
let rec loop s =
match Sub.head s with
| Some (' ' | '\t' | '\010' .. '\013')
| None ->
(Buffer.contents buf, a)
| Some c ->
Buffer.add_char buf c;
loop (Sub.tail s)
in
loop (ws s)
let def_list ind s =
let s = Sub.tail s in
match Sub.head s with
| Some (' ' | '\t' | '\010' .. '\013') ->
Ldef_list (ind + 2, String.trim (Sub.to_string s))
| _ -> raise Fail
let indented_code ind s =
if indent s + ind < 4 then raise Fail;
Lindented_code (Sub.offset (4 - ind) s)
let parse s0 =
let ind, s = sp3 s0 in
match Sub.head s with
| Some '>' ->
let s = Sub.offset 1 s in
let s =
if indent s > 0 then
Sub.offset 1 s
else
s
in
Lblockquote s
| Some '=' -> setext_heading s
| Some '-' ->
(setext_heading ||| thematic_break ||| unordered_list_item ind) s
| Some '_' -> thematic_break s
| Some '#' -> atx_heading s
| Some ('~' | '`') -> fenced_code ind s
| Some '<' -> raw_html s
| Some '*' -> (thematic_break ||| unordered_list_item ind) s
| Some '+' -> unordered_list_item ind s
| Some '0' .. '9' -> ordered_list_item ind s
| Some ':' -> def_list ind s
| Some _ -> (blank ||| indented_code ind) s
| None -> Lempty
let parse s =
try parse s with