Support parsing multiple abilities in a clause

This commit is contained in:
Ayaz Hafiz 2022-10-11 17:08:38 -05:00
parent bdc565762b
commit 548a235c25
No known key found for this signature in database
GPG Key ID: 0E2A37416A25EF58
14 changed files with 165 additions and 95 deletions

View File

@ -450,7 +450,8 @@ pub fn find_type_def_symbols(
stack.push(&annotation.value); stack.push(&annotation.value);
for has_clause in clauses.iter() { for has_clause in clauses.iter() {
stack.push(&has_clause.value.ability.value); // TODO(abilities)
stack.push(&has_clause.value.abilities[0].value);
} }
} }
Inferred | Wildcard | Malformed(_) => {} Inferred | Wildcard | Malformed(_) => {}
@ -920,7 +921,7 @@ fn canonicalize_has_clause(
) -> Result<(), Type> { ) -> Result<(), Type> {
let Loc { let Loc {
region, region,
value: roc_parse::ast::HasClause { var, ability }, value: roc_parse::ast::HasClause { var, abilities },
} = clause; } = clause;
let region = *region; let region = *region;
@ -931,6 +932,8 @@ fn canonicalize_has_clause(
); );
let var_name = Lowercase::from(var_name); let var_name = Lowercase::from(var_name);
// TODO(abilities)
let ability = abilities[0];
let ability = match ability.value { let ability = match ability.value {
TypeAnnotation::Apply(module_name, ident, _type_arguments) => { TypeAnnotation::Apply(module_name, ident, _type_arguments) => {
let symbol = make_apply_symbol(env, ability.region, scope, module_name, ident)?; let symbol = make_apply_symbol(env, ability.region, scope, module_name, ident)?;

View File

@ -546,7 +546,8 @@ impl<'a> Formattable for Tag<'a> {
impl<'a> Formattable for HasClause<'a> { impl<'a> Formattable for HasClause<'a> {
fn is_multiline(&self) -> bool { fn is_multiline(&self) -> bool {
self.ability.is_multiline() // TODO(abilities)
self.abilities[0].is_multiline()
} }
fn format_with_options<'buf>( fn format_with_options<'buf>(
@ -560,8 +561,8 @@ impl<'a> Formattable for HasClause<'a> {
buf.spaces(1); buf.spaces(1);
buf.push_str("has"); buf.push_str("has");
buf.spaces(1); buf.spaces(1);
self.ability // TODO(abilities)
.format_with_options(buf, parens, newlines, indent); self.abilities[0].format_with_options(buf, parens, newlines, indent);
} }
} }

View File

@ -795,7 +795,7 @@ impl<'a> RemoveSpaces<'a> for HasClause<'a> {
fn remove_spaces(&self, arena: &'a Bump) -> Self { fn remove_spaces(&self, arena: &'a Bump) -> Self {
HasClause { HasClause {
var: self.var.remove_spaces(arena), var: self.var.remove_spaces(arena),
ability: self.ability.remove_spaces(arena), abilities: self.abilities.remove_spaces(arena),
} }
} }
} }

View File

@ -394,10 +394,11 @@ fn ability_member_type_to_docs(
let has_clauses = has_clauses let has_clauses = has_clauses
.iter() .iter()
.map(|hc| { .map(|hc| {
let ast::HasClause { var, ability } = hc.value; let ast::HasClause { var, abilities } = hc.value;
( (
var.value.extract_spaces().item.to_string(), var.value.extract_spaces().item.to_string(),
type_to_docs(false, ability.value), // TODO(abilities)
type_to_docs(false, abilities[0].value),
) )
}) })
.collect(); .collect();

View File

@ -439,7 +439,7 @@ pub type AbilityName<'a> = Loc<TypeAnnotation<'a>>;
#[derive(Debug, Copy, Clone, PartialEq)] #[derive(Debug, Copy, Clone, PartialEq)]
pub struct HasClause<'a> { pub struct HasClause<'a> {
pub var: Loc<Spaced<'a, &'a str>>, pub var: Loc<Spaced<'a, &'a str>>,
pub ability: AbilityName<'a>, pub abilities: &'a [AbilityName<'a>],
} }
#[derive(Debug, Copy, Clone, PartialEq)] #[derive(Debug, Copy, Clone, PartialEq)]

View File

@ -2,7 +2,9 @@ use crate::ast::{
AssignedField, CommentOrNewline, HasAbilities, HasAbility, HasClause, HasImpls, Pattern, AssignedField, CommentOrNewline, HasAbilities, HasAbility, HasClause, HasImpls, Pattern,
Spaced, Tag, TypeAnnotation, TypeHeader, Spaced, Tag, TypeAnnotation, TypeHeader,
}; };
use crate::blankspace::{space0_around_ee, space0_before_e, space0_e}; use crate::blankspace::{
space0_around_ee, space0_before_e, space0_before_optional_after, space0_e,
};
use crate::expr::record_value_field; use crate::expr::record_value_field;
use crate::ident::lowercase_ident; use crate::ident::lowercase_ident;
use crate::keyword; use crate::keyword;
@ -410,6 +412,37 @@ fn loc_applied_args_e<'a>(
zero_or_more!(loc_applied_arg(min_indent, stop_at_surface_has)) zero_or_more!(loc_applied_arg(min_indent, stop_at_surface_has))
} }
// Hash & Eq & ...
fn ability_chain<'a>(
min_indent: u32,
) -> impl Parser<'a, Vec<'a, Loc<TypeAnnotation<'a>>>, EType<'a>> {
map!(
and!(
space0_before_optional_after(
specialize(EType::TApply, loc!(parse_concrete_type)),
min_indent,
EType::TIndentStart,
EType::TIndentEnd,
),
zero_or_more!(skip_first!(
word1(b'&', EType::THasClause),
space0_before_e(
specialize(EType::TApply, loc!(parse_concrete_type)),
min_indent,
EType::TIndentStart,
)
))
),
|(first_ability, mut other_abilities): (
Loc<TypeAnnotation<'a>>,
Vec<'a, Loc<TypeAnnotation<'a>>>
)| {
other_abilities.push(first_ability);
other_abilities
}
)
}
fn has_clause<'a>(min_indent: u32) -> impl Parser<'a, Loc<HasClause<'a>>, EType<'a>> { fn has_clause<'a>(min_indent: u32) -> impl Parser<'a, Loc<HasClause<'a>>, EType<'a>> {
map!( map!(
// Suppose we are trying to parse "a has Hash" // Suppose we are trying to parse "a has Hash"
@ -427,20 +460,22 @@ fn has_clause<'a>(min_indent: u32) -> impl Parser<'a, Loc<HasClause<'a>>, EType<
then( then(
// Parse "has"; we don't care about this keyword // Parse "has"; we don't care about this keyword
word3(b'h', b'a', b's', EType::THasClause), word3(b'h', b'a', b's', EType::THasClause),
// Parse "Hash"; this may be qualified from another module like "Hash.Hash" // Parse "Hash & ..."; this may be qualified from another module like "Hash.Hash"
|arena, state, _progress, _output| { |arena, state, _progress, _output| {
space0_before_e( ability_chain(state.column() + 1).parse(arena, state)
specialize(EType::TApply, loc!(parse_concrete_type)),
state.column() + 1,
EType::TIndentStart,
)
.parse(arena, state)
} }
) )
), ),
|(var, ability): (Loc<Spaced<'a, &'a str>>, Loc<TypeAnnotation<'a>>)| { |(var, abilities): (Loc<Spaced<'a, &'a str>>, Vec<'a, Loc<TypeAnnotation<'a>>>)| {
let region = Region::span_across(&var.region, &ability.region); let abilities_region = Region::span_across(
let has_clause = HasClause { var, ability }; &abilities.first().unwrap().region,
&abilities.last().unwrap().region,
);
let region = Region::span_across(&var.region, &abilities_region);
let has_clause = HasClause {
var,
abilities: abilities.into_bump_slice(),
};
Loc::at(region, has_clause) Loc::at(region, has_clause)
} }
) )

