Parse destructured tag annotations as annotations rather than aliases

Closes #178
This commit is contained in:
ayazhafiz 2021-12-26 15:39:36 -06:00
parent fe62e59e31
commit b3ddfa7515
8 changed files with 192 additions and 34 deletions

View file

@ -265,6 +265,17 @@ pub enum Def<'a> {
NotYetImplemented(&'static str),
}
impl<'a> Def<'a> {
pub fn unroll_spaces_before(&self) -> (&'a [CommentOrNewline<'a>], &Def) {
let (spaces, def): (&'a [_], &Def) = match self {
Def::SpaceBefore(def, spaces) => (spaces, def),
def => (&[], def),
};
debug_assert!(!matches!(def, Def::SpaceBefore(_, _)));
(spaces, def)
}
}
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum TypeAnnotation<'a> {
/// A function. The types of its arguments, then the type of its return value.

View file

@ -571,27 +571,8 @@ fn append_body_definition<'a>(
if spaces.len() <= 1 {
let last = defs.pop();
match last {
Some(Loc {
value: Def::Annotation(ann_pattern, ann_type),
..
}) => {
return append_body_definition_help(
arena,
defs,
region,
&[],
spaces,
loc_pattern,
loc_def_body,
ann_pattern,
ann_type,
);
}
Some(Loc {
value: Def::SpaceBefore(Def::Annotation(ann_pattern, ann_type), before_ann_spaces),
..
}) => {
match last.map(|d| d.value.unroll_spaces_before()) {
Some((before_ann_spaces, Def::Annotation(ann_pattern, ann_type))) => {
return append_body_definition_help(
arena,
defs,
@ -604,6 +585,37 @@ fn append_body_definition<'a>(
ann_type,
);
}
Some((
before_ann_spaces,
Def::Alias {
name,
vars,
ann: ann_type,
},
)) => {
// This is a case like
// UserId x : [ UserId Int ]
// UserId x = UserId 42
// We optimistically parsed the first line as an alias; we now turn it
// into an annotation.
let loc_name = arena.alloc(name.map(|x| Pattern::GlobalTag(x)));
let ann_pattern = Pattern::Apply(loc_name, vars);
let vars_region = Region::across_all(vars.iter().map(|v| &v.region));
let region_ann_pattern = Region::span_across(&loc_name.region, &vars_region);
let loc_ann_pattern = Loc::at(region_ann_pattern, ann_pattern);
return append_body_definition_help(
arena,
defs,
region,
before_ann_spaces,
spaces,
loc_pattern,
loc_def_body,
arena.alloc(loc_ann_pattern),
ann_type,
);
}
_ => {
defs.extend(last);
}