Refactor collections

This commit is contained in:
Joshua Warner 2022-11-24 07:34:53 -08:00
parent 9ce2f4e1ff
commit 451b2c0ded
No known key found for this signature in database
GPG key ID: 89AD497003F93FDD
7 changed files with 54 additions and 53 deletions

View file

@ -87,7 +87,6 @@ fn loc_expr_in_parens_help<'a>() -> impl Parser<'a, Loc<Expr<'a>>, EInParens<'a>
specialize_ref(EInParens::Expr, loc_expr_no_multi_backpassing()), specialize_ref(EInParens::Expr, loc_expr_no_multi_backpassing()),
word1(b',', EInParens::End), word1(b',', EInParens::End),
word1(b')', EInParens::End), word1(b')', EInParens::End),
EInParens::Open,
EInParens::IndentEnd, EInParens::IndentEnd,
Expr::SpaceBefore Expr::SpaceBefore
)), )),
@ -2467,7 +2466,6 @@ fn list_literal_help<'a>() -> impl Parser<'a, Expr<'a>, EList<'a>> {
specialize_ref(EList::Expr, loc_expr_no_multi_backpassing()), specialize_ref(EList::Expr, loc_expr_no_multi_backpassing()),
word1(b',', EList::End), word1(b',', EList::End),
word1(b']', EList::End), word1(b']', EList::End),
EList::Open,
EList::IndentEnd, EList::IndentEnd,
Expr::SpaceBefore Expr::SpaceBefore
), ),

View file

