Track state in whitespace

This commit is contained in:
Joshua Warner 2021-12-23 21:56:21 -08:00
parent a13c474f6b
commit beb0629e05

View file

@ -193,9 +193,9 @@ where
move |arena, mut state: State<'a>| { move |arena, mut state: State<'a>| {
let comments_and_newlines = Vec::new_in(arena); let comments_and_newlines = Vec::new_in(arena);
let col =state.xyzlcol;
match eat_spaces(state.bytes(), false, state.xyzlcol, state.pos(), comments_and_newlines) { match eat_spaces(state.bytes(), state.clone(), false, col, state.pos(), comments_and_newlines) {
HasTab(xyzlcol, pos) => { HasTab(xyzlcol, pos, new_state) => {
// there was a tab character // there was a tab character
let mut state = state; let mut state = state;
state.xyzlcol = xyzlcol; state.xyzlcol = xyzlcol;
@ -211,10 +211,15 @@ where
} }
Good { Good {
xyzcol: pos, xyzcol: pos,
state: new_state,
multiline, multiline,
bytes, bytes,
comments_and_newlines, comments_and_newlines,
} => { } => {
assert_eq!(
std::str::from_utf8(new_state.bytes()).unwrap(),
std::str::from_utf8(bytes).unwrap());
if bytes == state.bytes() { if bytes == state.bytes() {
Ok((NoProgress, &[] as &[_], state)) Ok((NoProgress, &[] as &[_], state))
} else if multiline { } else if multiline {
@ -244,15 +249,17 @@ where
enum SpaceState<'a> { enum SpaceState<'a> {
Good { Good {
xyzcol: JustColumn, xyzcol: JustColumn,
state: State<'a>,
multiline: bool, multiline: bool,
bytes: &'a [u8], bytes: &'a [u8],
comments_and_newlines: Vec<'a, CommentOrNewline<'a>>, comments_and_newlines: Vec<'a, CommentOrNewline<'a>>,
}, },
HasTab(JustColumn, Position), HasTab(JustColumn, Position, State<'a>),
} }
fn eat_spaces<'a>( fn eat_spaces<'a>(
mut bytes: &'a [u8], mut bytes: &'a [u8],
mut state: State<'a>,
mut multiline: bool, mut multiline: bool,
mut xyzlcol: JustColumn, mut xyzlcol: JustColumn,
mut pos: Position, mut pos: Position,
@ -265,26 +272,30 @@ fn eat_spaces<'a>(
b' ' => { b' ' => {
pos = pos.bump_column(1); pos = pos.bump_column(1);
bytes = &bytes[1..]; bytes = &bytes[1..];
state = state.advance(1);
xyzlcol.column += 1; xyzlcol.column += 1;
} }
b'\n' => { b'\n' => {
bytes = &bytes[1..]; bytes = &bytes[1..];
pos = pos.bump_newline(); pos = pos.bump_newline();
state = state.advance(1);
multiline = true; multiline = true;
xyzlcol.column = 0; xyzlcol.column = 0;
comments_and_newlines.push(CommentOrNewline::Newline); comments_and_newlines.push(CommentOrNewline::Newline);
} }
b'\r' => { b'\r' => {
bytes = &bytes[1..]; bytes = &bytes[1..];
state = state.advance(1);
pos = pos.bump_invisible(1); pos = pos.bump_invisible(1);
} }
b'\t' => { b'\t' => {
return HasTab(xyzlcol, pos); return HasTab(xyzlcol, pos, state);
} }
b'#' => { b'#' => {
xyzlcol.column += 1; xyzlcol.column += 1;
state = state.advance(1);
pos = pos.bump_column(1); pos = pos.bump_column(1);
return eat_line_comment(&bytes[1..], multiline, xyzlcol, pos, comments_and_newlines); return eat_line_comment(&bytes[1..], state, multiline, xyzlcol, pos, comments_and_newlines);
} }
_ => break, _ => break,
} }
@ -292,6 +303,7 @@ fn eat_spaces<'a>(
Good { Good {
xyzcol: xyzlcol, xyzcol: xyzlcol,
state,
multiline, multiline,
bytes, bytes,
comments_and_newlines, comments_and_newlines,
@ -300,6 +312,7 @@ fn eat_spaces<'a>(
fn eat_line_comment<'a>( fn eat_line_comment<'a>(
mut bytes: &'a [u8], mut bytes: &'a [u8],
mut state: State<'a>,
mut multiline: bool, mut multiline: bool,
mut xyzlcol: JustColumn, mut xyzlcol: JustColumn,
mut pos: Position, mut pos: Position,
@ -313,6 +326,7 @@ fn eat_line_comment<'a>(
bytes = &bytes[2..]; bytes = &bytes[2..];
xyzlcol.column += 2; xyzlcol.column += 2;
pos = pos.bump_column(2); pos = pos.bump_column(2);
state = state.advance(2);
true true
} }
@ -321,20 +335,23 @@ fn eat_line_comment<'a>(
bytes = &bytes[2..]; bytes = &bytes[2..];
pos = pos.bump_column(1); pos = pos.bump_column(1);
pos = pos.bump_newline(); pos = pos.bump_newline();
state = state.advance(2);
comments_and_newlines.push(CommentOrNewline::DocComment("")); comments_and_newlines.push(CommentOrNewline::DocComment(""));
multiline = true; multiline = true;
xyzlcol.column = 0; xyzlcol.column = 0;
return eat_spaces(bytes, multiline, xyzlcol, pos, comments_and_newlines); return eat_spaces(bytes, state, multiline, xyzlcol, pos, comments_and_newlines);
} }
None => { None => {
// consume the second # // consume the second #
xyzlcol.column += 1; xyzlcol.column += 1;
bytes = &bytes[1..]; bytes = &bytes[1..];
state = state.advance(1);
// pos = pos.bump_column(1); // pos = pos.bump_column(1);
return Good { return Good {
xyzcol: xyzlcol, xyzcol: xyzlcol,
state,
multiline, multiline,
bytes, bytes,
comments_and_newlines, comments_and_newlines,
@ -352,7 +369,7 @@ fn eat_line_comment<'a>(
for c in bytes { for c in bytes {
match c { match c {
b'\t' => return HasTab(xyzlcol, pos), b'\t' => return HasTab(xyzlcol, pos, state),
b'\n' => { b'\n' => {
let delta = (xyzlcol.column - initial_column) as usize; let delta = (xyzlcol.column - initial_column) as usize;
let comment = unsafe { std::str::from_utf8_unchecked(&initial[..delta]) }; let comment = unsafe { std::str::from_utf8_unchecked(&initial[..delta]) };
@ -363,12 +380,14 @@ fn eat_line_comment<'a>(
comments_and_newlines.push(CommentOrNewline::LineComment(comment)); comments_and_newlines.push(CommentOrNewline::LineComment(comment));
} }
pos = pos.bump_newline(); pos = pos.bump_newline();
state = state.advance(1);
multiline = true; multiline = true;
xyzlcol.column = 0; xyzlcol.column = 0;
return eat_spaces(&bytes[1..], multiline, xyzlcol, pos, comments_and_newlines); return eat_spaces(&bytes[1..], state, multiline, xyzlcol, pos, comments_and_newlines);
} }
_ => { _ => {
bytes = &bytes[1..]; bytes = &bytes[1..];
state = state.advance(1);
pos = pos.bump_column(1); pos = pos.bump_column(1);
xyzlcol.column += 1; xyzlcol.column += 1;
} }
@ -387,6 +406,7 @@ fn eat_line_comment<'a>(
Good { Good {
xyzcol: xyzlcol, xyzcol: xyzlcol,
state,
multiline, multiline,
bytes, bytes,
comments_and_newlines, comments_and_newlines,