Merge pull request #712 from rtfeldman/fix-region-parsing

Fix Region Parsing
This commit is contained in:
Richard Feldman 2020-11-21 12:59:34 -05:00 committed by GitHub
commit 6f9097da56
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 29 deletions

View file

@ -614,7 +614,7 @@ fn annotated_body<'a>(min_indent: u16) -> impl Parser<'a, AnnotationOrAnnotatedB
fn annotation_or_alias<'a>( fn annotation_or_alias<'a>(
arena: &'a Bump, arena: &'a Bump,
pattern: &Pattern<'a>, pattern: &Pattern<'a>,
region: Region, pattern_region: Region,
loc_ann: Located<TypeAnnotation<'a>>, loc_ann: Located<TypeAnnotation<'a>>,
) -> Def<'a> { ) -> Def<'a> {
use crate::ast::Pattern::*; use crate::ast::Pattern::*;
@ -625,21 +625,21 @@ fn annotation_or_alias<'a>(
GlobalTag(name) => Def::Alias { GlobalTag(name) => Def::Alias {
name: Located { name: Located {
value: name, value: name,
region, region: pattern_region,
}, },
vars: &[], vars: &[],
ann: loc_ann, ann: loc_ann,
}, },
Apply( Apply(
Located { Located {
region, region: pattern_region,
value: Pattern::GlobalTag(name), value: Pattern::GlobalTag(name),
}, },
loc_vars, loc_vars,
) => Def::Alias { ) => Def::Alias {
name: Located { name: Located {
value: name, value: name,
region: *region, region: *pattern_region,
}, },
vars: loc_vars, vars: loc_vars,
ann: loc_ann, ann: loc_ann,
@ -648,14 +648,14 @@ fn annotation_or_alias<'a>(
Def::NotYetImplemented("TODO gracefully handle invalid Apply in type annotation") Def::NotYetImplemented("TODO gracefully handle invalid Apply in type annotation")
} }
SpaceAfter(value, spaces_before) => Def::SpaceAfter( SpaceAfter(value, spaces_before) => Def::SpaceAfter(
arena.alloc(annotation_or_alias(arena, value, region, loc_ann)), arena.alloc(annotation_or_alias(arena, value, pattern_region, loc_ann)),
spaces_before, spaces_before,
), ),
SpaceBefore(value, spaces_before) => Def::SpaceBefore( SpaceBefore(value, spaces_before) => Def::SpaceBefore(
arena.alloc(annotation_or_alias(arena, value, region, loc_ann)), arena.alloc(annotation_or_alias(arena, value, pattern_region, loc_ann)),
spaces_before, spaces_before,
), ),
Nested(value) => annotation_or_alias(arena, value, region, loc_ann), Nested(value) => annotation_or_alias(arena, value, pattern_region, loc_ann),
PrivateTag(_) => { PrivateTag(_) => {
Def::NotYetImplemented("TODO gracefully handle trying to use a private tag as an annotation.") Def::NotYetImplemented("TODO gracefully handle trying to use a private tag as an annotation.")
@ -676,7 +676,7 @@ fn annotation_or_alias<'a>(
// This is a regular Annotation // This is a regular Annotation
Def::Annotation( Def::Annotation(
Located { Located {
region, region: pattern_region,
value: Pattern::Identifier(ident), value: Pattern::Identifier(ident),
}, },
loc_ann, loc_ann,
@ -686,7 +686,7 @@ fn annotation_or_alias<'a>(
// This is a record destructure Annotation // This is a record destructure Annotation
Def::Annotation( Def::Annotation(
Located { Located {
region, region: pattern_region,
value: Pattern::RecordDestructure(loc_patterns), value: Pattern::RecordDestructure(loc_patterns),
}, },
loc_ann, loc_ann,
@ -768,6 +768,8 @@ fn parse_def_expr<'a>(
region: loc_first_body.region, region: loc_first_body.region,
} }
}; };
let def_region =
Region::span_across(&loc_first_pattern.region, &loc_first_body.region);
let first_def: Def<'a> = let first_def: Def<'a> =
// TODO is there some way to eliminate this .clone() here? // TODO is there some way to eliminate this .clone() here?
@ -775,7 +777,7 @@ fn parse_def_expr<'a>(
let loc_first_def = Located { let loc_first_def = Located {
value: first_def, value: first_def,
region: loc_first_pattern.region, region: def_region,
}; };
// for formatting reasons, we must insert the first def first! // for formatting reasons, we must insert the first def first!
@ -866,15 +868,21 @@ fn parse_def_signature<'a>(
.map( .map(
move |(((loc_first_annotation, opt_body), (mut defs, loc_ret)), state)| { move |(((loc_first_annotation, opt_body), (mut defs, loc_ret)), state)| {
let loc_first_def: Located<Def<'a>> = match opt_body { let loc_first_def: Located<Def<'a>> = match opt_body {
None => Located { None => {
value: annotation_or_alias( let region = Region::span_across(
arena, &loc_first_pattern.region,
&loc_first_pattern.value, &loc_first_annotation.region,
loc_first_pattern.region, );
loc_first_annotation, Located {
), value: annotation_or_alias(
region: loc_first_pattern.region, arena,
}, &loc_first_pattern.value,
loc_first_pattern.region,
loc_first_annotation,
),
region,
}
}
Some((opt_comment, (body_pattern, body_expr))) => { Some((opt_comment, (body_pattern, body_expr))) => {
let region = let region =
Region::span_across(&loc_first_pattern.region, &body_expr.region); Region::span_across(&loc_first_pattern.region, &body_expr.region);

View file

@ -1486,7 +1486,7 @@ mod test_parse {
arena.alloc(Located::new(1, 1, 0, 1, Identifier("x"))), arena.alloc(Located::new(1, 1, 0, 1, Identifier("x"))),
arena.alloc(Located::new(1, 1, 2, 3, Num("5"))), arena.alloc(Located::new(1, 1, 2, 3, Num("5"))),
); );
let loc_def = &*arena.alloc(Located::new(1, 1, 0, 1, def)); let loc_def = &*arena.alloc(Located::new(1, 1, 0, 3, def));
let defs = &[loc_def]; let defs = &[loc_def];
let ret = Expr::SpaceBefore(arena.alloc(Num("42")), newlines.into_bump_slice()); let ret = Expr::SpaceBefore(arena.alloc(Num("42")), newlines.into_bump_slice());
let loc_ret = Located::new(3, 3, 0, 2, ret); let loc_ret = Located::new(3, 3, 0, 2, ret);
@ -1516,7 +1516,7 @@ mod test_parse {
arena.alloc(Located::new(1, 1, 0, 1, Identifier("x"))), arena.alloc(Located::new(1, 1, 0, 1, Identifier("x"))),
arena.alloc(Located::new(1, 1, 4, 5, Num("5"))), arena.alloc(Located::new(1, 1, 4, 5, Num("5"))),
); );
let loc_def = &*arena.alloc(Located::new(1, 1, 0, 1, def)); let loc_def = &*arena.alloc(Located::new(1, 1, 0, 5, def));
let defs = &[loc_def]; let defs = &[loc_def];
let ret = Expr::SpaceBefore(arena.alloc(Num("42")), newlines.into_bump_slice()); let ret = Expr::SpaceBefore(arena.alloc(Num("42")), newlines.into_bump_slice());
let loc_ret = Located::new(3, 3, 0, 2, ret); let loc_ret = Located::new(3, 3, 0, 2, ret);
@ -1547,7 +1547,7 @@ mod test_parse {
arena.alloc(Located::new(1, 1, 0, 1, Identifier("x"))), arena.alloc(Located::new(1, 1, 0, 1, Identifier("x"))),
arena.alloc(Located::new(1, 1, 4, 5, Num("5"))), arena.alloc(Located::new(1, 1, 4, 5, Num("5"))),
); );
let loc_def1 = &*arena.alloc(Located::new(1, 1, 0, 1, def1)); let loc_def1 = &*arena.alloc(Located::new(1, 1, 0, 5, def1));
let def2 = Def::SpaceBefore( let def2 = Def::SpaceBefore(
&*arena.alloc(Def::Body( &*arena.alloc(Def::Body(
arena.alloc(Located::new(2, 2, 0, 1, Identifier("y"))), arena.alloc(Located::new(2, 2, 0, 1, Identifier("y"))),
@ -1591,7 +1591,7 @@ mod test_parse {
arena.alloc(Located::new(1, 1, 1, 8, RecordDestructure(&fields))), arena.alloc(Located::new(1, 1, 1, 8, RecordDestructure(&fields))),
arena.alloc(Located::new(1, 1, 11, 12, Num("5"))), arena.alloc(Located::new(1, 1, 11, 12, Num("5"))),
); );
let loc_def1 = &*arena.alloc(Located::new(1, 1, 1, 8, def1)); let loc_def1 = &*arena.alloc(Located::new(1, 1, 1, 12, def1));
let def2 = Def::SpaceBefore( let def2 = Def::SpaceBefore(
&*arena.alloc(Def::Body( &*arena.alloc(Def::Body(
arena.alloc(Located::new(2, 2, 0, 1, Identifier("y"))), arena.alloc(Located::new(2, 2, 0, 1, Identifier("y"))),
@ -1679,7 +1679,7 @@ mod test_parse {
Located::new(0, 0, 6, 33, as_ann), Located::new(0, 0, 6, 33, as_ann),
); );
let loc_ann = &*arena.alloc(Located::new(0, 0, 0, 3, signature)); let loc_ann = &*arena.alloc(Located::new(0, 0, 0, 33, signature));
let defs = &[loc_ann]; let defs = &[loc_ann];
let ret = Expr::SpaceBefore(arena.alloc(Num("42")), newlines.into_bump_slice()); let ret = Expr::SpaceBefore(arena.alloc(Num("42")), newlines.into_bump_slice());
let loc_ret = Located::new(2, 2, 0, 2, ret); let loc_ret = Located::new(2, 2, 0, 2, ret);
@ -1715,7 +1715,7 @@ mod test_parse {
ann: Located::new(0, 0, 11, 26, applied_alias), ann: Located::new(0, 0, 11, 26, applied_alias),
}; };
let loc_ann = &*arena.alloc(Located::new(0, 0, 0, 4, signature)); let loc_ann = &*arena.alloc(Located::new(0, 0, 0, 26, signature));
let defs = &[loc_ann]; let defs = &[loc_ann];
let ret = Expr::SpaceBefore(arena.alloc(Num("42")), newlines.into_bump_slice()); let ret = Expr::SpaceBefore(arena.alloc(Num("42")), newlines.into_bump_slice());
let loc_ret = Located::new(2, 2, 0, 2, ret); let loc_ret = Located::new(2, 2, 0, 2, ret);
@ -2320,7 +2320,7 @@ mod test_parse {
arena.alloc(Located::new(0, 0, 0, 1, Identifier("x"))), arena.alloc(Located::new(0, 0, 0, 1, Identifier("x"))),
arena.alloc(Located::new(1, 1, 4, 5, Expr::SpaceBefore(num, &[Newline]))), arena.alloc(Located::new(1, 1, 4, 5, Expr::SpaceBefore(num, &[Newline]))),
); );
let loc_def = &*arena.alloc(Located::new(0, 0, 0, 1, def)); let loc_def = &*arena.alloc(Located::new(0, 1, 0, 5, def));
let defs = &[loc_def]; let defs = &[loc_def];
let ret = Expr::SpaceBefore(arena.alloc(Num("42")), newlines); let ret = Expr::SpaceBefore(arena.alloc(Num("42")), newlines);
let loc_ret = Located::new(3, 3, 0, 2, ret); let loc_ret = Located::new(3, 3, 0, 2, ret);
@ -2349,7 +2349,7 @@ mod test_parse {
arena.alloc(Located::new(6, 6, 0, 1, Identifier("x"))), arena.alloc(Located::new(6, 6, 0, 1, Identifier("x"))),
arena.alloc(Located::new(6, 6, 4, 5, Num("5"))), arena.alloc(Located::new(6, 6, 4, 5, Num("5"))),
); );
let loc_def = &*arena.alloc(Located::new(6, 6, 0, 1, def)); let loc_def = &*arena.alloc(Located::new(6, 6, 0, 5, def));
let defs = &[loc_def]; let defs = &[loc_def];
let ret = Expr::SpaceBefore(arena.alloc(Num("42")), newlines); let ret = Expr::SpaceBefore(arena.alloc(Num("42")), newlines);
let loc_ret = Located::new(8, 8, 0, 2, ret); let loc_ret = Located::new(8, 8, 0, 2, ret);
@ -2392,7 +2392,7 @@ mod test_parse {
arena.alloc(Located::new(4, 4, 0, 1, Identifier("x"))), arena.alloc(Located::new(4, 4, 0, 1, Identifier("x"))),
arena.alloc(Located::new(4, 4, 4, 5, Num("5"))), arena.alloc(Located::new(4, 4, 4, 5, Num("5"))),
); );
let loc_def = &*arena.alloc(Located::new(4, 4, 0, 1, def)); let loc_def = &*arena.alloc(Located::new(4, 4, 0, 5, def));
let defs = &[loc_def]; let defs = &[loc_def];
let ret = Expr::SpaceBefore(arena.alloc(Num("42")), newlines); let ret = Expr::SpaceBefore(arena.alloc(Num("42")), newlines);
let loc_ret = Located::new(6, 6, 0, 2, ret); let loc_ret = Located::new(6, 6, 0, 2, ret);
@ -2431,7 +2431,7 @@ mod test_parse {
arena.alloc(Located::new(4, 4, 0, 1, Identifier("x"))), arena.alloc(Located::new(4, 4, 0, 1, Identifier("x"))),
arena.alloc(Located::new(4, 4, 4, 5, Num("5"))), arena.alloc(Located::new(4, 4, 4, 5, Num("5"))),
); );
let loc_def = &*arena.alloc(Located::new(4, 4, 0, 1, def)); let loc_def = &*arena.alloc(Located::new(4, 4, 0, 5, def));
let defs = &[loc_def]; let defs = &[loc_def];
let ret = Expr::SpaceBefore(arena.alloc(Num("42")), newlines); let ret = Expr::SpaceBefore(arena.alloc(Num("42")), newlines);
let loc_ret = Located::new(6, 6, 0, 2, ret); let loc_ret = Located::new(6, 6, 0, 2, ret);