- java.lang.Object
-
- swim.codec.Decoder<O>
-
- swim.codec.Parser<O>
-
public abstract class Parser<O> extends Decoder<O>
Continuation of how to parse subsequentInput
tokens from a stream.Parser
enables efficient, interruptible parsing of network protocols and data formats, without intermediate buffering.Input tokens
A
Parser
reads tokens from anInput
reader. Input tokens are modeled as primitiveint
s, commonly representing Unicode code points, or raw octets. EachParser
implementation specifies the semantic type of input tokens it consumes.Parser states
A
Parser
is always in one of three states: continue, done, or error. The cont state indicates thatfeed
is ready to consumeInput
; the done state indicates that parsing terminated successfully, and thatbind
will return the parsed result; the error state indicates that parsing terminated in failure, and thattrap
will return the parse error.Parser
subclasses default to the cont state.Feeding input
The
feed(Input)
method incrementally parses as muchInput
as it can, before returning anotherParser
that represents the continuation of how to parse additionalInput
. TheInput
passed tofeed
is only guaranteed to be valid for the duration of the method call; references to the providedInput
instance must not be stored.Parser results
A
Parser
produces a parsed result of typeO
, obtained via thebind()
method.bind
is only guaranteed to return a result when in the done state; thoughbind
may optionally make available partial results in other states. A failedParser
provides a parse error via thetrap()
method.trap
is only guaranteed to return an error when in the error state.Continuations
A
Parser
instance represents a continuation of how to parse remainingInput
. Rather than parsing a complete input in one go, aParser
takes anInput
chunk and returns anotherParser
instance that knows how to parse subsequentInput
chunks. This enables non-blocking, incremental parsing that can be interrupted whenever anInput
reader runs out of immediately available data. AParser
terminates by returning a continuation in either the done state, or the error state.done(Object)
returns aParser
in the done state.error(Throwable)
returns aParser
in the error state.Iteratees
Parser
is an Iteratee. Though unlike strictly functional iteratees, aParser
statefully iterates over itsInput
, rather than allocating an object for each incremental input continutaion. This internal mutability minimizes garbage collector memory pressure, without violating the functional Iteratee abstraction, provided thatfeed
logically takes exclusive ownership of itsInput
when invoked, and logically returns ownership of theInput
in a state that's consistent with the returnedParser
continuation.Immutability
A
Parser
should be immutable. Specifically, an invocation offeed
should not alter the behavior of future calls tofeed
on the sameParser
instance. AParser
should only mutate its internal state if it's essential to do so, such as for critical path performance reasons.Backtracking
feed
can internallyclone
itsInput
, if it might need to backtrack. Keep in mind that, becauseInput
is only valid for the duration of a call tofeed
, input must be internally buffered if it needs to be preserved betweenfeed
invocations.Forking
The
fork(Object)
method passes an out-of-band condition to aParser
, yielding aParser
continuation whose behavior may be altered by the given condition. For example, an HTMLParser
mightfork
an inner text parser to directly parse an embedded micro format out of an HTML element, based on some out-of-band schema information. The types of conditions accepted byfork
, and their intended semantics, are implementation defined.
-
-
Constructor Summary
Constructors Constructor Description Parser()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description <O2> Parser<O2>
asError()
Casts an erroredParser
to a different output type.O
bind()
Returns the parsed result.static <O> Parser<O>
done()
Returns aParser
in the done state thatbind
s anull
parsed result.static <O> Parser<O>
done(O output)
Returns aParser
in the done state thatbind
s the given parsedoutput
.static <O> Parser<O>
error(Throwable error)
Returns aParser
in the error state thattrap
s the given parseerror
.static <O> Parser<O>
error(Diagnostic diagnostic)
abstract Parser<O>
feed(Input input)
Incrementally parses as muchinput
as possible, and returns anotherParser
that represents the continuation of how to parse additionalInput
.Parser<O>
feed(InputBuffer input)
Incrementally decodes as muchinput
buffer data as possible, and returns anotherDecoder
that represents the continuation of how to decode additional buffer data.Parser<O>
fork(Object condition)
Returns aParser
continuation whose behavior may be altered by the given out-of-bandcondition
.boolean
isCont()
boolean
isDone()
Returnstrue
when parsing has terminated successfully, andbind
will return the parsed result.boolean
isError()
Returnstrue
when parsing has terminated in failure, andtrap
will return the parse error.Throwable
trap()
Returns the parse error.
-
-
-
Method Detail
-
isDone
public boolean isDone()
Returnstrue
when parsing has terminated successfully, andbind
will return the parsed result. i.e. thisParser
is in the done state.
-
isError
public boolean isError()
Returnstrue
when parsing has terminated in failure, andtrap
will return the parse error. i.e. thisParser
is in the error state.
-
feed
public abstract Parser<O> feed(Input input)
Incrementally parses as muchinput
as possible, and returns anotherParser
that represents the continuation of how to parse additionalInput
. Ifinput
enters the done state,feed
must return a terminatedParser
, i.e. aParser
in the done state, or in the error state. The giveninput
is only guaranteed to be valid for the duration of the method call; references toinput
must not be stored.
-
feed
public Parser<O> feed(InputBuffer input)
Description copied from class:Decoder
Incrementally decodes as muchinput
buffer data as possible, and returns anotherDecoder
that represents the continuation of how to decode additional buffer data. IfisLast
istrue
, thenfeed
must return a terminatedDecoder
, i.e. aDecoder
in the done state, or in the error state. The giveninput
buffer is only guaranteed to be valid for the duration of the method call; references toinput
must not be stored.
-
fork
public Parser<O> fork(Object condition)
Returns aParser
continuation whose behavior may be altered by the given out-of-bandcondition
.
-
bind
public O bind()
Returns the parsed result. Only guaranteed to return a result when in the done state.- Overrides:
bind
in classDecoder<O>
- Throws:
IllegalStateException
- if thisParser
is not in the done state.
-
trap
public Throwable trap()
Returns the parse error. Only guaranteed to return an error when in the error state.- Overrides:
trap
in classDecoder<O>
- Throws:
IllegalStateException
- if thisParser
is not in the error state.
-
asError
public <O2> Parser<O2> asError()
Casts an erroredParser
to a different output type. AParser
in the error state can have any output type.- Overrides:
asError
in classDecoder<O>
- Throws:
IllegalStateException
- if thisParser
is not in the error state.
-
done
public static <O> Parser<O> done()
Returns aParser
in the done state thatbind
s anull
parsed result.
-
done
public static <O> Parser<O> done(O output)
Returns aParser
in the done state thatbind
s the given parsedoutput
.
-
error
public static <O> Parser<O> error(Throwable error)
Returns aParser
in the error state thattrap
s the given parseerror
.
-
error
public static <O> Parser<O> error(Diagnostic diagnostic)
-
-