pub struct StreamCharSpan<P>where
P: PosnInCharStream,{ /* 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>where
P: PosnInCharStream,
impl<P> StreamCharSpan<P>where
P: PosnInCharStream,
Sourcepub fn new(start: P, end: P) -> Self
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 }
Sourcepub fn new_at(posn: &P) -> Self
pub fn new_at(posn: &P) -> Self
Create a new StreamCharSpan
Sourcepub fn end_at(self, posn: &P) -> Self
pub fn end_at(self, posn: &P) -> Self
Create a new StreamCharSpan
Sourcepub fn byte_range(&self) -> Range<usize>
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>where
P: PosnInCharStream + Clone,
impl<P> Clone for StreamCharSpan<P>where
P: PosnInCharStream + Clone,
Source§fn clone(&self) -> StreamCharSpan<P>
fn clone(&self) -> StreamCharSpan<P>
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moreSource§impl<P> Debug for StreamCharSpan<P>where
P: PosnInCharStream + Debug,
impl<P> Debug for StreamCharSpan<P>where
P: PosnInCharStream + Debug,
impl<P> Copy for StreamCharSpan<P>where
P: PosnInCharStream + Copy,
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more