use EExpr expr parser in more places

This commit is contained in:
Folkert 2021-02-27 23:20:11 +01:00
parent 18229bcf54
commit 10799c52b3
3 changed files with 210 additions and 42 deletions

View file

@ -165,10 +165,12 @@ enum Context {
enum Node {
WhenCondition,
WhenBranch,
WhenIfGuard,
IfCondition,
IfThenBranch,
IfElseBranch,
ListElement,
InsideParens,
}
fn to_expr_report<'a>(
@ -191,13 +193,15 @@ fn to_expr_report<'a>(
EExpr::Str(string, row, col) => {
to_str_report(alloc, filename, context, &string, *row, *col)
}
EExpr::InParens(expr, row, col) => {
to_expr_in_parens_report(alloc, filename, context, &expr, *row, *col)
}
EExpr::Type(tipe, row, col) => to_type_report(alloc, filename, &tipe, *row, *col),
EExpr::Def(syntax, row, col) => to_syntax_report(alloc, filename, syntax, *row, *col),
EExpr::ElmStyleFunction(region, row, col) => {
let surroundings = Region::from_rows_cols(start_row, start_col, *row, *col);
let region = *region;
// let region = Region::from_row_col(*row, *col);
let doc = alloc.stack(vec![
alloc.reflow(r"I am in the middle of parsing a definition, but I got stuck here:"),
@ -222,7 +226,7 @@ fn to_expr_report<'a>(
EExpr::Start(row, col) => {
let (context_row, context_col, a_thing) = match context {
Context::InNode(node, r, c, _) => match node {
Node::WhenCondition | Node::WhenBranch => (
Node::WhenCondition | Node::WhenBranch | Node::WhenIfGuard => (
r,
c,
alloc.concat(vec![
@ -241,6 +245,7 @@ fn to_expr_report<'a>(
]),
),
Node::ListElement => (r, c, alloc.text("a list")),
Node::InsideParens => (r, c, alloc.text("some parentheses")),
},
Context::InDef(r, c) => (r, c, alloc.text("a definition")),
};
@ -421,7 +426,14 @@ fn to_lambda_report<'a>(
ELambda::Start(_row, _col) => unreachable!("another branch would have been taken"),
ELambda::Syntax(syntax, row, col) => to_syntax_report(alloc, filename, syntax, row, col),
ELambda::Body(expr, row, col) => to_expr_report(
alloc,
filename,
Context::InDef(start_row, start_col),
expr,
row,
col,
),
ELambda::Pattern(ref pattern, row, col) => {
to_pattern_report(alloc, filename, pattern, row, col)
}
@ -625,6 +637,75 @@ fn to_str_report<'a>(
}
}
}
fn to_expr_in_parens_report<'a>(
alloc: &'a RocDocAllocator<'a>,
filename: PathBuf,
context: Context,
parse_problem: &roc_parse::parser::EInParens<'a>,
start_row: Row,
start_col: Col,
) -> Report<'a> {
use roc_parse::parser::EInParens;
match *parse_problem {
EInParens::Space(error, row, col) => to_space_report(alloc, filename, &error, row, col),
EInParens::Expr(expr, row, col) => to_expr_report(
alloc,
filename,
Context::InNode(Node::InsideParens, start_row, start_col, Box::new(context)),
expr,
row,
col,
),
EInParens::End(row, col) | EInParens::IndentEnd(row, col) => {
let surroundings = Region::from_rows_cols(start_row, start_col, row, col);
let region = Region::from_row_col(row, col);
let doc = alloc.stack(vec![
alloc
.reflow("I am partway through parsing a record pattern, but I got stuck here:"),
alloc.region_with_subregion(surroundings, region),
alloc.concat(vec![
alloc.reflow(
r"I was expecting to see a closing parenthesis next, so try adding a ",
),
alloc.parser_suggestion(")"),
alloc.reflow(" and see if that helps?"),
]),
]);
Report {
filename,
doc,
title: "UNFINISHED PARENTHESES".to_string(),
}
}
EInParens::Open(row, col) | EInParens::IndentOpen(row, col) => {
let surroundings = Region::from_rows_cols(start_row, start_col, row, col);
let region = Region::from_row_col(row, col);
let doc = alloc.stack(vec![
alloc.reflow(
r"I just started parsing an expression in parentheses, but I got stuck here:",
),
alloc.region_with_subregion(surroundings, region),
alloc.concat(vec![
alloc.reflow(r"An expression in parentheses looks like "),
alloc.parser_suggestion("(32)"),
alloc.reflow(r" or "),
alloc.parser_suggestion("(\"hello\")"),
alloc.reflow(" so I was expecting to see an expression next."),
]),
]);
Report {
filename,
doc,
title: "UNFINISHED PARENTHESES".to_string(),
}
}
}
}
fn to_list_report<'a>(
alloc: &'a RocDocAllocator<'a>,
@ -886,18 +967,14 @@ fn to_when_report<'a>(
title: "IF GUARD NO CONDITION".to_string(),
}
}
_ => {
// to_expr_report(
// alloc,
// filename,
// Context::InNode(Node::WhenIfGuard, start_row, start_col, Box::new(context)),
// expr,
// row,
// col,
// )
to_syntax_report(alloc, filename, nested, row, col)
}
_ => to_expr_report(
alloc,
filename,
Context::InNode(Node::WhenIfGuard, start_row, start_col, Box::new(context)),
nested,
row,
col,
),
},
When::Arrow(row, col) => {
let surroundings = Region::from_rows_cols(start_row, start_col, row, col);