Skip to content

Commit b555fa9

Browse files
committed
chore: simplify span handling in ParserState
1 parent d28b464 commit b555fa9

File tree

1 file changed

+9
-13
lines changed

1 file changed

+9
-13
lines changed

parse-it/src/parser.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,7 @@
99
//!
1010
//! [`ParseIt::parse`]: crate::ParseIt::parse
1111
12-
use std::{
13-
cell::{Cell, RefCell},
14-
fmt::Debug,
15-
rc::Rc,
16-
};
12+
use std::{cell::RefCell, fmt::Debug, rc::Rc};
1713

1814
use crate::lexer::Lexer;
1915

@@ -104,7 +100,7 @@ impl Error {
104100
/// assert_eq!(parse_option(&mut state, |state| state.parse('b')).unwrap(), None);
105101
/// ```
106102
pub struct ParserState<L> {
107-
span: Cell<Span>,
103+
span: Span,
108104
lexer: L,
109105
stack: Rc<RefCell<Vec<(&'static str, usize)>>>,
110106
}
@@ -113,7 +109,7 @@ impl<'a, L: Lexer<'a>> ParserState<L> {
113109
/// Create a new parser state from the given lexer.
114110
pub fn new(lexer: L) -> Self {
115111
Self {
116-
span: Cell::new(Span { start: 0, end: 0 }),
112+
span: Span { start: 0, end: 0 },
117113
lexer,
118114
stack: Rc::new(RefCell::new(Vec::new())),
119115
}
@@ -128,11 +124,11 @@ impl<'a, L: Lexer<'a>> ParserState<L> {
128124
fn next(&mut self) -> Option<L::Token> {
129125
match self.lexer.next() {
130126
(Some(token), advance) => {
131-
let Span { end, .. } = self.span.get();
132-
self.span.set(Span {
127+
let Span { end, .. } = self.span;
128+
self.span = Span {
133129
start: end,
134130
end: end + advance,
135-
});
131+
};
136132
Some(token)
137133
}
138134
_ => None,
@@ -149,7 +145,7 @@ impl<'a, L: Lexer<'a>> ParserState<L> {
149145

150146
/// Report an error at the current position.
151147
pub fn error(&self) -> Error {
152-
Error::new(self.span.get())
148+
Error::new(self.span)
153149
}
154150

155151
/// Whether the parser is at the end of the input.
@@ -177,15 +173,15 @@ impl<'a, L: Lexer<'a>> ParserState<L> {
177173
/// Create a fork of the current state for speculative parsing.
178174
pub fn fork(&self) -> Self {
179175
Self {
180-
span: self.span.clone(),
176+
span: self.span,
181177
lexer: self.lexer.fork(),
182178
stack: self.stack.clone(),
183179
}
184180
}
185181

186182
/// Push the given name onto the stack (for debugging purposes).
187183
pub fn push(&self, name: &'static str) {
188-
self.stack.borrow_mut().push((name, self.span.get().end));
184+
self.stack.borrow_mut().push((name, self.span.end));
189185
}
190186

191187
/// Pop the last name from the stack (for debugging purposes).

0 commit comments

Comments
 (0)