mirror of
https://github.com/roc-lang/roc.git
synced 2025-08-02 19:32:17 +00:00
Rename Derived to HasAbility
This commit is contained in:
parent
0946a7816a
commit
d4d073d8c6
7 changed files with 202 additions and 81 deletions
|
@ -298,7 +298,7 @@ pub enum TypeDef<'a> {
|
|||
Opaque {
|
||||
header: TypeHeader<'a>,
|
||||
typ: Loc<TypeAnnotation<'a>>,
|
||||
derived: Option<Loc<Derived<'a>>>,
|
||||
derived: Option<Loc<HasAbilities<'a>>>,
|
||||
},
|
||||
|
||||
/// An ability definition. E.g.
|
||||
|
@ -434,18 +434,32 @@ pub struct HasClause<'a> {
|
|||
pub ability: AbilityName<'a>,
|
||||
}
|
||||
|
||||
/// `Eq` or `Eq { eq: myEq }`
|
||||
#[derive(Debug, Copy, Clone, PartialEq)]
|
||||
pub enum Derived<'a> {
|
||||
/// `has [Eq, Hash]`
|
||||
Has(Collection<'a, AbilityName<'a>>),
|
||||
pub enum HasAbility<'a> {
|
||||
HasAbility {
|
||||
/// Should be a zero-argument `Apply` or an error; we'll check this in canonicalization
|
||||
ability: Loc<TypeAnnotation<'a>>,
|
||||
impls: Collection<'a, Loc<AssignedField<'a, TypeAnnotation<'a>>>>,
|
||||
},
|
||||
|
||||
// We preserve this for the formatter; canonicalization ignores it.
|
||||
SpaceBefore(&'a Derived<'a>, &'a [CommentOrNewline<'a>]),
|
||||
SpaceAfter(&'a Derived<'a>, &'a [CommentOrNewline<'a>]),
|
||||
SpaceBefore(&'a HasAbility<'a>, &'a [CommentOrNewline<'a>]),
|
||||
SpaceAfter(&'a HasAbility<'a>, &'a [CommentOrNewline<'a>]),
|
||||
}
|
||||
|
||||
impl Derived<'_> {
|
||||
pub fn collection(&self) -> &Collection<AbilityName> {
|
||||
#[derive(Debug, Copy, Clone, PartialEq)]
|
||||
pub enum HasAbilities<'a> {
|
||||
/// `has [Eq { eq: myEq }, Hash]`
|
||||
Has(Collection<'a, Loc<HasAbility<'a>>>),
|
||||
|
||||
// We preserve this for the formatter; canonicalization ignores it.
|
||||
SpaceBefore(&'a HasAbilities<'a>, &'a [CommentOrNewline<'a>]),
|
||||
SpaceAfter(&'a HasAbilities<'a>, &'a [CommentOrNewline<'a>]),
|
||||
}
|
||||
|
||||
impl HasAbilities<'_> {
|
||||
pub fn collection(&self) -> &Collection<Loc<HasAbility>> {
|
||||
let mut it = self;
|
||||
loop {
|
||||
match it {
|
||||
|
@ -879,6 +893,12 @@ impl<'a, T: Debug> Debug for Collection<'a, T> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a, T> Default for Collection<'a, T> {
|
||||
fn default() -> Self {
|
||||
Self::empty()
|
||||
}
|
||||
}
|
||||
|
||||
pub trait Spaceable<'a> {
|
||||
fn before(&'a self, _: &'a [CommentOrNewline<'a>]) -> Self;
|
||||
fn after(&'a self, _: &'a [CommentOrNewline<'a>]) -> Self;
|
||||
|
@ -967,12 +987,21 @@ impl<'a> Spaceable<'a> for Has<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a> Spaceable<'a> for Derived<'a> {
|
||||
impl<'a> Spaceable<'a> for HasAbility<'a> {
|
||||
fn before(&'a self, spaces: &'a [CommentOrNewline<'a>]) -> Self {
|
||||
Derived::SpaceBefore(self, spaces)
|
||||
HasAbility::SpaceBefore(self, spaces)
|
||||
}
|
||||
fn after(&'a self, spaces: &'a [CommentOrNewline<'a>]) -> Self {
|
||||
Derived::SpaceAfter(self, spaces)
|
||||
HasAbility::SpaceAfter(self, spaces)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Spaceable<'a> for HasAbilities<'a> {
|
||||
fn before(&'a self, spaces: &'a [CommentOrNewline<'a>]) -> Self {
|
||||
HasAbilities::SpaceBefore(self, spaces)
|
||||
}
|
||||
fn after(&'a self, spaces: &'a [CommentOrNewline<'a>]) -> Self {
|
||||
HasAbilities::SpaceAfter(self, spaces)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1059,6 +1088,7 @@ impl_extract_spaces!(Pattern);
|
|||
impl_extract_spaces!(Tag);
|
||||
impl_extract_spaces!(AssignedField<T>);
|
||||
impl_extract_spaces!(TypeAnnotation);
|
||||
impl_extract_spaces!(HasAbility);
|
||||
|
||||
impl<'a, T: Copy> ExtractSpaces<'a> for Spaced<'a, T> {
|
||||
type Item = T;
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
use crate::ast::{
|
||||
AssignedField, Collection, CommentOrNewline, Defs, Derived, Expr, ExtractSpaces, Has, Pattern,
|
||||
Spaceable, TypeAnnotation, TypeDef, TypeHeader, ValueDef,
|
||||
AssignedField, Collection, CommentOrNewline, Defs, Expr, ExtractSpaces, Has, HasAbilities,
|
||||
Pattern, Spaceable, TypeAnnotation, TypeDef, TypeHeader, ValueDef,
|
||||
};
|
||||
use crate::blankspace::{
|
||||
space0_after_e, space0_around_ee, space0_before_e, space0_before_optional_after, space0_e,
|
||||
|
@ -953,7 +953,7 @@ fn alias_signature_with_space_before<'a>(
|
|||
|
||||
fn opaque_signature_with_space_before<'a>(
|
||||
min_indent: u32,
|
||||
) -> impl Parser<'a, (Loc<TypeAnnotation<'a>>, Option<Loc<Derived<'a>>>), EExpr<'a>> {
|
||||
) -> impl Parser<'a, (Loc<TypeAnnotation<'a>>, Option<Loc<HasAbilities<'a>>>), EExpr<'a>> {
|
||||
and!(
|
||||
specialize(
|
||||
EExpr::Type,
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
use crate::ast::{
|
||||
AssignedField, CommentOrNewline, Derived, HasClause, Pattern, Spaced, Tag, TypeAnnotation,
|
||||
TypeHeader,
|
||||
AssignedField, CommentOrNewline, HasAbilities, HasAbility, HasClause, Pattern, Spaced, Tag,
|
||||
TypeAnnotation, TypeHeader,
|
||||
};
|
||||
use crate::blankspace::{space0_around_ee, space0_before_e, space0_e};
|
||||
use crate::ident::lowercase_ident;
|
||||
|
@ -479,7 +479,7 @@ fn has_clause_chain<'a>(
|
|||
}
|
||||
|
||||
/// Parse a has-abilities clause, e.g. `has [Eq, Hash]`.
|
||||
pub fn has_abilities<'a>(min_indent: u32) -> impl Parser<'a, Loc<Derived<'a>>, EType<'a>> {
|
||||
pub fn has_abilities<'a>(min_indent: u32) -> impl Parser<'a, Loc<HasAbilities<'a>>, EType<'a>> {
|
||||
skip_first!(
|
||||
// Parse "has"; we don't care about this keyword
|
||||
word3(b'h', b'a', b's', EType::THasClause),
|
||||
|
@ -488,22 +488,34 @@ pub fn has_abilities<'a>(min_indent: u32) -> impl Parser<'a, Loc<Derived<'a>>, E
|
|||
loc!(map!(
|
||||
collection_trailing_sep_e!(
|
||||
word1(b'[', EType::TStart),
|
||||
specialize(EType::TApply, loc!(parse_concrete_type)),
|
||||
loc!(parse_has_ability(min_indent)),
|
||||
word1(b',', EType::TEnd),
|
||||
word1(b']', EType::TEnd),
|
||||
min_indent + 1,
|
||||
EType::TStart,
|
||||
EType::TIndentEnd,
|
||||
TypeAnnotation::SpaceBefore
|
||||
HasAbility::SpaceBefore
|
||||
),
|
||||
Derived::Has
|
||||
HasAbilities::Has
|
||||
)),
|
||||
min_indent + 1,
|
||||
EType::TIndentEnd
|
||||
EType::TIndentEnd,
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
fn parse_has_ability<'a>(_min_indent: u32) -> impl Parser<'a, HasAbility<'a>, EType<'a>> {
|
||||
map!(
|
||||
loc!(specialize(EType::TApply, parse_concrete_type)),
|
||||
|ability| {
|
||||
HasAbility::HasAbility {
|
||||
ability,
|
||||
impls: Default::default(),
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
fn expression<'a>(
|
||||
min_indent: u32,
|
||||
is_trailing_comma_valid: bool,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue