- java.lang.Object
-
- swim.codec.Decoder<O>
-
- swim.codec.Parser<O>
-
public abstract class Parser<O> extends Decoder<O>
Continuation of how to parse subsequentInputtokens from a stream.Parserenables efficient, interruptible parsing of network protocols and data formats, without intermediate buffering.Input tokens
A
Parserreads tokens from anInputreader. Input tokens are modeled as primitiveints, commonly representing Unicode code points, or raw octets. EachParserimplementation specifies the semantic type of input tokens it consumes.Parser states
A
Parseris always in one of three states: continue, done, or error. The cont state indicates thatfeedis ready to consumeInput; the done state indicates that parsing terminated successfully, and thatbindwill return the parsed result; the error state indicates that parsing terminated in failure, and thattrapwill return the parse error.Parsersubclasses default to the cont state.Feeding input
The
feed(Input)method incrementally parses as muchInputas it can, before returning anotherParserthat represents the continuation of how to parse additionalInput. TheInputpassed tofeedis only guaranteed to be valid for the duration of the method call; references to the providedInputinstance must not be stored.Parser results
A
Parserproduces a parsed result of typeO, obtained via thebind()method.bindis only guaranteed to return a result when in the done state; thoughbindmay optionally make available partial results in other states. A failedParserprovides a parse error via thetrap()method.trapis only guaranteed to return an error when in the error state.Continuations
A
Parserinstance represents a continuation of how to parse remainingInput. Rather than parsing a complete input in one go, aParsertakes anInputchunk and returns anotherParserinstance that knows how to parse subsequentInputchunks. This enables non-blocking, incremental parsing that can be interrupted whenever anInputreader runs out of immediately available data. AParserterminates by returning a continuation in either the done state, or the error state.done(Object)returns aParserin the done state.error(Throwable)returns aParserin the error state.Iteratees
Parseris an Iteratee. Though unlike strictly functional iteratees, aParserstatefully 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 thatfeedlogically takes exclusive ownership of itsInputwhen invoked, and logically returns ownership of theInputin a state that's consistent with the returnedParsercontinuation.Immutability
A
Parsershould be immutable. Specifically, an invocation offeedshould not alter the behavior of future calls tofeedon the sameParserinstance. AParsershould only mutate its internal state if it's essential to do so, such as for critical path performance reasons.Backtracking
feedcan internallycloneitsInput, if it might need to backtrack. Keep in mind that, becauseInputis only valid for the duration of a call tofeed, input must be internally buffered if it needs to be preserved betweenfeedinvocations.Forking
The
fork(Object)method passes an out-of-band condition to aParser, yielding aParsercontinuation whose behavior may be altered by the given condition. For example, an HTMLParsermightforkan 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 erroredParserto a different output type.Obind()Returns the parsed result.static <O> Parser<O>done()Returns aParserin the done state thatbinds anullparsed result.static <O> Parser<O>done(O output)Returns aParserin the done state thatbinds the given parsedoutput.static <O> Parser<O>error(Throwable error)Returns aParserin the error state thattraps the given parseerror.static <O> Parser<O>error(Diagnostic diagnostic)abstract Parser<O>feed(Input input)Incrementally parses as muchinputas possible, and returns anotherParserthat represents the continuation of how to parse additionalInput.Parser<O>feed(InputBuffer input)Incrementally decodes as muchinputbuffer data as possible, and returns anotherDecoderthat represents the continuation of how to decode additional buffer data.Parser<O>fork(Object condition)Returns aParsercontinuation whose behavior may be altered by the given out-of-bandcondition.booleanisCont()booleanisDone()Returnstruewhen parsing has terminated successfully, andbindwill return the parsed result.booleanisError()Returnstruewhen parsing has terminated in failure, andtrapwill return the parse error.Throwabletrap()Returns the parse error.
-
-
-
Method Detail
-
isDone
public boolean isDone()
Returnstruewhen parsing has terminated successfully, andbindwill return the parsed result. i.e. thisParseris in the done state.
-
isError
public boolean isError()
Returnstruewhen parsing has terminated in failure, andtrapwill return the parse error. i.e. thisParseris in the error state.
-
feed
public abstract Parser<O> feed(Input input)
Incrementally parses as muchinputas possible, and returns anotherParserthat represents the continuation of how to parse additionalInput. Ifinputenters the done state,feedmust return a terminatedParser, i.e. aParserin the done state, or in the error state. The giveninputis only guaranteed to be valid for the duration of the method call; references toinputmust not be stored.
-
feed
public Parser<O> feed(InputBuffer input)
Description copied from class:DecoderIncrementally decodes as muchinputbuffer data as possible, and returns anotherDecoderthat represents the continuation of how to decode additional buffer data. IfisLastistrue, thenfeedmust return a terminatedDecoder, i.e. aDecoderin the done state, or in the error state. The giveninputbuffer is only guaranteed to be valid for the duration of the method call; references toinputmust not be stored.
-
fork
public Parser<O> fork(Object condition)
Returns aParsercontinuation 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:
bindin classDecoder<O>- Throws:
IllegalStateException- if thisParseris 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:
trapin classDecoder<O>- Throws:
IllegalStateException- if thisParseris not in the error state.
-
asError
public <O2> Parser<O2> asError()
Casts an erroredParserto a different output type. AParserin the error state can have any output type.- Overrides:
asErrorin classDecoder<O>- Throws:
IllegalStateException- if thisParseris not in the error state.
-
done
public static <O> Parser<O> done()
Returns aParserin the done state thatbinds anullparsed result.
-
done
public static <O> Parser<O> done(O output)
Returns aParserin the done state thatbinds the given parsedoutput.
-
error
public static <O> Parser<O> error(Throwable error)
Returns aParserin the error state thattraps the given parseerror.
-
error
public static <O> Parser<O> error(Diagnostic diagnostic)
-
-