Track line start separately

This commit is contained in:
Joshua Warner 2021-12-23 22:06:22 -08:00
parent 5ac3394a73
commit 422cdea112
2 changed files with 17 additions and 3 deletions

View file

@ -265,13 +265,13 @@ fn eat_spaces<'a>(
xyzlcol.column += 1; xyzlcol.column += 1;
} }
b'\n' => { b'\n' => {
state = state.advance(1); state = state.advance_newline();
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' => {
state = state.advance(1); state = state.advance_newline();
} }
b'\t' => { b'\t' => {
return HasTab(xyzlcol, state); return HasTab(xyzlcol, state);
@ -353,11 +353,15 @@ fn eat_line_comment<'a>(
} else { } else {
comments_and_newlines.push(CommentOrNewline::LineComment(comment)); comments_and_newlines.push(CommentOrNewline::LineComment(comment));
} }
state = state.advance(1); state = state.advance_newline();
multiline = true; multiline = true;
xyzlcol.column = 0; xyzlcol.column = 0;
return eat_spaces(state, multiline, xyzlcol, comments_and_newlines); return eat_spaces(state, multiline, xyzlcol, comments_and_newlines);
} }
b'\r' => {
state = state.advance_newline();
xyzlcol.column += 1;
}
_ => { _ => {
state = state.advance(1); state = state.advance(1);
xyzlcol.column += 1; xyzlcol.column += 1;

View file

@ -14,6 +14,8 @@ pub struct State<'a> {
/// Length of the original input in bytes /// Length of the original input in bytes
input_len: usize, input_len: usize,
line_start: Position,
/// Current position within the input (line/column) /// Current position within the input (line/column)
pub xyzlcol: JustColumn, pub xyzlcol: JustColumn,
@ -33,6 +35,7 @@ impl<'a> State<'a> {
State { State {
bytes, bytes,
input_len: bytes.len(), input_len: bytes.len(),
line_start: Position::zero(),
xyzlcol: JustColumn { column: 0 }, xyzlcol: JustColumn { column: 0 },
indent_column: 0, indent_column: 0,
} }
@ -49,6 +52,13 @@ impl<'a> State<'a> {
state state
} }
#[must_use]
pub fn advance_newline(&self) -> State<'a> {
let mut state = self.advance(1);
state.line_start = state.pos();
state
}
/// Returns the current position /// Returns the current position
pub const fn pos(&self) -> Position { pub const fn pos(&self) -> Position {
Position::new((self.input_len - self.bytes.len()) as u32) Position::new((self.input_len - self.bytes.len()) as u32)