Do not drop import parse errors

This commit is contained in:
Agus Zubiaga 2024-05-07 22:39:43 -03:00
parent 9a66abf95f
commit 2a8ecbba28
No known key found for this signature in database
10 changed files with 108 additions and 86 deletions

View file

@ -655,8 +655,9 @@ pub fn parse_single_def<'a>(
min_indent, min_indent,
) { ) {
Err((NoProgress, _)) => { Err((NoProgress, _)) => {
match loc!(import()).parse(arena, state.clone(), min_indent) { let pos_before_import = state.pos();
Err((_, _)) => { match import().parse(arena, state.clone(), min_indent) {
Err((NoProgress, _)) => {
match parse_expect.parse(arena, state.clone(), min_indent) { match parse_expect.parse(arena, state.clone(), min_indent) {
Err((_, _)) => { Err((_, _)) => {
// a hacky way to get expression-based error messages. TODO fix this // a hacky way to get expression-based error messages. TODO fix this
@ -683,12 +684,16 @@ pub fn parse_single_def<'a>(
), ),
} }
} }
Ok((_, loc_import, state)) => Ok(( Err((MadeProgress, err)) => {
Err((MadeProgress, EExpr::Import(err, pos_before_import)))
}
Ok((_, (loc_import, spaces_after), state)) => Ok((
MadeProgress, MadeProgress,
Some(SingleDef { Some(SingleDef {
type_or_value: Either::Second(loc_import.value), type_or_value: Either::Second(loc_import.value),
region: loc_import.region, region: loc_import.region,
spaces_before: spaces_before_current, spaces_before: spaces_before_current,
spaces_after,
}), }),
state, state,
)), )),
@ -752,6 +757,7 @@ pub fn parse_single_def<'a>(
type_or_value: Either::First(type_def), type_or_value: Either::First(type_def),
region: def_region, region: def_region,
spaces_before: spaces_before_current, spaces_before: spaces_before_current,
spaces_after: &[],
}), }),
state, state,
)); ));
@ -812,6 +818,7 @@ pub fn parse_single_def<'a>(
type_or_value: Either::First(type_def), type_or_value: Either::First(type_def),
region, region,
spaces_before: spaces_before_current, spaces_before: spaces_before_current,
spaces_after: &[],
}), }),
state, state,
)); ));
@ -835,6 +842,7 @@ pub fn parse_single_def<'a>(
type_or_value: Either::First(type_def), type_or_value: Either::First(type_def),
region, region,
spaces_before: spaces_before_current, spaces_before: spaces_before_current,
spaces_after: &[],
}), }),
state, state,
)); ));
@ -848,6 +856,7 @@ pub fn parse_single_def<'a>(
type_or_value: Either::Second(value_def), type_or_value: Either::Second(value_def),
region, region,
spaces_before: spaces_before_current, spaces_before: spaces_before_current,
spaces_after: &[],
}), }),
state, state,
)); ));
@ -886,6 +895,7 @@ pub fn parse_single_def<'a>(
type_or_value: Either::First(type_def), type_or_value: Either::First(type_def),
region, region,
spaces_before: spaces_before_current, spaces_before: spaces_before_current,
spaces_after: &[],
}), }),
state, state,
)); ));
@ -910,6 +920,7 @@ pub fn parse_single_def<'a>(
type_or_value: Either::First(type_def), type_or_value: Either::First(type_def),
region, region,
spaces_before: spaces_before_current, spaces_before: spaces_before_current,
spaces_after: &[],
}), }),
state, state,
)); ));
@ -923,6 +934,7 @@ pub fn parse_single_def<'a>(
type_or_value: Either::Second(value_def), type_or_value: Either::Second(value_def),
region, region,
spaces_before: spaces_before_current, spaces_before: spaces_before_current,
spaces_after: &[],
}), }),
state, state,
)); ));
@ -956,10 +968,23 @@ pub fn parse_single_def<'a>(
} }
} }
fn import<'a>() -> impl Parser<'a, ValueDef<'a>, EImport<'a>> { fn import<'a>() -> impl Parser<'a, (Loc<ValueDef<'a>>, &'a [CommentOrNewline<'a>]), EImport<'a>> {
skip_first!( then(
and!(
loc!(skip_first!(
parser::keyword(keyword::IMPORT, EImport::Import), parser::keyword(keyword::IMPORT, EImport::Import),
increment_min_indent(one_of!(import_body(), import_ingested_file_body())) increment_min_indent(one_of!(import_body(), import_ingested_file_body()))
)),
space0_e(EImport::EndNewline)
),
|_arena, state, progress, (import, spaces_after)| {
if !spaces_after.is_empty() || state.has_reached_end() {
Ok((progress, (import, spaces_after), state))
} else {
// We require EOF, comment, or newline after import
Err((progress, EImport::EndNewline(state.pos())))
}
},
) )
} }
@ -1142,6 +1167,7 @@ pub fn parse_single_def_assignment<'a>(
type_or_value: Either::Second(value_def), type_or_value: Either::Second(value_def),
region, region,
spaces_before: spaces_before_current, spaces_before: spaces_before_current,
spaces_after: &[],
}), }),
state_after_rest_of_def, state_after_rest_of_def,
)); ));
@ -1163,6 +1189,7 @@ pub fn parse_single_def_assignment<'a>(
type_or_value: Either::Second(value_def), type_or_value: Either::Second(value_def),
region, region,
spaces_before: spaces_before_current, spaces_before: spaces_before_current,
spaces_after: &[],
}), }),
state_after_first_expression, state_after_first_expression,
)); ));
@ -1178,6 +1205,7 @@ pub fn parse_single_def_assignment<'a>(
type_or_value: Either::Second(value_def), type_or_value: Either::Second(value_def),
region, region,
spaces_before: spaces_before_current, spaces_before: spaces_before_current,
spaces_after: &[],
}), }),
state_after_first_expression, state_after_first_expression,
)) ))
@ -1225,6 +1253,7 @@ fn parse_statement_inside_def<'a>(
type_or_value: Either::Second(value_def), type_or_value: Either::Second(value_def),
region, region,
spaces_before: spaces_before_current, spaces_before: spaces_before_current,
spaces_after: &[],
}), }),
state, state,
)) ))
@ -1308,10 +1337,16 @@ fn parse_defs_end<'a>(
Ok((_, Some(single_def), next_state)) => { Ok((_, Some(single_def), next_state)) => {
let region = single_def.region; let region = single_def.region;
let spaces_before_current = single_def.spaces_before; let spaces_before_current = single_def.spaces_before;
let spaces_after_current = single_def.spaces_after;
match single_def.type_or_value { match single_def.type_or_value {
Either::First(type_def) => { Either::First(type_def) => {
defs.push_type_def(type_def, region, spaces_before_current, &[]); defs.push_type_def(
type_def,
region,
spaces_before_current,
spaces_after_current,
);
} }
Either::Second(value_def) => { Either::Second(value_def) => {
// If we got a ValueDef::Body, check if a type annotation preceded it. // If we got a ValueDef::Body, check if a type annotation preceded it.
@ -1373,7 +1408,12 @@ fn parse_defs_end<'a>(
if !joined { if !joined {
// the previous and current def can't be joined up // the previous and current def can't be joined up
defs.push_value_def(value_def, region, spaces_before_current, &[]); defs.push_value_def(
value_def,
region,
spaces_before_current,
spaces_after_current,
);
} }
} }
} }
@ -1395,6 +1435,7 @@ pub struct SingleDef<'a> {
pub type_or_value: Either<TypeDef<'a>, ValueDef<'a>>, pub type_or_value: Either<TypeDef<'a>, ValueDef<'a>>,
pub region: Region, pub region: Region,
pub spaces_before: &'a [CommentOrNewline<'a>], pub spaces_before: &'a [CommentOrNewline<'a>],
pub spaces_after: &'a [CommentOrNewline<'a>],
} }
fn parse_defs_expr<'a>( fn parse_defs_expr<'a>(
@ -2882,28 +2923,11 @@ fn dbg_help<'a>(options: ExprParseOptions) -> impl Parser<'a, Expr<'a>, EExpect<
fn import_help<'a>(options: ExprParseOptions) -> impl Parser<'a, Expr<'a>, EExpr<'a>> { fn import_help<'a>(options: ExprParseOptions) -> impl Parser<'a, Expr<'a>, EExpr<'a>> {
move |arena: &'a Bump, state: State<'a>, min_indent: u32| { move |arena: &'a Bump, state: State<'a>, min_indent: u32| {
let original_pos = state.pos(); let (_, (import_def, spaces_after), state) =
specialize_err(EExpr::Import, import()).parse(arena, state, min_indent)?;
let (_, import_def, state) =
loc!(specialize_err(EExpr::Import, import())).parse(arena, state, min_indent)?;
let (_, spaces_after_import, state) =
space0_e(EExpr::IndentEnd).parse(arena, state, min_indent)?;
if !spaces_after_import.iter().any(|s| s.is_newline()) {
return Err((
MadeProgress,
EExpr::Import(EImport::EndNewline(state.pos()), original_pos),
));
}
let mut defs = Defs::default(); let mut defs = Defs::default();
defs.push_value_def( defs.push_value_def(import_def.value, import_def.region, &[], spaces_after);
import_def.value,
import_def.region,
&[],
spaces_after_import,
);
parse_defs_expr(options, min_indent, defs, arena, state) parse_defs_expr(options, min_indent, defs, arena, state)
} }

View file

@ -1 +1 @@
NotEndOfFile(@12) Expr(Import(LowercaseAlias(@15-19), @0), @0)

View file

@ -13,14 +13,14 @@ Defs {
], ],
space_before: [ space_before: [
Slice(start = 0, length = 0), Slice(start = 0, length = 0),
Slice(start = 1, length = 0),
Slice(start = 2, length = 0),
Slice(start = 3, length = 0),
],
space_after: [
Slice(start = 0, length = 1), Slice(start = 0, length = 1),
Slice(start = 1, length = 1), Slice(start = 1, length = 1),
Slice(start = 2, length = 1), Slice(start = 2, length = 1),
],
space_after: [
Slice(start = 0, length = 0),
Slice(start = 1, length = 0),
Slice(start = 2, length = 0),
Slice(start = 3, length = 0), Slice(start = 3, length = 0),
], ],
spaces: [ spaces: [

View file

@ -9,10 +9,10 @@ Defs {
], ],
space_before: [ space_before: [
Slice(start = 0, length = 0), Slice(start = 0, length = 0),
Slice(start = 0, length = 1), Slice(start = 1, length = 0),
], ],
space_after: [ space_after: [
Slice(start = 0, length = 0), Slice(start = 0, length = 1),
Slice(start = 1, length = 0), Slice(start = 1, length = 0),
], ],
spaces: [ spaces: [

View file

@ -31,6 +31,20 @@ Defs {
], ],
space_before: [ space_before: [
Slice(start = 0, length = 0), Slice(start = 0, length = 0),
Slice(start = 2, length = 0),
Slice(start = 4, length = 0),
Slice(start = 6, length = 0),
Slice(start = 8, length = 0),
Slice(start = 10, length = 0),
Slice(start = 12, length = 0),
Slice(start = 14, length = 0),
Slice(start = 16, length = 0),
Slice(start = 18, length = 0),
Slice(start = 20, length = 0),
Slice(start = 23, length = 0),
Slice(start = 25, length = 0),
],
space_after: [
Slice(start = 0, length = 2), Slice(start = 0, length = 2),
Slice(start = 2, length = 2), Slice(start = 2, length = 2),
Slice(start = 4, length = 2), Slice(start = 4, length = 2),
@ -43,20 +57,6 @@ Defs {
Slice(start = 18, length = 2), Slice(start = 18, length = 2),
Slice(start = 20, length = 3), Slice(start = 20, length = 3),
Slice(start = 23, length = 2), Slice(start = 23, length = 2),
],
space_after: [
Slice(start = 0, length = 0),
Slice(start = 2, length = 0),
Slice(start = 4, length = 0),
Slice(start = 6, length = 0),
Slice(start = 8, length = 0),
Slice(start = 10, length = 0),
Slice(start = 12, length = 0),
Slice(start = 14, length = 0),
Slice(start = 16, length = 0),
Slice(start = 18, length = 0),
Slice(start = 20, length = 0),
Slice(start = 23, length = 0),
Slice(start = 25, length = 0), Slice(start = 25, length = 0),
], ],
spaces: [ spaces: [

View file

@ -11,12 +11,12 @@ Defs {
], ],
space_before: [ space_before: [
Slice(start = 0, length = 0), Slice(start = 0, length = 0),
Slice(start = 0, length = 1), Slice(start = 1, length = 0),
Slice(start = 1, length = 1), Slice(start = 2, length = 0),
], ],
space_after: [ space_after: [
Slice(start = 0, length = 0), Slice(start = 0, length = 1),
Slice(start = 1, length = 0), Slice(start = 1, length = 1),
Slice(start = 2, length = 0), Slice(start = 2, length = 0),
], ],
spaces: [ spaces: [

View file

@ -9,10 +9,10 @@ Defs {
], ],
space_before: [ space_before: [
Slice(start = 0, length = 0), Slice(start = 0, length = 0),
Slice(start = 0, length = 1), Slice(start = 1, length = 0),
], ],
space_after: [ space_after: [
Slice(start = 0, length = 0), Slice(start = 0, length = 1),
Slice(start = 1, length = 0), Slice(start = 1, length = 0),
], ],
spaces: [ spaces: [

View file

@ -14,10 +14,12 @@ Defs(
], ],
space_after: [ space_after: [
Slice(start = 0, length = 1), Slice(start = 0, length = 1),
Slice(start = 1, length = 0), Slice(start = 1, length = 2),
], ],
spaces: [ spaces: [
Newline, Newline,
Newline,
Newline,
], ],
type_defs: [], type_defs: [],
value_defs: [ value_defs: [
@ -73,8 +75,7 @@ Defs(
), ),
], ],
}, },
@53-71 SpaceBefore( @53-71 Apply(
Apply(
@53-62 Var { @53-62 Var {
module_name: "JE", module_name: "JE",
ident: "encode", ident: "encode",
@ -97,9 +98,4 @@ Defs(
], ],
Space, Space,
), ),
[
Newline,
Newline,
],
),
) )

View file

@ -46,10 +46,10 @@ Full {
], ],
space_before: [ space_before: [
Slice(start = 0, length = 2), Slice(start = 0, length = 2),
Slice(start = 2, length = 2), Slice(start = 4, length = 0),
], ],
space_after: [ space_after: [
Slice(start = 2, length = 0), Slice(start = 2, length = 2),
Slice(start = 4, length = 0), Slice(start = 4, length = 0),
], ],
spaces: [ spaces: [

View file

@ -324,6 +324,7 @@ pub fn parse_src<'a>(arena: &'a Bump, line: &'a str) -> ParseOutcome<'a> {
Either::Second(ValueDef::Body(loc_pattern, loc_def_expr)), Either::Second(ValueDef::Body(loc_pattern, loc_def_expr)),
region, region,
spaces_before, spaces_before,
spaces_after: _,
}), }),
_, _,
)) if spaces_before.len() <= 1 => { )) if spaces_before.len() <= 1 => {
@ -373,6 +374,7 @@ pub fn parse_src<'a>(arena: &'a Bump, line: &'a str) -> ParseOutcome<'a> {
Either::Second(ValueDef::Body(loc_pattern, loc_def_expr)), Either::Second(ValueDef::Body(loc_pattern, loc_def_expr)),
region, region,
spaces_before, spaces_before,
spaces_after: _,
}), }),
_, _,
)) if spaces_before.len() <= 1 => { )) if spaces_before.len() <= 1 => {