Un-macro skip_second

This commit is contained in:
Jackson Wambolt 2024-04-14 20:52:29 -05:00
parent e4713ce2c5
commit e8ae2e12f8
No known key found for this signature in database
GPG key ID: 76F29A42FEE8811C
6 changed files with 55 additions and 47 deletions

View file

@ -11,9 +11,9 @@ use crate::ident::{integer_ident, lowercase_ident, parse_ident, Accessor, Ident}
use crate::keyword; use crate::keyword;
use crate::parser::{ use crate::parser::{
self, backtrackable, byte, byte_indent, increment_min_indent, line_min_indent, optional, self, backtrackable, byte, byte_indent, increment_min_indent, line_min_indent, optional,
reset_min_indent, sep_by1, sep_by1_e, set_min_indent, specialize_err, specialize_err_ref, then, reset_min_indent, sep_by1, sep_by1_e, set_min_indent, skip_second, specialize_err,
two_bytes, EClosure, EExpect, EExpr, EIf, EInParens, EList, ENumber, EPattern, ERecord, specialize_err_ref, then, two_bytes, EClosure, EExpect, EExpr, EIf, EInParens, EList, ENumber,
EString, EType, EWhen, Either, ParseResult, Parser, EPattern, ERecord, EString, EType, EWhen, Either, ParseResult, Parser,
}; };
use crate::pattern::{closure_param, loc_implements_parser}; use crate::pattern::{closure_param, loc_implements_parser};
use crate::state::State; use crate::state::State;
@ -43,9 +43,9 @@ pub fn test_parse_expr<'a>(
arena: &'a bumpalo::Bump, arena: &'a bumpalo::Bump,
state: State<'a>, state: State<'a>,
) -> Result<Loc<Expr<'a>>, EExpr<'a>> { ) -> Result<Loc<Expr<'a>>, EExpr<'a>> {
let parser = skip_second!( let parser = skip_second(
space0_before_optional_after(loc_expr(true), EExpr::IndentStart, EExpr::IndentEnd), space0_before_optional_after(loc_expr(true), EExpr::IndentStart, EExpr::IndentEnd),
expr_end() expr_end(),
); );
match parser.parse(arena, state, min_indent) { match parser.parse(arena, state, min_indent) {
@ -2629,9 +2629,9 @@ mod when {
} }
fn if_branch<'a>() -> impl Parser<'a, (Loc<Expr<'a>>, Loc<Expr<'a>>), EIf<'a>> { fn if_branch<'a>() -> impl Parser<'a, (Loc<Expr<'a>>, Loc<Expr<'a>>), EIf<'a>> {
skip_second!( skip_second(
and!( and!(
skip_second!( skip_second(
space0_around_ee( space0_around_ee(
specialize_err_ref(EIf::Condition, loc_expr(true)), specialize_err_ref(EIf::Condition, loc_expr(true)),
EIf::IndentCondition, EIf::IndentCondition,
@ -2645,7 +2645,7 @@ fn if_branch<'a>() -> impl Parser<'a, (Loc<Expr<'a>>, Loc<Expr<'a>>), EIf<'a>> {
EIf::IndentElseToken, EIf::IndentElseToken,
) )
), ),
parser::keyword(keyword::ELSE, EIf::Else) parser::keyword(keyword::ELSE, EIf::Else),
) )
} }
@ -3073,7 +3073,7 @@ fn record_help<'a>() -> impl Parser<'a, RecordHelp<'a>, ERecord<'a>> {
reset_min_indent(record!(RecordHelp { reset_min_indent(record!(RecordHelp {
// You can optionally have an identifier followed by an '&' to // You can optionally have an identifier followed by an '&' to
// make this a record update, e.g. { Foo.user & username: "blah" }. // make this a record update, e.g. { Foo.user & username: "blah" }.
update: optional(backtrackable(skip_second!( update: optional(backtrackable(skip_second(
spaces_around( spaces_around(
// We wrap the ident in an Expr here, // We wrap the ident in an Expr here,
// so that we have a Spaceable value to work with, // so that we have a Spaceable value to work with,

View file

@ -4,7 +4,7 @@ use crate::ast::{
use crate::blankspace::space0_e; use crate::blankspace::space0_e;
use crate::expr::merge_spaces; use crate::expr::merge_spaces;
use crate::ident::{lowercase_ident, UppercaseIdent}; use crate::ident::{lowercase_ident, UppercaseIdent};
use crate::parser::{byte, specialize_err, EPackageEntry, EPackageName, Parser}; use crate::parser::{byte, skip_second, specialize_err, EPackageEntry, EPackageName, Parser};
use crate::parser::{optional, then}; use crate::parser::{optional, then};
use crate::string_literal; use crate::string_literal;
use roc_module::symbol::{ModuleId, Symbol}; use roc_module::symbol::{ModuleId, Symbol};
@ -344,7 +344,7 @@ pub fn package_entry<'a>() -> impl Parser<'a, Spaced<'a, PackageEntry<'a>>, EPac
// (Indirect dependencies don't have a shorthand.) // (Indirect dependencies don't have a shorthand.)
and!( and!(
optional(and!( optional(and!(
skip_second!( skip_second(
and!( and!(
specialize_err(|_, pos| EPackageEntry::Shorthand(pos), lowercase_ident()), specialize_err(|_, pos| EPackageEntry::Shorthand(pos), lowercase_ident()),
space0_e(EPackageEntry::IndentPackage) space0_e(EPackageEntry::IndentPackage)

View file

@ -9,9 +9,9 @@ use crate::header::{
use crate::ident::{self, lowercase_ident, unqualified_ident, uppercase, UppercaseIdent}; use crate::ident::{self, lowercase_ident, unqualified_ident, uppercase, UppercaseIdent};
use crate::parser::Progress::{self, *}; use crate::parser::Progress::{self, *};
use crate::parser::{ use crate::parser::{
backtrackable, byte, increment_min_indent, optional, reset_min_indent, specialize_err, backtrackable, byte, increment_min_indent, optional, reset_min_indent, skip_second,
two_bytes, EExposes, EGenerates, EGeneratesWith, EHeader, EImports, EPackages, EProvides, specialize_err, two_bytes, EExposes, EGenerates, EGeneratesWith, EHeader, EImports, EPackages,
ERequires, ETypedIdent, Parser, SourceError, SpaceProblem, SyntaxError, EProvides, ERequires, ETypedIdent, Parser, SourceError, SpaceProblem, SyntaxError,
}; };
use crate::state::State; use crate::state::State;
use crate::string_literal::{self, parse_str_literal}; use crate::string_literal::{self, parse_str_literal};
@ -30,9 +30,9 @@ fn end_of_file<'a>() -> impl Parser<'a, (), SyntaxError<'a>> {
#[inline(always)] #[inline(always)]
pub fn module_defs<'a>() -> impl Parser<'a, Defs<'a>, SyntaxError<'a>> { pub fn module_defs<'a>() -> impl Parser<'a, Defs<'a>, SyntaxError<'a>> {
skip_second!( skip_second(
specialize_err(SyntaxError::Expr, crate::expr::toplevel_defs(),), specialize_err(SyntaxError::Expr, crate::expr::toplevel_defs()),
end_of_file() end_of_file(),
) )
} }
@ -345,7 +345,7 @@ fn requires<'a>(
#[inline(always)] #[inline(always)]
fn platform_requires<'a>() -> impl Parser<'a, PlatformRequires<'a>, ERequires<'a>> { fn platform_requires<'a>() -> impl Parser<'a, PlatformRequires<'a>, ERequires<'a>> {
record!(PlatformRequires { record!(PlatformRequires {
rigids: skip_second!(requires_rigids(), space0_e(ERequires::ListStart)), rigids: skip_second(requires_rigids(), space0_e(ERequires::ListStart)),
signature: requires_typed_ident() signature: requires_typed_ident()
}) })
} }
@ -369,7 +369,7 @@ fn requires_rigids<'a>(
fn requires_typed_ident<'a>() -> impl Parser<'a, Loc<Spaced<'a, TypedIdent<'a>>>, ERequires<'a>> { fn requires_typed_ident<'a>() -> impl Parser<'a, Loc<Spaced<'a, TypedIdent<'a>>>, ERequires<'a>> {
skip_first!( skip_first!(
byte(b'{', ERequires::ListStart), byte(b'{', ERequires::ListStart),
skip_second!( skip_second(
reset_min_indent(space0_around_ee( reset_min_indent(space0_around_ee(
specialize_err(ERequires::TypedIdent, loc!(typed_ident()),), specialize_err(ERequires::TypedIdent, loc!(typed_ident()),),
ERequires::ListStart, ERequires::ListStart,
@ -414,10 +414,13 @@ where
{ {
map!( map!(
and!( and!(
skip_second!( skip_second(
// parse any leading space before the keyword
backtrackable(space0_e(indent_problem1)), backtrackable(space0_e(indent_problem1)),
// parse the keyword
crate::parser::keyword(K::KEYWORD, expectation) crate::parser::keyword(K::KEYWORD, expectation)
), ),
// parse the trailing space
space0_e(indent_problem2) space0_e(indent_problem2)
), ),
|(before, after)| { |(before, after)| {
@ -611,7 +614,7 @@ fn imports_entry<'a>() -> impl Parser<'a, Spaced<'a, ImportsEntry<'a>>, EImports
and!( and!(
and!( and!(
// e.g. `pf.` // e.g. `pf.`
optional(backtrackable(skip_second!( optional(backtrackable(skip_second(
shortname(), shortname(),
byte(b'.', EImports::ShorthandDot) byte(b'.', EImports::ShorthandDot)
))), ))),

View file

@ -1427,7 +1427,7 @@ macro_rules! skip_first {
/// # use bumpalo::Bump; /// # use bumpalo::Bump;
/// # let arena = Bump::new(); /// # let arena = Bump::new();
/// # fn foo<'a>(arena: &'a Bump) { /// # fn foo<'a>(arena: &'a Bump) {
/// let parser = skip_second!( /// let parser = skip_second(
/// lowercase_ident(), /// lowercase_ident(),
/// word(", world", |_| ()) /// word(", world", |_| ())
/// ); /// );
@ -1439,19 +1439,21 @@ macro_rules! skip_first {
/// # } /// # }
/// # foo(&arena); /// # foo(&arena);
/// ``` /// ```
#[macro_export] pub fn skip_second<'a, P1, First, P2, Second, E>(p1: P1, p2: P2) -> impl Parser<'a, First, E>
macro_rules! skip_second { where
($p1:expr, $p2:expr) => { E: 'a,
move |arena, state: $crate::state::State<'a>, min_indent: u32| match $p1 P1: Parser<'a, First, E>,
.parse(arena, state, min_indent) P2: Parser<'a, Second, E>,
{ {
Ok((p1, out1, state)) => match $p2.parse(arena, state, min_indent) { move |arena, state: crate::state::State<'a>, min_indent: u32| match p1
Ok((p2, _, state)) => Ok((p1.or(p2), out1, state)), .parse(arena, state, min_indent)
Err((p2, fail)) => Err((p1.or(p2), fail)), {
}, Ok((p1, out1, state)) => match p2.parse(arena, state, min_indent) {
Err((progress, fail)) => Err((progress, fail)), Ok((p2, _, state)) => Ok((p1.or(p2), out1, state)),
} Err((p2, fail)) => Err((p1.or(p2), fail)),
}; },
Err((progress, fail)) => Err((progress, fail)),
}
} }
#[macro_export] #[macro_export]
@ -2535,7 +2537,10 @@ macro_rules! either {
#[macro_export] #[macro_export]
macro_rules! between { macro_rules! between {
($opening_brace:expr, $parser:expr, $closing_brace:expr) => { ($opening_brace:expr, $parser:expr, $closing_brace:expr) => {
skip_first!($opening_brace, skip_second!($parser, $closing_brace)) skip_first!(
$opening_brace,
$crate::parser::skip_second($parser, $closing_brace)
)
}; };
} }

View file

@ -2,8 +2,8 @@ use crate::ast::{EscapedChar, SingleQuoteLiteral, StrLiteral, StrSegment};
use crate::expr; use crate::expr;
use crate::parser::Progress::{self, *}; use crate::parser::Progress::{self, *};
use crate::parser::{ use crate::parser::{
allocated, byte, loc, reset_min_indent, specialize_err_ref, then, BadInputError, ESingleQuote, allocated, byte, loc, reset_min_indent, skip_second, specialize_err_ref, then, BadInputError,
EString, Parser, ESingleQuote, EString, Parser,
}; };
use crate::state::State; use crate::state::State;
use bumpalo::collections::vec::Vec; use bumpalo::collections::vec::Vec;
@ -374,12 +374,12 @@ pub fn parse_str_like_literal<'a>() -> impl Parser<'a, StrLikeLiteral<'a>, EStri
// Parse an arbitrary expression, then give a // Parse an arbitrary expression, then give a
// canonicalization error if that expression variant // canonicalization error if that expression variant
// is not allowed inside a string interpolation. // is not allowed inside a string interpolation.
let (_progress, loc_expr, new_state) = skip_second!( let (_progress, loc_expr, new_state) = skip_second(
specialize_err_ref( specialize_err_ref(
EString::Format, EString::Format,
loc(allocated(reset_min_indent(expr::expr_help()))) loc(allocated(reset_min_indent(expr::expr_help()))),
), ),
byte(b')', EString::FormatEnd) byte(b')', EString::FormatEnd),
) )
.parse(arena, state, min_indent)?; .parse(arena, state, min_indent)?;
@ -483,12 +483,12 @@ pub fn parse_str_like_literal<'a>() -> impl Parser<'a, StrLikeLiteral<'a>, EStri
let original_byte_count = state.bytes().len(); let original_byte_count = state.bytes().len();
// Parse an arbitrary expression, followed by ')' // Parse an arbitrary expression, followed by ')'
let (_progress, loc_expr, new_state) = skip_second!( let (_progress, loc_expr, new_state) = skip_second(
specialize_err_ref( specialize_err_ref(
EString::Format, EString::Format,
loc(allocated(reset_min_indent(expr::expr_help()))) loc(allocated(reset_min_indent(expr::expr_help()))),
), ),
byte(b')', EString::FormatEnd) byte(b')', EString::FormatEnd),
) )
.parse(arena, state, min_indent)?; .parse(arena, state, min_indent)?;

View file

@ -9,7 +9,7 @@ use crate::expr::{record_field, FoundApplyValue};
use crate::ident::{lowercase_ident, lowercase_ident_keyword_e}; 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, skip_second, then, ERecord, ETypeAbilityImpl,
}; };
use crate::parser::{ use crate::parser::{
allocated, backtrackable, byte, fail, optional, specialize_err, specialize_err_ref, two_bytes, allocated, backtrackable, byte, fail, optional, specialize_err, specialize_err_ref, two_bytes,
@ -137,7 +137,7 @@ fn term<'a>(stop_at_surface_has: bool) -> impl Parser<'a, Loc<TypeAnnotation<'a>
one_of![ one_of![
map!( map!(
and!( and!(
skip_second!( skip_second(
backtrackable(space0_e(EType::TIndentEnd)), backtrackable(space0_e(EType::TIndentEnd)),
crate::parser::keyword(keyword::AS, EType::TEnd) crate::parser::keyword(keyword::AS, EType::TEnd)
), ),
@ -596,7 +596,7 @@ fn expression<'a>(
] ]
)) ))
.trace("type_annotation:expression:rest_args"), .trace("type_annotation:expression:rest_args"),
skip_second!( skip_second(
space0_e(EType::TIndentStart), space0_e(EType::TIndentStart),
two_bytes(b'-', b'>', EType::TStart) two_bytes(b'-', b'>', EType::TStart)
) )