Copyright 2008 - 2010 Tony Morris, Runar Bjarnason, Tom Adams, Brad Clow, Ricky Clarkson, Nick Partridge, Jason Zaugg

This software is released under an open source BSD licence.

fj.parser
Class Parser<I,A,E>

java.lang.Object
  extended by fj.parser.Parser<I,A,E>

public class Parser<I,A,E>
extends Object

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.


Nested Class Summary
static class Parser.CharsParser
          Parsers that accept Stream<Character> input.
static class Parser.StreamParser
          Parsers that accept Stream input.
 
Method Summary
<B> Parser<I,B,E>
apply(Parser<I,F<A,B>,E> p)
          Performs function application within a parser.
<B> Parser<I,B,E>
bind(F<A,Parser<I,B,E>> f)
          Binds the given function across the parser with a final join.
<B,C> Parser<I,C,E>
bind(Parser<I,B,E> pb, F<A,F<B,C>> f)
          Binds the given function across the parsers with a final join.
<B,C,D> Parser<I,D,E>
bind(Parser<I,B,E> pb, Parser<I,C,E> pc, F<A,F<B,F<C,D>>> f)
          Binds the given function across the parsers with a final join.
<B,C,D,E$> Parser<I,E$,E>
bind(Parser<I,B,E> pb, Parser<I,C,E> pc, Parser<I,D,E> pd, F<A,F<B,F<C,F<D,E$>>>> f)
          Binds the given function across the parsers with a final join.
<B,C,D,E$,F$>
Parser<I,F$,E>
bind(Parser<I,B,E> pb, Parser<I,C,E> pc, Parser<I,D,E> pd, Parser<I,E$,E> pe, F<A,F<B,F<C,F<D,F<E$,F$>>>>> f)
          Binds the given function across the parsers with a final join.
<B,C,D,E$,F$,G>
Parser<I,G,E>
bind(Parser<I,B,E> pb, Parser<I,C,E> pc, Parser<I,D,E> pd, Parser<I,E$,E> pe, Parser<I,F$,E> pf, F<A,F<B,F<C,F<D,F<E$,F<F$,G>>>>>> f)
          Binds the given function across the parsers with a final join.
<B,C,D,E$,F$,G,H>
Parser<I,H,E>
bind(Parser<I,B,E> pb, Parser<I,C,E> pc, Parser<I,D,E> pd, Parser<I,E$,E> pe, Parser<I,F$,E> pf, Parser<I,G,E> pg, F<A,F<B,F<C,F<D,F<E$,F<F$,F<G,H>>>>>>> f)
          Binds the given function across the parsers with a final join.
<B,C,D,E$,F$,G,H,I$>
Parser<I,I$,E>
bind(Parser<I,B,E> pb, Parser<I,C,E> pc, Parser<I,D,E> pd, Parser<I,E$,E> pe, Parser<I,F$,E> pf, Parser<I,G,E> pg, Parser<I,H,E> ph, F<A,F<B,F<C,F<D,F<E$,F<F$,F<G,F<H,I$>>>>>>>> f)
          Binds the given function across the parsers with a final join.
static
<I,A,E> Parser<I,A,E>
fail(E e)
          Returns a parser that always fails with the given error.
 Parser<I,A,E> filter(F<A,Boolean> f, E e)
          Returns a parser that fails with the given error if the result value does not meet the given predicate.
<B> Parser<I,B,E>
map(F<A,B> f)
          Maps the given result type across this parser.
<K> Parser<I,A,K>
mapError(F<E,K> f)
          Maps the given function across this parser's error.
 Parser<I,Unit,E> not(E e)
          Returns a parser that negates this parser.
 Parser<I,Unit,E> not(P1<E> e)
          Returns a parser that negates this parser.
 Parser<I,A,E> or(P1<Parser<I,A,E>> alt)
          Returns a parser that tries this parser and if it fails, then tries the given parser.
 Parser<I,A,E> or(P1<Parser<I,A,E>> alt, Semigroup<E> s)
          Returns a parser that tries this parser and if it fails, then tries the given parser.
 Parser<I,A,E> or(Parser<I,A,E> alt)
          Returns a parser that tries this parser and if it fails, then tries the given parser.
 Parser<I,A,E> or(Parser<I,A,E> alt, Semigroup<E> s)
          Returns a parser that tries this parser and if it fails, then tries the given parser.
 Validation<E,Result<I,A>> parse(I i)
          Parses the input to produce a result or error.
static
<I,A,E> Parser<I,A,E>
parser(F<I,Validation<E,Result<I,A>>> f)
          Returns a parser that computes using the given function.
 Parser<I,Stream<A>,E> repeat()
          Returns a parser that repeats application of this parser zero or many times.
 Parser<I,Stream<A>,E> repeat1()
          Returns a parser that repeats application of this parser one or many times.
static
<I,A,E> Parser<I,List<A>,E>
sequence(List<Parser<I,A,E>> ps)
          Sequence the list of parsers through bind(fj.F>).
