mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-28 06:14:46 +00:00
Remove line tracking in parser
This commit is contained in:
parent
8e1241adea
commit
a13c474f6b
3 changed files with 40 additions and 28 deletions
|
@ -1,10 +1,10 @@
|
|||
use crate::ast::CommentOrNewline;
|
||||
use crate::ast::Spaceable;
|
||||
use crate::parser::{self, and, backtrackable, BadInputError, Parser, Progress::*};
|
||||
use crate::state::JustColumn;
|
||||
use crate::state::State;
|
||||
use bumpalo::collections::vec::Vec;
|
||||
use bumpalo::Bump;
|
||||
use roc_region::all::LineColumn;
|
||||
use roc_region::all::Loc;
|
||||
use roc_region::all::Position;
|
||||
|
||||
|
@ -194,7 +194,7 @@ where
|
|||
move |arena, mut state: State<'a>| {
|
||||
let comments_and_newlines = Vec::new_in(arena);
|
||||
|
||||
match eat_spaces(state.bytes(), state.xyzlcol, state.pos(), comments_and_newlines) {
|
||||
match eat_spaces(state.bytes(), false, state.xyzlcol, state.pos(), comments_and_newlines) {
|
||||
HasTab(xyzlcol, pos) => {
|
||||
// there was a tab character
|
||||
let mut state = state;
|
||||
|
@ -211,12 +211,13 @@ where
|
|||
}
|
||||
Good {
|
||||
xyzcol: pos,
|
||||
multiline,
|
||||
bytes,
|
||||
comments_and_newlines,
|
||||
} => {
|
||||
if bytes == state.bytes() {
|
||||
Ok((NoProgress, &[] as &[_], state))
|
||||
} else if state.xyzlcol.line != pos.line {
|
||||
} else if multiline {
|
||||
// we parsed at least one newline
|
||||
|
||||
state.indent_column = pos.column;
|
||||
|
@ -242,16 +243,18 @@ where
|
|||
|
||||
enum SpaceState<'a> {
|
||||
Good {
|
||||
xyzcol: LineColumn,
|
||||
xyzcol: JustColumn,
|
||||
multiline: bool,
|
||||
bytes: &'a [u8],
|
||||
comments_and_newlines: Vec<'a, CommentOrNewline<'a>>,
|
||||
},
|
||||
HasTab(LineColumn, Position),
|
||||
HasTab(JustColumn, Position),
|
||||
}
|
||||
|
||||
fn eat_spaces<'a>(
|
||||
mut bytes: &'a [u8],
|
||||
mut xyzlcol: LineColumn,
|
||||
mut multiline: bool,
|
||||
mut xyzlcol: JustColumn,
|
||||
mut pos: Position,
|
||||
mut comments_and_newlines: Vec<'a, CommentOrNewline<'a>>,
|
||||
) -> SpaceState<'a> {
|
||||
|
@ -267,7 +270,7 @@ fn eat_spaces<'a>(
|
|||
b'\n' => {
|
||||
bytes = &bytes[1..];
|
||||
pos = pos.bump_newline();
|
||||
xyzlcol.line += 1;
|
||||
multiline = true;
|
||||
xyzlcol.column = 0;
|
||||
comments_and_newlines.push(CommentOrNewline::Newline);
|
||||
}
|
||||
|
@ -281,7 +284,7 @@ fn eat_spaces<'a>(
|
|||
b'#' => {
|
||||
xyzlcol.column += 1;
|
||||
pos = pos.bump_column(1);
|
||||
return eat_line_comment(&bytes[1..], xyzlcol, pos, comments_and_newlines);
|
||||
return eat_line_comment(&bytes[1..], multiline, xyzlcol, pos, comments_and_newlines);
|
||||
}
|
||||
_ => break,
|
||||
}
|
||||
|
@ -289,6 +292,7 @@ fn eat_spaces<'a>(
|
|||
|
||||
Good {
|
||||
xyzcol: xyzlcol,
|
||||
multiline,
|
||||
bytes,
|
||||
comments_and_newlines,
|
||||
}
|
||||
|
@ -296,7 +300,8 @@ fn eat_spaces<'a>(
|
|||
|
||||
fn eat_line_comment<'a>(
|
||||
mut bytes: &'a [u8],
|
||||
mut xyzlcol: LineColumn,
|
||||
mut multiline: bool,
|
||||
mut xyzlcol: JustColumn,
|
||||
mut pos: Position,
|
||||
mut comments_and_newlines: Vec<'a, CommentOrNewline<'a>>,
|
||||
) -> SpaceState<'a> {
|
||||
|
@ -318,9 +323,9 @@ fn eat_line_comment<'a>(
|
|||
pos = pos.bump_newline();
|
||||
|
||||
comments_and_newlines.push(CommentOrNewline::DocComment(""));
|
||||
xyzlcol.line += 1;
|
||||
multiline = true;
|
||||
xyzlcol.column = 0;
|
||||
return eat_spaces(bytes, xyzlcol, pos, comments_and_newlines);
|
||||
return eat_spaces(bytes, multiline, xyzlcol, pos, comments_and_newlines);
|
||||
}
|
||||
None => {
|
||||
// consume the second #
|
||||
|
@ -330,6 +335,7 @@ fn eat_line_comment<'a>(
|
|||
|
||||
return Good {
|
||||
xyzcol: xyzlcol,
|
||||
multiline,
|
||||
bytes,
|
||||
comments_and_newlines,
|
||||
};
|
||||
|
@ -357,9 +363,9 @@ fn eat_line_comment<'a>(
|
|||
comments_and_newlines.push(CommentOrNewline::LineComment(comment));
|
||||
}
|
||||
pos = pos.bump_newline();
|
||||
xyzlcol.line += 1;
|
||||
multiline = true;
|
||||
xyzlcol.column = 0;
|
||||
return eat_spaces(&bytes[1..], xyzlcol, pos, comments_and_newlines);
|
||||
return eat_spaces(&bytes[1..], multiline, xyzlcol, pos, comments_and_newlines);
|
||||
}
|
||||
_ => {
|
||||
bytes = &bytes[1..];
|
||||
|
@ -381,6 +387,7 @@ fn eat_line_comment<'a>(
|
|||
|
||||
Good {
|
||||
xyzcol: xyzlcol,
|
||||
multiline,
|
||||
bytes,
|
||||
comments_and_newlines,
|
||||
}
|
||||
|
|
|
@ -11,12 +11,12 @@ use crate::parser::{
|
|||
EPattern, ERecord, EString, EType, EWhen, Either, ParseResult, Parser,
|
||||
};
|
||||
use crate::pattern::loc_closure_param;
|
||||
use crate::state::State;
|
||||
use crate::state::{State, JustColumn};
|
||||
use crate::type_annotation;
|
||||
use bumpalo::collections::Vec;
|
||||
use bumpalo::Bump;
|
||||
use roc_module::called_via::{BinOp, CalledVia, UnaryOp};
|
||||
use roc_region::all::{Loc, Position, Region, LineColumn};
|
||||
use roc_region::all::{Loc, Position, Region};
|
||||
|
||||
use crate::parser::Progress::{self, *};
|
||||
|
||||
|
@ -310,7 +310,7 @@ fn unary_negate<'a>() -> impl Parser<'a, (), EExpr<'a>> {
|
|||
fn parse_expr_start<'a>(
|
||||
min_indent: u16,
|
||||
options: ExprParseOptions,
|
||||
start: LineColumn,
|
||||
start: JustColumn,
|
||||
arena: &'a Bump,
|
||||
state: State<'a>,
|
||||
) -> ParseResult<'a, Loc<Expr<'a>>, EExpr<'a>> {
|
||||
|
@ -331,7 +331,7 @@ fn parse_expr_start<'a>(
|
|||
fn parse_expr_operator_chain<'a>(
|
||||
min_indent: u16,
|
||||
options: ExprParseOptions,
|
||||
start: LineColumn,
|
||||
start: JustColumn,
|
||||
arena: &'a Bump,
|
||||
state: State<'a>,
|
||||
) -> ParseResult<'a, Expr<'a>, EExpr<'a>> {
|
||||
|
@ -777,7 +777,7 @@ struct DefState<'a> {
|
|||
|
||||
fn parse_defs_end<'a>(
|
||||
options: ExprParseOptions,
|
||||
start: LineColumn,
|
||||
start: JustColumn,
|
||||
mut def_state: DefState<'a>,
|
||||
arena: &'a Bump,
|
||||
state: State<'a>,
|
||||
|
@ -892,7 +892,7 @@ fn parse_defs_end<'a>(
|
|||
|
||||
fn parse_defs_expr<'a>(
|
||||
options: ExprParseOptions,
|
||||
start: LineColumn,
|
||||
start: JustColumn,
|
||||
def_state: DefState<'a>,
|
||||
arena: &'a Bump,
|
||||
state: State<'a>,
|
||||
|
@ -933,7 +933,7 @@ fn parse_defs_expr<'a>(
|
|||
fn parse_expr_operator<'a>(
|
||||
min_indent: u16,
|
||||
options: ExprParseOptions,
|
||||
start: LineColumn,
|
||||
start: JustColumn,
|
||||
mut expr_state: ExprState<'a>,
|
||||
loc_op: Loc<BinOp>,
|
||||
arena: &'a Bump,
|
||||
|
@ -1222,7 +1222,7 @@ fn parse_expr_operator<'a>(
|
|||
fn parse_expr_end<'a>(
|
||||
min_indent: u16,
|
||||
options: ExprParseOptions,
|
||||
start: LineColumn,
|
||||
start: JustColumn,
|
||||
mut expr_state: ExprState<'a>,
|
||||
arena: &'a Bump,
|
||||
state: State<'a>,
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
use crate::parser::Progress::*;
|
||||
use crate::parser::{BadInputError, Progress};
|
||||
use bumpalo::Bump;
|
||||
use roc_region::all::{Position, Region, LineColumn};
|
||||
use roc_region::all::{Position, Region};
|
||||
use std::fmt;
|
||||
|
||||
/// A position in a source file.
|
||||
|
@ -15,19 +15,25 @@ pub struct State<'a> {
|
|||
input_len: usize,
|
||||
|
||||
/// Current position within the input (line/column)
|
||||
pub xyzlcol: LineColumn,
|
||||
pub xyzlcol: JustColumn,
|
||||
|
||||
/// Current indentation level, in columns
|
||||
/// (so no indent is col 1 - this saves an arithmetic operation.)
|
||||
pub indent_column: u16,
|
||||
}
|
||||
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct JustColumn {
|
||||
pub column: u16,
|
||||
}
|
||||
|
||||
impl<'a> State<'a> {
|
||||
pub fn new(bytes: &'a [u8]) -> State<'a> {
|
||||
State {
|
||||
bytes,
|
||||
input_len: bytes.len(),
|
||||
xyzlcol: LineColumn::default(),
|
||||
xyzlcol: JustColumn { column: 0 },
|
||||
indent_column: 0,
|
||||
}
|
||||
}
|
||||
|
@ -80,8 +86,7 @@ impl<'a> State<'a> {
|
|||
Some(column_usize) if column_usize <= u16::MAX as usize => {
|
||||
Ok(State {
|
||||
bytes: &self.bytes[quantity..],
|
||||
xyzlcol: LineColumn {
|
||||
line: self.xyzlcol.line,
|
||||
xyzlcol: JustColumn {
|
||||
column: column_usize as u16,
|
||||
},
|
||||
// Once we hit a nonspace character, we are no longer indenting.
|
||||
|
@ -125,8 +130,8 @@ impl<'a> fmt::Debug for State<'a> {
|
|||
|
||||
write!(
|
||||
f,
|
||||
"\n\t(line, col): ({}, {}),",
|
||||
self.xyzlcol.line, self.xyzlcol.column
|
||||
"\n\t(col): {},",
|
||||
self.xyzlcol.column
|
||||
)?;
|
||||
write!(f, "\n\tindent_column: {}", self.indent_column)?;
|
||||
write!(f, "\n}}")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue