-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbark.ml
987 lines (850 loc) · 21.9 KB
/
bark.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
(* Helpers *)
let slice : int -> int -> string -> string =
fun lo hi s ->
String.sub s lo (hi - lo)
let uncons : string -> (char * string) option =
fun s ->
let len =
String.length s
in
if len = 0 then
None
else
Some (String.get s 0, String.sub s 1 (len - 1))
(* Character Predicates *)
(* Source: https://stackoverflow.com/a/49184157/1157526 *)
let is_alpha : char -> bool =
function
| 'a' .. 'z'
| 'A' .. 'Z' ->
true
| _ ->
false
let is_num : char -> bool =
function
| '0' .. '9' ->
true
| _ ->
false
(* Low-Level Helpers *)
let is_sub_string : string -> int -> int -> int -> string -> int * int * int =
fun small_string offset row col big_string ->
let small_length =
String.length small_string
in
let (offset', row', col') =
(ref offset, ref row, ref col)
in
let is_good =
ref (offset + small_length <= String.length big_string)
in
let i =
ref 0
in
while (!is_good && !i < small_length) do
let big_letter =
String.get big_string !offset'
in
is_good :=
String.get small_string !i = big_letter;
i :=
!i + 1;
offset' :=
!offset' + 1;
begin if big_letter = '\n' then
begin
row' := !row' + 1;
col' := 1
end
else
begin
col' := !col' + 1
end
end
done;
( (if !is_good then !offset' else -1)
, !row'
, !col'
)
let is_sub_char : (char -> bool) -> int -> string -> int =
fun predicate offset string ->
if String.length string <= offset then
-1
else if predicate (String.get string offset) then
if String.get string offset = '\n' then
-2
else
offset + 1
else
-1
let find_sub_string : string -> int -> int -> int -> string -> int * int * int =
fun small_string offset row col big_string ->
let small_re =
Str.regexp_string small_string
in
let new_offset =
try
Str.search_forward small_re big_string offset
with
Not_found ->
-1
in
let target =
if new_offset < 0 then
String.length big_string
else
new_offset + String.length small_string
in
let (offset', row', col') =
(ref offset, ref row, ref col)
in
while (!offset' < target) do
offset' :=
!offset' + 1;
begin if String.get big_string !offset' = '\n' then
begin
row' := !row' + 1;
col' := 1
end
else
begin
col' := !col' + 1
end
end
done;
( new_offset
, !row'
, !col'
)
(* Parsers *)
type 'context located =
{ row : int
; col : int
; context : 'context
}
type 'context state =
{ src : string
; offset : int
; indent : int
; context : 'context located list
; row : int
; col :int
}
type ('context, 'problem) dead_end =
{ row : int
; col : int
; problem : 'problem
; context_stack : 'context located list
}
type ('c, 'x) bag =
| Empty
| AddRight of ('c, 'x) bag * ('c, 'x) dead_end
| Append of ('c, 'x) bag * ('c, 'x) bag
type ('context, 'problem, 'value) pstep =
| Good of bool * 'value * 'context state
| Bad of bool * ('context, 'problem) bag
type ('context, 'problem, 'value) parser =
'context state -> ('context, 'problem, 'value) pstep
(* Problems *)
let from_state : 'c state -> 'x -> ('c, 'x) bag =
fun s x ->
AddRight
( Empty
, { row = s.row
; col = s.col
; problem = x
; context_stack = s.context
}
)
let from_info : int -> int -> 'x -> 'c located list -> ('c, 'x) bag =
fun row col x context ->
AddRight
( Empty
, { row = row
; col = col
; problem = x
; context_stack = context
}
)
let rec bag_to_list :
('c, 'x) bag -> ('c, 'x) dead_end list -> ('c, 'x) dead_end list =
fun bag ls ->
match bag with
| Empty ->
ls
| AddRight (bag1, x) ->
bag_to_list bag1 (x :: ls)
| Append (bag1, bag2) ->
bag_to_list bag1 (bag_to_list bag2 ls)
(* Run *)
let run : ('c, 'x, 'a) parser -> string -> ('a, ('c, 'x) dead_end list) result =
fun parse src ->
match
parse
{ src = src
; offset = 0
; indent = 1
; context = []
; row = 1
; col = 1
}
with
| Good (_, value, _) ->
Ok value
| Bad (_, bag) ->
Error (bag_to_list bag [])
(* Primitives *)
let succeed : 'a -> ('c, 'x, 'a) parser =
fun a ->
fun s -> Good (false, a, s)
let problem : 'x -> ('c, 'x, 'a) parser =
fun x ->
fun s -> Bad (false, from_state s x)
(* Mapping *)
let map : ('a -> 'b) -> ('c, 'x, 'a) parser -> ('c, 'x, 'b) parser =
fun func parse ->
fun s0 ->
match parse s0 with
| Good (p, a, s1) ->
Good (p, func a, s1)
| Bad (p, x) ->
Bad (p, x)
let map2 :
('a -> 'b -> 'value) ->
('c, 'x, 'a) parser ->
('c, 'x, 'b) parser ->
('c, 'x, 'value) parser =
fun func parse_a parse_b ->
fun s0 ->
match parse_a s0 with
| Bad (p, x) ->
Bad (p, x)
| Good (p1, a, s1) ->
begin match parse_b s1 with
| Bad (p2, x) ->
Bad (p1 || p2, x)
| Good (p2, b, s2) ->
Good (p1 || p2, func a b, s2)
end
let keeper :
('c, 'x, 'a -> 'b) parser ->
('c, 'x, 'a) parser ->
('c, 'x, 'b) parser =
fun parse_func parse_arg ->
map2 (@@) parse_func parse_arg
let ignorer :
('c, 'x, 'keep) parser ->
('c, 'x, 'ignore) parser ->
('c, 'x, 'keep) parser =
fun keep_parser ignore_parser ->
map2 (fun k _ -> k) keep_parser ignore_parser
(* And Then *)
let and_then :
('a -> ('c, 'x, 'b) parser) ->
('c, 'x, 'a) parser ->
('c, 'x, 'b) parser =
fun callback parse_a ->
fun s0 ->
match parse_a s0 with
| Bad (p, x) ->
Bad (p, x)
| Good (p1, a, s1) ->
let parse_b =
callback a
in
begin match parse_b s1 with
| Bad (p2, x) ->
Bad (p1 || p2, x)
| Good (p2, b, s2) ->
Good (p1 || p2, b, s2)
end
(* Lazily *)
let lazily : (unit -> ('c, 'x, 'a) parser) -> ('c, 'x, 'a) parser =
fun thunk ->
fun s ->
let parse =
thunk ()
in
parse s
(* One Of *)
let rec one_of_help :
'c state ->
('c, 'x) bag ->
('c, 'x, 'a) parser list ->
('c, 'x, 'a) pstep =
fun s0 bag parsers ->
match parsers with
| [] ->
Bad (false, bag)
| parse :: remaining_parsers ->
begin match parse s0 with
| Good (_, _, _) as step ->
step
| Bad (p, x) as step ->
if p then
step
else
one_of_help s0 (Append (bag, x)) remaining_parsers
end
let one_of : ('c, 'x, 'a) parser list -> ('c, 'x, 'a) parser =
fun parsers ->
fun s ->
one_of_help s Empty parsers
(* Loop *)
type ('state, 'a) step =
| Loop of 'state
| Done of 'a
let rec loop_help :
bool ->
'state ->
('state -> ('c, 'x, ('state, 'a) step) parser) ->
'c state ->
('c, 'x, 'a) pstep =
fun p state callback s0 ->
let parse =
callback state
in
match parse s0 with
| Good (p1, step, s1) ->
begin match step with
| Loop new_state ->
loop_help (p || p1) new_state callback s1
| Done result ->
Good (p || p1, result, s1)
end
| Bad (p1, x) ->
Bad (p || p1, x)
let loop :
'state ->
('state -> ('c, 'x, ('state, 'a) step) parser) ->
('c, 'x, 'a) parser =
fun state callback ->
fun s ->
loop_help false state callback s
(* Backtrackable *)
let backtrackable : ('c, 'x, 'a) parser -> ('c, 'x, 'a) parser =
fun parse ->
fun s0 ->
match parse s0 with
| Bad (_, x) ->
Bad (false, x)
| Good (_, a, s1) ->
Good (false, a, s1)
let commit : 'a -> ('c, 'x, 'a) parser =
fun a ->
fun s ->
Good (true, a, s)
(* Token *)
type 'x token =
| Token of string * 'x
let token : 'x token -> ('c, 'x, unit) parser =
fun (Token (str, expecting)) ->
let progress =
str <> ""
in
fun s ->
let (new_offset, new_row, new_col) =
is_sub_string str s.offset s.row s.col s.src
in
if new_offset = -1 then
Bad (false, from_state s expecting)
else
Good
( progress
, ()
, { src = s.src
; offset = new_offset
; indent = s.indent
; context = s.context
; row = new_row
; col = new_col
}
)
(* Symbol *)
let symbol : 'x token -> ('c, 'x, unit) parser =
token
(* Keyword *)
let keyword : 'x token -> ('c, 'x, unit) parser =
fun (Token (kwd, expecting)) ->
let progress =
kwd <> ""
in
fun s ->
let (new_offset, new_row, new_col) =
is_sub_string kwd s.offset s.row s.col s.src
in
if
new_offset = -1 ||
0 <= is_sub_char
(fun c -> is_alpha c || is_num c || c = '_')
new_offset
s.src
then
Bad (false, from_state s expecting)
else
Good
( progress
, ()
, { src = s.src
; offset = new_offset
; indent = s.indent
; context = s.context
; row = new_row
; col = new_col
}
)
(* End *)
let endd : 'x -> ('c, 'x, unit) parser =
fun x ->
fun s ->
if String.length s.src = s.offset then
Good (false, (), s)
else
Bad (false, from_state s x)
(* Chomped Strings *)
let map_chomped_string :
(string -> 'a -> 'b) -> ('c, 'x, 'a) parser -> ('c, 'x, 'b) parser =
fun func parse ->
fun s0 ->
match parse s0 with
| Bad (p, x) ->
Bad (p, x)
| Good (p, a, s1) ->
Good (p, func (slice s0.offset s1.offset s0.src) a, s1)
let get_chomped_string : ('c, 'x, 'a) parser -> ('c, 'x, string) parser =
fun parse ->
map_chomped_string (fun s _ -> s) parse
(* Chomp If *)
let chomp_if : (char -> bool) -> 'x -> ('c, 'x, unit) parser =
fun is_good expecting ->
fun s ->
let new_offset =
is_sub_char is_good s.offset s.src
in
if new_offset = -1 then
Bad (false, from_state s expecting)
else if new_offset = -2 then
Good
( true
, ()
, { src = s.src
; offset = s.offset + 1
; indent = s.indent
; context = s.context
; row = s.row + 1
; col = 1
}
)
else
Good
( true
, ()
, { src = s.src
; offset = new_offset
; indent = s.indent
; context = s.context
; row = s.row
; col = s.col + 1
}
)
(* Chomp While *)
let rec chomp_while_help :
(char -> bool) ->
int -> int -> int ->
'c state ->
('c, 'x, unit) pstep =
fun is_good offset row col s0 ->
let new_offset =
is_sub_char is_good offset s0.src
in
if new_offset = -1 then
Good
( s0.offset < offset
, ()
, { src = s0.src
; offset = offset
; indent = s0.indent
; context = s0.context
; row = row
; col = col
}
)
else if new_offset = -2 then
chomp_while_help is_good (offset + 1) (row + 1) 1 s0
else
chomp_while_help is_good new_offset row (col + 1) s0
let chomp_while : (char -> bool) -> ('c, 'x, unit) parser =
fun is_good ->
fun s ->
chomp_while_help is_good s.offset s.row s.col s
(* Chomp Until *)
let chomp_until : 'x token -> ('c, 'x, unit) parser =
fun (Token (str, expecting)) ->
fun s ->
let (new_offset, new_row, new_col) =
find_sub_string str s.offset s.row s.col s.src
in
if new_offset = -1 then
Bad (false, from_info new_row new_col expecting s.context)
else
Good
( s.offset < new_offset
, ()
, { src = s.src
; offset = new_offset
; indent = s.indent
; context = s.context
; row = new_row
; col = new_col
}
)
let chomp_until_end_or : string -> ('c, 'x, unit) parser =
fun str ->
fun s ->
let (new_offset, new_row, new_col) =
find_sub_string str s.offset s.row s.col s.src
in
let adjusted_offset =
if new_offset < 0 then
String.length s.src
else
new_offset
in
Good
( s.offset < adjusted_offset
, ()
, { src = s.src
; offset = adjusted_offset
; indent = s.indent
; context = s.context
; row = new_row
; col = new_col
}
)
(* Context *)
let change_context : 'c located list -> 'c state -> 'c state =
fun new_context s ->
{ src = s.src
; offset = s.offset
; indent = s.indent
; context = new_context
; row = s.row
; col = s.col
}
let in_context :
'context ->
('context, 'x, 'a) parser ->
('context, 'x, 'a) parser =
fun context parse ->
fun s0 ->
match
parse @@
change_context
( { row = s0.row
; col = s0.col
; context = context
} :: s0.context
)
s0
with
| Good (p, a, s1) ->
Good (p, a, change_context s0.context s1)
| Bad (_, _) as step ->
step
(* Indentation *)
let get_indent : ('c, 'x, int) parser =
fun s ->
Good (false, s.indent, s)
let change_indent : int -> 'c state -> 'c state =
fun new_indent s ->
{ src = s.src
; offset = s.offset
; indent = new_indent
; context = s.context
; row = s.row
; col = s.col
}
let with_indent : int -> ('c, 'x, 'a) parser -> ('c, 'x, 'a) parser =
fun new_indent parse ->
fun s0 ->
match parse (change_indent new_indent s0) with
| Good (p, a, s1) ->
Good (p, a, change_indent s0.indent s1)
| Bad (p, x) ->
Bad (p, x)
(* Position *)
let get_position : ('c, 'x, int * int) parser =
fun s ->
Good (false, (s.row, s.col), s)
let get_row : ('c, 'x, int) parser =
fun s ->
Good (false, s.row, s)
let get_col : ('c, 'x, int) parser =
fun s ->
Good (false, s.col, s)
let get_offset : ('c, 'x, int) parser =
fun s ->
Good (false, s.offset, s)
let get_source : ('c, 'x, string) parser =
fun s ->
Good (false, s.src, s)
(* Variables *)
let rec var_help :
(char -> bool) ->
int -> int -> int ->
string ->
int ->
'c located list ->
'c state =
fun is_good offset row col src indent context ->
let new_offset =
is_sub_char is_good offset src
in
if new_offset = -1 then
{ src = src
; offset = offset
; indent = indent
; context = context
; row = row
; col = col
}
else if new_offset = -2 then
var_help is_good (offset + 1) (row + 1) 1 src indent context
else
var_help is_good new_offset row (col + 1) src indent context
module String_set =
Set.Make(String)
let variable ~start ~inner ~reserved ~expecting =
fun s ->
let first_offset =
is_sub_char start s.offset s.src
in
if first_offset = -1 then
Bad (false, from_state s expecting)
else
let s1 =
if first_offset = -2 then
var_help
inner (s.offset + 1) (s.row + 1) 1 s.src s.indent s.context
else
var_help
inner first_offset s.row (s.col + 1) s.src s.indent s.context
in
let name =
slice s.offset s1.offset s.src
in
if String_set.mem name reserved then
Bad (false, from_state s expecting)
else
Good (true, name, s1)
(* Sequences *)
let skip :
('c, 'x, 'ignore) parser ->
('c, 'x, 'keep) parser ->
('c, 'x, 'keep) parser =
fun ignore_parser keep_parser ->
map2 (fun _ k -> k) ignore_parser keep_parser
type trailing =
| Forbidden
| Optional
| Mandatory
let sequence_end_forbidden :
('c, 'x, unit) parser ->
('c, 'x, unit) parser ->
('c, 'x, 'a) parser ->
('c, 'x, unit) parser ->
'a list ->
('c, 'x, ('a list, 'a list) step) parser =
fun ender ws parse_item sep rev_items ->
skip ws @@
one_of
[ skip sep @@ skip ws @@
map (fun item -> Loop (item :: rev_items)) parse_item
; ender |> map (fun _ -> Done (List.rev rev_items))
]
let sequence_end_optional :
('c, 'x, unit) parser ->
('c, 'x, unit) parser ->
('c, 'x, 'a) parser ->
('c, 'x, unit) parser ->
'a list ->
('c, 'x, ('a list, 'a list) step) parser =
fun ender ws parse_item sep rev_items ->
let parse_end =
map (fun _ -> Done (List.rev rev_items)) ender
in
skip ws @@
one_of
[ skip sep @@ skip ws @@
one_of
[ parse_item |> map (fun item -> Loop (item :: rev_items))
; parse_end
]
; parse_end
]
let sequence_end_mandatory :
('c, 'x, unit) parser ->
('c, 'x, 'a) parser ->
('c, 'x, unit) parser ->
'a list ->
('c, 'x, ('a list, 'a list) step) parser =
fun ws parse_item sep rev_items ->
one_of
[ map (fun item -> Loop (item :: rev_items)) @@
ignorer parse_item (ignorer ws (ignorer sep ws))
; map (fun _ -> Done (List.rev rev_items)) (succeed ())
]
let sequence_end :
('c, 'x, unit) parser ->
('c, 'x, unit) parser ->
('c, 'x, 'a) parser ->
('c, 'x, unit) parser ->
trailing ->
('c, 'x, 'a list) parser =
fun ender ws parse_item sep trailing ->
let chomp_rest item =
match trailing with
| Forbidden ->
loop [item] (sequence_end_forbidden ender ws parse_item sep)
| Optional ->
loop [item] (sequence_end_optional ender ws parse_item sep)
| Mandatory ->
ignorer
( skip ws @@ skip sep @@ skip ws @@
loop [item] (sequence_end_mandatory ws parse_item sep)
)
ender
in
one_of
[ parse_item |> and_then chomp_rest
; ender |> map (fun _ -> [])
]
let sequence ~start ~separator ~endd ~spaces ~item ~trailing =
skip (token start) @@
skip spaces @@
sequence_end (token endd) spaces item (token separator) trailing
(* Whitespace *)
let spaces : ('c, 'x, unit) parser =
fun s ->
chomp_while (fun c -> c = ' ' || c = '\n' || c = '\r') s
let line_comment : 'x token -> ('c, 'x, unit) parser =
fun start ->
ignorer (token start) (chomp_until_end_or "\n")
let rec nestable_help :
(char -> bool) ->
('c, 'x, unit) parser ->
('c, 'x, unit) parser ->
'x ->
int ->
('c, 'x, unit) parser =
fun is_not_relevant openn close expecting_close nest_level ->
skip (chomp_while is_not_relevant) @@
one_of
[ if nest_level = 1 then
close
else
close
|> and_then
( fun _ ->
nestable_help
is_not_relevant
openn
close
expecting_close
(nest_level - 1)
)
; openn
|> and_then
( fun _ ->
nestable_help
is_not_relevant
openn
close
expecting_close
(nest_level + 1)
)
; chomp_if (fun _ -> true) expecting_close
|> and_then
( fun _ ->
nestable_help
is_not_relevant
openn
close
expecting_close
nest_level
)
]
let nestable_comment : 'x token -> 'x token -> ('c, 'x, unit) parser =
fun (Token (o_str, o_x) as openn) (Token (c_str, c_x) as close) ->
match uncons o_str with
| None ->
problem o_x
| Some (open_char, _) ->
begin match uncons c_str with
| None ->
problem c_x
| Some (close_char, _) ->
let is_not_relevant c =
c <> open_char && c <> close_char
in
let chomp_open =
token openn
in
ignorer
chomp_open
(nestable_help is_not_relevant chomp_open (token close) c_x 1)
end
type nestable =
| NotNestable
| Nestable
let multi_comment : 'x token -> 'x token -> nestable -> ('c, 'x, unit) parser =
fun openn close nestable ->
match nestable with
| NotNestable ->
ignorer (token openn) (chomp_until close)
| Nestable ->
nestable_comment openn close
(* Syntax *)
module Syntax = struct
let ( let+ ) p f =
map f p
let ( and+ ) pa pb =
map2 (fun x y -> (x, y)) pa pb
let ( and* ) pa pb =
( and+ ) pa pb
let ( let* ) p f =
p |> and_then f
end
let (|=) = keeper
let (|.) = ignorer
(* Int *)
let int : 'x -> ('c, 'x, int) parser =
fun expecting ->
let open Syntax in
let* s =
get_chomped_string (chomp_while is_num)
in
if s = "" then
problem expecting
else
succeed (int_of_string s)
(* Float *)
let float : 'x -> 'x -> ('c, 'x, float) parser =
fun expecting invalid ->
let open Syntax in
let* s1 =
get_chomped_string (chomp_while is_num)
in
if s1 = "" then
problem expecting
else
let* _ =
symbol (Token (".", invalid))
in
let* s2 =
get_chomped_string (chomp_while is_num)
in
if s2 = "" then
problem invalid
else
succeed (float_of_string @@ s1 ^ "." ^ s2)