The return type of a Parser step.
Result types: The parser driver feeds the input stream to the parser one
element at a time, representing a parse Step. If the step result
SPartial indicates that a parse result is available and the parser can
accept more input, we can extract the result using the parser's extract
function and feed more input to the parser. If the result is SContinue, we
must feed more input in order to get a result. If the parser returns SDone
then a result is available and the parser can no longer take any more input.
Stream position: The n in SPartial n, Scontinue n and SDone n is a
count by which we adjust the current stream position after this step. If the
count is positive we move forward in the stream, if it is 0 then we stay
where we are, if it is negative then we move backward in the stream.
Essentially, if the input stream position was pos before processing the
current element then the new stream position after processing the element
would be pos + n.
We can also think of this count as the number of items consumed by the
parser. If the current input item is consumed then n is 1, if the current
input item should be presented to the next parser step then n is 0. If n
is less than 0 then the parser backtracks by n elements before the current
element before the next parsing step is invoked. n is not allowed to be
greater than 1 in the regular stream parsers, but it can be more than 1 in
an array parser because it can consume more than one elements from the
array.
Backtracking: If the parser result is SContinue, the parser driver
retains the input in a backtracking buffer, in case of failure the parser
can backtrack maximum up to the length of the backtracking buffer. Whenever
the result is SPartial the current backtracking buffer is discarded; this
means that we cannot backtrack beyond the currrent position in the stream.
The parser must ensure that the backtrack position is always within the
bounds of the backtracking buffer, otherwise a runtime error will occur.
Failure: If the parser is not yet done, we can use the extract operation
on the state of the parser to extract a result. If the parser never
yielded a result in the past, extract fails with a ParseError exception.
If the parser yielded a Partial result in the past then extract returns
the latest partial result. Therefore, if a parser yields a partial result
once then it cannot fail later on.
Pre-release
Constructors
SPartial !Int !sSPartial count state. The following statements hold on an SPartial
result:
extract on state would succeed and give a result.
Input stream position is updated to current position + count.
All buffered input before the new position is dropped. The parser can
never backtrack before this position.
SContinue !Int !sSContinue count state. The following statements hold on an SContinue
result:
If SPartial result was returned in the past, extract on state
would give that result otherwise it will return SError or SContinue.
Input stream position is updated to current position + count.
the previous input is retained in a backtrack buffer.
SDone !Int !bDone with leftover input count and result.
SDone count result means the parser has finished, it will not accept
any more input, the final stream position must be set to current
position + count and the result of the parser is in result.
SError !StringParser failed without generating any output.
The parsing operation may backtrack to the beginning and try another
alternative.