Pass State by value.

This commit is contained in:
Richard Feldman 2019-09-03 22:37:53 -04:00
parent 6a77f00ff9
commit f9f63f88c6
2 changed files with 15 additions and 15 deletions

View file

@ -149,17 +149,17 @@ fn state_size() {
pub type ParseResult<'a, Output> = Result<(State<'a>, Output), (State<'a>, Attempting)>; pub type ParseResult<'a, Output> = Result<(State<'a>, Output), (State<'a>, Attempting)>;
pub trait Parser<'a, Output> { pub trait Parser<'a, Output> {
fn parse(&self, &'a Bump, &'a State<'a>, attempting: Attempting) -> ParseResult<'a, Output>; fn parse(&self, &'a Bump, State<'a>, attempting: Attempting) -> ParseResult<'a, Output>;
} }
impl<'a, F, Output> Parser<'a, Output> for F impl<'a, F, Output> Parser<'a, Output> for F
where where
F: Fn(&'a Bump, &'a State<'a>, Attempting) -> ParseResult<'a, Output>, F: Fn(&'a Bump, State<'a>, Attempting) -> ParseResult<'a, Output>,
{ {
fn parse( fn parse(
&self, &self,
arena: &'a Bump, arena: &'a Bump,
state: &'a State<'a>, state: State<'a>,
attempting: Attempting, attempting: Attempting,
) -> ParseResult<'a, Output> { ) -> ParseResult<'a, Output> {
self(arena, state, attempting) self(arena, state, attempting)
@ -191,7 +191,7 @@ pub fn keyword<'a>(kw: &'static str) -> impl Parser<'a, ()> {
// in the state, only the column. // in the state, only the column.
debug_assert!(!kw.contains("\n")); debug_assert!(!kw.contains("\n"));
move |_arena: &'a Bump, state: &'a State<'a>, attempting| { move |_arena: &'a Bump, state: State<'a>, attempting| {
let input = state.input; let input = state.input;
match input.get(0..kw.len()) { match input.get(0..kw.len()) {
@ -210,7 +210,7 @@ where
P: Parser<'a, A>, P: Parser<'a, A>,
F: Fn(&A) -> bool, F: Fn(&A) -> bool,
{ {
move |arena: &'a Bump, state: &'a State<'a>, attempting| { move |arena: &'a Bump, state: State<'a>, attempting| {
if let Ok((next_state, output)) = parser.parse(arena, state, attempting) { if let Ok((next_state, output)) = parser.parse(arena, state, attempting) {
if predicate(&output) { if predicate(&output) {
return Ok((next_state, output)); return Ok((next_state, output));
@ -223,7 +223,7 @@ where
pub fn any<'a>( pub fn any<'a>(
_arena: &'a Bump, _arena: &'a Bump,
state: &'a State<'a>, state: State<'a>,
attempting: Attempting, attempting: Attempting,
) -> ParseResult<'a, char> { ) -> ParseResult<'a, char> {
let input = state.input; let input = state.input;

View file

@ -8,14 +8,14 @@ use std::char;
use std::iter::Peekable; use std::iter::Peekable;
pub fn string_literal<'a>() -> impl Parser<'a, Expr<'a>> { pub fn string_literal<'a>() -> impl Parser<'a, Expr<'a>> {
move |arena: &'a Bump, state: &'a State<'a>, attempting: Attempting| { move |arena: &'a Bump, state: State<'a>, attempting: Attempting| {
let mut problems = Vec::new(); let mut problems = Vec::new();
let mut chars = state.input.chars().peekable(); let mut chars = state.input.chars().peekable();
// String literals must start with a quote. // String literals must start with a quote.
// If this doesn't, it must not be a string literal! // If this doesn't, it must not be a string literal!
if chars.next() != Some('"') { if chars.next() != Some('"') {
return Err((state.clone(), attempting)); return Err((state, attempting));
} }
// If we have precisely an empty string here, don't bother allocating // If we have precisely an empty string here, don't bother allocating
@ -88,14 +88,14 @@ pub fn string_literal<'a>() -> impl Parser<'a, Expr<'a>> {
} }
// We ran out of characters before finding a closed quote // We ran out of characters before finding a closed quote
Err((state.clone(), Attempting::StringLiteral)) Err((state, Attempting::StringLiteral))
} }
} }
fn escaped_char_problem<'a, 'p>( fn escaped_char_problem<'a, 'p>(
problems: &'p mut Problems, problems: &'p mut Problems,
problem: Problem, problem: Problem,
state: &'a State<'a>, state: State<'a>,
buf_len: usize, buf_len: usize,
) { ) {
let start_line = state.line; let start_line = state.line;
@ -120,7 +120,7 @@ fn escaped_char_problem<'a, 'p>(
fn escaped_unicode_problem<'a, 'p>( fn escaped_unicode_problem<'a, 'p>(
problems: &'p mut Problems, problems: &'p mut Problems,
problem: Problem, problem: Problem,
state: &'a State<'a>, state: State<'a>,
buf_len: usize, buf_len: usize,
hex_str_len: usize, hex_str_len: usize,
) { ) {
@ -148,7 +148,7 @@ fn escaped_unicode_problem<'a, 'p>(
#[inline(always)] #[inline(always)]
fn handle_escaped_char<'a, 'p, I>( fn handle_escaped_char<'a, 'p, I>(
arena: &'a Bump, arena: &'a Bump,
state: &'a State<'a>, state: State<'a>,
ch: char, ch: char,
chars: &mut Peekable<I>, chars: &mut Peekable<I>,
buf: &mut String<'a>, buf: &mut String<'a>,
@ -181,7 +181,7 @@ where
// We can't safely assume where the string was supposed to end. // We can't safely assume where the string was supposed to end.
escaped_char_problem(problems, Problem::NewlineInLiteral, state, buf.len()); escaped_char_problem(problems, Problem::NewlineInLiteral, state, buf.len());
return Err((state.clone(), Attempting::UnicodeEscape)); return Err((state, Attempting::UnicodeEscape));
} }
_ => { _ => {
// Report and continue. // Report and continue.
@ -196,7 +196,7 @@ where
#[inline(always)] #[inline(always)]
fn handle_escaped_unicode<'a, 'p, I>( fn handle_escaped_unicode<'a, 'p, I>(
arena: &'a Bump, arena: &'a Bump,
state: &'a State<'a>, state: State<'a>,
chars: &mut Peekable<I>, chars: &mut Peekable<I>,
buf: &mut String<'a>, buf: &mut String<'a>,
problems: &'p mut Problems, problems: &'p mut Problems,
@ -342,7 +342,7 @@ where
hex_str.len(), hex_str.len(),
); );
return Err((state.clone(), Attempting::UnicodeEscape)); return Err((state, Attempting::UnicodeEscape));
} }
normal_char => hex_str.push(normal_char), normal_char => hex_str.push(normal_char),
} }