@ -111,6 +111,22 @@ pub fn integer_ident<'a>() -> impl Parser<'a, &'a str, ()> {
} }
} }
/// Like `lowercase_ident`, but returns an error with MadeProgress if the
/// identifier is a keyword.
pub fn lowercase_ident_keyword_e<'a>() -> impl Parser<'a, &'a str, ()> {
move |_, state: State<'a>, _min_indent: u32| match chomp_lowercase_part(state.bytes()) {
Err(progress) => Err((progress, ())),
Ok(ident) => {
if crate::keyword::KEYWORDS.iter().any(|kw| &ident == kw) {
Err((MadeProgress, ()))
} else {
let width = ident.len();
Ok((MadeProgress, ident, state.advance(width)))
}
}
}
}
pub fn tag_name<'a>() -> impl Parser<'a, &'a str, ()> { pub fn tag_name<'a>() -> impl Parser<'a, &'a str, ()> {
move |arena, state: State<'a>, min_indent: u32| { move |arena, state: State<'a>, min_indent: u32| {
uppercase_ident().parse(arena, state, min_indent) uppercase_ident().parse(arena, state, min_indent)

View file

@ -427,7 +427,6 @@ fn provides_without_to<'a>() -> impl Parser<
exposes_entry(EProvides::Identifier), exposes_entry(EProvides::Identifier),
word1(b',', EProvides::ListEnd), word1(b',', EProvides::ListEnd),
word1(b']', EProvides::ListEnd), word1(b']', EProvides::ListEnd),
EProvides::Open,
EProvides::IndentListEnd, EProvides::IndentListEnd,
Spaced::SpaceBefore Spaced::SpaceBefore
), ),
@ -457,7 +456,6 @@ fn provides_types<'a>(
provides_type_entry(EProvides::Identifier), provides_type_entry(EProvides::Identifier),
word1(b',', EProvides::ListEnd), word1(b',', EProvides::ListEnd),
word1(b'}', EProvides::ListEnd), word1(b'}', EProvides::ListEnd),
EProvides::Open,
EProvides::IndentListEnd, EProvides::IndentListEnd,
Spaced::SpaceBefore Spaced::SpaceBefore
) )
@ -534,7 +532,6 @@ fn requires_rigids<'a>(
), ),
word1(b',', ERequires::ListEnd), word1(b',', ERequires::ListEnd),
word1(b'}', ERequires::ListEnd), word1(b'}', ERequires::ListEnd),
ERequires::Open,
ERequires::IndentListEnd, ERequires::IndentListEnd,
Spaced::SpaceBefore Spaced::SpaceBefore
) )
@ -576,7 +573,6 @@ fn exposes_values<'a>() -> impl Parser<
exposes_entry(EExposes::Identifier), exposes_entry(EExposes::Identifier),
word1(b',', EExposes::ListEnd), word1(b',', EExposes::ListEnd),
word1(b']', EExposes::ListEnd), word1(b']', EExposes::ListEnd),
EExposes::Open,
EExposes::IndentListEnd, EExposes::IndentListEnd,
Spaced::SpaceBefore Spaced::SpaceBefore
) )
@ -622,7 +618,6 @@ fn exposes_modules<'a>() -> impl Parser<
exposes_module(EExposes::Identifier), exposes_module(EExposes::Identifier),
word1(b',', EExposes::ListEnd), word1(b',', EExposes::ListEnd),
word1(b']', EExposes::ListEnd), word1(b']', EExposes::ListEnd),
EExposes::Open,
EExposes::IndentListEnd, EExposes::IndentListEnd,
Spaced::SpaceBefore Spaced::SpaceBefore
) )
@ -665,7 +660,6 @@ fn packages<'a>() -> impl Parser<'a, Packages<'a>, EPackages<'a>> {
specialize(EPackages::PackageEntry, loc!(package_entry())), specialize(EPackages::PackageEntry, loc!(package_entry())),
word1(b',', EPackages::ListEnd), word1(b',', EPackages::ListEnd),
word1(b'}', EPackages::ListEnd), word1(b'}', EPackages::ListEnd),
EPackages::Open,
EPackages::IndentListEnd, EPackages::IndentListEnd,
Spaced::SpaceBefore Spaced::SpaceBefore
) )
@ -724,7 +718,6 @@ fn generates_with<'a>() -> impl Parser<
exposes_entry(EGeneratesWith::Identifier), exposes_entry(EGeneratesWith::Identifier),
word1(b',', EGeneratesWith::ListEnd), word1(b',', EGeneratesWith::ListEnd),
word1(b']', EGeneratesWith::ListEnd), word1(b']', EGeneratesWith::ListEnd),
EGeneratesWith::Open,
EGeneratesWith::IndentListEnd, EGeneratesWith::IndentListEnd,
Spaced::SpaceBefore Spaced::SpaceBefore
) )
@ -752,7 +745,6 @@ fn imports<'a>() -> impl Parser<
loc!(imports_entry()), loc!(imports_entry()),
word1(b',', EImports::ListEnd), word1(b',', EImports::ListEnd),
word1(b']', EImports::ListEnd), word1(b']', EImports::ListEnd),
EImports::Open,
EImports::IndentListEnd, EImports::IndentListEnd,
Spaced::SpaceBefore Spaced::SpaceBefore
) )
@ -833,7 +825,6 @@ fn imports_entry<'a>() -> impl Parser<'a, Spaced<'a, ImportsEntry<'a>>, EImports
exposes_entry(EImports::Identifier), exposes_entry(EImports::Identifier),
word1(b',', EImports::SetEnd), word1(b',', EImports::SetEnd),
word1(b'}', EImports::SetEnd), word1(b'}', EImports::SetEnd),
EImports::Open,
EImports::IndentSetEnd, EImports::IndentSetEnd,
Spaced::SpaceBefore Spaced::SpaceBefore
) )

View file

