Introduce SourceError to represent an error + original source

This commit is contained in:
Joshua Warner 2022-01-01 14:40:03 -07:00
parent 8d6eb178b1
commit 5c1084c453
9 changed files with 96 additions and 48 deletions

View file

@ -2,6 +2,7 @@ use crate::ast;
use crate::module::module_defs;
// use crate::module::module_defs;
use crate::parser::Parser;
use crate::parser::SourceError;
use crate::parser::SyntaxError;
use crate::state::State;
use bumpalo::collections::Vec as BumpVec;
@ -12,19 +13,21 @@ pub fn parse_expr_with<'a>(
arena: &'a Bump,
input: &'a str,
) -> Result<ast::Expr<'a>, SyntaxError<'a>> {
parse_loc_with(arena, input).map(|loc_expr| loc_expr.value)
parse_loc_with(arena, input)
.map(|loc_expr| loc_expr.value)
.map_err(|e| e.problem)
}
#[allow(dead_code)]
pub fn parse_loc_with<'a>(
arena: &'a Bump,
input: &'a str,
) -> Result<Loc<ast::Expr<'a>>, SyntaxError<'a>> {
) -> Result<Loc<ast::Expr<'a>>, SourceError<'a, SyntaxError<'a>>> {
let state = State::new(input.trim().as_bytes());
match crate::expr::test_parse_expr(0, arena, state) {
match crate::expr::test_parse_expr(0, arena, state.clone()) {
Ok(loc_expr) => Ok(loc_expr),
Err(fail) => Err(SyntaxError::Expr(fail)),
Err(fail) => Err(SyntaxError::Expr(fail).into_source_error(&state)),
}
}