Abstract Abstract feedIncrementally parses as much input as possible, and returns another
Parser that represents the continuation of how to parse additional
Input. If input enters the done state, feed must return a
terminated Parser, i.e. a Parser in the done state, or in the
error state. The given input is only guaranteed to be valid for the
duration of the method call; references to input must not be stored.
Static doneStatic errorReturns a Parser in the error state that traps the given parse error.
Generated using TypeDoc
Continuation of how to parse subsequent [[Input]] tokens 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 primitive numbers, 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: _cont_​inue, done, or error. The cont state indicates that [[feed]] is ready to consumeInput; the done state indicates that parsing terminated successfully, and that [[bind]] will return the parsed result; the error state indicates that parsing terminated in failure, and that [[trap]] will return the parse error.Parsersubclasses default to the cont state.Feeding input
The [[feed]] method incrementally parses as much
Inputas 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 the [[bind]] 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 the [[trap]] 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. [[Parser.done]] returns aParserin the done state. [[Parser.error]] 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 internally [[Input.clone clone]] itsInput, 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]] method passes an out-of-band condition to a
Parser, 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.