Parse has-derived clauses

This commit is contained in:
Ayaz Hafiz 2022-05-19 14:43:59 -04:00
parent a6ec43af5e
commit fcf464e9da
No known key found for this signature in database
GPG key ID: 0E2A37416A25EF58
9 changed files with 421 additions and 105 deletions

View file

@ -297,6 +297,7 @@ pub enum TypeDef<'a> {
Opaque {
header: TypeHeader<'a>,
typ: Loc<TypeAnnotation<'a>>,
derived: Option<Loc<Derived<'a>>>,
},
/// An ability definition. E.g.
@ -380,11 +381,23 @@ impl<'a> From<ValueDef<'a>> for Def<'a> {
}
}
/// Should always be a zero-argument `Apply`; we'll check this in canonicalization
pub type AbilityName<'a> = Loc<TypeAnnotation<'a>>;
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct HasClause<'a> {
pub var: Loc<Spaced<'a, &'a str>>,
// Should always be a zero-argument `Apply`; we'll check this in canonicalization
pub ability: Loc<TypeAnnotation<'a>>,
pub ability: AbilityName<'a>,
}
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum Derived<'a> {
/// `has [ Eq, Hash ]`
Has(Collection<'a, AbilityName<'a>>),
// We preserve this for the formatter; canonicalization ignores it.
SpaceBefore(&'a Derived<'a>, &'a [CommentOrNewline<'a>]),
SpaceAfter(&'a Derived<'a>, &'a [CommentOrNewline<'a>]),
}
#[derive(Debug, Copy, Clone, PartialEq)]
@ -901,6 +914,15 @@ impl<'a> Spaceable<'a> for Has<'a> {
}
}
impl<'a> Spaceable<'a> for Derived<'a> {
fn before(&'a self, spaces: &'a [CommentOrNewline<'a>]) -> Self {
Derived::SpaceBefore(self, spaces)
}
fn after(&'a self, spaces: &'a [CommentOrNewline<'a>]) -> Self {
Derived::SpaceAfter(self, spaces)
}
}
impl<'a> Expr<'a> {
pub fn loc_ref(&'a self, region: Region) -> Loc<&'a Self> {
Loc {