Struct LexerOfStr

Source
pub struct LexerOfStr<'a, P, T, E>{ /* private fields */ }
Expand description

A Lexer of a str, using an arbitrary stream position type, lexer token, and lexer error.

This provides implementations of Lexer and CharStream.

The Lexer implementation means that a LexerOfStr has a ‘parse’ method that can be invoked to parse a single token at a position within the str, and another ‘iter’ method that can be invoked to generate an iterator that returns all the tokens in the str

If the iterator or parser return an Err, the that error is of the generic type ‘E’ supplied to the LexerOfStr which must implement LexerError of the generic position ‘P’ - so a failure to parse a character in the string can be indicated at a particular location (byte offset, with line number and column potentially).

The actual parsing of tokens is supported through the Lexer trait for both the ‘parser’ and ‘iter’ trait methods using a &BoxDynLexerParseFn. These must be boxed functions with the signature like:

   fn parse(stream: &LexerOfStr<P, T, E>, pos:P, ch:char) ->
              LexerParseResult<P, T, E>

where

   LexerParseResult<P, T, E> = Result<Option<P, T>, E>

See the Lexer trait for more details on these parse functions

The LexerOfStr also provides a CharStream implementation, which provides methods that are can be used by the parse functions.

This provides methods to match strings, get

Implementations§

Source§

impl<'a, P, T, E> LexerOfStr<'a, P, T, E>

Source

pub fn new(text: &'a str) -> Self

Create a new LexerOfStr by borrowing a str

Trait Implementations§

Source§

impl<'a, P, T, E> CharStream<P> for LexerOfStr<'a, P, T, E>

Source§

fn range_as_bytes(&self, ofs: usize, n: usize) -> &[u8]

Borrow some bytes of the stream from an offset

Return None if the bytes are out of range

Source§

fn get_text_span(&self, span: &StreamCharSpan<P>) -> &str

Get the text of a StreamCharSpan provided by a parser

§Safety

The StreamCharSpan must have been provided by a parser and so the byte offsets are indeed utf8 character boundaries

Source§

fn get_text(&self, start: P, end: P) -> &str

Get the text between two crate::StreamCharPos provided by a parser

§Safety

The crate::StreamCharPos must have been provided by a parser and so the byte offsets are indeed utf8 character boundaries

Source§

fn peek_at(&self, state: &P) -> Option<char>

Get the utf8 chararacter at the byte offset, or None at the end of a string

§Safety

‘state’ is maintained as a utf8 character point boundary within or at the end of the ‘str’ borrowed by Self

Source§

fn matches_bytes(&self, state: &P, s: &[u8]) -> bool

Match the text at the offset with a str

Source§

fn matches_str(&self, pos: &P, pat: &str) -> bool

Match the text at the offset with a str

Source§

fn consumed(&self, state: P, n: usize) -> P

Move the stream state forward by the specified number of characters Read more
Source§

fn do_while<F: Fn(usize, char) -> bool>( &self, state: P, ch: char, f: &F, ) -> (P, Option<(P, usize)>)

Steps along the stream starting at the provided state (and character) while the provided function returns true; the function is provided with the index and character (starting at 0 / ch), and it returns true if the token continues, otherwise false Read more
Source§

fn fold<T, F: Fn(&Self, T, &P, usize, char) -> (T, Option<P>)>( &self, state: P, ch: char, acc: T, f: &F, ) -> (P, Option<(P, usize, T)>)

Steps along the stream starting at the provided state, character and accumulator value while the provided function returns (true, new accumulator); the function is provided with the latest accumulator, index, character (starting at 0 / ch), and it returns true and a new accumulator if the token continues, otherwise false and the final accumulator value Read more
Source§

fn consumed_char(&self, state: P, ch: char) -> P

Get a stream state after consuming the specified character at its current state
Source§

unsafe fn consumed_newline(&self, state: P, num_bytes: usize) -> P

Get a stream state after consuming a newline at its current state Read more
Source§

fn consumed_ascii_str(&self, state: P, s: &str) -> P

Get the state after consuming a particular ascii string without newlines Read more
Source§

unsafe fn consumed_chars( &self, state: P, num_bytes: usize, num_chars: usize, ) -> P

Become the span after consuming a particular string of known character length Read more
Source§

fn commit_consumed(&self, _up_to: &P)

Invoked by the Lexer to indicate that the stream has been consumed up to a certain point, and that (for parsing) no state earlier in the stream will be requested in the future Read more
Source§

impl<'a, P, T, E> Clone for LexerOfStr<'a, P, T, E>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a, P, T: Debug, E: Debug> Debug for LexerOfStr<'a, P, T, E>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a, P, T, E> Lexer for LexerOfStr<'a, P, T, E>
where P: PosnInCharStream, T: Debug + Clone, E: LexerError<P>,

Source§

type Token = T

The Token type is the type of the token to be returned by the Lexer; it is used as part of the result of the Lexer parse functions.
Source§

type Error = E

The error type returned by the parser functions in the lexical analyzer
Source§

type State = P

The State of the stream that is used and returned by the parse functions; it must be copy as it is replicated constantly throughout the parsing process. Read more
Source§

fn parse<'iter>( &'iter self, state: Self::State, parsers: &[BoxDynLexerParseFn<'iter, Self>], ) -> LexerParseResult<Self::State, Self::Token, Self::Error>

This attempts to parse the next token found at the state of the Lexer stream, by applying the parsers in order. Read more
Source§

fn iter<'iter>( &'iter self, parsers: &'iter [BoxDynLexerParseFn<'iter, Self>], ) -> Box<dyn Iterator<Item = Result<T, E>> + 'iter>

This creates an iterator over all of the tokens in the Lexer stream, by applying the parsers in order at the current stream position whenever the ‘next’ method is invoked. Read more
Source§

impl<'a, P, T, E> Copy for LexerOfStr<'a, P, T, E>

Auto Trait Implementations§

§

impl<'a, P, T, E> Freeze for LexerOfStr<'a, P, T, E>

§

impl<'a, P, T, E> RefUnwindSafe for LexerOfStr<'a, P, T, E>

§

impl<'a, P, T, E> Send for LexerOfStr<'a, P, T, E>
where P: Sync, T: Sync, E: Sync,

§

impl<'a, P, T, E> Sync for LexerOfStr<'a, P, T, E>
where P: Sync, T: Sync, E: Sync,

§

impl<'a, P, T, E> Unpin for LexerOfStr<'a, P, T, E>

§

impl<'a, P, T, E> UnwindSafe for LexerOfStr<'a, P, T, E>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.