Skip to content

Commit

Permalink
Make ListStart branch less brittle; Add ListEnd branch
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredramirez committed Dec 29, 2024
1 parent 69c36af commit 5bbc26a
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 41 deletions.
65 changes: 57 additions & 8 deletions crates/compiler/load/tests/test_reporting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6459,7 +6459,7 @@ All branches in an `if` must have the same type!
}

#[test]
fn unneeded_module_name() {
fn invalid_exposes_start() {
report_header_problem_as(
indoc!(
r"
Expand All @@ -6468,7 +6468,7 @@ All branches in an `if` must have the same type!
),
indoc!(
r#"
── WEIRD MODULE NAME in /code/proj/Main.roc ────────────────────────────────────
── WEIRD EXPOSES in /code/proj/Main.roc ────────────────────────────────────────
I am partway through parsing a header, but I got stuck here:
Expand All @@ -6477,15 +6477,64 @@ All branches in an `if` must have the same type!
I am expecting a list of exports like
module [func, value]
[func, value]
"#
),
)
}

#[test]
fn invalid_exposes_missing_comma() {
report_header_problem_as(
indoc!(
r"
module [value func]
"
),
indoc!(
r#"
── WEIRD EXPOSES in /code/proj/Main.roc ────────────────────────────────────────
I am partway through parsing a header, but I got stuck here:
1│ module [value func]
^
I am expecting a list of exports like
[func, value]
or module params like
Did you forget a comma or a `]`?
"#
),
)
}

#[test]
fn invalid_exposes_end() {
report_header_problem_as(
indoc!(
r"
module [value
main = 1
"
),
indoc!(
r#"
── WEIRD EXPOSES in /code/proj/Main.roc ────────────────────────────────────────
I am partway through parsing a header, but I got stuck here:
1│ module [value
2│
^
I am expecting a list of exports like
module { echo } -> [func, value]
[func, value]
If you're trying to specify a module name, recall that unlike
application names, module names are not specified in the header.
Instead, they are derived from the name of the module's filename.
Did you forget a comma or a `]`?
"#
),
)
Expand Down
72 changes: 39 additions & 33 deletions crates/reporting/src/error/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4049,20 +4049,19 @@ fn to_exposes_report<'a>(
let severity = Severity::RuntimeError;

match *parse_problem {
EExposes::ListEnd(pos) | // TODO: give this its own error message
EExposes::Identifier(pos) => {
let surroundings = Region::new(start, pos);
let region = LineColumnRegion::from_pos(lines.convert_pos(pos));

let doc = alloc.stack([
alloc.reflow(r"I am partway through parsing an `exposes` list, but I got stuck here:"),
alloc.reflow(
r"I am partway through parsing an `exposes` list, but I got stuck here:",
),
alloc.region_with_subregion(lines.convert_region(surroundings), region, severity),
alloc.concat([alloc.reflow(
"I was expecting a type name, value name or function name next, like",
)]),
alloc
.parser_suggestion("[Animal, default, tame]")
.indent(4),
alloc.parser_suggestion("[Animal, default, tame]").indent(4),
]);

Report {
Expand All @@ -4085,9 +4084,7 @@ fn to_exposes_report<'a>(
alloc.keyword("exposes"),
alloc.reflow(" keyword next, like"),
]),
alloc
.parser_suggestion("[Animal, default, tame]")
.indent(4),
alloc.parser_suggestion("[Animal, default, tame]").indent(4),
]);

Report {
Expand All @@ -4100,48 +4097,57 @@ fn to_exposes_report<'a>(

EExposes::Space(error, pos) => to_space_report(alloc, lines, filename, &error, pos),

EExposes::ListStart(pos@Position { offset: 7 }) => {
EExposes::ListStart(pos) => {
let surroundings = Region::new(start, pos);
let region = LineColumnRegion::from_pos(lines.convert_pos(pos));

let doc = alloc.stack([
alloc.reflow(r"I am partway through parsing a header, but I got stuck here:"),
alloc.region_with_subregion(lines.convert_region(surroundings), region, severity),
alloc.concat([
alloc.reflow("I am expecting a list of exports like"),
]),
alloc
.parser_suggestion("module [func, value]")
.indent(4),
alloc.concat([
alloc.reflow("or module params like"),
]),
alloc
.parser_suggestion("module { echo } -> [func, value]")
.indent(4),
alloc.concat([
alloc.reflow("If you're trying to specify a module name, recall that unlike "),
alloc.reflow("application names, module names are not specified in the header. "),
alloc.reflow("Instead, they are derived from the name of the module's filename."),
]),
alloc.concat([alloc.reflow("I am expecting a list of exports like")]),
alloc.parser_suggestion("[func, value]").indent(4),
]);

Report {
filename,
doc,
title: "WEIRD EXPOSES".to_string(),
severity,
}
}

EExposes::ListEnd(pos) => {
let surroundings = Region::new(start, pos);
let region = LineColumnRegion::from_pos(lines.convert_pos(pos));

let doc = alloc.stack([
alloc.reflow(r"I am partway through parsing a header, but I got stuck here:"),
alloc.region_with_subregion(lines.convert_region(surroundings), region, severity),
alloc.concat([alloc.reflow("I am expecting a list of exports like")]),
alloc.parser_suggestion("[func, value]").indent(4),
alloc.concat([alloc.reflow("Did you forget a comma or a `]`?")]),
]);

Report {
filename,
doc,
title: "WEIRD MODULE NAME".to_string(),
title: "WEIRD EXPOSES".to_string(),
severity,
}
},
}

// If you're adding or changing syntax, please handle the case with a
// good error message above instead of adding more unhandled cases below.
EExposes::Open(pos) |
EExposes::IndentExposes(pos) |
EExposes::IndentListStart(pos) |
EExposes::ListStart(pos) =>
to_unhandled_parse_error_report(alloc, lines, filename, format!("{:?}", parse_problem), pos, start)
EExposes::Open(pos) | EExposes::IndentExposes(pos) | EExposes::IndentListStart(pos) => {
to_unhandled_parse_error_report(
alloc,
lines,
filename,
format!("{:?}", parse_problem),
pos,
start,
)
}
}
}

Expand Down

0 comments on commit 5bbc26a

Please sign in to comment.