Change syntax for requires in platform modules

This commit is contained in:
Richard Feldman 2020-11-07 12:20:09 -05:00
parent 61edcc7d03
commit ea150b0101
3 changed files with 28 additions and 33 deletions

View File

@ -19,7 +19,9 @@ use roc_mono::ir::{
CapturedSymbols, ExternalSpecializations, PartialProc, PendingSpecialization, Proc, Procs,
};
use roc_mono::layout::{Layout, LayoutCache};
use roc_parse::ast::{self, Attempting, ExposesEntry, ImportsEntry, PlatformHeader};
use roc_parse::ast::{
self, Attempting, ExposesEntry, ImportsEntry, PlatformHeader, TypeAnnotation, TypedIdent,
};
use roc_parse::module::module_defs;
use roc_parse::parser::{self, Fail, Parser};
use roc_region::all::{Located, Region};
@ -2434,18 +2436,11 @@ fn build_effect_actual(effect_tag_name: TagName, a_type: Type, var_store: &mut V
fn unpack_exposes_entries<'a>(
arena: &'a Bump,
entries: &'a [Located<roc_parse::ast::EffectsEntry<'a>>],
) -> bumpalo::collections::Vec<
'a,
(
&'a Located<&'a str>,
&'a Located<roc_parse::ast::TypeAnnotation<'a>>,
),
> {
entries: &'a [Located<TypedIdent<'a>>],
) -> bumpalo::collections::Vec<'a, (&'a Located<&'a str>, &'a Located<TypeAnnotation<'a>>)> {
use bumpalo::collections::Vec;
use roc_parse::ast::EffectsEntry;
let mut stack: Vec<&EffectsEntry> = Vec::with_capacity_in(entries.len(), arena);
let mut stack: Vec<&TypedIdent> = Vec::with_capacity_in(entries.len(), arena);
let mut output = Vec::with_capacity_in(entries.len(), arena);
for entry in entries.iter() {
@ -2454,14 +2449,14 @@ fn unpack_exposes_entries<'a>(
while let Some(effects_entry) = stack.pop() {
match effects_entry {
EffectsEntry::Effect {
TypedIdent::Entry {
ident,
spaces_before_colon: _,
ann,
} => {
output.push((ident, ann));
}
EffectsEntry::SpaceAfter(nested, _) | EffectsEntry::SpaceBefore(nested, _) => {
TypedIdent::SpaceAfter(nested, _) | TypedIdent::SpaceBefore(nested, _) => {
stack.push(nested);
}
}

View File

@ -52,7 +52,7 @@ pub struct AppHeader<'a> {
pub struct PlatformHeader<'a> {
pub name: Loc<PackageName<'a>>,
pub provides: Vec<'a, Loc<ExposesEntry<'a>>>,
pub requires: Vec<'a, Loc<ExposesEntry<'a>>>,
pub requires: Vec<'a, Loc<TypedIdent<'a>>>,
pub imports: Vec<'a, Loc<ImportsEntry<'a>>>,
pub effects: Effects<'a>,
@ -72,23 +72,23 @@ pub struct Effects<'a> {
pub spaces_after_effects_keyword: &'a [CommentOrNewline<'a>],
pub spaces_after_type_name: &'a [CommentOrNewline<'a>],
pub type_name: &'a str,
pub entries: Vec<'a, Loc<EffectsEntry<'a>>>,
pub entries: Vec<'a, Loc<TypedIdent<'a>>>,
}
#[derive(Clone, Debug, PartialEq)]
pub enum EffectsEntry<'a> {
pub enum TypedIdent<'a> {
/// e.g.
///
/// printLine : Str -> Effect {}
Effect {
Entry {
ident: Loc<&'a str>,
spaces_before_colon: &'a [CommentOrNewline<'a>],
ann: Loc<TypeAnnotation<'a>>,
},
// Spaces
SpaceBefore(&'a EffectsEntry<'a>, &'a [CommentOrNewline<'a>]),
SpaceAfter(&'a EffectsEntry<'a>, &'a [CommentOrNewline<'a>]),
SpaceBefore(&'a TypedIdent<'a>, &'a [CommentOrNewline<'a>]),
SpaceAfter(&'a TypedIdent<'a>, &'a [CommentOrNewline<'a>]),
}
#[derive(Clone, Debug, PartialEq)]
@ -628,12 +628,12 @@ impl<'a> Spaceable<'a> for ImportsEntry<'a> {
}
}
impl<'a> Spaceable<'a> for EffectsEntry<'a> {
impl<'a> Spaceable<'a> for TypedIdent<'a> {
fn before(&'a self, spaces: &'a [CommentOrNewline<'a>]) -> Self {
EffectsEntry::SpaceBefore(self, spaces)
TypedIdent::SpaceBefore(self, spaces)
}
fn after(&'a self, spaces: &'a [CommentOrNewline<'a>]) -> Self {
EffectsEntry::SpaceAfter(self, spaces)
TypedIdent::SpaceAfter(self, spaces)
}
}

View File

@ -1,6 +1,6 @@
use crate::ast::{
AppHeader, Attempting, CommentOrNewline, Def, Effects, EffectsEntry, ExposesEntry,
ImportsEntry, InterfaceHeader, Module, PlatformHeader,
AppHeader, Attempting, CommentOrNewline, Def, Effects, ExposesEntry, ImportsEntry,
InterfaceHeader, Module, PlatformHeader, TypedIdent,
};
use crate::blankspace::{space0, space0_around, space0_before, space1};
use crate::expr::def;
@ -268,16 +268,16 @@ fn requires<'a>() -> impl Parser<
'a,
(
(&'a [CommentOrNewline<'a>], &'a [CommentOrNewline<'a>]),
Vec<'a, Located<ExposesEntry<'a>>>,
Vec<'a, Located<TypedIdent<'a>>>,
),
> {
and!(
and!(skip_second!(space1(1), ascii_string("requires")), space1(1)),
collection!(
ascii_char(b'['),
loc!(exposes_entry()),
ascii_char(b'{'),
loc!(typed_ident()),
ascii_char(b','),
ascii_char(b']'),
ascii_char(b'}'),
1
)
)
@ -333,7 +333,7 @@ fn effects<'a>() -> impl Parser<'a, Effects<'a>> {
and!(uppercase_ident(), space1(0)).parse(arena, state)?;
let (entries, state) = collection!(
ascii_char(b'{'),
loc!(effects_entry()),
loc!(typed_ident()),
ascii_char(b','),
ascii_char(b'}'),
1
@ -354,7 +354,7 @@ fn effects<'a>() -> impl Parser<'a, Effects<'a>> {
}
#[inline(always)]
fn effects_entry<'a>() -> impl Parser<'a, EffectsEntry<'a>> {
fn typed_ident<'a>() -> impl Parser<'a, TypedIdent<'a>> {
move |arena, state| {
// You must have a field name, e.g. "email"
let (ident, state) = loc!(lowercase_ident()).parse(arena, state)?;
@ -372,7 +372,7 @@ fn effects_entry<'a>() -> impl Parser<'a, EffectsEntry<'a>> {
// printLine : Str -> Effect {}
Ok((
EffectsEntry::Effect {
TypedIdent::Entry {
ident,
spaces_before_colon,
ann,
@ -397,10 +397,10 @@ fn imports_entry<'a>() -> impl Parser<'a, ImportsEntry<'a>> {
optional(skip_first!(
ascii_char(b'.'),
collection!(
ascii_char(b'{'),
ascii_char(b'['),
loc!(exposes_entry()),
ascii_char(b','),
ascii_char(b'}'),
ascii_char(b']'),
1
)
))