<B> Parser<I,B,E>
sequence(Parser<I,B,E> p)
          Binds anonymously, ignoring the result value.
static
<I,A,E> Parser<I,A,E>
value(A a)
          Constructs a parser that always returns the given value.
<Z> Parser<Z,A,E>
xmap(F<I,Z> f, F<Z,I> g)
          Maps the parse input type through an invariant functor.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

parse

public Validation<E,Result<I,A>> parse(I i)
Parses the input to produce a result or error.

Parameters:
i - The input to parse.
Returns:
A parse result with the remaining input or an error.

xmap

public <Z> Parser<Z,A,E> xmap(F<I,Z> f,
                              F<Z,I> g)
Maps the parse input type through an invariant functor.

Parameters:
f - The function to covariant map.
g - The function to contra-variant map.
Returns:
A parser with the new input type.

map

public <B> Parser<I,B,E> map(F<A,B> f)
Maps the given result type across this parser.

Parameters:
f - The function to map.
Returns:
A parser with the new result type.

filter

public Parser<I,A,E> filter(F<A,Boolean> f,
                            E e)
Returns a parser that fails with the given error if the result value does not meet the given predicate.

Parameters:
f - The predicate to filter on.
e - The error to in the event that the predicate is not met.
Returns:
A parser that fails with the given error if the result value does not meet the given predicate.

bind

public <B> Parser<I,B,E> bind(F<A,Parser<I,B,E>> f)
Binds the given function across the parser with a final join.

Parameters:
f - The function to apply to the element of this parser.
Returns:
A new parser after performing the map, then final join.

bind

public <B,C> Parser<I,C,E> bind(Parser<I,B,E> pb,
                                F<A,F<B,C>> f)
Binds the given function across the parsers with a final join.

Parameters:
f - The function to apply to the element of the parsers.
pb - A given parser to bind the given function with.
Returns:
A new parser after performing the map, then final join.

bind

public <B,C,D> Parser<I,D,E> bind(Parser<I,B,E> pb,
                                  Parser<I,C,E> pc,
                                  F<A,F<B,F<C,D>>> f)
Binds the given function across the parsers with a final join.

Parameters:
f - The function to apply to the element of the parsers.
pb - A given parser to bind the given function with.
pc - A given parser to bind the given function with.
Returns:
A new parser after performing the map, then final join.

bind

public <B,C,D,E$> Parser<I,E$,E> bind(Parser<I,B,E> pb,
                                      Parser<I,C,E> pc,
                                      Parser<I,D,E> pd,
                                      F<A,F<B,F<C,F<D,E$>>>> f)
Binds the given function across the parsers with a final join.

Parameters:
f - The function to apply to the element of the parsers.
pb - A given parser to bind the given function with.
pc - A given parser to bind the given function with.
pd - A given parser to bind the given function with.
Returns:
A new parser after performing the map, then final join.

bind

public <B,C,D,E$,F$> Parser<I,F$,E> bind(Parser<I,B,E> pb,
                                         Parser<I,C,E> pc,
                                         Parser<I,D,E> pd,
                                         Parser<I,E$,E> pe,
                                         F<A,F<B,F<C,F<D,F<E$,F$>>>>> f)
Binds the given function across the parsers with a final join.

Parameters:
f - The function to apply to the element of the parsers.
pb - A given parser to bind the given function with.
pc - A given parser to bind the given function with.
pd - A given parser to bind the given function with.
pe - A given parser to bind the given function with.
Returns:
A new parser after performing the map, then final join.

bind

public <B,C,D,E$,F$,G> Parser<I,G,E> bind(Parser<I,B,E> pb,
                                          Parser<I,C,E> pc,
                                          Parser<I,D,E> pd,
                                          Parser<I,E$,E> pe,
                                          Parser<I,F$,E> pf,
                                          F<A,F<B,F<C,F<D,F<E$,F<F$,G>>>>>> f)
Binds the given function across the parsers with a final join.

Parameters:
f - The function to apply to the element of the parsers.
pb - A given parser to bind the given function with.
pc - A given parser to bind the given function with.
pd - A given parser to bind the given function with.
pe - A given parser to bind the given function with.
pf - A given parser to bind the given function with.
Returns:
A new parser after performing the map, then final join.

bind

public <B,C,D,E$,F$,G,H> Parser<I,H,E> bind(Parser<I,B,E> pb,
                                            Parser<I,C,E> pc,
                                            Parser<I,D,E> pd,
                                            Parser<I,E$,E> pe,
                                            Parser<I,F$,E> pf,
                                            Parser<I,G,E> pg,
                                            F<A,F<B,F<C,F<D,F<E$,F<F$,F<G,H>>>>>>> f)
Binds the given function across the parsers with a final join.

