more cleanup

This commit is contained in:
Folkert 2021-03-12 01:11:34 +01:00
parent 0bc1a0e50b
commit 9265cf82b9
3 changed files with 78 additions and 109 deletions

View file

@ -52,79 +52,11 @@ impl<'a> State<'a> {
}
}
pub fn check_indent(
self,
_arena: &'a Bump,
min_indent: u16,
) -> Result<Self, (SyntaxError<'a>, Self)> {
if self.indent_col < min_indent {
Err((SyntaxError::OutdentedTooFar, self))
} else {
Ok(self)
}
}
pub fn check_indent_e<TE, E>(
self,
_arena: &'a Bump,
min_indent: u16,
to_error: TE,
row: Row,
col: Col,
) -> Result<Self, (E, Self)>
where
TE: Fn(Row, Col) -> E,
{
if self.indent_col < min_indent {
Err((to_error(row, col), self))
} else {
Ok(self)
}
}
/// Returns the total number of bytes consumed since the parser began parsing.
///
/// So if the parser has consumed 8 bytes, this function will return 8.
pub fn bytes_consumed(&self) -> usize {
self.original_len - self.bytes.len()
}
/// Returns whether the parser has reached the end of the input
pub fn has_reached_end(&self) -> bool {
self.bytes.is_empty()
}
/// Increments the line, then resets column, indent_col, and is_indenting.
/// Advances the input by 1, to consume the newline character.
pub fn newline(&self, arena: &'a Bump) -> Result<Self, (Progress, SyntaxError<'a>, Self)> {
self.newline_e(arena, |_, _, _| SyntaxError::TooManyLines)
}
pub fn newline_e<TE, E>(
&self,
arena: &'a Bump,
to_error: TE,
) -> Result<Self, (Progress, E, Self)>
where
TE: Fn(BadInputError, Row, Col) -> E,
{
match self.line.checked_add(1) {
Some(line) => Ok(State {
bytes: &self.bytes[1..],
line,
column: 0,
indent_col: 0,
is_indenting: true,
original_len: self.original_len,
}),
None => Err((
Progress::NoProgress,
to_error(BadInputError::TooManyLines, self.line, self.column),
self.clone(),
)),
}
}
/// Use advance_spaces to advance with indenting.
/// This assumes we are *not* advancing with spaces, or at least that
/// any spaces on the line were preceded by non-spaces - which would mean
@ -1131,17 +1063,6 @@ pub fn ascii_char<'a>(expected: u8) -> impl Parser<'a, (), SyntaxError<'a>> {
}
}
/// A single '\n' character.
/// Use this instead of ascii_char('\n') because it properly handles
/// incrementing the line number.
pub fn newline_char<'a>() -> impl Parser<'a, (), SyntaxError<'a>> {
move |arena, state: State<'a>| match state.bytes.first() {
Some(b'\n') => Ok((Progress::MadeProgress, (), state.newline(arena)?)),
Some(_) => Err(unexpected(0, Attempting::Keyword, state)),
_ => Err(unexpected_eof(arena, state, 0)),
}
}
/// One or more ASCII hex digits. (Useful when parsing unicode escape codes,
/// which must consist entirely of ASCII hex digits.)
pub fn ascii_hex_digits<'a>() -> impl Parser<'a, &'a str, SyntaxError<'a>> {