diff --git a/compiler/parse/src/blankspace.rs b/compiler/parse/src/blankspace.rs index 326773cffc..0fde30d21d 100644 --- a/compiler/parse/src/blankspace.rs +++ b/compiler/parse/src/blankspace.rs @@ -265,13 +265,13 @@ fn eat_spaces<'a>( xyzlcol.column += 1; } b'\n' => { - state = state.advance(1); + state = state.advance_newline(); multiline = true; xyzlcol.column = 0; comments_and_newlines.push(CommentOrNewline::Newline); } b'\r' => { - state = state.advance(1); + state = state.advance_newline(); } b'\t' => { return HasTab(xyzlcol, state); @@ -353,11 +353,15 @@ fn eat_line_comment<'a>( } else { comments_and_newlines.push(CommentOrNewline::LineComment(comment)); } - state = state.advance(1); + state = state.advance_newline(); multiline = true; xyzlcol.column = 0; return eat_spaces(state, multiline, xyzlcol, comments_and_newlines); } + b'\r' => { + state = state.advance_newline(); + xyzlcol.column += 1; + } _ => { state = state.advance(1); xyzlcol.column += 1; diff --git a/compiler/parse/src/state.rs b/compiler/parse/src/state.rs index 5bf1adbd35..ce7db0fbec 100644 --- a/compiler/parse/src/state.rs +++ b/compiler/parse/src/state.rs @@ -14,6 +14,8 @@ pub struct State<'a> { /// Length of the original input in bytes input_len: usize, + line_start: Position, + /// Current position within the input (line/column) pub xyzlcol: JustColumn, @@ -33,6 +35,7 @@ impl<'a> State<'a> { State { bytes, input_len: bytes.len(), + line_start: Position::zero(), xyzlcol: JustColumn { column: 0 }, indent_column: 0, } @@ -49,6 +52,13 @@ impl<'a> State<'a> { 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 pub const fn pos(&self) -> Position { Position::new((self.input_len - self.bytes.len()) as u32)