@ -1330,33 +1330,38 @@ macro_rules! collection {
#[macro_export] #[macro_export]
macro_rules! collection_trailing_sep_e { macro_rules! collection_trailing_sep_e {
($opening_brace:expr, $elem:expr, $delimiter:expr, $closing_brace:expr, $open_problem:expr, $indent_problem:expr, $space_before:expr) => { ($opening_brace:expr, $elem:expr, $delimiter:expr, $closing_brace:expr, $indent_problem:expr, $space_before:expr) => {
skip_first!( map_with_arena!(
$opening_brace, skip_first!(
|arena, state, min_indent| { $opening_brace,
let (_, spaces, state) = space0_e($indent_problem) and!(
.parse(arena, state, min_indent)?; and!(
space0_e($indent_problem),
let (_, (mut parsed_elems, mut final_comments), state) = $crate::parser::trailing_sep_by0(
and!( $delimiter,
$crate::parser::trailing_sep_by0( $crate::blankspace::space0_before_optional_after(
$delimiter, $elem,
$crate::blankspace::space0_before_optional_after( $indent_problem,
$elem, $indent_problem
$indent_problem, )
$indent_problem )
) ),
), skip_second!(
$crate::parser::reset_min_indent($crate::blankspace::space0_e($indent_problem)) $crate::parser::reset_min_indent($crate::blankspace::space0_e(
).parse(arena, state, min_indent)?; $indent_problem
)),
let (_,_, state) = $closing_brace
if parsed_elems.is_empty() { )
one_of_with_error![$open_problem; $closing_brace].parse(arena, state, min_indent)? )
} else { ),
$closing_brace.parse(arena, state, min_indent)? |arena: &'a bumpalo::Bump,
}; ((spaces, mut parsed_elems), mut final_comments): (
(
&'a [$crate::ast::CommentOrNewline<'a>],
bumpalo::collections::vec::Vec<'a, Loc<_>>
),
&'a [$crate::ast::CommentOrNewline<'a>]
)| {
if !spaces.is_empty() { if !spaces.is_empty() {
if let Some(first) = parsed_elems.first_mut() { if let Some(first) = parsed_elems.first_mut() {
first.value = $space_before(arena.alloc(first.value), spaces) first.value = $space_before(arena.alloc(first.value), spaces)
@ -1366,12 +1371,11 @@ macro_rules! collection_trailing_sep_e {
} }
} }
let collection = $crate::ast::Collection::with_items_and_comments( $crate::ast::Collection::with_items_and_comments(
arena, arena,
parsed_elems.into_bump_slice(), parsed_elems.into_bump_slice(),
final_comments); final_comments,
)
Ok((MadeProgress, collection, state))
} }
) )
}; };

View file

@ -135,7 +135,6 @@ fn loc_pattern_in_parens_help<'a>() -> impl Parser<'a, Loc<Pattern<'a>>, PInPare
specialize_ref(PInParens::Pattern, loc_pattern_help()), specialize_ref(PInParens::Pattern, loc_pattern_help()),
word1(b',', PInParens::End), word1(b',', PInParens::End),
word1(b')', PInParens::End), word1(b')', PInParens::End),
PInParens::Open,
PInParens::IndentOpen, PInParens::IndentOpen,
Pattern::SpaceBefore Pattern::SpaceBefore
)), )),
@ -209,7 +208,6 @@ fn list_pattern_help<'a>() -> impl Parser<'a, Pattern<'a>, PList<'a>> {
list_element_pattern(), list_element_pattern(),
word1(b',', PList::End), word1(b',', PList::End),
word1(b']', PList::End), word1(b']', PList::End),
PList::Open,
PList::IndentEnd, PList::IndentEnd,
Pattern::SpaceBefore Pattern::SpaceBefore
), ),
@ -380,7 +378,6 @@ fn record_pattern_help<'a>() -> impl Parser<'a, Pattern<'a>, PRecord<'a>> {
record_pattern_field(), record_pattern_field(),
word1(b',', PRecord::End), word1(b',', PRecord::End),
word1(b'}', PRecord::End), word1(b'}', PRecord::End),
PRecord::Open,
PRecord::IndentEnd, PRecord::IndentEnd,
Pattern::SpaceBefore Pattern::SpaceBefore
), ),

View file