View File

@ -39,11 +39,13 @@ Defs(
[ [
@27-37 HasClause { @27-37 HasClause {
var: @27-28 "a", var: @27-28 "a",
ability: @33-37 Apply( abilities: [
@33-37 Apply(
"", "",
"Hash", "Hash",
[], [],
), ),
],
}, },
], ],
), ),

View File

@ -45,11 +45,13 @@ Defs(
[ [
@24-33 HasClause { @24-33 HasClause {
var: @24-25 "a", var: @24-25 "a",
ability: @30-33 Apply( abilities: [
@30-33 Apply(
"", "",
"Ab1", "Ab1",
[], [],
), ),
],
}, },
], ],
), ),
@ -80,11 +82,13 @@ Defs(
[ [
@59-68 HasClause { @59-68 HasClause {
var: @59-60 "a", var: @59-60 "a",
ability: @65-68 Apply( abilities: [
@65-68 Apply(
"", "",
"Ab2", "Ab2",
[], [],
), ),
],
}, },
], ],
), ),

View File

@ -114,11 +114,13 @@ Defs(
[ [
@33-44 HasClause { @33-44 HasClause {
var: @33-34 "a", var: @33-34 "a",
ability: @39-44 Apply( abilities: [
@39-44 Apply(
"", "",
"Other", "Other",
[], [],
), ),
],
}, },
], ],
), ),
@ -157,11 +159,13 @@ Defs(
[ [
@70-81 HasClause { @70-81 HasClause {
var: @70-71 "a", var: @70-71 "a",
ability: @76-81 Apply( abilities: [
@76-81 Apply(
"", "",
"Other", "Other",
[], [],
), ),
],
}, },
], ],
), ),
@ -394,11 +398,13 @@ Defs(
[ [
@260-271 HasClause { @260-271 HasClause {
var: @260-261 "a", var: @260-261 "a",
ability: @266-271 Apply( abilities: [
@266-271 Apply(
"", "",
"Other", "Other",
[], [],
), ),
],
}, },
], ],
), ),

