ast::Has -> ast::Implements

This commit is contained in:
Bryce Miller 2023-05-20 08:12:20 -04:00
parent d2fed03cb1
commit 64c34a5c6d
No known key found for this signature in database
GPG key ID: F1E97BF8DF152350
4 changed files with 31 additions and 26 deletions

View file

@ -3,9 +3,9 @@ use bumpalo::Bump;
use roc_module::called_via::{BinOp, UnaryOp}; use roc_module::called_via::{BinOp, UnaryOp};
use roc_parse::{ use roc_parse::{
ast::{ ast::{
AbilityMember, AssignedField, Collection, CommentOrNewline, Defs, Expr, Has, HasAbilities, AbilityMember, AssignedField, Collection, CommentOrNewline, Defs, Expr, HasAbilities,
HasAbility, HasClause, HasImpls, Header, Module, Pattern, RecordBuilderField, Spaced, HasAbility, HasClause, HasImpls, Header, Implements, Module, Pattern, RecordBuilderField,
Spaces, StrLiteral, StrSegment, Tag, TypeAnnotation, TypeDef, TypeHeader, ValueDef, Spaced, Spaces, StrLiteral, StrSegment, Tag, TypeAnnotation, TypeDef, TypeHeader, ValueDef,
WhenBranch, WhenBranch,
}, },
header::{ header::{
@ -569,9 +569,9 @@ impl<'a> RemoveSpaces<'a> for ValueDef<'a> {
} }
} }
impl<'a> RemoveSpaces<'a> for Has<'a> { impl<'a> RemoveSpaces<'a> for Implements<'a> {
fn remove_spaces(&self, _arena: &'a Bump) -> Self { fn remove_spaces(&self, _arena: &'a Bump) -> Self {
Has::Has Implements::Implements
} }
} }

View file

@ -357,12 +357,12 @@ impl<'a> TypeHeader<'a> {
} }
} }
/// The `has` keyword associated with ability definitions. /// The `implements` keyword associated with ability definitions.
#[derive(Debug, Clone, Copy, PartialEq)] #[derive(Debug, Clone, Copy, PartialEq)]
pub enum Has<'a> { pub enum Implements<'a> {
Has, Implements,
SpaceBefore(&'a Has<'a>, &'a [CommentOrNewline<'a>]), SpaceBefore(&'a Implements<'a>, &'a [CommentOrNewline<'a>]),
SpaceAfter(&'a Has<'a>, &'a [CommentOrNewline<'a>]), SpaceAfter(&'a Implements<'a>, &'a [CommentOrNewline<'a>]),
} }
/// An ability demand is a value defining the ability; for example `hash : a -> U64 | a has Hash` /// An ability demand is a value defining the ability; for example `hash : a -> U64 | a has Hash`
@ -402,7 +402,7 @@ pub enum TypeDef<'a> {
/// hash : a -> U64 | a has Hash /// hash : a -> U64 | a has Hash
Ability { Ability {
header: TypeHeader<'a>, header: TypeHeader<'a>,
loc_has: Loc<Has<'a>>, loc_has: Loc<Implements<'a>>,
members: &'a [AbilityMember<'a>], members: &'a [AbilityMember<'a>],
}, },
} }
@ -1245,12 +1245,12 @@ impl<'a> Spaceable<'a> for Tag<'a> {
} }
} }
impl<'a> Spaceable<'a> for Has<'a> { impl<'a> Spaceable<'a> for Implements<'a> {
fn before(&'a self, spaces: &'a [CommentOrNewline<'a>]) -> Self { fn before(&'a self, spaces: &'a [CommentOrNewline<'a>]) -> Self {
Has::SpaceBefore(self, spaces) Implements::SpaceBefore(self, spaces)
} }
fn after(&'a self, spaces: &'a [CommentOrNewline<'a>]) -> Self { fn after(&'a self, spaces: &'a [CommentOrNewline<'a>]) -> Self {
Has::SpaceAfter(self, spaces) Implements::SpaceAfter(self, spaces)
} }
} }
@ -1698,11 +1698,11 @@ impl<'a> Malformed for AbilityMember<'a> {
} }
} }
impl<'a> Malformed for Has<'a> { impl<'a> Malformed for Implements<'a> {
fn is_malformed(&self) -> bool { fn is_malformed(&self) -> bool {
match self { match self {
Has::Has => false, Implements::Implements => false,
Has::SpaceBefore(has, _) | Has::SpaceAfter(has, _) => has.is_malformed(), Implements::SpaceBefore(has, _) | Implements::SpaceAfter(has, _) => has.is_malformed(),
} }
} }
} }

