implement error message for HasMisplacedCarriageReturn

Also had to filter out the escape characters
so the erroe message does not get messed up
This commit is contained in:
Fabian Schmalzried 2023-09-10 17:17:26 +02:00
parent 49a1a3b46d
commit 3e41f6eb5a
No known key found for this signature in database
GPG Key ID: D691D5DA4CEF42E7
3 changed files with 47 additions and 13 deletions

View File

@ -3888,6 +3888,23 @@ fn to_space_report<'a>(
}
}
BadInputError::HasMisplacedCarriageReturn => {
let region = LineColumnRegion::from_pos(lines.convert_pos(pos));
let doc = alloc.stack([
alloc.reflow(r"I encountered a carriage return (\r)"),
alloc.region(region),
alloc.reflow(r"A carriage return (\r) has to be followed by a newline (\n)."),
]);
Report {
filename,
doc,
title: "MISPLACED CARRIAGE RETURN".to_string(),
severity: Severity::RuntimeError,
}
}
_ => todo!("unhandled type parse error: {:?}", &parse_problem),
}
}

View File

@ -674,7 +674,15 @@ impl<'a> RocDocAllocator<'a> {
let line_number = line_number_string;
let this_line_number_length = line_number.len();
let line: &str = self.src_lines.get(i as usize).unwrap_or(&"");
// filter out any escape characters for the current line that could mess up the output.
let line: String = self
.src_lines
.get(i as usize)
.unwrap_or(&"")
.chars()
.filter(|&c| !c.is_ascii_control() || c == '\t')
.collect::<String>();
let is_line_empty = line.trim().is_empty();
let rest_of_line = if !is_line_empty {
self.text(line)

View File

@ -4559,26 +4559,35 @@ mod test_reporting {
"###
);
test_report!(
comment_with_control_character,
"# comment with a \x07\n",
|golden| pretty_assertions::assert_eq!(
golden,
&format!(
r###"── ASII CONTROL CHARACTER ──────── tmp/comment_with_control_character/Test.roc ─
@r###"
ASII CONTROL CHARACTER tmp/comment_with_control_character/Test.roc
I encountered an ASCII control character
I encountered an ASCII control character
4 # comment with a {}
^
4 # comment with a
^
ASCII control characters are not allowed."###,
"\x07"
)
)
ASCII control characters are not allowed.
"###
);
test_report!(
record_type_carriage_return,
"f : { \r foo }",
@r###"
MISPLACED CARRIAGE RETURN tmp/record_type_carriage_return/Test.roc
I encountered a carriage return (\r)
4 f : { foo }
^
A carriage return (\r) has to be followed by a newline (\n).
"###
);
// TODO bad error message
test_report!(