View File

@ -40,11 +40,13 @@ Defs(
[ [
@20-27 HasClause { @20-27 HasClause {
var: @20-21 "a", var: @20-21 "a",
ability: @26-27 Apply( abilities: [
@26-27 Apply(
"", "",
"A", "A",
[], [],
), ),
],
}, },
], ],
), ),

View File

@ -40,27 +40,33 @@ Defs(
[ [
@20-27 HasClause { @20-27 HasClause {
var: @20-21 "a", var: @20-21 "a",
ability: @26-27 Apply( abilities: [
@26-27 Apply(
"", "",
"A", "A",
[], [],
), ),
],
}, },
@29-37 HasClause { @29-37 HasClause {
var: @29-30 "b", var: @29-30 "b",
ability: @35-37 Apply( abilities: [
@35-37 Apply(
"", "",
"Eq", "Eq",
[], [],
), ),
],
}, },
@39-48 HasClause { @39-48 HasClause {
var: @39-40 "c", var: @39-40 "c",
ability: @45-48 Apply( abilities: [
@45-48 Apply(
"", "",
"Ord", "Ord",
[], [],
), ),
],
}, },
], ],
), ),

View File

@ -45,11 +45,13 @@ Defs(
[ [
@24-34 HasClause { @24-34 HasClause {
var: @24-25 "a", var: @24-25 "a",
ability: @30-34 Apply( abilities: [
@30-34 Apply(
"", "",
"Hash", "Hash",
[], [],
), ),
],
}, },
@42-50 HasClause { @42-50 HasClause {
var: @42-43 SpaceBefore( var: @42-43 SpaceBefore(
@ -58,11 +60,13 @@ Defs(
Newline, Newline,
], ],
), ),
ability: @48-50 Apply( abilities: [
@48-50 Apply(
"", "",
"Eq", "Eq",
[], [],
), ),
],
}, },
@58-67 HasClause { @58-67 HasClause {
var: @58-59 SpaceBefore( var: @58-59 SpaceBefore(
@ -71,11 +75,13 @@ Defs(
Newline, Newline,
], ],
), ),
ability: @64-67 Apply( abilities: [
@64-67 Apply(
"", "",
"Ord", "Ord",
[], [],
), ),
],
}, },
], ],
), ),

View File

@ -26,11 +26,13 @@ Defs(
[ [
@8-15 HasClause { @8-15 HasClause {
var: @8-9 "a", var: @8-9 "a",
ability: @14-15 Apply( abilities: [
@14-15 Apply(
"", "",
"A", "A",
[], [],
), ),
],
}, },
], ],
), ),

View File

@ -40,11 +40,13 @@ Defs(
[ [
@19-29 HasClause { @19-29 HasClause {
var: @19-20 "a", var: @19-20 "a",
ability: @25-29 Apply( abilities: [
@25-29 Apply(
"", "",
"Hash", "Hash",
[], [],
), ),
],
}, },
], ],
), ),