View file

@ -1,6 +1,7 @@
use crate::ast::{ use crate::ast::{
AssignedField, Collection, CommentOrNewline, Defs, Expr, ExtractSpaces, Has, HasAbilities, AssignedField, Collection, CommentOrNewline, Defs, Expr, ExtractSpaces, HasAbilities,
Pattern, RecordBuilderField, Spaceable, Spaces, TypeAnnotation, TypeDef, TypeHeader, ValueDef, Implements, Pattern, RecordBuilderField, Spaceable, Spaces, TypeAnnotation, TypeDef,
TypeHeader, ValueDef,
}; };
use crate::blankspace::{ use crate::blankspace::{
space0_after_e, space0_around_e_no_after_indent_check, space0_around_ee, space0_before_e, space0_after_e, space0_around_e_no_after_indent_check, space0_around_ee, space0_before_e,
@ -1075,7 +1076,7 @@ fn opaque_signature_with_space_before<'a>(
), ),
optional(backtrackable(specialize( optional(backtrackable(specialize(
EExpr::Type, EExpr::Type,
space0_before_e(type_annotation::has_abilities(), EType::TIndentStart,), space0_before_e(type_annotation::implements_abilities(), EType::TIndentStart,),
))) )))
) )
} }
@ -1363,7 +1364,7 @@ fn finish_parsing_ability_def_help<'a>(
start_column: u32, start_column: u32,
name: Loc<&'a str>, name: Loc<&'a str>,
args: &'a [Loc<Pattern<'a>>], args: &'a [Loc<Pattern<'a>>],
loc_has: Loc<Has<'a>>, loc_has: Loc<Implements<'a>>,
arena: &'a Bump, arena: &'a Bump,
state: State<'a>, state: State<'a>,
) -> ParseResult<'a, (TypeDef<'a>, Region), EExpr<'a>> { ) -> ParseResult<'a, (TypeDef<'a>, Region), EExpr<'a>> {
@ -1664,10 +1665,10 @@ fn parse_expr_end<'a>(
// Attach any spaces to the `has` keyword // Attach any spaces to the `has` keyword
let has = if !expr_state.spaces_after.is_empty() { let has = if !expr_state.spaces_after.is_empty() {
arena arena
.alloc(Has::Has) .alloc(Implements::Implements)
.with_spaces_before(expr_state.spaces_after, has.region) .with_spaces_before(expr_state.spaces_after, has.region)
} else { } else {
Loc::at(has.region, Has::Has) Loc::at(has.region, Implements::Implements)
}; };
let args = arguments.into_bump_slice(); let args = arguments.into_bump_slice();

View file

@ -1,4 +1,4 @@
use crate::ast::{Has, Pattern, PatternAs, Spaceable}; use crate::ast::{Implements, Pattern, PatternAs, Spaceable};
use crate::blankspace::{space0_e, spaces, spaces_before}; use crate::blankspace::{space0_e, spaces, spaces_before};
use crate::ident::{lowercase_ident, parse_ident, Accessor, Ident}; use crate::ident::{lowercase_ident, parse_ident, Accessor, Ident};
use crate::keyword; use crate::keyword;
@ -154,12 +154,16 @@ fn loc_tag_pattern_arg<'a>(
} }
} }
pub fn loc_has_parser<'a>() -> impl Parser<'a, Loc<Has<'a>>, EPattern<'a>> { pub fn loc_has_parser<'a>() -> impl Parser<'a, Loc<Implements<'a>>, EPattern<'a>> {
then( then(
loc_tag_pattern_arg(false), loc_tag_pattern_arg(false),
|_arena, state, progress, pattern| { |_arena, state, progress, pattern| {
if matches!(pattern.value, Pattern::Identifier("has")) { if matches!(pattern.value, Pattern::Identifier("has")) {
Ok((progress, Loc::at(pattern.region, Has::Has), state)) Ok((
progress,
Loc::at(pattern.region, Implements::Implements),
state,
))
} else { } else {
Err((progress, EPattern::End(state.pos()))) Err((progress, EPattern::End(state.pos())))
} }