001    package fj.parser;
002    
003    import fj.F;
004    import static fj.P.p;
005    import fj.P1;
006    import fj.Unit;
007    import fj.Digit;
008    import static fj.Unit.unit;
009    import fj.data.List;
010    import static fj.data.List.cons_;
011    import fj.data.Stream;
012    import fj.data.Validation;
013    import static fj.data.Validation.success;
014    import static fj.parser.Result.result;
015    import fj.pre.Semigroup;
016    
017    /**
018     * A parser is a function that takes some input (I) and produces either an error (E) or a parse result (A) and the
019     * remainder of the input.
020     *
021     * @version %build.number%<br>
022     *          <ul>
023     *          <li>$LastChangedRevision: 122 $</li>
024     *          <li>$LastChangedDate: 2009-04-25 08:24:38 +1000 (Sat, 25 Apr 2009) $</li>
025     *          </ul>
026     */
027    public class Parser<I, A, E> {
028      private final F<I, Validation<E, Result<I, A>>> f;
029    
030      private Parser(final F<I, Validation<E, Result<I, A>>> f) {
031        this.f = f;
032      }
033    
034      /**
035       * Parses the input to produce a result or error.
036       *
037       * @param i The input to parse.
038       * @return A parse result with the remaining input or an error.
039       */
040      public Validation<E, Result<I, A>> parse(final I i) {
041        return f.f(i);
042      }
043    
044      /**
045       * Maps the parse input type through an invariant functor.
046       *
047       * @param f The function to covariant map.
048       * @param g The function to contra-variant map.
049       * @return A parser with the new input type.
050       */
051      public <Z> Parser<Z, A, E> xmap(final F<I, Z> f, final F<Z, I> g) {
052        return parser(new F<Z, Validation<E, Result<Z, A>>>() {
053          public Validation<E, Result<Z, A>> f(final Z z) {
054            return parse(g.f(z)).map(new F<Result<I, A>, Result<Z, A>>() {
055              public Result<Z, A> f(final Result<I, A> r) {
056                return r.mapRest(f);
057              }
058            });
059          }
060        });
061      }
062    
063      /**
064       * Maps the given result type across this parser.
065       *
066       * @param f The function to map.
067       * @return A parser with the new result type.
068       */
069      public <B> Parser<I, B, E> map(final F<A, B> f) {
070        return parser(new F<I, Validation<E, Result<I, B>>>() {
071          public Validation<E, Result<I, B>> f(final I i) {
072            return parse(i).map(new F<Result<I, A>, Result<I, B>>() {
073              public Result<I, B> f(final Result<I, A> r) {
074                return r.mapValue(f);
075              }
076            });
077          }
078        });
079      }
080    
081      /**
082       * Returns a parser that fails with the given error if the result value does not meet the given predicate.
083       *
084       * @param f The predicate to filter on.
085       * @param e The error to in the event that the predicate is not met.
086       * @return A parser that fails with the given error if the result value does not meet the given predicate.
087       */
088      public Parser<I, A, E> filter(final F<A, Boolean> f, final E e) {
089        return parser(new F<I, Validation<E, Result<I, A>>>() {
090          public Validation<E, Result<I, A>> f(final I i) {
091            return parse(i).bind(new F<Result<I, A>, Validation<E, Result<I, A>>>() {
092              public Validation<E, Result<I, A>> f(final Result<I, A> r) {
093                final A v = r.value();
094                return f.f(v) ?
095                    Validation.<E, Result<I, A>>success(result(r.rest(), v)) :
096                    Validation.<E, Result<I, A>>fail(e);
097              }
098            });
099          }
100        });
101      }
102    
103      /**
104       * Binds the given function across the parser with a final join.
105       *
106       * @param f The function to apply to the element of this parser.
107       * @return A new parser after performing the map, then final join.
108       */
109      public <B> Parser<I, B, E> bind(final F<A, Parser<I, B, E>> f) {
110        return parser(new F<I, Validation<E, Result<I, B>>>() {
111          public Validation<E, Result<I, B>> f(final I i) {
112            return parse(i).bind(new F<Result<I, A>, Validation<E, Result<I, B>>>() {
113              public Validation<E, Result<I, B>> f(final Result<I, A> r) {
114                return f.f(r.value()).parse(r.rest());
115              }
116            });
117          }
118        });
119      }
120    
121      /**
122       * Binds the given function across the parsers with a final join.
123       *
124       * @param f  The function to apply to the element of the parsers.
125       * @param pb A given parser to bind the given function with.
126       * @return A new parser after performing the map, then final join.
127       */
128      public <B, C> Parser<I, C, E> bind(final Parser<I, B, E> pb, final F<A, F<B, C>> f) {
129        return pb.apply(map(f));
130      }
131    
132      /**
133       * Binds the given function across the parsers with a final join.
134       *
135       * @param f  The function to apply to the element of the parsers.
136       * @param pb A given parser to bind the given function with.
137       * @param pc A given parser to bind the given function with.
138       * @return A new parser after performing the map, then final join.
139       */
140      public <B, C, D> Parser<I, D, E> bind(final Parser<I, B, E> pb, final Parser<I, C, E> pc,
141                                            final F<A, F<B, F<C, D>>> f) {
142        return pc.apply(bind(pb, f));
143      }
144    
145      /**
146       * Binds the given function across the parsers with a final join.
147       *
148       * @param f  The function to apply to the element of the parsers.
149       * @param pb A given parser to bind the given function with.
150       * @param pc A given parser to bind the given function with.
151       * @param pd A given parser to bind the given function with.
152       * @return A new parser after performing the map, then final join.
153       */
154      public <B, C, D, E$> Parser<I, E$, E> bind(final Parser<I, B, E> pb, final Parser<I, C, E> pc,
155                                                 final Parser<I, D, E> pd, final F<A, F<B, F<C, F<D, E$>>>> f) {
156        return pd.apply(bind(pb, pc, f));
157      }
158    
159      /**
160       * Binds the given function across the parsers with a final join.
161       *
162       * @param f  The function to apply to the element of the parsers.
163       * @param pb A given parser to bind the given function with.
164       * @param pc A given parser to bind the given function with.
165       * @param pd A given parser to bind the given function with.
166       * @param pe A given parser to bind the given function with.
167       * @return A new parser after performing the map, then final join.
168       */
169      public <B, C, D, E$, F$> Parser<I, F$, E> bind(final Parser<I, B, E> pb, final Parser<I, C, E> pc,
170                                                     final Parser<I, D, E> pd, final Parser<I, E$, E> pe,
171                                                     final F<A, F<B, F<C, F<D, F<E$, F$>>>>> f) {
172        return pe.apply(bind(pb, pc, pd, f));
173      }
174    
175      /**
176       * Binds the given function across the parsers with a final join.
177       *
178       * @param f  The function to apply to the element of the parsers.
179       * @param pb A given parser to bind the given function with.
180       * @param pc A given parser to bind the given function with.
181       * @param pd A given parser to bind the given function with.
182       * @param pe A given parser to bind the given function with.
183       * @param pf A given parser to bind the given function with.
184       * @return A new parser after performing the map, then final join.
185       */
186      public <B, C, D, E$, F$, G> Parser<I, G, E> bind(final Parser<I, B, E> pb, final Parser<I, C, E> pc,
187                                                       final Parser<I, D, E> pd, final Parser<I, E$, E> pe,
188                                                       final Parser<I, F$, E> pf,
189                                                       final F<A, F<B, F<C, F<D, F<E$, F<F$, G>>>>>> f) {
190        return pf.apply(bind(pb, pc, pd, pe, f));
191      }
192    
193      /**
194       * Binds the given function across the parsers with a final join.
195       *
196       * @param f  The function to apply to the element of the parsers.
197       * @param pb A given parser to bind the given function with.
198       * @param pc A given parser to bind the given function with.
199       * @param pd A given parser to bind the given function with.
200       * @param pe A given parser to bind the given function with.
201       * @param pf A given parser to bind the given function with.
202       * @param pg A given parser to bind the given function with.
203       * @return A new parser after performing the map, then final join.
204       */
205      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,
206                                                          final Parser<I, D, E> pd, final Parser<I, E$, E> pe,
207                                                          final Parser<I, F$, E> pf, final Parser<I, G, E> pg,
208                                                          final F<A, F<B, F<C, F<D, F<E$, F<F$, F<G, H>>>>>>> f) {
209        return pg.apply(bind(pb, pc, pd, pe, pf, f));
210      }
211    
212      /**
213       * Binds the given function across the parsers with a final join.
214       *
215       * @param f  The function to apply to the element of the parsers.
216       * @param pb A given parser to bind the given function with.
217       * @param pc A given parser to bind the given function with.
218       * @param pd A given parser to bind the given function with.
219       * @param pe A given parser to bind the given function with.
220       * @param pf A given parser to bind the given function with.
221       * @param pg A given parser to bind the given function with.
222       * @param ph A given parser to bind the given function with.
223       * @return A new parser after performing the map, then final join.
224       */
225      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,
226                                                               final Parser<I, D, E> pd, final Parser<I, E$, E> pe,
227                                                               final Parser<I, F$, E> pf, final Parser<I, G, E> pg,
228                                                               final Parser<I, H, E> ph,
229                                                               final F<A, F<B, F<C, F<D, F<E$, F<F$, F<G, F<H, I$>>>>>>>> f) {
230        return ph.apply(bind(pb, pc, pd, pe, pf, pg, f));
231      }
232    
233      /**
234       * Binds anonymously, ignoring the result value.
235       *
236       * @param p The parser to bind with.
237       * @return A parser after binding anonymously.
238       */
239      public <B> Parser<I, B, E> sequence(final Parser<I, B, E> p) {
240        return bind(new F<A, Parser<I, B, E>>() {
241          public Parser<I, B, E> f(final A a) {
242            return p;
243          }
244        });
245      }
246    
247      /**
248       * Performs function application within a parser.
249       *
250       * @param p The parser returning a function value.
251       * @return A new parser after function application.
252       */
253      public <B> Parser<I, B, E> apply(final Parser<I, F<A, B>, E> p) {
254        return p.bind(new F<F<A, B>, Parser<I, B, E>>() {
255          public Parser<I, B, E> f(final F<A, B> f) {
256            return map(f);
257          }
258        });
259      }
260    
261      /**
262       * Returns a parser that tries this parser and if it fails, then tries the given parser.
263       *
264       * @param alt The parser to try if this parser fails.
265       * @return A parser that tries this parser and if it fails, then tries the given parser.
266       */
267      public Parser<I, A, E> or(final P1<Parser<I, A, E>> alt) {
268        return parser(new F<I, Validation<E, Result<I, A>>>() {
269          public Validation<E, Result<I, A>> f(final I i) {
270            return parse(i).f().sequence(alt._1().parse(i));
271          }
272        });
273      }
274    
275      /**
276       * Returns a parser that tries this parser and if it fails, then tries the given parser.
277       *
278       * @param alt The parser to try if this parser fails.
279       * @return A parser that tries this parser and if it fails, then tries the given parser.
280       */
281      public Parser<I, A, E> or(final Parser<I, A, E> alt) {
282        return or(p(alt));
283      }
284    
285      /**
286       * Returns a parser that tries this parser and if it fails, then tries the given parser. If both parsers fail, then
287       * append their errors with the given semigroup.
288       *
289       * @param alt The parser to try if this parser fails.
290       * @param s   The semigroup to append error messages if both parsers fail.
291       * @return A parser that tries this parser and if it fails, then tries the given parser.
292       */
293      public Parser<I, A, E> or(final P1<Parser<I, A, E>> alt, final Semigroup<E> s) {
294        return parser(new F<I, Validation<E, Result<I, A>>>() {
295          public Validation<E, Result<I, A>> f(final I i) {
296            return parse(i).f().bind(new F<E, Validation<E, Result<I, A>>>() {
297              public Validation<E, Result<I, A>> f(final E e) {
298                return alt._1().parse(i).f().map(s.sum(e));
299              }
300            });
301          }
302        });
303      }
304    
305      /**
306       * Returns a parser that tries this parser and if it fails, then tries the given parser. If both parsers fail, then
307       * append their errors with the given semigroup.
308       *
309       * @param alt The parser to try if this parser fails.
310       * @param s   The semigroup to append error messages if both parsers fail.
311       * @return A parser that tries this parser and if it fails, then tries the given parser.
312       */
313      public Parser<I, A, E> or(final Parser<I, A, E> alt, final Semigroup<E> s) {
314        return or(p(alt), s);
315      }
316    
317      /**
318       * Returns a parser that negates this parser. If this parser succeeds, then the returned parser fails and vice versa.
319       *
320       * @param e The error message to fail with if this parser succeeds.
321       * @return A parser that negates this parser.
322       */
323      public Parser<I, Unit, E> not(final P1<E> e) {
324        return parser(new F<I, Validation<E, Result<I, Unit>>>() {
325          public Validation<E, Result<I, Unit>> f(final I i) {
326            return parse(i).isFail() ?
327                Validation.<E, Result<I, Unit>>success(result(i, unit())) :
328                Validation.<E, Result<I, Unit>>fail(e._1());
329          }
330        });
331      }
332    
333      /**
334       * Returns a parser that negates this parser. If this parser succeeds, then the returned parser fails and vice versa.
335       *
336       * @param e The error message to fail with if this parser succeeds.
337       * @return A parser that negates this parser.
338       */
339      public Parser<I, Unit, E> not(final E e) {
340        return not(p(e));
341      }
342    
343      /**
344       * Returns a parser that repeats application of this parser zero or many times.
345       *
346       * @return A parser that repeats application of this parser zero or many times.
347       */
348      public Parser<I, Stream<A>, E> repeat() {
349        return repeat1().or(new P1<Parser<I, Stream<A>, E>>() {
350          public Parser<I, Stream<A>, E> _1() {
351            return value(Stream.<A>nil());
352          }
353        });
354      }
355    
356      /**
357       * Returns a parser that repeats application of this parser one or many times.
358       *
359       * @return A parser that repeats application of this parser one or many times.
360       */
361      public Parser<I, Stream<A>, E> repeat1() {
362        return bind(repeat(), Stream.<A>cons_());
363      }
364    
365      /**
366       * Maps the given function across this parser's error.
367       *
368       * @param f The function to map this parser's error with.
369       * @return A new parser with a new error type.
370       */
371      public <K> Parser<I, A, K> mapError(final F<E, K> f) {
372        return parser(new F<I, Validation<K, Result<I, A>>>() {
373          public Validation<K, Result<I, A>> f(final I i) {
374            return Parser.this.f.f(i).f().map(f);
375          }
376        });
377      }
378    
379      /**
380       * Returns a parser that computes using the given function.
381       *
382       * @param f The function to construct the parser with.
383       * @return A parser that computes using the given function.
384       */
385      public static <I, A, E> Parser<I, A, E> parser(final F<I, Validation<E, Result<I, A>>> f) {
386        return new Parser<I, A, E>(f);
387      }
388    
389      /**
390       * Constructs a parser that always returns the given value. The unital for a parser.
391       *
392       * @param a The value to consistently return from a parser.
393       * @return A parser that always returns the given value.
394       */
395      public static <I, A, E> Parser<I, A, E> value(final A a) {
396        return parser(new F<I, Validation<E, Result<I, A>>>() {
397          public Validation<E, Result<I, A>> f(final I i) {
398            return success(result(i, a));
399          }
400        });
401      }
402    
403      /**
404       * Returns a parser that always fails with the given error.
405       *
406       * @param e The error to fail with.
407       * @return A parser that always fails with the given error.
408       */
409      public static <I, A, E> Parser<I, A, E> fail(final E e) {
410        return parser(new F<I, Validation<E, Result<I, A>>>() {
411          public Validation<E, Result<I, A>> f(final I i) {
412            return Validation.fail(e);
413          }
414        });
415      }
416    
417      /**
418       * Sequence the list of parsers through {@link #bind}.
419       *
420       * @param ps The parsers to sequence.
421       * @return A parser after sequencing.
422       */
423      public static <I, A, E> Parser<I, List<A>, E> sequence(final List<Parser<I, A, E>> ps) {
424        return ps.isEmpty() ?
425            Parser.<I, List<A>, E>value(List.<A>nil()) :
426            ps.head().bind(new F<A, Parser<I, List<A>, E>>() {
427              public Parser<I, List<A>, E> f(final A a) {
428                return sequence(ps.tail()).map(cons_(a));
429              }
430            });
431      }
432    
433      /**
434       * Parsers that accept {@link Stream} input.
435       */
436      public static final class StreamParser {
437        private StreamParser() {
438    
439        }
440    
441        /**
442         * Returns a parser that produces an element from the stream if it is available and fails otherwise.
443         *
444         * @param e The error to fail with if no element is available.
445         * @return A parser that produces an element from the stream if it is available and fails otherwise.
446         */
447        public static <I, E> Parser<Stream<I>, I, E> element(final P1<E> e) {
448          return parser(new F<Stream<I>, Validation<E, Result<Stream<I>, I>>>() {
449            public Validation<E, Result<Stream<I>, I>> f(final Stream<I> is) {
450              return is.isEmpty() ?
451                  Validation.<E, Result<Stream<I>, I>>fail(e._1()) :
452                  Validation.<E, Result<Stream<I>, I>>success(result(is.tail()._1(), is.head()));
453            }
454          });
455        }
456    
457        /**
458         * Returns a parser that produces an element from the stream if it is available and fails otherwise.
459         *
460         * @param e The error to fail with if no element is available.
461         * @return A parser that produces an element from the stream if it is available and fails otherwise.
462         */
463        public static <I, E> Parser<Stream<I>, I, E> element(final E e) {
464          return element(p(e));
465        }
466    
467        /**
468         * Returns a parser that produces an element from the stream that satisfies the given predicate, or fails.
469         *
470         * @param missing The error if no element is available.
471         * @param sat     The error if the element does not satisfy the predicate.
472         * @param f       The predicate that the element should satisfy.
473         * @return A parser that produces an element from the stream that satisfies the given predicate, or fails.
474         */
475        public static <I, E> Parser<Stream<I>, I, E> satisfy(final P1<E> missing, final F<I, E> sat,
476                                                             final F<I, Boolean> f) {
477          return StreamParser.<I, E>element(missing).bind(new F<I, Parser<Stream<I>, I, E>>() {
478            public Parser<Stream<I>, I, E> f(final I x) {
479              return f.f(x) ?
480                  Parser.<Stream<I>, I, E>value(x) :
481                  Parser.<Stream<I>, I, E>fail(sat.f(x));
482            }
483          });
484        }
485    
486        /**
487         * Returns a parser that produces an element from the stream that satisfies the given predicate, or fails.
488         *
489         * @param missing The error if no element is available.
490         * @param sat     The error if the element does not satisfy the predicate.
491         * @param f       The predicate that the element should satisfy.
492         * @return A parser that produces an element from the stream that satisfies the given predicate, or fails.
493         */
494        public static <I, E> Parser<Stream<I>, I, E> satisfy(final E missing, final F<I, E> sat, final F<I, Boolean> f) {
495          return satisfy(p(missing), sat, f);
496        }
497      }
498    
499      /**
500       * Parsers that accept {@link Stream Stream&lt;Character&gt;} input.
501       */
502      public static final class CharsParser {
503        private CharsParser() {
504    
505        }
506    
507        /**
508         * Returns a parser that produces a character if one is available or fails with the given error.
509         *
510         * @param e The error to fail with if a character is unavailable.
511         * @return A parser that produces a character if one is available or fails with the given error.
512         */
513        public static <E> Parser<Stream<Character>, Character, E> character(final P1<E> e) {
514          return StreamParser.element(e);
515        }
516    
517        /**
518         * Returns a parser that produces a character if one is available or fails with the given error.
519         *
520         * @param e The error to fail with if a character is unavailable.
521         * @return A parser that produces a character if one is available or fails with the given error.
522         */
523        public static <E> Parser<Stream<Character>, Character, E> character(final E e) {
524          return character(p(e));
525        }
526    
527        /**
528         * Returns a parser that produces the given character or fails otherwise.
529         *
530         * @param missing The error if no character is available.
531         * @param sat     The error if the produced character is not the one given.
532         * @param c       The character to produce in the parser.
533         * @return A parser that produces the given character or fails otherwise.
534         */
535        public static <E> Parser<Stream<Character>, Character, E> character(final P1<E> missing, final F<Character, E> sat,
536                                                                            final char c) {
537          return StreamParser.satisfy(missing, sat, new F<Character, Boolean>() {
538            public Boolean f(final Character x) {
539              return x == c;
540            }
541          });
542        }
543    
544        /**
545         * Returns a parser that produces the given character or fails otherwise.
546         *
547         * @param missing The error if no character is available.
548         * @param sat     The error if the produced character is not the one given.
549         * @param c       The character to produce in the parser.
550         * @return A parser that produces the given character or fails otherwise.
551         */
552        public static <E> Parser<Stream<Character>, Character, E> character(final E missing, final F<Character, E> sat,
553                                                                            final char c) {
554          return character(p(missing), sat, c);
555        }
556    
557        /**
558         * Returns a parser that produces the given number of characters, or fails with the given error.
559         *
560         * @param missing The error if the given number of characters is unavailable.
561         * @param n       The number of characters to produce in the parse result.
562         * @return A parser that produces the given number of characters, or fails with the given error.
563         */
564        public static <E> Parser<Stream<Character>, Stream<Character>, E> characters(final P1<E> missing, final int n) {
565          return n <= 0 ?
566              Parser.<Stream<Character>, Stream<Character>, E>value(Stream.<Character>nil()) :
567              CharsParser.character(missing).bind(CharsParser.characters(missing, n - 1), Stream.<Character>cons_());
568        }
569    
570        /**
571         * Returns a parser that produces the given number of characters, or fails with the given error.
572         *
573         * @param missing The error if the given number of characters is unavailable.
574         * @param n       The number of characters to produce in the parse result.
575         * @return A parser that produces the given number of characters, or fails with the given error.
576         */
577        public static <E> Parser<Stream<Character>, Stream<Character>, E> characters(final E missing, final int n) {
578          return characters(p(missing), n);
579        }
580    
581        /**
582         * Returns a parser that produces the given stream of characters or fails otherwise.
583         *
584         * @param missing The error if the producing stream could not supply more characters.
585         * @param sat     The error if a character was produced that is not the given stream of characters.
586         * @param cs      The stream of characters to produce.
587         * @return A parser that produces the given stream of characters or fails otherwise.
588         */
589        public static <E> Parser<Stream<Character>, Stream<Character>, E> characters(final P1<E> missing,
590                                                                                     final F<Character, E> sat,
591                                                                                     final Stream<Character> cs) {
592          return cs.isEmpty() ?
593              Parser.<Stream<Character>, Stream<Character>, E>value(Stream.<Character>nil()) :
594              character(missing, sat, cs.head()).bind(characters(missing, sat, cs.tail()._1()), Stream.<Character>cons_());
595        }
596    
597        /**
598         * Returns a parser that produces the given stream of characters or fails otherwise.
599         *
600         * @param missing The error if the producing stream could not supply more characters.
601         * @param sat     The error if a character was produced that is not the given stream of characters.
602         * @param cs      The stream of characters to produce.
603         * @return A parser that produces the given stream of characters or fails otherwise.
604         */
605        public static <E> Parser<Stream<Character>, Stream<Character>, E> characters(final E missing,
606                                                                                     final F<Character, E> sat,
607                                                                                     final Stream<Character> cs) {
608          return characters(p(missing), sat, cs);
609        }
610    
611        /**
612         * Returns a parser that produces the given string or fails otherwise.
613         *
614         * @param missing The error if the producing stream could not supply more characters.
615         * @param sat     The error if a character was produced that is not the given string.
616         * @param s       The string to produce.
617         * @return A parser that produces the given string or fails otherwise.
618         */
619        public static <E> Parser<Stream<Character>, String, E> string(final P1<E> missing, final F<Character, E> sat,
620                                                                      final String s) {
621          return characters(missing, sat, List.fromString(s).toStream()).map(new F<Stream<Character>, String>() {
622            public String f(final Stream<Character> cs) {
623              return List.asString(cs.toList());
624            }
625          });
626        }
627    
628        /**
629         * Returns a parser that produces the given string or fails otherwise.
630         *
631         * @param missing The error if the producing stream could not supply more characters.
632         * @param sat     The error if a character was produced that is not the given string.
633         * @param s       The string to produce.
634         * @return A parser that produces the given string or fails otherwise.
635         */
636        public static <E> Parser<Stream<Character>, String, E> string(final E missing, final F<Character, E> sat,
637                                                                      final String s) {
638          return string(p(missing), sat, s);
639        }
640    
641        /**
642         * Returns a parser that produces a digit (0 to 9).
643         *
644         * @param missing The error if there is no character on the stream to produce a digit with.
645         * @param sat     The error if the produced character is not a digit.
646         * @return A parser that produces a digit (0 to 9).
647         */
648        public static <E> Parser<Stream<Character>, Digit, E> digit(final P1<E> missing, final F<Character, E> sat) {
649          return StreamParser.satisfy(missing, sat, new F<Character, Boolean>() {
650            public Boolean f(final Character c) {
651              return Character.isDigit(c);
652            }
653          }).map(new F<Character, Digit>() {
654            public Digit f(final Character c) {
655              return Digit.fromChar(c).some();
656            }
657          });
658        }
659    
660        /**
661         * Returns a parser that produces a digit (0 to 9).
662         *
663         * @param missing The error if there is no character on the stream to produce a digit with.
664         * @param sat     The error if the produced character is not a digit.
665         * @return A parser that produces a digit (0 to 9).
666         */
667        public static <E> Parser<Stream<Character>, Digit, E> digit(final E missing, final F<Character, E> sat) {
668          return digit(p(missing), sat);
669        }
670    
671        /**
672         * Returns a parser that produces a lower-case character.
673         *
674         * @param missing The error if there is no character on the stream to produce a lower-case character with.
675         * @param sat     The error if the produced character is not a lower-case character.
676         * @return A parser that produces a lower-case character.
677         * @see Character#isLowerCase(char)
678         */
679        public static <E> Parser<Stream<Character>, Character, E> lower(final P1<E> missing, final F<Character, E> sat) {
680          return StreamParser.satisfy(missing, sat, new F<Character, Boolean>() {
681            public Boolean f(final Character c) {
682              return Character.isLowerCase(c);
683            }
684          });
685        }
686    
687        /**
688         * Returns a parser that produces a lower-case character.
689         *
690         * @param missing The error if there is no character on the stream to produce a lower-case character with.
691         * @param sat     The error if the produced character is not a lower-case character.
692         * @return A parser that produces a lower-case character.
693         * @see Character#isLowerCase(char)
694         */
695        public static <E> Parser<Stream<Character>, Character, E> lower(final E missing, final F<Character, E> sat) {
696          return lower(p(missing), sat);
697        }
698    
699        /**
700         * Returns a parser that produces a upper-case character.
701         *
702         * @param missing The error if there is no character on the stream to produce a upper-case character with.
703         * @param sat     The error if the produced character is not a upper-case character.
704         * @return A parser that produces a upper-case character.
705         * @see Character#isUpperCase(char)
706         */
707        public static <E> Parser<Stream<Character>, Character, E> upper(final P1<E> missing, final F<Character, E> sat) {
708          return StreamParser.satisfy(missing, sat, new F<Character, Boolean>() {
709            public Boolean f(final Character c) {
710              return Character.isUpperCase(c);
711            }
712          });
713        }
714    
715        /**
716         * Returns a parser that produces a upper-case character.
717         *
718         * @param missing The error if there is no character on the stream to produce a upper-case character with.
719         * @param sat     The error if the produced character is not a upper-case character.
720         * @return A parser that produces a upper-case character.
721         * @see Character#isUpperCase(char)
722         */
723        public static <E> Parser<Stream<Character>, Character, E> upper(final E missing, final F<Character, E> sat) {
724          return upper(p(missing), sat);
725        }
726    
727        /**
728         * Returns a parser that produces a defined character.
729         *
730         * @param missing The error if there is no character on the stream to produce a defined character with.
731         * @param sat     The error if the produced character is not a defined character.
732         * @return A parser that produces a defined character.
733         * @see Character#isDefined(char)
734         */
735        public static <E> Parser<Stream<Character>, Character, E> defined(final P1<E> missing, final F<Character, E> sat) {
736          return StreamParser.satisfy(missing, sat, new F<Character, Boolean>() {
737            public Boolean f(final Character c) {
738              return Character.isDefined(c);
739            }
740          });
741        }
742    
743        /**
744         * Returns a parser that produces a defined character.
745         *
746         * @param missing The error if there is no character on the stream to produce a defined character with.
747         * @param sat     The error if the produced character is not a defined character.
748         * @return A parser that produces a defined character.
749         * @see Character#isDefined(char)
750         */
751        public static <E> Parser<Stream<Character>, Character, E> defined(final E missing, final F<Character, E> sat) {
752          return defined(p(missing), sat);
753        }
754    
755        /**
756         * Returns a parser that produces a high-surrogate character.
757         *
758         * @param missing The error if there is no character on the stream to produce a high-surrogate character with.
759         * @param sat     The error if the produced character is not a high-surrogate character.
760         * @return A parser that produces a high-surrogate character.
761         * @see Character#isHighSurrogate(char)
762         */
763        public static <E> Parser<Stream<Character>, Character, E> highSurrogate(final P1<E> missing,
764                                                                                final F<Character, E> sat) {
765          return StreamParser.satisfy(missing, sat, new F<Character, Boolean>() {
766            public Boolean f(final Character c) {
767              return Character.isHighSurrogate(c);
768            }
769          });
770        }
771    
772        /**
773         * Returns a parser that produces a high-surrogate character.
774         *
775         * @param missing The error if there is no character on the stream to produce a high-surrogate character with.
776         * @param sat     The error if the produced character is not a high-surrogate character.
777         * @return A parser that produces a high-surrogate character.
778         * @see Character#isHighSurrogate(char)
779         */
780        public static <E> Parser<Stream<Character>, Character, E> highSurrogate(final E missing,
781                                                                                final F<Character, E> sat) {
782          return highSurrogate(p(missing), sat);
783        }
784    
785        /**
786         * Returns a parser that produces an identifier-ignorable character.
787         *
788         * @param missing The error if there is no character on the stream to produce an identifier-ignorable character with.
789         * @param sat     The error if the produced character is not an identifier-ignorable character.
790         * @return A parser that produces an identifier-ignorable character.
791         * @see Character#isIdentifierIgnorable(char)
792         */
793        public static <E> Parser<Stream<Character>, Character, E> identifierIgnorable(final P1<E> missing,
794                                                                                      final F<Character, E> sat) {
795          return StreamParser.satisfy(missing, sat, new F<Character, Boolean>() {
796            public Boolean f(final Character c) {
797              return Character.isIdentifierIgnorable(c);
798            }
799          });
800        }
801    
802        /**
803         * Returns a parser that produces an identifier-ignorable character.
804         *
805         * @param missing The error if there is no character on the stream to produce an identifier-ignorable character with.
806         * @param sat     The error if the produced character is not an identifier-ignorable character.
807         * @return A parser that produces an identifier-ignorable character.
808         * @see Character#isIdentifierIgnorable(char)
809         */
810        public static <E> Parser<Stream<Character>, Character, E> identifierIgnorable(final E missing,
811                                                                                      final F<Character, E> sat) {
812          return identifierIgnorable(p(missing), sat);
813        }
814    
815        /**
816         * Returns a parser that produces an ISO control character.
817         *
818         * @param missing The error if there is no character on the stream to produce an ISO control character with.
819         * @param sat     The error if the produced character is not an ISO control character.
820         * @return A parser that produces an ISO control character.
821         * @see Character#isISOControl(char)
822         */
823        public static <E> Parser<Stream<Character>, Character, E> isoControl(final P1<E> missing,
824                                                                             final F<Character, E> sat) {
825          return StreamParser.satisfy(missing, sat, new F<Character, Boolean>() {
826            public Boolean f(final Character c) {
827              return Character.isISOControl(c);
828            }
829          });
830        }
831    
832        /**
833         * Returns a parser that produces an ISO control character.
834         *
835         * @param missing The error if there is no character on the stream to produce an ISO control character with.
836         * @param sat     The error if the produced character is not an ISO control character.
837         * @return A parser that produces an ISO control character.
838         * @see Character#isISOControl(char)
839         */
840        public static <E> Parser<Stream<Character>, Character, E> isoControl(final E missing, final F<Character, E> sat) {
841          return isoControl(p(missing), sat);
842        }
843    
844        /**
845         * Returns a parser that produces a Java identifier part character.
846         *
847         * @param missing The error if there is no character on the stream to produce a Java identifier part character with.
848         * @param sat     The error if the produced character is not a Java identifier part character.
849         * @return A parser that produces a Java identifier part character.
850         * @see Character#isJavaIdentifierPart(char)
851         */
852        public static <E> Parser<Stream<Character>, Character, E> javaIdentifierPart(final P1<E> missing,
853                                                                                     final F<Character, E> sat) {
854          return StreamParser.satisfy(missing, sat, new F<Character, Boolean>() {
855            public Boolean f(final Character c) {
856              return Character.isJavaIdentifierPart(c);
857            }
858          });
859        }
860    
861        /**
862         * Returns a parser that produces a Java identifier part character.
863         *
864         * @param missing The error if there is no character on the stream to produce a Java identifier part character with.
865         * @param sat     The error if the produced character is not a Java identifier part character.
866         * @return A parser that produces a Java identifier part character.
867         * @see Character#isJavaIdentifierPart(char)
868         */
869        public static <E> Parser<Stream<Character>, Character, E> javaIdentifierPart(final E missing,
870                                                                                     final F<Character, E> sat) {
871          return javaIdentifierPart(p(missing), sat);
872        }
873    
874        /**
875         * Returns a parser that produces a Java identifier start character.
876         *
877         * @param missing The error if there is no character on the stream to produce a Java identifier start character with.
878         * @param sat     The error if the produced character is not a Java identifier start character.
879         * @return A parser that produces a Java identifier start character.
880         * @see Character#isJavaIdentifierStart(char)
881         */
882        public static <E> Parser<Stream<Character>, Character, E> javaIdentifierStart(final P1<E> missing,
883                                                                                      final F<Character, E> sat) {
884          return StreamParser.satisfy(missing, sat, new F<Character, Boolean>() {
885            public Boolean f(final Character c) {
886              return Character.isJavaIdentifierStart(c);
887            }
888          });
889        }
890    
891        /**
892         * Returns a parser that produces a Java identifier start character.
893         *
894         * @param missing The error if there is no character on the stream to produce a Java identifier start character with.
895         * @param sat     The error if the produced character is not a Java identifier start character.
896         * @return A parser that produces a Java identifier start character.
897         * @see Character#isJavaIdentifierStart(char)
898         */
899        public static <E> Parser<Stream<Character>, Character, E> javaIdentifierStart(final E missing,
900                                                                                      final F<Character, E> sat) {
901          return javaIdentifierStart(p(missing), sat);
902        }
903    
904        /**
905         * Returns a parser that produces an alpha character.
906         *
907         * @param missing The error if there is no character on the stream to produce an alpha character with.
908         * @param sat     The error if the produced character is not an alpha character.
909         * @return A parser that produces an alpha character.
910         * @see Character#isLetter(char)
911         */
912        public static <E> Parser<Stream<Character>, Character, E> alpha(final P1<E> missing, final F<Character, E> sat) {
913          return StreamParser.satisfy(missing, sat, new F<Character, Boolean>() {
914            public Boolean f(final Character c) {
915              return Character.isLetter(c);
916            }
917          });
918        }
919    
920        /**
921         * Returns a parser that produces an alpha character.
922         *
923         * @param missing The error if there is no character on the stream to produce an alpha character with.
924         * @param sat     The error if the produced character is not an alpha character.
925         * @return A parser that produces an alpha character.
926         * @see Character#isLetter(char)
927         */
928        public static <E> Parser<Stream<Character>, Character, E> alpha(final E missing, final F<Character, E> sat) {
929          return alpha(p(missing), sat);
930        }
931    
932        /**
933         * Returns a parser that produces an alpha-numeric character.
934         *
935         * @param missing The error if there is no character on the stream to produce an alpha-numeric character with.
936         * @param sat     The error if the produced character is not an alpha-numeric character.
937         * @return A parser that produces an alpha-numeric character.
938         * @see Character#isLetterOrDigit(char)
939         */
940        public static <E> Parser<Stream<Character>, Character, E> alphaNum(final P1<E> missing, final F<Character, E> sat) {
941          return StreamParser.satisfy(missing, sat, new F<Character, Boolean>() {
942            public Boolean f(final Character c) {
943              return Character.isLetterOrDigit(c);
944            }
945          });
946        }
947    
948        /**
949         * Returns a parser that produces an alpha-numeric character.
950         *
951         * @param missing The error if there is no character on the stream to produce an alpha-numeric character with.
952         * @param sat     The error if the produced character is not an alpha-numeric character.
953         * @return A parser that produces an alpha-numeric character.
954         * @see Character#isLetterOrDigit(char)
955         */
956        public static <E> Parser<Stream<Character>, Character, E> alphaNum(final E missing, final F<Character, E> sat) {
957          return alphaNum(p(missing), sat);
958        }
959    
960        /**
961         * Returns a parser that produces a low-surrogate character.
962         *
963         * @param missing The error if there is no character on the stream to produce a low-surrogate character with.
964         * @param sat     The error if the produced character is not a low-surrogate character.
965         * @return A parser that produces a low-surrogate character.
966         * @see Character#isLowSurrogate(char)
967         */
968        public static <E> Parser<Stream<Character>, Character, E> lowSurrogate(final P1<E> missing,
969                                                                               final F<Character, E> sat) {
970          return StreamParser.satisfy(missing, sat, new F<Character, Boolean>() {
971            public Boolean f(final Character c) {
972              return Character.isLowSurrogate(c);
973            }
974          });
975        }
976    
977        /**
978         * Returns a parser that produces a low-surrogate character.
979         *
980         * @param missing The error if there is no character on the stream to produce a low-surrogate character with.
981         * @param sat     The error if the produced character is not a low-surrogate character.
982         * @return A parser that produces a low-surrogate character.
983         * @see Character#isLowSurrogate(char)
984         */
985        public static <E> Parser<Stream<Character>, Character, E> lowSurrogate(final E missing, final F<Character, E> sat) {
986          return lowSurrogate(p(missing), sat);
987        }
988    
989        /**
990         * Returns a parser that produces a mirrored character.
991         *
992         * @param missing The error if there is no character on the stream to produce a mirrored character with.
993         * @param sat     The error if the produced character is not a mirrored character.
994         * @return A parser that produces a mirrored character.
995         * @see Character#isMirrored(char)
996         */
997        public static <E> Parser<Stream<Character>, Character, E> mirrored(final P1<E> missing, final F<Character, E> sat) {
998          return StreamParser.satisfy(missing, sat, new F<Character, Boolean>() {
999            public Boolean f(final Character c) {
1000              return Character.isMirrored(c);
1001            }
1002          });
1003        }
1004    
1005        /**
1006         * Returns a parser that produces a mirrored character.
1007         *
1008         * @param missing The error if there is no character on the stream to produce a mirrored character with.
1009         * @param sat     The error if the produced character is not a mirrored character.
1010         * @return A parser that produces a mirrored character.
1011         * @see Character#isMirrored(char)
1012         */
1013        public static <E> Parser<Stream<Character>, Character, E> mirrored(final E missing, final F<Character, E> sat) {
1014          return mirrored(p(missing), sat);
1015        }
1016    
1017        /**
1018         * Returns a parser that produces a space character.
1019         *
1020         * @param missing The error if there is no character on the stream to produce a space character with.
1021         * @param sat     The error if the produced character is not a space character.
1022         * @return A parser that produces a space character.
1023         * @see Character#isSpace(char)
1024         */
1025        public static <E> Parser<Stream<Character>, Character, E> space(final P1<E> missing, final F<Character, E> sat) {
1026          return StreamParser.satisfy(missing, sat, new F<Character, Boolean>() {
1027            public Boolean f(final Character c) {
1028              return Character.isSpaceChar(c);
1029            }
1030          });
1031        }
1032    
1033        /**
1034         * Returns a parser that produces a space character.
1035         *
1036         * @param missing The error if there is no character on the stream to produce a space character with.
1037         * @param sat     The error if the produced character is not a space character.
1038         * @return A parser that produces a space character.
1039         * @see Character#isSpace(char)
1040         */
1041        public static <E> Parser<Stream<Character>, Character, E> space(final E missing, final F<Character, E> sat) {
1042          return space(p(missing), sat);
1043        }
1044    
1045        /**
1046         * Returns a parser that produces a title-case character.
1047         *
1048         * @param missing The error if there is no character on the stream to produce a title-case character with.
1049         * @param sat     The error if the produced character is not a title-case character.
1050         * @return A parser that produces a title-case character.
1051         * @see Character#isTitleCase(char)
1052         */
1053        public static <E> Parser<Stream<Character>, Character, E> titleCase(final P1<E> missing,
1054                                                                            final F<Character, E> sat) {
1055          return StreamParser.satisfy(missing, sat, new F<Character, Boolean>() {
1056            public Boolean f(final Character c) {
1057              return Character.isTitleCase(c);
1058            }
1059          });
1060        }
1061    
1062        /**
1063         * Returns a parser that produces a title-case character.
1064         *
1065         * @param missing The error if there is no character on the stream to produce a title-case character with.
1066         * @param sat     The error if the produced character is not a title-case character.
1067         * @return A parser that produces a title-case character.
1068         * @see Character#isTitleCase(char)
1069         */
1070        public static <E> Parser<Stream<Character>, Character, E> titleCase(final E missing, final F<Character, E> sat) {
1071          return titleCase(p(missing), sat);
1072        }
1073    
1074        /**
1075         * Returns a parser that produces a unicode identifier part character.
1076         *
1077         * @param missing The error if there is no character on the stream to produce a unicode identifier part character with.
1078         * @param sat     The error if the produced character is not a unicode identifier part character.
1079         * @return A parser that produces a unicode identifier part character.
1080         * @see Character#isUnicodeIdentifierPart(char)
1081         */
1082        public static <E> Parser<Stream<Character>, Character, E> unicodeIdentiferPart(final P1<E> missing,
1083                                                                                       final F<Character, E> sat) {
1084          return StreamParser.satisfy(missing, sat, new F<Character, Boolean>() {
1085            public Boolean f(final Character c) {
1086              return Character.isUnicodeIdentifierPart(c);
1087            }
1088          });
1089        }
1090    
1091        /**
1092         * Returns a parser that produces a unicode identifier part character.
1093         *
1094         * @param missing The error if there is no character on the stream to produce a unicode identifier part character with.
1095         * @param sat     The error if the produced character is not a unicode identifier part character.
1096         * @return A parser that produces a unicode identifier part character.
1097         * @see Character#isUnicodeIdentifierPart(char)
1098         */
1099        public static <E> Parser<Stream<Character>, Character, E> unicodeIdentiferPart(final E missing,
1100                                                                                       final F<Character, E> sat) {
1101          return unicodeIdentiferPart(p(missing), sat);
1102        }
1103    
1104        /**
1105         * Returns a parser that produces a unicode identifier start character.
1106         *
1107         * @param missing The error if there is no character on the stream to produce a unicode identifier start character with.
1108         * @param sat     The error if the produced character is not a unicode identifier start character.
1109         * @return A parser that produces a unicode identifier start character.
1110         * @see Character#isUnicodeIdentifierStart(char)
1111         */
1112        public static <E> Parser<Stream<Character>, Character, E> unicodeIdentiferStart(final P1<E> missing,
1113                                                                                        final F<Character, E> sat) {
1114          return StreamParser.satisfy(missing, sat, new F<Character, Boolean>() {
1115            public Boolean f(final Character c) {
1116              return Character.isUnicodeIdentifierStart(c);
1117            }
1118          });
1119        }
1120    
1121        /**
1122         * Returns a parser that produces a unicode identifier start character.
1123         *
1124         * @param missing The error if there is no character on the stream to produce a unicode identifier start character with.
1125         * @param sat     The error if the produced character is not a unicode identifier start character.
1126         * @return A parser that produces a unicode identifier start character.
1127         * @see Character#isUnicodeIdentifierStart(char)
1128         */
1129        public static <E> Parser<Stream<Character>, Character, E> unicodeIdentiferStart(final E missing,
1130                                                                                        final F<Character, E> sat) {
1131          return unicodeIdentiferStart(p(missing), sat);
1132        }
1133    
1134        /**
1135         * Returns a parser that produces a white-space character.
1136         *
1137         * @param missing The error if there is no character on the stream to produce a white-space character with.
1138         * @param sat     The error if the produced character is not a white-space character.
1139         * @return A parser that produces a white-space character.
1140         * @see Character#isWhitespace(char)
1141         */
1142        public static <E> Parser<Stream<Character>, Character, E> whitespace(final P1<E> missing,
1143                                                                             final F<Character, E> sat) {
1144          return StreamParser.satisfy(missing, sat, new F<Character, Boolean>() {
1145            public Boolean f(final Character c) {
1146              return Character.isWhitespace(c);
1147            }
1148          });
1149        }
1150    
1151        /**
1152         * Returns a parser that produces a white-space character.
1153         *
1154         * @param missing The error if there is no character on the stream to produce a white-space character with.
1155         * @param sat     The error if the produced character is not a white-space character.
1156         * @return A parser that produces a white-space character.
1157         * @see Character#isWhitespace(char)
1158         */
1159        public static <E> Parser<Stream<Character>, Character, E> whitespace(final E missing, final F<Character, E> sat) {
1160          return whitespace(p(missing), sat);
1161        }
1162      }
1163    }