forked from functionaljava/functionaljava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParser.java
More file actions
1167 lines (1083 loc) · 49.6 KB
/
Parser.java
File metadata and controls
1167 lines (1083 loc) · 49.6 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
package fj.parser;
import fj.F;
import static fj.P.p;
import fj.P1;
import fj.Semigroup;
import fj.Unit;
import fj.Digit;
import static fj.Unit.unit;
import fj.data.List;
import static fj.data.List.cons_;
import fj.data.Stream;
import fj.data.Validation;
import static fj.data.Validation.success;
import static fj.parser.Result.result;
/**
* A parser is a function that takes some input (I) and produces either an error (E) or a parse result (A) and the
* remainder of the input.
*
* @version %build.number%
*/
public final class Parser<I, A, E> {
private final F<I, Validation<E, Result<I, A>>> f;
private Parser(final F<I, Validation<E, Result<I, A>>> f) {
this.f = f;
}
/**
* Parses the input to produce a result or error.
*
* @param i The input to parse.
* @return A parse result with the remaining input or an error.
*/
public Validation<E, Result<I, A>> parse(final I i) {
return f.f(i);
}
/**
* Maps the parse input type through an invariant functor.
*
* @param f The function to covariant map.
* @param g The function to contra-variant map.
* @return A parser with the new input type.
*/
public <Z> Parser<Z, A, E> xmap(final F<I, Z> f, final F<Z, I> g) {
return parser(new F<Z, Validation<E, Result<Z, A>>>() {
public Validation<E, Result<Z, A>> f(final Z z) {
return parse(g.f(z)).map(new F<Result<I, A>, Result<Z, A>>() {
public Result<Z, A> f(final Result<I, A> r) {
return r.mapRest(f);
}
});
}
});
}
/**
* Maps the given result type across this parser.
*
* @param f The function to map.
* @return A parser with the new result type.
*/
public <B> Parser<I, B, E> map(final F<A, B> f) {
return parser(new F<I, Validation<E, Result<I, B>>>() {
public Validation<E, Result<I, B>> f(final I i) {
return parse(i).map(new F<Result<I, A>, Result<I, B>>() {
public Result<I, B> f(final Result<I, A> r) {
return r.mapValue(f);
}
});
}
});
}
/**
* Returns a parser that fails with the given error if the result value does not meet the given predicate.
*
* @param f The predicate to filter on.
* @param e The error to in the event that the predicate is not met.
* @return A parser that fails with the given error if the result value does not meet the given predicate.
*/
public Parser<I, A, E> filter(final F<A, Boolean> f, final E e) {
return parser(new F<I, Validation<E, Result<I, A>>>() {
public Validation<E, Result<I, A>> f(final I i) {
return parse(i).bind(new F<Result<I, A>, Validation<E, Result<I, A>>>() {
public Validation<E, Result<I, A>> f(final Result<I, A> r) {
final A v = r.value();
return f.f(v) ?
Validation.<E, Result<I, A>>success(result(r.rest(), v)) :
Validation.<E, Result<I, A>>fail(e);
}
});
}
});
}
/**
* Binds the given function across the parser with a final join.
*
* @param f The function to apply to the element of this parser.
* @return A new parser after performing the map, then final join.
*/
public <B> Parser<I, B, E> bind(final F<A, Parser<I, B, E>> f) {
return parser(new F<I, Validation<E, Result<I, B>>>() {
public Validation<E, Result<I, B>> f(final I i) {
return parse(i).bind(new F<Result<I, A>, Validation<E, Result<I, B>>>() {
public Validation<E, Result<I, B>> f(final Result<I, A> r) {
return f.f(r.value()).parse(r.rest());
}
});
}
});
}
/**
* Binds the given function across the parsers with a final join.
*
* @param f The function to apply to the element of the parsers.
* @param pb A given parser to bind the given function with.
* @return A new parser after performing the map, then final join.
*/
public <B, C> Parser<I, C, E> bind(final Parser<I, B, E> pb, final F<A, F<B, C>> f) {
return pb.apply(map(f));
}
/**
* Binds the given function across the parsers with a final join.
*
* @param f The function to apply to the element of the parsers.
* @param pb A given parser to bind the given function with.
* @param pc A given parser to bind the given function with.
* @return A new parser after performing the map, then final join.
*/
public <B, C, D> Parser<I, D, E> bind(final Parser<I, B, E> pb, final Parser<I, C, E> pc,
final F<A, F<B, F<C, D>>> f) {
return pc.apply(bind(pb, f));
}
/**
* Binds the given function across the parsers with a final join.
*
* @param f The function to apply to the element of the parsers.
* @param pb A given parser to bind the given function with.
* @param pc A given parser to bind the given function with.
* @param pd A given parser to bind the given function with.
* @return A new parser after performing the map, then final join.
*/
public <B, C, D, E$> Parser<I, E$, E> bind(final Parser<I, B, E> pb, final Parser<I, C, E> pc,
final Parser<I, D, E> pd, final F<A, F<B, F<C, F<D, E$>>>> f) {
return pd.apply(bind(pb, pc, f));
}
/**
* Binds the given function across the parsers with a final join.
*
* @param f The function to apply to the element of the parsers.
* @param pb A given parser to bind the given function with.
* @param pc A given parser to bind the given function with.
* @param pd A given parser to bind the given function with.
* @param pe A given parser to bind the given function with.
* @return A new parser after performing the map, then final join.
*/
public <B, C, D, E$, F$> Parser<I, F$, E> bind(final Parser<I, B, E> pb, final Parser<I, C, E> pc,
final Parser<I, D, E> pd, final Parser<I, E$, E> pe,
final F<A, F<B, F<C, F<D, F<E$, F$>>>>> f) {
return pe.apply(bind(pb, pc, pd, f));
}
/**
* Binds the given function across the parsers with a final join.
*
* @param f The function to apply to the element of the parsers.
* @param pb A given parser to bind the given function with.
* @param pc A given parser to bind the given function with.
* @param pd A given parser to bind the given function with.
* @param pe A given parser to bind the given function with.
* @param pf A given parser to bind the given function with.
* @return A new parser after performing the map, then final join.
*/
public <B, C, D, E$, F$, G> Parser<I, G, E> bind(final Parser<I, B, E> pb, final Parser<I, C, E> pc,
final Parser<I, D, E> pd, final Parser<I, E$, E> pe,
final Parser<I, F$, E> pf,
final F<A, F<B, F<C, F<D, F<E$, F<F$, G>>>>>> f) {
return pf.apply(bind(pb, pc, pd, pe, f));
}
/**
* Binds the given function across the parsers with a final join.
*
* @param f The function to apply to the element of the parsers.
* @param pb A given parser to bind the given function with.
* @param pc A given parser to bind the given function with.
* @param pd A given parser to bind the given function with.
* @param pe A given parser to bind the given function with.
* @param pf A given parser to bind the given function with.
* @param pg A given parser to bind the given function with.
* @return A new parser after performing the map, then final join.
*/
public <B, C, D, E$, F$, G, H> Parser<I, H, E> bind(final Parser<I, B, E> pb, final Parser<I, C, E> pc,
final Parser<I, D, E> pd, final Parser<I, E$, E> pe,
final Parser<I, F$, E> pf, final Parser<I, G, E> pg,
final F<A, F<B, F<C, F<D, F<E$, F<F$, F<G, H>>>>>>> f) {
return pg.apply(bind(pb, pc, pd, pe, pf, f));
}
/**
* Binds the given function across the parsers with a final join.
*
* @param f The function to apply to the element of the parsers.
* @param pb A given parser to bind the given function with.
* @param pc A given parser to bind the given function with.
* @param pd A given parser to bind the given function with.
* @param pe A given parser to bind the given function with.
* @param pf A given parser to bind the given function with.
* @param pg A given parser to bind the given function with.
* @param ph A given parser to bind the given function with.
* @return A new parser after performing the map, then final join.
*/
public <B, C, D, E$, F$, G, H, I$> Parser<I, I$, E> bind(final Parser<I, B, E> pb, final Parser<I, C, E> pc,
final Parser<I, D, E> pd, final Parser<I, E$, E> pe,
final Parser<I, F$, E> pf, final Parser<I, G, E> pg,
final Parser<I, H, E> ph,
final F<A, F<B, F<C, F<D, F<E$, F<F$, F<G, F<H, I$>>>>>>>> f) {
return ph.apply(bind(pb, pc, pd, pe, pf, pg, f));
}
/**
* Binds anonymously, ignoring the result value.
*
* @param p The parser to bind with.
* @return A parser after binding anonymously.
*/
public <B> Parser<I, B, E> sequence(final Parser<I, B, E> p) {
return bind(new F<A, Parser<I, B, E>>() {
public Parser<I, B, E> f(final A a) {
return p;
}
});
}
/**
* Performs function application within a parser.
*
* @param p The parser returning a function value.
* @return A new parser after function application.
*/
public <B> Parser<I, B, E> apply(final Parser<I, F<A, B>, E> p) {
return p.bind(new F<F<A, B>, Parser<I, B, E>>() {
public Parser<I, B, E> f(final F<A, B> f) {
return map(f);
}
});
}
/**
* Returns a parser that tries this parser and if it fails, then tries the given parser.
*
* @param alt The parser to try if this parser fails.
* @return A parser that tries this parser and if it fails, then tries the given parser.
*/
public Parser<I, A, E> or(final P1<Parser<I, A, E>> alt) {
return parser(new F<I, Validation<E, Result<I, A>>>() {
public Validation<E, Result<I, A>> f(final I i) {
return parse(i).f().sequence(alt._1().parse(i));
}
});
}
/**
* Returns a parser that tries this parser and if it fails, then tries the given parser.
*
* @param alt The parser to try if this parser fails.
* @return A parser that tries this parser and if it fails, then tries the given parser.
*/
public Parser<I, A, E> or(final Parser<I, A, E> alt) {
return or(p(alt));
}
/**
* Returns a parser that tries this parser and if it fails, then tries the given parser. If both parsers fail, then
* append their errors with the given semigroup.
*
* @param alt The parser to try if this parser fails.
* @param s The semigroup to append error messages if both parsers fail.
* @return A parser that tries this parser and if it fails, then tries the given parser.
*/
public Parser<I, A, E> or(final P1<Parser<I, A, E>> alt, final Semigroup<E> s) {
return parser(new F<I, Validation<E, Result<I, A>>>() {
public Validation<E, Result<I, A>> f(final I i) {
return parse(i).f().bind(new F<E, Validation<E, Result<I, A>>>() {
public Validation<E, Result<I, A>> f(final E e) {
return alt._1().parse(i).f().map(s.sum(e));
}
});
}
});
}
/**
* Returns a parser that tries this parser and if it fails, then tries the given parser. If both parsers fail, then
* append their errors with the given semigroup.
*
* @param alt The parser to try if this parser fails.
* @param s The semigroup to append error messages if both parsers fail.
* @return A parser that tries this parser and if it fails, then tries the given parser.
*/
public Parser<I, A, E> or(final Parser<I, A, E> alt, final Semigroup<E> s) {
return or(p(alt), s);
}
/**
* Returns a parser that negates this parser. If this parser succeeds, then the returned parser fails and vice versa.
*
* @param e The error message to fail with if this parser succeeds.
* @return A parser that negates this parser.
*/
public Parser<I, Unit, E> not(final P1<E> e) {
return parser(new F<I, Validation<E, Result<I, Unit>>>() {
public Validation<E, Result<I, Unit>> f(final I i) {
return parse(i).isFail() ?
Validation.<E, Result<I, Unit>>success(result(i, unit())) :
Validation.<E, Result<I, Unit>>fail(e._1());
}
});
}
/**
* Returns a parser that negates this parser. If this parser succeeds, then the returned parser fails and vice versa.
*
* @param e The error message to fail with if this parser succeeds.
* @return A parser that negates this parser.
*/
public Parser<I, Unit, E> not(final E e) {
return not(p(e));
}
/**
* Returns a parser that repeats application of this parser zero or many times.
*
* @return A parser that repeats application of this parser zero or many times.
*/
public Parser<I, Stream<A>, E> repeat() {
return repeat1().or(new P1<Parser<I, Stream<A>, E>>() {
public Parser<I, Stream<A>, E> _1() {
return value(Stream.<A>nil());
}
});
}
/**
* Returns a parser that repeats application of this parser one or many times.
*
* @return A parser that repeats application of this parser one or many times.
*/
public Parser<I, Stream<A>, E> repeat1() {
return bind(new F<A, Parser<I, Stream<A>, E>>() {
public Parser<I, Stream<A>, E> f(final A a) {
return repeat().map(new F<Stream<A>, Stream<A>>() {
public Stream<A> f(final Stream<A> as) {
return as.cons(a);
}
});
}
});
}
/**
* Maps the given function across this parser's error.
*
* @param f The function to map this parser's error with.
* @return A new parser with a new error type.
*/
public <K> Parser<I, A, K> mapError(final F<E, K> f) {
return parser(new F<I, Validation<K, Result<I, A>>>() {
public Validation<K, Result<I, A>> f(final I i) {
return Parser.this.f.f(i).f().map(f);
}
});
}
/**
* Returns a parser that computes using the given function.
*
* @param f The function to construct the parser with.
* @return A parser that computes using the given function.
*/
public static <I, A, E> Parser<I, A, E> parser(final F<I, Validation<E, Result<I, A>>> f) {
return new Parser<I, A, E>(f);
}
/**
* Constructs a parser that always returns the given value. The unital for a parser.
*
* @param a The value to consistently return from a parser.
* @return A parser that always returns the given value.
*/
public static <I, A, E> Parser<I, A, E> value(final A a) {
return parser(new F<I, Validation<E, Result<I, A>>>() {
public Validation<E, Result<I, A>> f(final I i) {
return success(result(i, a));
}
});
}
/**
* Returns a parser that always fails with the given error.
*
* @param e The error to fail with.
* @return A parser that always fails with the given error.
*/
public static <I, A, E> Parser<I, A, E> fail(final E e) {
return parser(new F<I, Validation<E, Result<I, A>>>() {
public Validation<E, Result<I, A>> f(final I i) {
return Validation.fail(e);
}
});
}
/**
* Sequence the list of parsers through {@link #bind}.
*
* @param ps The parsers to sequence.
* @return A parser after sequencing.
*/
public static <I, A, E> Parser<I, List<A>, E> sequence(final List<Parser<I, A, E>> ps) {
return ps.isEmpty() ?
Parser.<I, List<A>, E>value(List.<A>nil()) :
ps.head().bind(new F<A, Parser<I, List<A>, E>>() {
public Parser<I, List<A>, E> f(final A a) {
return sequence(ps.tail()).map(cons_(a));
}
});
}
/**
* Parsers that accept {@link Stream} input.
*/
public static final class StreamParser {
private StreamParser() {
}
/**
* Returns a parser that produces an element from the stream if it is available and fails otherwise.
*
* @param e The error to fail with if no element is available.
* @return A parser that produces an element from the stream if it is available and fails otherwise.
*/
public static <I, E> Parser<Stream<I>, I, E> element(final P1<E> e) {
return parser(new F<Stream<I>, Validation<E, Result<Stream<I>, I>>>() {
public Validation<E, Result<Stream<I>, I>> f(final Stream<I> is) {
return is.isEmpty() ?
Validation.<E, Result<Stream<I>, I>>fail(e._1()) :
Validation.<E, Result<Stream<I>, I>>success(result(is.tail()._1(), is.head()));
}
});
}
/**
* Returns a parser that produces an element from the stream if it is available and fails otherwise.
*
* @param e The error to fail with if no element is available.
* @return A parser that produces an element from the stream if it is available and fails otherwise.
*/
public static <I, E> Parser<Stream<I>, I, E> element(final E e) {
return element(p(e));
}
/**
* Returns a parser that produces an element from the stream that satisfies the given predicate, or fails.
*
* @param missing The error if no element is available.
* @param sat The error if the element does not satisfy the predicate.
* @param f The predicate that the element should satisfy.
* @return A parser that produces an element from the stream that satisfies the given predicate, or fails.
*/
public static <I, E> Parser<Stream<I>, I, E> satisfy(final P1<E> missing, final F<I, E> sat,
final F<I, Boolean> f) {
return StreamParser.<I, E>element(missing).bind(new F<I, Parser<Stream<I>, I, E>>() {
public Parser<Stream<I>, I, E> f(final I x) {
return f.f(x) ?
Parser.<Stream<I>, I, E>value(x) :
Parser.<Stream<I>, I, E>fail(sat.f(x));
}
});
}
/**
* Returns a parser that produces an element from the stream that satisfies the given predicate, or fails.
*
* @param missing The error if no element is available.
* @param sat The error if the element does not satisfy the predicate.
* @param f The predicate that the element should satisfy.
* @return A parser that produces an element from the stream that satisfies the given predicate, or fails.
*/
public static <I, E> Parser<Stream<I>, I, E> satisfy(final E missing, final F<I, E> sat, final F<I, Boolean> f) {
return satisfy(p(missing), sat, f);
}
}
/**
* Parsers that accept {@link Stream Stream<Character>} input.
*/
public static final class CharsParser {
private CharsParser() {
}
/**
* Returns a parser that produces a character if one is available or fails with the given error.
*
* @param e The error to fail with if a character is unavailable.
* @return A parser that produces a character if one is available or fails with the given error.
*/
public static <E> Parser<Stream<Character>, Character, E> character(final P1<E> e) {
return StreamParser.element(e);
}
/**
* Returns a parser that produces a character if one is available or fails with the given error.
*
* @param e The error to fail with if a character is unavailable.
* @return A parser that produces a character if one is available or fails with the given error.
*/
public static <E> Parser<Stream<Character>, Character, E> character(final E e) {
return character(p(e));
}
/**
* Returns a parser that produces the given character or fails otherwise.
*
* @param missing The error if no character is available.
* @param sat The error if the produced character is not the one given.
* @param c The character to produce in the parser.
* @return A parser that produces the given character or fails otherwise.
*/
public static <E> Parser<Stream<Character>, Character, E> character(final P1<E> missing, final F<Character, E> sat,
final char c) {
return StreamParser.satisfy(missing, sat, new F<Character, Boolean>() {
public Boolean f(final Character x) {
return x == c;
}
});
}
/**
* Returns a parser that produces the given character or fails otherwise.
*
* @param missing The error if no character is available.
* @param sat The error if the produced character is not the one given.
* @param c The character to produce in the parser.
* @return A parser that produces the given character or fails otherwise.
*/
public static <E> Parser<Stream<Character>, Character, E> character(final E missing, final F<Character, E> sat,
final char c) {
return character(p(missing), sat, c);
}
/**
* Returns a parser that produces the given number of characters, or fails with the given error.
*
* @param missing The error if the given number of characters is unavailable.
* @param n The number of characters to produce in the parse result.
* @return A parser that produces the given number of characters, or fails with the given error.
*/
public static <E> Parser<Stream<Character>, Stream<Character>, E> characters(final P1<E> missing, final int n) {
return n <= 0 ?
Parser.<Stream<Character>, Stream<Character>, E>value(Stream.<Character>nil()) :
character(missing).bind(characters(missing, n - 1), Stream.<Character>cons_());
}
/**
* Returns a parser that produces the given number of characters, or fails with the given error.
*
* @param missing The error if the given number of characters is unavailable.
* @param n The number of characters to produce in the parse result.
* @return A parser that produces the given number of characters, or fails with the given error.
*/
public static <E> Parser<Stream<Character>, Stream<Character>, E> characters(final E missing, final int n) {
return characters(p(missing), n);
}
/**
* Returns a parser that produces the given stream of characters or fails otherwise.
*
* @param missing The error if the producing stream could not supply more characters.
* @param sat The error if a character was produced that is not the given stream of characters.
* @param cs The stream of characters to produce.
* @return A parser that produces the given stream of characters or fails otherwise.
*/
public static <E> Parser<Stream<Character>, Stream<Character>, E> characters(final P1<E> missing,
final F<Character, E> sat,
final Stream<Character> cs) {
return cs.isEmpty() ?
Parser.<Stream<Character>, Stream<Character>, E>value(Stream.<Character>nil()) :
character(missing, sat, cs.head()).bind(characters(missing, sat, cs.tail()._1()), Stream.<Character>cons_());
}
/**
* Returns a parser that produces the given stream of characters or fails otherwise.
*
* @param missing The error if the producing stream could not supply more characters.
* @param sat The error if a character was produced that is not the given stream of characters.
* @param cs The stream of characters to produce.
* @return A parser that produces the given stream of characters or fails otherwise.
*/
public static <E> Parser<Stream<Character>, Stream<Character>, E> characters(final E missing,
final F<Character, E> sat,
final Stream<Character> cs) {
return characters(p(missing), sat, cs);
}
/**
* Returns a parser that produces the given string or fails otherwise.
*
* @param missing The error if the producing stream could not supply more characters.
* @param sat The error if a character was produced that is not the given string.
* @param s The string to produce.
* @return A parser that produces the given string or fails otherwise.
*/
public static <E> Parser<Stream<Character>, String, E> string(final P1<E> missing, final F<Character, E> sat,
final String s) {
return characters(missing, sat, List.fromString(s).toStream()).map(new F<Stream<Character>, String>() {
public String f(final Stream<Character> cs) {
return List.asString(cs.toList());
}
});
}
/**
* Returns a parser that produces the given string or fails otherwise.
*
* @param missing The error if the producing stream could not supply more characters.
* @param sat The error if a character was produced that is not the given string.
* @param s The string to produce.
* @return A parser that produces the given string or fails otherwise.
*/
public static <E> Parser<Stream<Character>, String, E> string(final E missing, final F<Character, E> sat,
final String s) {
return string(p(missing), sat, s);
}
/**
* Returns a parser that produces a digit (0 to 9).
*
* @param missing The error if there is no character on the stream to produce a digit with.
* @param sat The error if the produced character is not a digit.
* @return A parser that produces a digit (0 to 9).
*/
public static <E> Parser<Stream<Character>, Digit, E> digit(final P1<E> missing, final F<Character, E> sat) {
return StreamParser.satisfy(missing, sat, new F<Character, Boolean>() {
public Boolean f(final Character c) {
return Character.isDigit(c);
}
}).map(new F<Character, Digit>() {
public Digit f(final Character c) {
return Digit.fromChar(c).some();
}
});
}
/**
* Returns a parser that produces a digit (0 to 9).
*
* @param missing The error if there is no character on the stream to produce a digit with.
* @param sat The error if the produced character is not a digit.
* @return A parser that produces a digit (0 to 9).
*/
public static <E> Parser<Stream<Character>, Digit, E> digit(final E missing, final F<Character, E> sat) {
return digit(p(missing), sat);
}
/**
* Returns a parser that produces a lower-case character.
*
* @param missing The error if there is no character on the stream to produce a lower-case character with.
* @param sat The error if the produced character is not a lower-case character.
* @return A parser that produces a lower-case character.
* @see Character#isLowerCase(char)
*/
public static <E> Parser<Stream<Character>, Character, E> lower(final P1<E> missing, final F<Character, E> sat) {
return StreamParser.satisfy(missing, sat, new F<Character, Boolean>() {
public Boolean f(final Character c) {
return Character.isLowerCase(c);
}
});
}
/**
* Returns a parser that produces a lower-case character.
*
* @param missing The error if there is no character on the stream to produce a lower-case character with.
* @param sat The error if the produced character is not a lower-case character.
* @return A parser that produces a lower-case character.
* @see Character#isLowerCase(char)
*/
public static <E> Parser<Stream<Character>, Character, E> lower(final E missing, final F<Character, E> sat) {
return lower(p(missing), sat);
}
/**
* Returns a parser that produces a upper-case character.
*
* @param missing The error if there is no character on the stream to produce a upper-case character with.
* @param sat The error if the produced character is not a upper-case character.
* @return A parser that produces a upper-case character.
* @see Character#isUpperCase(char)
*/
public static <E> Parser<Stream<Character>, Character, E> upper(final P1<E> missing, final F<Character, E> sat) {
return StreamParser.satisfy(missing, sat, new F<Character, Boolean>() {
public Boolean f(final Character c) {
return Character.isUpperCase(c);
}
});
}
/**
* Returns a parser that produces a upper-case character.
*
* @param missing The error if there is no character on the stream to produce a upper-case character with.
* @param sat The error if the produced character is not a upper-case character.
* @return A parser that produces a upper-case character.
* @see Character#isUpperCase(char)
*/
public static <E> Parser<Stream<Character>, Character, E> upper(final E missing, final F<Character, E> sat) {
return upper(p(missing), sat);
}
/**
* Returns a parser that produces a defined character.
*
* @param missing The error if there is no character on the stream to produce a defined character with.
* @param sat The error if the produced character is not a defined character.
* @return A parser that produces a defined character.
* @see Character#isDefined(char)
*/
public static <E> Parser<Stream<Character>, Character, E> defined(final P1<E> missing, final F<Character, E> sat) {
return StreamParser.satisfy(missing, sat, new F<Character, Boolean>() {
public Boolean f(final Character c) {
return Character.isDefined(c);
}
});
}
/**
* Returns a parser that produces a defined character.
*
* @param missing The error if there is no character on the stream to produce a defined character with.
* @param sat The error if the produced character is not a defined character.
* @return A parser that produces a defined character.
* @see Character#isDefined(char)
*/
public static <E> Parser<Stream<Character>, Character, E> defined(final E missing, final F<Character, E> sat) {
return defined(p(missing), sat);
}
/**
* Returns a parser that produces a high-surrogate character.
*
* @param missing The error if there is no character on the stream to produce a high-surrogate character with.
* @param sat The error if the produced character is not a high-surrogate character.
* @return A parser that produces a high-surrogate character.
* @see Character#isHighSurrogate(char)
*/
public static <E> Parser<Stream<Character>, Character, E> highSurrogate(final P1<E> missing,
final F<Character, E> sat) {
return StreamParser.satisfy(missing, sat, new F<Character, Boolean>() {
public Boolean f(final Character c) {
return Character.isHighSurrogate(c);
}
});
}
/**
* Returns a parser that produces a high-surrogate character.
*
* @param missing The error if there is no character on the stream to produce a high-surrogate character with.
* @param sat The error if the produced character is not a high-surrogate character.
* @return A parser that produces a high-surrogate character.
* @see Character#isHighSurrogate(char)
*/
public static <E> Parser<Stream<Character>, Character, E> highSurrogate(final E missing,
final F<Character, E> sat) {
return highSurrogate(p(missing), sat);
}
/**
* Returns a parser that produces an identifier-ignorable character.
*
* @param missing The error if there is no character on the stream to produce an identifier-ignorable character with.
* @param sat The error if the produced character is not an identifier-ignorable character.
* @return A parser that produces an identifier-ignorable character.
* @see Character#isIdentifierIgnorable(char)
*/
public static <E> Parser<Stream<Character>, Character, E> identifierIgnorable(final P1<E> missing,
final F<Character, E> sat) {
return StreamParser.satisfy(missing, sat, new F<Character, Boolean>() {
public Boolean f(final Character c) {
return Character.isIdentifierIgnorable(c);
}
});
}
/**
* Returns a parser that produces an identifier-ignorable character.
*
* @param missing The error if there is no character on the stream to produce an identifier-ignorable character with.
* @param sat The error if the produced character is not an identifier-ignorable character.
* @return A parser that produces an identifier-ignorable character.
* @see Character#isIdentifierIgnorable(char)
*/
public static <E> Parser<Stream<Character>, Character, E> identifierIgnorable(final E missing,
final F<Character, E> sat) {
return identifierIgnorable(p(missing), sat);
}
/**
* Returns a parser that produces an ISO control character.
*
* @param missing The error if there is no character on the stream to produce an ISO control character with.
* @param sat The error if the produced character is not an ISO control character.
* @return A parser that produces an ISO control character.
* @see Character#isISOControl(char)
*/
public static <E> Parser<Stream<Character>, Character, E> isoControl(final P1<E> missing,
final F<Character, E> sat) {
return StreamParser.satisfy(missing, sat, new F<Character, Boolean>() {
public Boolean f(final Character c) {
return Character.isISOControl(c);
}
});
}
/**
* Returns a parser that produces an ISO control character.
*
* @param missing The error if there is no character on the stream to produce an ISO control character with.
* @param sat The error if the produced character is not an ISO control character.
* @return A parser that produces an ISO control character.
* @see Character#isISOControl(char)
*/
public static <E> Parser<Stream<Character>, Character, E> isoControl(final E missing, final F<Character, E> sat) {
return isoControl(p(missing), sat);
}
/**
* Returns a parser that produces a Java identifier part character.
*
* @param missing The error if there is no character on the stream to produce a Java identifier part character with.
* @param sat The error if the produced character is not a Java identifier part character.
* @return A parser that produces a Java identifier part character.
* @see Character#isJavaIdentifierPart(char)
*/
public static <E> Parser<Stream<Character>, Character, E> javaIdentifierPart(final P1<E> missing,
final F<Character, E> sat) {
return StreamParser.satisfy(missing, sat, new F<Character, Boolean>() {
public Boolean f(final Character c) {
return Character.isJavaIdentifierPart(c);
}
});
}
/**
* Returns a parser that produces a Java identifier part character.
*
* @param missing The error if there is no character on the stream to produce a Java identifier part character with.
* @param sat The error if the produced character is not a Java identifier part character.
* @return A parser that produces a Java identifier part character.
* @see Character#isJavaIdentifierPart(char)
*/
public static <E> Parser<Stream<Character>, Character, E> javaIdentifierPart(final E missing,
final F<Character, E> sat) {
return javaIdentifierPart(p(missing), sat);
}
/**
* Returns a parser that produces a Java identifier start character.
*
* @param missing The error if there is no character on the stream to produce a Java identifier start character with.
* @param sat The error if the produced character is not a Java identifier start character.
* @return A parser that produces a Java identifier start character.
* @see Character#isJavaIdentifierStart(char)
*/
public static <E> Parser<Stream<Character>, Character, E> javaIdentifierStart(final P1<E> missing,
final F<Character, E> sat) {
return StreamParser.satisfy(missing, sat, new F<Character, Boolean>() {
public Boolean f(final Character c) {
return Character.isJavaIdentifierStart(c);
}
});
}
/**
* Returns a parser that produces a Java identifier start character.
*
* @param missing The error if there is no character on the stream to produce a Java identifier start character with.
* @param sat The error if the produced character is not a Java identifier start character.
* @return A parser that produces a Java identifier start character.
* @see Character#isJavaIdentifierStart(char)
*/
public static <E> Parser<Stream<Character>, Character, E> javaIdentifierStart(final E missing,
final F<Character, E> sat) {
return javaIdentifierStart(p(missing), sat);
}
/**
* Returns a parser that produces an alpha character.
*
* @param missing The error if there is no character on the stream to produce an alpha character with.
* @param sat The error if the produced character is not an alpha character.
* @return A parser that produces an alpha character.
* @see Character#isLetter(char)
*/
public static <E> Parser<Stream<Character>, Character, E> alpha(final P1<E> missing, final F<Character, E> sat) {
return StreamParser.satisfy(missing, sat, new F<Character, Boolean>() {
public Boolean f(final Character c) {
return Character.isLetter(c);
}
});
}
/**
* Returns a parser that produces an alpha character.
*
* @param missing The error if there is no character on the stream to produce an alpha character with.
* @param sat The error if the produced character is not an alpha character.
* @return A parser that produces an alpha character.
* @see Character#isLetter(char)
*/
public static <E> Parser<Stream<Character>, Character, E> alpha(final E missing, final F<Character, E> sat) {
return alpha(p(missing), sat);
}
/**
* Returns a parser that produces an alpha-numeric character.
*
* @param missing The error if there is no character on the stream to produce an alpha-numeric character with.
* @param sat The error if the produced character is not an alpha-numeric character.
* @return A parser that produces an alpha-numeric character.
* @see Character#isLetterOrDigit(char)
*/
public static <E> Parser<Stream<Character>, Character, E> alphaNum(final P1<E> missing, final F<Character, E> sat) {
return StreamParser.satisfy(missing, sat, new F<Character, Boolean>() {
public Boolean f(final Character c) {
return Character.isLetterOrDigit(c);
}
});
}
/**
* Returns a parser that produces an alpha-numeric character.
*
* @param missing The error if there is no character on the stream to produce an alpha-numeric character with.
* @param sat The error if the produced character is not an alpha-numeric character.
* @return A parser that produces an alpha-numeric character.
* @see Character#isLetterOrDigit(char)
*/
public static <E> Parser<Stream<Character>, Character, E> alphaNum(final E missing, final F<Character, E> sat) {
return alphaNum(p(missing), sat);
}
/**
* Returns a parser that produces a low-surrogate character.
*
* @param missing The error if there is no character on the stream to produce a low-surrogate character with.
* @param sat The error if the produced character is not a low-surrogate character.
* @return A parser that produces a low-surrogate character.
* @see Character#isLowSurrogate(char)
*/
public static <E> Parser<Stream<Character>, Character, E> lowSurrogate(final P1<E> missing,
final F<Character, E> sat) {
return StreamParser.satisfy(missing, sat, new F<Character, Boolean>() {
public Boolean f(final Character c) {
return Character.isLowSurrogate(c);
}
});
}
/**
* Returns a parser that produces a low-surrogate character.
*
* @param missing The error if there is no character on the stream to produce a low-surrogate character with.
* @param sat The error if the produced character is not a low-surrogate character.
* @return A parser that produces a low-surrogate character.
* @see Character#isLowSurrogate(char)
*/
public static <E> Parser<Stream<Character>, Character, E> lowSurrogate(final E missing, final F<Character, E> sat) {
return lowSurrogate(p(missing), sat);
}
/**
* Returns a parser that produces a mirrored character.
*
* @param missing The error if there is no character on the stream to produce a mirrored character with.
* @param sat The error if the produced character is not a mirrored character.
* @return A parser that produces a mirrored character.
* @see Character#isMirrored(char)
*/