Parameters:
f - The function to apply to the element of the parsers.
pb - A given parser to bind the given function with.
pc - A given parser to bind the given function with.
pd - A given parser to bind the given function with.
pe - A given parser to bind the given function with.
pf - A given parser to bind the given function with.
pg - A given parser to bind the given function with.
Returns:
A new parser after performing the map, then final join.

bind

public <B,C,D,E$,F$,G,H,I$> Parser<I,I$,E> bind(Parser<I,B,E> pb,
                                                Parser<I,C,E> pc,
                                                Parser<I,D,E> pd,
                                                Parser<I,E$,E> pe,
                                                Parser<I,F$,E> pf,
                                                Parser<I,G,E> pg,
                                                Parser<I,H,E> ph,
                                                F<A,F<B,F<C,F<D,F<E$,F<F$,F<G,F<H,I$>>>>>>>> f)
Binds the given function across the parsers with a final join.

Parameters:
f - The function to apply to the element of the parsers.
pb - A given parser to bind the given function with.
pc - A given parser to bind the given function with.
pd - A given parser to bind the given function with.
pe - A given parser to bind the given function with.
pf - A given parser to bind the given function with.
pg - A given parser to bind the given function with.
ph - A given parser to bind the given function with.
Returns:
A new parser after performing the map, then final join.

sequence

public <B> Parser<I,B,E> sequence(Parser<I,B,E> p)
Binds anonymously, ignoring the result value.

Parameters:
p - The parser to bind with.
Returns:
A parser after binding anonymously.

apply

public <B> Parser<I,B,E> apply(Parser<I,F<A,B>,E> p)
Performs function application within a parser.

Parameters:
p - The parser returning a function value.
Returns:
A new parser after function application.

or

public Parser<I,A,E> or(P1<Parser<I,A,E>> alt)
Returns a parser that tries this parser and if it fails, then tries the given parser.

Parameters:
alt - The parser to try if this parser fails.
Returns:
A parser that tries this parser and if it fails, then tries the given parser.

or

public Parser<I,A,E> or(Parser<I,A,E> alt)
Returns a parser that tries this parser and if it fails, then tries the given parser.

Parameters:
alt - The parser to try if this parser fails.
Returns:
A parser that tries this parser and if it fails, then tries the given parser.

or

public Parser<I,A,E> or(P1<Parser<I,A,E>> alt,
                        Semigroup<E> s)
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.

Parameters:
alt - The parser to try if this parser fails.
s - The semigroup to append error messages if both parsers fail.
Returns:
A parser that tries this parser and if it fails, then tries the given parser.

or

public Parser<I,A,E> or(Parser<I,A,E> alt,
                        Semigroup<E> s)
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.

Parameters:
alt - The parser to try if this parser fails.
s - The semigroup to append error messages if both parsers fail.
Returns:
A parser that tries this parser and if it fails, then tries the given parser.

not

public Parser<I,Unit,E> not(P1<E> e)
Returns a parser that negates this parser. If this parser succeeds, then the returned parser fails and vice versa.

Parameters:
e - The error message to fail with if this parser succeeds.
Returns:
A parser that negates this parser.

not

public Parser<I,Unit,E> not(E e)
Returns a parser that negates this parser. If this parser succeeds, then the returned parser fails and vice versa.

Parameters:
e - The error message to fail with if this parser succeeds.
Returns:
A parser that negates this parser.

repeat

public Parser<I,Stream<A>,E> repeat()
Returns a parser that repeats application of this parser zero or many times.

Returns:
A parser that repeats application of this parser zero or many times.

repeat1

public Parser<I,Stream<A>,E> repeat1()
Returns a parser that repeats application of this parser one or many times.

Returns:
A parser that repeats application of this parser one or many times.

mapError

public <K> Parser<I,A,K> mapError(F<E,K> f)
Maps the given function across this parser's error.

Parameters:
f - The function to map this parser's error with.
Returns:
A new parser with a new error type.

parser

public static <I,A,E> Parser<I,A,E> parser(F<I,Validation<E,Result<I,A>>> f)
Returns a parser that computes using the given function.

Parameters:
f - The function to construct the parser with.
Returns:
A parser that computes using the given function.

value

public static <I,A,E> Parser<I,A,E> value(A a)
Constructs a parser that always returns the given value. The unital for a parser.

Parameters:
a - The value to consistently return from a parser.
Returns:
A parser that always returns the given value.

fail

public static <I,A,E> Parser<I,A,E> fail(E e)
Returns a parser that always fails with the given error.

Parameters:
e - The error to fail with.
Returns:
A parser that always fails with the given error.

sequence

public static <I,A,E> Parser<I,List<A>,E> sequence(List<Parser<I,A,E>> ps)
Sequence the list of parsers through bind(fj.F>).

Parameters:
ps - The parsers to sequence.
Returns:
A parser after sequencing.

Copyright 2008 - 2010 Tony Morris, Runar Bjarnason, Tom Adams, Brad Clow, Ricky Clarkson, Nick Partridge, Jason Zaugg

This software is released under an open source BSD licence.