Refactor ParseProblem

* Remove the `pos` field, which was always being assigned Position::default()
* Remove one use of this `pos`, by removing the never-used SyntaxError::ConditionFailed variant
* Adjust the other use to do what was probably intended - which is to say, pointing to the beginning of the def with the error
* Rename to FileError, reuse `SourceError` as an inner field, to avoid duplicating the `bytes`
This commit is contained in:
Joshua Warner 2022-01-03 19:52:33 -08:00
parent 226237d1cd
commit f22f96843e
9 changed files with 53 additions and 66 deletions

View file

@ -7,8 +7,8 @@ use crate::header::{
use crate::ident::{lowercase_ident, unqualified_ident, uppercase_ident};
use crate::parser::Progress::{self, *};
use crate::parser::{
backtrackable, specialize, word1, word2, EEffects, EExposes, EHeader, EImports, EPackages,
EProvides, ERequires, ETypedIdent, Parser, SourceError, SyntaxError,
backtrackable, specialize, specialize_region, word1, word2, EEffects, EExposes, EHeader,
EImports, EPackages, EProvides, ERequires, ETypedIdent, Parser, SourceError, SyntaxError,
};
use crate::state::State;
use crate::string_literal;
@ -31,7 +31,10 @@ pub fn module_defs<'a>() -> impl Parser<'a, Vec<'a, Loc<Def<'a>>>, SyntaxError<'
// force that we parse until the end of the input
let min_indent = 0;
skip_second!(
specialize(|e, _| SyntaxError::Expr(e), crate::expr::defs(min_indent),),
specialize_region(
|e, r| SyntaxError::Expr(e, r.start()),
crate::expr::defs(min_indent),
),
end_of_file()
)
}

View file

@ -48,7 +48,6 @@ impl Progress {
pub enum SyntaxError<'a> {
Unexpected(Region),
OutdentedTooFar,
ConditionFailed,
TooManyLines,
Eof(Region),
InvalidPattern,
@ -59,7 +58,7 @@ pub enum SyntaxError<'a> {
Todo,
Type(EType<'a>),
Pattern(EPattern<'a>),
Expr(EExpr<'a>),
Expr(EExpr<'a>, Position),
Header(EHeader<'a>),
Space(BadInputError),
NotEndOfFile(Position),
@ -237,12 +236,10 @@ impl<'a, T> SourceError<'a, T> {
}
}
pub fn into_parse_problem(self, filename: std::path::PathBuf) -> ParseProblem<'a, T> {
ParseProblem {
pos: Position::default(),
problem: self.problem,
pub fn into_file_error(self, filename: std::path::PathBuf) -> FileError<'a, T> {
FileError {
problem: self,
filename,
bytes: self.bytes,
}
}
}
@ -255,17 +252,12 @@ impl<'a> SyntaxError<'a> {
}
}
pub fn into_parse_problem(
pub fn into_file_error(
self,
filename: std::path::PathBuf,
state: &State<'a>,
) -> ParseProblem<'a, SyntaxError<'a>> {
ParseProblem {
pos: Position::default(),
problem: self,
filename,
bytes: state.original_bytes(),
}
) -> FileError<'a, SyntaxError<'a>> {
self.into_source_error(state).into_file_error(filename)
}
}
@ -594,11 +586,9 @@ pub struct SourceError<'a, T> {
}
#[derive(Debug)]
pub struct ParseProblem<'a, T> {
pub pos: Position,
pub problem: T,
pub struct FileError<'a, T> {
pub problem: SourceError<'a, T>,
pub filename: std::path::PathBuf,
pub bytes: &'a [u8],
}
pub trait Parser<'a, Output, Error> {
@ -1295,6 +1285,22 @@ where
}
}
/// Like `specialize`, except the error function receives a Region representing the begin/end of the error
pub fn specialize_region<'a, F, P, T, X, Y>(map_error: F, parser: P) -> impl Parser<'a, T, Y>
where
F: Fn(X, Region) -> Y,
P: Parser<'a, T, X>,
Y: 'a,
{
move |a, s: State<'a>| {
let start = s.pos();
match parser.parse(a, s) {
Ok(t) => Ok(t),
Err((p, error, s)) => Err((p, map_error(error, Region::new(start, s.pos())), s)),
}
}
}
pub fn specialize_ref<'a, F, P, T, X, Y>(map_error: F, parser: P) -> impl Parser<'a, T, Y>
where
F: Fn(&'a X, Position) -> Y,

View file

@ -8,6 +8,7 @@ use crate::state::State;
use bumpalo::collections::Vec as BumpVec;
use bumpalo::Bump;
use roc_region::all::Loc;
use roc_region::all::Position;
pub fn parse_expr_with<'a>(
arena: &'a Bump,
@ -27,7 +28,7 @@ pub fn parse_loc_with<'a>(
match crate::expr::test_parse_expr(0, arena, state.clone()) {
Ok(loc_expr) => Ok(loc_expr),
Err(fail) => Err(SyntaxError::Expr(fail).into_source_error(&state)),
Err(fail) => Err(SyntaxError::Expr(fail, Position::default()).into_source_error(&state)),
}
}