error messages for escaped things

This commit is contained in:
Folkert 2021-02-26 22:02:42 +01:00
parent 41720ec5db
commit 2f4099f1f0
5 changed files with 218 additions and 31 deletions

View file

@ -305,24 +305,35 @@ pub fn can_problem<'b>(
alloc.reflow(" can occur in this position."),
]),
]),
Problem::InvalidHexadecimal(region) => {
todo!(
"TODO report an invalid hexadecimal number in a \\u(...) code point at region {:?}",
region
);
}
Problem::InvalidUnicodeCodePoint(region) => {
todo!(
"TODO report an invalid \\u(...) code point at region {:?}",
region
);
}
Problem::InvalidInterpolation(region) => {
todo!(
"TODO report an invalid string interpolation at region {:?}",
region
);
}
Problem::InvalidHexadecimal(region) => alloc.stack(vec![
alloc.reflow("This unicode code point is invalid:"),
alloc.region(region),
alloc.concat(vec![
alloc.reflow(r"I was expecting a hexadecimal number, like "),
alloc.parser_suggestion("\\u(1100)"),
alloc.reflow(" or "),
alloc.parser_suggestion("\\u(00FF)"),
alloc.text("."),
]),
alloc.reflow(r"Learn more about working with unicode in roc at TODO"),
]),
Problem::InvalidUnicodeCodePoint(region) => alloc.stack(vec![
alloc.reflow("This unicode code point is invalid:"),
alloc.region(region),
alloc.reflow("Learn more about working with unicode in roc at TODO"),
]),
Problem::InvalidInterpolation(region) => alloc.stack(vec![
alloc.reflow("This string interpolation is invalid:"),
alloc.region(region),
alloc.concat(vec![
alloc.reflow(r"I was expecting an identifier, like "),
alloc.parser_suggestion("\\u(message)"),
alloc.reflow(" or "),
alloc.parser_suggestion("\\u(LoremIpsum.text)"),
alloc.text("."),
]),
alloc.reflow(r"Learn more about string interpolation at TODO"),
]),
Problem::RuntimeError(runtime_error) => pretty_runtime_error(alloc, runtime_error),
};

View file

@ -181,6 +181,9 @@ fn to_expr_report<'a>(
to_lambda_report(alloc, filename, context, &lambda, *row, *col)
}
EExpr::List(list, row, col) => to_list_report(alloc, filename, context, &list, *row, *col),
EExpr::Str(string, row, col) => {
to_str_report(alloc, filename, context, &string, *row, *col)
}
_ => todo!("unhandled parse error: {:?}", parse_problem),
}
}
@ -410,6 +413,88 @@ fn to_unfinished_lambda_report<'a>(
}
}
fn to_str_report<'a>(
alloc: &'a RocDocAllocator<'a>,
filename: PathBuf,
_context: Context,
parse_problem: &roc_parse::parser::EString<'a>,
start_row: Row,
start_col: Col,
) -> Report<'a> {
use roc_parse::parser::EString;
match *parse_problem {
EString::Format(syntax, row, col) => to_syntax_report(alloc, filename, syntax, row, col),
EString::Space(error, row, col) => to_space_report(alloc, filename, &error, row, col),
EString::UnknownEscape(row, col) => {
let surroundings = Region::from_rows_cols(start_row, start_col, row, col);
let region = Region::from_rows_cols(row, col, row, col + 2);
let suggestion = |msg, sugg| {
alloc
.text("- ")
.append(alloc.reflow(msg))
.append(alloc.parser_suggestion(sugg))
};
let doc = alloc.stack(vec![
alloc.concat(vec![
alloc.reflow(r"I was partway through parsing a "),
alloc.reflow(r" string literal, but I got stuck here:"),
]),
alloc.region_with_subregion(surroundings, region),
alloc.concat(vec![
alloc.reflow(r"This is not an escape sequence I recognize."),
alloc.reflow(r" After a backslash, I am looking for one of these:"),
]),
alloc
.vcat(vec![
suggestion("A newline: ", "\\n"),
suggestion("A caret return: ", "\\r"),
suggestion("A tab: ", "\\t"),
suggestion("An escaped quote: ", "\\\""),
suggestion("An escaped backslash: ", "\\\\"),
suggestion("A unicode code point: ", "\\u(00FF)"),
suggestion("An interpolated string: ", "\\(myVariable)"),
])
.indent(4),
]);
Report {
filename,
doc,
title: "WEIRD ESCAPE".to_string(),
}
}
EString::CodePointOpen(row, col) | EString::CodePointEnd(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 am partway through parsing a unicode code point, but I got stuck here:",
),
alloc.region_with_subregion(surroundings, region),
alloc.concat(vec![
alloc.reflow(r"I was expecting a hexadecimal number, like "),
alloc.parser_suggestion("\\u(1100)"),
alloc.reflow(" or "),
alloc.parser_suggestion("\\u(00FF)"),
alloc.text("."),
]),
alloc.reflow(r"Learn more about working with unicode in roc at TODO"),
]);
Report {
filename,
doc,
title: "WEIRD CODE POINT".to_string(),
}
}
ref other => todo!("{:?}", other),
}
}
fn to_list_report<'a>(
alloc: &'a RocDocAllocator<'a>,
filename: PathBuf,