Use newline_char over ascii_char('\n')

This commit is contained in:
Richard Feldman 2020-11-03 09:53:03 -05:00 committed by Sébastien Besnier
parent 01b9b4a08d
commit 4633619e18
2 changed files with 24 additions and 5 deletions

View file

@ -432,8 +432,12 @@ fn line_too_long(attempting: Attempting, state: State<'_>) -> (Fail, State<'_>)
(fail, state)
}
/// A single ASCII char.
/// A single ASCII char that isn't a newline.
/// (For newlines, use newline_char(), which handles line numbers)
pub fn ascii_char<'a>(expected: u8) -> impl Parser<'a, ()> {
// Make sure this really is not a newline!
debug_assert_ne!(expected, '\n');
move |_arena, state: State<'a>| match state.bytes.first() {
Some(&actual) if expected == actual => Ok(((), state.advance_without_indenting(1)?)),
Some(_) => Err(unexpected(0, state, Attempting::Keyword)),
@ -441,6 +445,17 @@ pub fn ascii_char<'a>(expected: u8) -> impl Parser<'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, ()> {
move |_arena, state: State<'a>| match state.bytes.first() {
Some(b'\n') => Ok(((), state.newline()?)),
Some(_) => Err(unexpected(0, state, Attempting::Keyword)),
_ => Err(unexpected_eof(0, Attempting::Keyword, state)),
}
}
/// 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> {