mirror of
https://github.com/roc-lang/roc.git
synced 2025-11-02 05:48:17 +00:00
Be more lenient with required indentation in collections
... and also remove a bunch of now-dead errors that can't be triggered.
This commit is contained in:
parent
9171799c67
commit
0da50a612d
22 changed files with 333 additions and 516 deletions
|
|
@ -67,6 +67,24 @@ where
|
|||
)
|
||||
}
|
||||
|
||||
pub fn spaces_before_optional_after<'a, P, S, E>(parser: P) -> impl Parser<'a, Loc<S>, E>
|
||||
where
|
||||
S: 'a + Spaceable<'a>,
|
||||
P: 'a + Parser<'a, Loc<S>, E>,
|
||||
E: 'a + SpaceProblem,
|
||||
{
|
||||
parser::map_with_arena(
|
||||
and(
|
||||
spaces(),
|
||||
and(
|
||||
parser,
|
||||
one_of![backtrackable(spaces()), succeed!(&[] as &[_]),],
|
||||
),
|
||||
),
|
||||
spaces_around_help,
|
||||
)
|
||||
}
|
||||
|
||||
fn spaces_around_help<'a, S>(
|
||||
arena: &'a Bump,
|
||||
tuples: (
|
||||
|
|
@ -102,6 +120,26 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
pub fn spaces_before<'a, P, S, E>(parser: P) -> impl Parser<'a, Loc<S>, E>
|
||||
where
|
||||
S: 'a + Spaceable<'a>,
|
||||
P: 'a + Parser<'a, Loc<S>, E>,
|
||||
E: 'a + SpaceProblem,
|
||||
{
|
||||
parser::map_with_arena(
|
||||
and!(spaces(), parser),
|
||||
|arena: &'a Bump, (space_list, loc_expr): (&'a [CommentOrNewline<'a>], Loc<S>)| {
|
||||
if space_list.is_empty() {
|
||||
loc_expr
|
||||
} else {
|
||||
arena
|
||||
.alloc(loc_expr.value)
|
||||
.with_spaces_before(space_list, loc_expr.region)
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
pub fn space0_before_e<'a, P, S, E>(
|
||||
parser: P,
|
||||
indent_problem: fn(Position) -> E,
|
||||
|
|
@ -331,7 +369,7 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
fn spaces<'a, E>() -> impl Parser<'a, &'a [CommentOrNewline<'a>], E>
|
||||
pub fn spaces<'a, E>() -> impl Parser<'a, &'a [CommentOrNewline<'a>], E>
|
||||
where
|
||||
E: 'a + SpaceProblem,
|
||||
{
|
||||
|
|
|
|||
|
|
@ -81,7 +81,6 @@ fn loc_expr_in_parens_help<'a>() -> impl Parser<'a, Loc<Expr<'a>>, EInParens<'a>
|
|||
specialize_ref(EInParens::Expr, loc_expr(false)),
|
||||
word1(b',', EInParens::End),
|
||||
word1(b')', EInParens::End),
|
||||
EInParens::IndentEnd,
|
||||
Expr::SpaceBefore
|
||||
)),
|
||||
move |arena, state, _, loc_elements| {
|
||||
|
|
@ -2459,7 +2458,6 @@ fn list_literal_help<'a>() -> impl Parser<'a, Expr<'a>, EList<'a>> {
|
|||
specialize_ref(EList::Expr, loc_expr(false)),
|
||||
word1(b',', EList::End),
|
||||
word1(b']', EList::End),
|
||||
EList::IndentEnd,
|
||||
Expr::SpaceBefore
|
||||
),
|
||||
|arena, elements: Collection<'a, _>| {
|
||||
|
|
|
|||
|
|
@ -239,7 +239,6 @@ fn provides_to<'a>() -> impl Parser<'a, ProvidesTo<'a>, EProvides<'a>> {
|
|||
exposes_entry(EProvides::Identifier),
|
||||
word1(b',', EProvides::ListEnd),
|
||||
word1(b']', EProvides::ListEnd),
|
||||
EProvides::IndentListEnd,
|
||||
Spaced::SpaceBefore
|
||||
),
|
||||
types: optional(backtrackable(provides_types())),
|
||||
|
|
@ -271,7 +270,6 @@ fn provides_exposed<'a>() -> impl Parser<
|
|||
exposes_entry(EProvides::Identifier),
|
||||
word1(b',', EProvides::ListEnd),
|
||||
word1(b']', EProvides::ListEnd),
|
||||
EProvides::IndentListEnd,
|
||||
Spaced::SpaceBefore
|
||||
),
|
||||
})
|
||||
|
|
@ -297,7 +295,6 @@ fn provides_types<'a>(
|
|||
provides_type_entry(EProvides::Identifier),
|
||||
word1(b',', EProvides::ListEnd),
|
||||
word1(b'}', EProvides::ListEnd),
|
||||
EProvides::IndentListEnd,
|
||||
Spaced::SpaceBefore
|
||||
)
|
||||
)
|
||||
|
|
@ -364,7 +361,6 @@ fn requires_rigids<'a>(
|
|||
),
|
||||
word1(b',', ERequires::ListEnd),
|
||||
word1(b'}', ERequires::ListEnd),
|
||||
ERequires::IndentListEnd,
|
||||
Spaced::SpaceBefore
|
||||
)
|
||||
}
|
||||
|
|
@ -402,7 +398,6 @@ fn exposes_values<'a>() -> impl Parser<
|
|||
exposes_entry(EExposes::Identifier),
|
||||
word1(b',', EExposes::ListEnd),
|
||||
word1(b']', EExposes::ListEnd),
|
||||
EExposes::IndentListEnd,
|
||||
Spaced::SpaceBefore
|
||||
)
|
||||
})
|
||||
|
|
@ -453,7 +448,6 @@ fn exposes_modules<'a>() -> impl Parser<
|
|||
exposes_module(EExposes::Identifier),
|
||||
word1(b',', EExposes::ListEnd),
|
||||
word1(b']', EExposes::ListEnd),
|
||||
EExposes::IndentListEnd,
|
||||
Spaced::SpaceBefore
|
||||
),
|
||||
})
|
||||
|
|
@ -491,7 +485,6 @@ fn packages<'a>() -> impl Parser<
|
|||
specialize(EPackages::PackageEntry, loc!(package_entry())),
|
||||
word1(b',', EPackages::ListEnd),
|
||||
word1(b'}', EPackages::ListEnd),
|
||||
EPackages::IndentListEnd,
|
||||
Spaced::SpaceBefore
|
||||
)
|
||||
})
|
||||
|
|
@ -529,7 +522,6 @@ fn generates_with<'a>() -> impl Parser<
|
|||
exposes_entry(EGeneratesWith::Identifier),
|
||||
word1(b',', EGeneratesWith::ListEnd),
|
||||
word1(b']', EGeneratesWith::ListEnd),
|
||||
EGeneratesWith::IndentListEnd,
|
||||
Spaced::SpaceBefore
|
||||
)
|
||||
})
|
||||
|
|
@ -553,7 +545,6 @@ fn imports<'a>() -> impl Parser<
|
|||
loc!(imports_entry()),
|
||||
word1(b',', EImports::ListEnd),
|
||||
word1(b']', EImports::ListEnd),
|
||||
EImports::IndentListEnd,
|
||||
Spaced::SpaceBefore
|
||||
)
|
||||
})
|
||||
|
|
@ -634,7 +625,6 @@ fn imports_entry<'a>() -> impl Parser<'a, Spaced<'a, ImportsEntry<'a>>, EImports
|
|||
exposes_entry(EImports::Identifier),
|
||||
word1(b',', EImports::SetEnd),
|
||||
word1(b'}', EImports::SetEnd),
|
||||
EImports::IndentSetEnd,
|
||||
Spaced::SpaceBefore
|
||||
)
|
||||
))
|
||||
|
|
|
|||
|
|
@ -142,7 +142,6 @@ pub enum EProvides<'a> {
|
|||
IndentProvides(Position),
|
||||
IndentTo(Position),
|
||||
IndentListStart(Position),
|
||||
IndentListEnd(Position),
|
||||
IndentPackage(Position),
|
||||
ListStart(Position),
|
||||
ListEnd(Position),
|
||||
|
|
@ -157,7 +156,6 @@ pub enum EExposes {
|
|||
Open(Position),
|
||||
IndentExposes(Position),
|
||||
IndentListStart(Position),
|
||||
IndentListEnd(Position),
|
||||
ListStart(Position),
|
||||
ListEnd(Position),
|
||||
Identifier(Position),
|
||||
|
|
@ -170,7 +168,6 @@ pub enum ERequires<'a> {
|
|||
Open(Position),
|
||||
IndentRequires(Position),
|
||||
IndentListStart(Position),
|
||||
IndentListEnd(Position),
|
||||
ListStart(Position),
|
||||
ListEnd(Position),
|
||||
TypedIdent(ETypedIdent<'a>, Position),
|
||||
|
|
@ -234,7 +231,6 @@ pub enum EImports {
|
|||
ModuleName(Position),
|
||||
Space(BadInputError, Position),
|
||||
IndentSetStart(Position),
|
||||
IndentSetEnd(Position),
|
||||
SetStart(Position),
|
||||
SetEnd(Position),
|
||||
}
|
||||
|
|
@ -457,9 +453,6 @@ pub enum EInParens<'a> {
|
|||
|
||||
///
|
||||
Space(BadInputError, Position),
|
||||
///
|
||||
IndentOpen(Position),
|
||||
IndentEnd(Position),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
|
|
@ -484,9 +477,6 @@ pub enum EList<'a> {
|
|||
Space(BadInputError, Position),
|
||||
|
||||
Expr(&'a EExpr<'a>, Position),
|
||||
|
||||
IndentOpen(Position),
|
||||
IndentEnd(Position),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
|
|
@ -585,11 +575,6 @@ pub enum PRecord<'a> {
|
|||
Expr(&'a EExpr<'a>, Position),
|
||||
|
||||
Space(BadInputError, Position),
|
||||
|
||||
IndentOpen(Position),
|
||||
IndentColon(Position),
|
||||
IndentOptional(Position),
|
||||
IndentEnd(Position),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
|
|
@ -601,9 +586,6 @@ pub enum PList<'a> {
|
|||
Pattern(&'a EPattern<'a>, Position),
|
||||
|
||||
Space(BadInputError, Position),
|
||||
|
||||
IndentOpen(Position),
|
||||
IndentEnd(Position),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
|
|
@ -614,8 +596,6 @@ pub enum PInParens<'a> {
|
|||
Pattern(&'a EPattern<'a>, Position),
|
||||
|
||||
Space(BadInputError, Position),
|
||||
IndentOpen(Position),
|
||||
IndentEnd(Position),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
|
|
@ -670,9 +650,6 @@ pub enum ETypeTagUnion<'a> {
|
|||
Type(&'a EType<'a>, Position),
|
||||
|
||||
Space(BadInputError, Position),
|
||||
|
||||
IndentOpen(Position),
|
||||
IndentEnd(Position),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
|
|
@ -1318,29 +1295,20 @@ macro_rules! collection {
|
|||
|
||||
#[macro_export]
|
||||
macro_rules! collection_trailing_sep_e {
|
||||
($opening_brace:expr, $elem:expr, $delimiter:expr, $closing_brace:expr, $indent_problem:expr, $space_before:expr) => {
|
||||
($opening_brace:expr, $elem:expr, $delimiter:expr, $closing_brace:expr, $space_before:expr) => {
|
||||
map_with_arena!(
|
||||
skip_first!(
|
||||
$opening_brace,
|
||||
and!(
|
||||
$crate::parser::reset_min_indent(and!(
|
||||
and!(
|
||||
space0_e($indent_problem),
|
||||
$crate::blankspace::spaces(),
|
||||
$crate::parser::trailing_sep_by0(
|
||||
$delimiter,
|
||||
$crate::blankspace::space0_before_optional_after(
|
||||
$elem,
|
||||
$indent_problem,
|
||||
$indent_problem
|
||||
)
|
||||
$crate::blankspace::spaces_before_optional_after($elem,)
|
||||
)
|
||||
),
|
||||
skip_second!(
|
||||
$crate::parser::reset_min_indent($crate::blankspace::space0_e(
|
||||
$indent_problem
|
||||
)),
|
||||
$closing_brace
|
||||
)
|
||||
)
|
||||
skip_second!($crate::blankspace::spaces(), $closing_brace)
|
||||
))
|
||||
),
|
||||
|arena: &'a bumpalo::Bump,
|
||||
((spaces, mut parsed_elems), mut final_comments): (
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
use crate::ast::{Has, Pattern, PatternAs, Spaceable};
|
||||
use crate::blankspace::{space0_before_e, space0_e};
|
||||
use crate::blankspace::{space0_e, spaces, spaces_before};
|
||||
use crate::ident::{lowercase_ident, parse_ident, Ident};
|
||||
use crate::keyword;
|
||||
use crate::parser::Progress::{self, *};
|
||||
|
|
@ -190,7 +190,6 @@ fn loc_pattern_in_parens_help<'a>() -> impl Parser<'a, Loc<Pattern<'a>>, PInPare
|
|||
specialize_ref(PInParens::Pattern, loc_pattern_help()),
|
||||
word1(b',', PInParens::End),
|
||||
word1(b')', PInParens::End),
|
||||
PInParens::IndentOpen,
|
||||
Pattern::SpaceBefore
|
||||
)),
|
||||
move |_arena, state, _, loc_elements| {
|
||||
|
|
@ -263,7 +262,6 @@ fn list_pattern_help<'a>() -> impl Parser<'a, Pattern<'a>, PList<'a>> {
|
|||
list_element_pattern(),
|
||||
word1(b',', PList::End),
|
||||
word1(b']', PList::End),
|
||||
PList::IndentEnd,
|
||||
Pattern::SpaceBefore
|
||||
),
|
||||
Pattern::List
|
||||
|
|
@ -459,7 +457,6 @@ fn record_pattern_help<'a>() -> impl Parser<'a, Pattern<'a>, PRecord<'a>> {
|
|||
record_pattern_field(),
|
||||
word1(b',', PRecord::End),
|
||||
word1(b'}', PRecord::End),
|
||||
PRecord::IndentEnd,
|
||||
Pattern::SpaceBefore
|
||||
),
|
||||
Pattern::RecordDestructure
|
||||
|
|
@ -480,7 +477,7 @@ fn record_pattern_field<'a>() -> impl Parser<'a, Loc<Pattern<'a>>, PRecord<'a>>
|
|||
.parse(arena, state, min_indent)?;
|
||||
debug_assert_eq!(progress, MadeProgress);
|
||||
|
||||
let (_, spaces, state) = space0_e(PRecord::IndentEnd).parse(arena, state, min_indent)?;
|
||||
let (_, spaces, state) = spaces().parse(arena, state, min_indent)?;
|
||||
|
||||
// Having a value is optional; both `{ email }` and `{ email: blah }` work.
|
||||
// (This is true in both literals and types.)
|
||||
|
|
@ -493,8 +490,8 @@ fn record_pattern_field<'a>() -> impl Parser<'a, Loc<Pattern<'a>>, PRecord<'a>>
|
|||
match opt_loc_val {
|
||||
Some(First(_)) => {
|
||||
let val_parser = specialize_ref(PRecord::Pattern, loc_pattern_help());
|
||||
let (_, loc_val, state) = space0_before_e(val_parser, PRecord::IndentColon)
|
||||
.parse(arena, state, min_indent)?;
|
||||
let (_, loc_val, state) =
|
||||
spaces_before(val_parser).parse(arena, state, min_indent)?;
|
||||
|
||||
let Loc {
|
||||
value: label,
|
||||
|
|
@ -520,8 +517,8 @@ fn record_pattern_field<'a>() -> impl Parser<'a, Loc<Pattern<'a>>, PRecord<'a>>
|
|||
Some(Second(_)) => {
|
||||
let val_parser = specialize_ref(PRecord::Expr, crate::expr::loc_expr(false));
|
||||
|
||||
let (_, loc_val, state) = space0_before_e(val_parser, PRecord::IndentColon)
|
||||
.parse(arena, state, min_indent)?;
|
||||
let (_, loc_val, state) =
|
||||
spaces_before(val_parser).parse(arena, state, min_indent)?;
|
||||
|
||||
let Loc {
|
||||
value: label,
|
||||
|
|
|
|||
|
|
@ -43,7 +43,6 @@ fn tag_union_type<'a>(
|
|||
loc!(tag_type(false)),
|
||||
word1(b',', ETypeTagUnion::End),
|
||||
word1(b']', ETypeTagUnion::End),
|
||||
ETypeTagUnion::IndentEnd,
|
||||
Tag::SpaceBefore
|
||||
)
|
||||
.parse(arena, state, min_indent)?;
|
||||
|
|
@ -236,7 +235,6 @@ fn loc_type_in_parens<'a>(
|
|||
specialize_ref(ETypeInParens::Type, expression(true, false)),
|
||||
word1(b',', ETypeInParens::End),
|
||||
word1(b')', ETypeInParens::End),
|
||||
ETypeInParens::IndentEnd,
|
||||
TypeAnnotation::SpaceBefore
|
||||
),
|
||||
optional(allocated(specialize_ref(
|
||||
|
|
@ -376,7 +374,6 @@ fn record_type<'a>(
|
|||
loc!(record_type_field()),
|
||||
word1(b',', ETypeRecord::End),
|
||||
word1(b'}', ETypeRecord::End),
|
||||
ETypeRecord::IndentEnd,
|
||||
AssignedField::SpaceBefore
|
||||
),
|
||||
ext: optional(allocated(specialize_ref(
|
||||
|
|
@ -522,7 +519,6 @@ pub fn has_abilities<'a>() -> impl Parser<'a, Loc<HasAbilities<'a>>, EType<'a>>
|
|||
loc!(parse_has_ability()),
|
||||
word1(b',', EType::TEnd),
|
||||
word1(b']', EType::TEnd),
|
||||
EType::TIndentEnd,
|
||||
HasAbility::SpaceBefore
|
||||
),
|
||||
HasAbilities::Has
|
||||
|
|
@ -544,7 +540,6 @@ fn parse_has_ability<'a>() -> impl Parser<'a, HasAbility<'a>, EType<'a>> {
|
|||
specialize(|e: ERecord<'_>, _| e.into(), loc!(record_value_field())),
|
||||
word1(b',', ETypeAbilityImpl::End),
|
||||
word1(b'}', ETypeAbilityImpl::End),
|
||||
ETypeAbilityImpl::IndentEnd,
|
||||
AssignedField::SpaceBefore
|
||||
)
|
||||
),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue