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