Trait combpop::stream::Stream
[−]
[src]
pub trait Stream { type Item; fn lookahead(&mut self, len: usize) -> ParseResult<()>; fn get(&self, idx: usize) -> &Self::Item; fn advance(&mut self, len: usize); fn mark(&mut self) -> StreamMark; fn rollback(&mut self, pos: StreamMark); fn commit(&mut self); }
Stream
is a stream of tokens with lookahead/rollback ability.
Semantics
A stream is thought to be a set of the following data:
- The sequence of tokens
S
- The monotone sequence of marks
M
- The current position
i
, which is not less than any marks - The lookahead position
j
, which is not less than the current position
The actual streams have to remember only the part of the tokens, due to the contract
described in the Stream::get
method below.
Associated Types
type Item
The type of the token. Note that Stream::get
actually returns a reference to I
.
Required Methods
fn lookahead(&mut self, len: usize) -> ParseResult<()>
Tries to push the lookahead position forward.
- If
i + len <= j
, then it does nothing. - If
i + len > j
and it hit an I/O error, then it returnsErr
. - If
i + len > j
and it hit an EOF, then it returnsErr(ParseError::EOF)
. - Otherwise, it updates
j
toi + len
and returnsOk
.
fn get(&self, idx: usize) -> &Self::Item
Retrieves the looked-ahead token.
The caller must ensure that i + idx < j
. Otherwise it may panic.
fn advance(&mut self, len: usize)
Advances the current position.
The caller must ensure that i + idx < j
. Otherwise it may panic.
fn mark(&mut self) -> StreamMark
Pushes the current position to the mark stack M
.
fn rollback(&mut self, pos: StreamMark)
Pops i
from the mark stack M
.
pos
argument must match to the corresponding Stream::mark
call.
fn commit(&mut self)
Pops a value from the mark stack M
and discard it.
The mark stack M
must not be empty.
Implementors
impl<'a, T: Copy + 'a> Stream for SliceStream<'a, T> type Item = T;