@ -6,7 +6,7 @@ use crate::blankspace::{
space0_around_ee, space0_before_e, space0_before_optional_after, space0_e, space0_around_ee, space0_before_e, space0_before_optional_after, space0_e,
}; };
use crate::expr::record_value_field; use crate::expr::record_value_field;
use crate::ident::lowercase_ident; use crate::ident::{lowercase_ident, lowercase_ident_keyword_e};
use crate::keyword; use crate::keyword;
use crate::parser::{ use crate::parser::{
absolute_column_min_indent, increment_min_indent, then, ERecord, ETypeAbilityImpl, absolute_column_min_indent, increment_min_indent, then, ERecord, ETypeAbilityImpl,
@ -43,7 +43,6 @@ fn tag_union_type<'a>(
loc!(tag_type(false)), loc!(tag_type(false)),
word1(b',', ETypeTagUnion::End), word1(b',', ETypeTagUnion::End),
word1(b']', ETypeTagUnion::End), word1(b']', ETypeTagUnion::End),
ETypeTagUnion::Open,
ETypeTagUnion::IndentEnd, ETypeTagUnion::IndentEnd,
Tag::SpaceBefore Tag::SpaceBefore
) )
@ -216,7 +215,6 @@ fn loc_type_in_parens<'a>(
specialize_ref(ETypeInParens::Type, expression(true, false)), specialize_ref(ETypeInParens::Type, expression(true, false)),
word1(b',', ETypeInParens::End), word1(b',', ETypeInParens::End),
word1(b')', ETypeInParens::End), word1(b')', ETypeInParens::End),
ETypeInParens::Open,
ETypeInParens::IndentEnd, ETypeInParens::IndentEnd,
TypeAnnotation::SpaceBefore TypeAnnotation::SpaceBefore
), ),
@ -292,7 +290,7 @@ fn record_type_field<'a>() -> impl Parser<'a, AssignedField<'a, TypeAnnotation<'
let pos = state.pos(); let pos = state.pos();
let (progress, loc_label, state) = loc!(specialize( let (progress, loc_label, state) = loc!(specialize(
move |_, _| ETypeRecord::Field(pos), move |_, _| ETypeRecord::Field(pos),
lowercase_ident() lowercase_ident_keyword_e()
)) ))
.parse(arena, state, min_indent)?; .parse(arena, state, min_indent)?;
debug_assert_eq!(progress, MadeProgress); debug_assert_eq!(progress, MadeProgress);
@ -358,7 +356,6 @@ fn record_type<'a>(
loc!(record_type_field()), loc!(record_type_field()),
word1(b',', ETypeRecord::End), word1(b',', ETypeRecord::End),
word1(b'}', ETypeRecord::End), word1(b'}', ETypeRecord::End),
ETypeRecord::Open,
ETypeRecord::IndentEnd, ETypeRecord::IndentEnd,
AssignedField::SpaceBefore AssignedField::SpaceBefore
), ),
@ -507,7 +504,6 @@ pub fn has_abilities<'a>() -> impl Parser<'a, Loc<HasAbilities<'a>>, EType<'a>>
loc!(parse_has_ability()), loc!(parse_has_ability()),
word1(b',', EType::TEnd), word1(b',', EType::TEnd),
word1(b']', EType::TEnd), word1(b']', EType::TEnd),
EType::TStart,
EType::TIndentEnd, EType::TIndentEnd,
HasAbility::SpaceBefore HasAbility::SpaceBefore
), ),
@ -531,7 +527,6 @@ fn parse_has_ability<'a>() -> impl Parser<'a, HasAbility<'a>, EType<'a>> {
specialize(|e: ERecord<'_>, _| e.into(), loc!(record_value_field())), specialize(|e: ERecord<'_>, _| e.into(), loc!(record_value_field())),
word1(b',', ETypeAbilityImpl::End), word1(b',', ETypeAbilityImpl::End),
word1(b'}', ETypeAbilityImpl::End), word1(b'}', ETypeAbilityImpl::End),
ETypeAbilityImpl::Open,
ETypeAbilityImpl::IndentEnd, ETypeAbilityImpl::IndentEnd,
AssignedField::SpaceBefore AssignedField::SpaceBefore
) )

View file

@ -3832,7 +3832,7 @@ fn to_requires_report<'a>(
} }
} }
ERequires::Open(pos) => { ERequires::ListEnd(pos) | ERequires::Open(pos) => {
let surroundings = Region::new(start, pos); let surroundings = Region::new(start, pos);
let region = LineColumnRegion::from_pos(lines.convert_pos(pos)); let region = LineColumnRegion::from_pos(lines.convert_pos(pos));