Struct StreamCharSpan

Source
pub struct StreamCharSpan<P>{ /* private fields */ }
Expand description

This provides a span between two byte offsets within a stream; the start and end have an associated position that might also ccurately provide line and column numbers

This can be used as within tokens for a lexer; if crate::LineColumn is used for the generic argument then the lexer will correctly track the line and column of the span of tokens, and parsers can correctly track real spans of the tokens, which allows for suitable formatting of errors etc within their context (see crate::FmtContext also).

Implementations§

Source§

impl<P> StreamCharSpan<P>

Source

pub fn new(start: P, end: P) -> Self

Create a new StreamCharSpan

Examples found in repository?
examples/simple.rs (line 74)
61    pub fn parse_comment_line<L>(
62        stream: &L,
63        state: L::State,
64        ch: char,
65    ) -> LexerParseResult<P, Self, L::Error>
66    where
67        L: CharStream<P>,
68        L: Lexer<Token = Self, State = P>,
69    {
70        match stream.do_while(state, ch, &|n, ch| {
71            ((n < 2) && (ch == '/')) || ((n >= 2) && ch != '\n')
72        }) {
73            (state, Some((start, _n))) => {
74                let span = StreamCharSpan::new(start, state);
75                Ok(Some((state, SimpleToken::CommentLine(span))))
76            }
77            (_, None) => Ok(None),
78        }
79    }
80
81    //fp parse_digits
82    pub fn parse_digits<L>(
83        stream: &L,
84        state: L::State,
85        ch: char,
86    ) -> LexerParseResult<P, Self, L::Error>
87    where
88        L: CharStream<P>,
89        L: Lexer<Token = Self, State = P>,
90    {
91        match stream.do_while(state, ch, &|_, ch| ch.is_ascii_digit()) {
92            (state, Some((start, _n))) => {
93                let span = StreamCharSpan::new(start, state);
94                Ok(Some((state, SimpleToken::Digits(span))))
95            }
96            (_, None) => Ok(None),
97        }
98    }
99
100    //fp parse_whitespace
101    pub fn parse_whitespace<L>(
102        stream: &L,
103        state: L::State,
104        ch: char,
105    ) -> LexerParseResult<P, Self, L::Error>
106    where
107        L: CharStream<P>,
108        L: Lexer<Token = Self, State = P>,
109    {
110        match stream.do_while(state, ch, &|_, ch| (ch == ' ' || ch == '\t')) {
111            (state, Some((start, _))) => {
112                let span = StreamCharSpan::new(start, state);
113                Ok(Some((state, SimpleToken::Whitespace(span))))
114            }
115            (_, None) => Ok(None),
116        }
117    }
118
119    //fp parse_id
120    pub fn parse_id<L, F1, F2>(
121        stream: &L,
122        state: L::State,
123        ch: char,
124        is_id_start: F1,
125        is_id: F2,
126    ) -> LexerParseResult<P, Self, L::Error>
127    where
128        L: CharStream<P>,
129        L: Lexer<Token = Self, State = P>,
130        F1: Fn(char) -> bool,
131        F2: Fn(char) -> bool,
132    {
133        match stream.do_while(state, ch, &|n, ch| {
134            (n == 0 && is_id_start(ch)) || ((n > 0) && is_id(ch))
135        }) {
136            (state, Some((start, _))) => {
137                let span = StreamCharSpan::new(start, state);
138                Ok(Some((state, SimpleToken::Id(span))))
139            }
140            (_, None) => Ok(None),
141        }
142    }
Source

pub fn new_at(posn: &P) -> Self

Create a new StreamCharSpan

Source

pub fn end_at(self, posn: &P) -> Self

Create a new StreamCharSpan

Source

pub fn start(&self) -> &P

Get the start of the span

Source

pub fn end(&self) -> &P

Get the end of the span

Source

pub fn byte_range(&self) -> Range<usize>

Get the range between the two positions of this StreamCharSpan

Trait Implementations§

Source§

impl<P> Clone for StreamCharSpan<P>

Source§

fn clone(&self) -> StreamCharSpan<P>

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<P> Debug for StreamCharSpan<P>

Source§

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

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

impl<P> Copy for StreamCharSpan<P>

Auto Trait Implementations§

§

impl<P> Freeze for StreamCharSpan<P>
where P: Freeze,

§

impl<P> RefUnwindSafe for StreamCharSpan<P>
where P: RefUnwindSafe,

§

impl<P> Send for StreamCharSpan<P>
where P: Send,

§

impl<P> Sync for StreamCharSpan<P>
where P: Sync,

§

impl<P> Unpin for StreamCharSpan<P>
where P: Unpin,

§

impl<P> UnwindSafe for StreamCharSpan<P>
where P: UnwindSafe,

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.