mirror of
https://github.com/roc-lang/roc.git
synced 2025-10-03 08:34:33 +00:00
Un-macro skip_second
This commit is contained in:
parent
e4713ce2c5
commit
e8ae2e12f8
6 changed files with 55 additions and 47 deletions
|
@ -11,9 +11,9 @@ use crate::ident::{integer_ident, lowercase_ident, parse_ident, Accessor, Ident}
|
|||
use crate::keyword;
|
||||
use crate::parser::{
|
||||
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,
|
||||
two_bytes, EClosure, EExpect, EExpr, EIf, EInParens, EList, ENumber, EPattern, ERecord,
|
||||
EString, EType, EWhen, Either, ParseResult, Parser,
|
||||
reset_min_indent, sep_by1, sep_by1_e, set_min_indent, skip_second, specialize_err,
|
||||
specialize_err_ref, then, two_bytes, EClosure, EExpect, EExpr, EIf, EInParens, EList, ENumber,
|
||||
EPattern, ERecord, EString, EType, EWhen, Either, ParseResult, Parser,
|
||||
};
|
||||
use crate::pattern::{closure_param, loc_implements_parser};
|
||||
use crate::state::State;
|
||||
|
@ -43,9 +43,9 @@ pub fn test_parse_expr<'a>(
|
|||
arena: &'a bumpalo::Bump,
|
||||
state: State<'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),
|
||||
expr_end()
|
||||
expr_end(),
|
||||
);
|
||||
|
||||
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>> {
|
||||
skip_second!(
|
||||
skip_second(
|
||||
and!(
|
||||
skip_second!(
|
||||
skip_second(
|
||||
space0_around_ee(
|
||||
specialize_err_ref(EIf::Condition, loc_expr(true)),
|
||||
EIf::IndentCondition,
|
||||
|
@ -2645,7 +2645,7 @@ fn if_branch<'a>() -> impl Parser<'a, (Loc<Expr<'a>>, Loc<Expr<'a>>), EIf<'a>> {
|
|||
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 {
|
||||
// You can optionally have an identifier followed by an '&' to
|
||||
// make this a record update, e.g. { Foo.user & username: "blah" }.
|
||||
update: optional(backtrackable(skip_second!(
|
||||
update: optional(backtrackable(skip_second(
|
||||
spaces_around(
|
||||
// We wrap the ident in an Expr here,
|
||||
// so that we have a Spaceable value to work with,
|
||||
|
|
|
@ -4,7 +4,7 @@ use crate::ast::{
|
|||
use crate::blankspace::space0_e;
|
||||
use crate::expr::merge_spaces;
|
||||
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::string_literal;
|
||||
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.)
|
||||
and!(
|
||||
optional(and!(
|
||||
skip_second!(
|
||||
skip_second(
|
||||
and!(
|
||||
specialize_err(|_, pos| EPackageEntry::Shorthand(pos), lowercase_ident()),
|
||||
space0_e(EPackageEntry::IndentPackage)
|
||||
|
|
|
@ -9,9 +9,9 @@ use crate::header::{
|
|||
use crate::ident::{self, lowercase_ident, unqualified_ident, uppercase, UppercaseIdent};
|
||||
use crate::parser::Progress::{self, *};
|
||||
use crate::parser::{
|
||||
backtrackable, byte, increment_min_indent, optional, reset_min_indent, specialize_err,
|
||||
two_bytes, EExposes, EGenerates, EGeneratesWith, EHeader, EImports, EPackages, EProvides,
|
||||
ERequires, ETypedIdent, Parser, SourceError, SpaceProblem, SyntaxError,
|
||||
backtrackable, byte, increment_min_indent, optional, reset_min_indent, skip_second,
|
||||
specialize_err, two_bytes, EExposes, EGenerates, EGeneratesWith, EHeader, EImports, EPackages,
|
||||
EProvides, ERequires, ETypedIdent, Parser, SourceError, SpaceProblem, SyntaxError,
|
||||
};
|
||||
use crate::state::State;
|
||||
use crate::string_literal::{self, parse_str_literal};
|
||||
|
@ -30,9 +30,9 @@ fn end_of_file<'a>() -> impl Parser<'a, (), SyntaxError<'a>> {
|
|||
|
||||
#[inline(always)]
|
||||
pub fn module_defs<'a>() -> impl Parser<'a, Defs<'a>, SyntaxError<'a>> {
|
||||
skip_second!(
|
||||
specialize_err(SyntaxError::Expr, crate::expr::toplevel_defs(),),
|
||||
end_of_file()
|
||||
skip_second(
|
||||
specialize_err(SyntaxError::Expr, crate::expr::toplevel_defs()),
|
||||
end_of_file(),
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -345,7 +345,7 @@ fn requires<'a>(
|
|||
#[inline(always)]
|
||||
fn platform_requires<'a>() -> impl Parser<'a, PlatformRequires<'a>, ERequires<'a>> {
|
||||
record!(PlatformRequires {
|
||||
rigids: skip_second!(requires_rigids(), space0_e(ERequires::ListStart)),
|
||||
rigids: skip_second(requires_rigids(), space0_e(ERequires::ListStart)),
|
||||
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>> {
|
||||
skip_first!(
|
||||
byte(b'{', ERequires::ListStart),
|
||||
skip_second!(
|
||||
skip_second(
|
||||
reset_min_indent(space0_around_ee(
|
||||
specialize_err(ERequires::TypedIdent, loc!(typed_ident()),),
|
||||
ERequires::ListStart,
|
||||
|
@ -414,10 +414,13 @@ where
|
|||
{
|
||||
map!(
|
||||
and!(
|
||||
skip_second!(
|
||||
skip_second(
|
||||
// parse any leading space before the keyword
|
||||
backtrackable(space0_e(indent_problem1)),
|
||||
// parse the keyword
|
||||
crate::parser::keyword(K::KEYWORD, expectation)
|
||||
),
|
||||
// parse the trailing space
|
||||
space0_e(indent_problem2)
|
||||
),
|
||||
|(before, after)| {
|
||||
|
@ -611,7 +614,7 @@ fn imports_entry<'a>() -> impl Parser<'a, Spaced<'a, ImportsEntry<'a>>, EImports
|
|||
and!(
|
||||
and!(
|
||||
// e.g. `pf.`
|
||||
optional(backtrackable(skip_second!(
|
||||
optional(backtrackable(skip_second(
|
||||
shortname(),
|
||||
byte(b'.', EImports::ShorthandDot)
|
||||
))),
|
||||
|
|
|
@ -1427,7 +1427,7 @@ macro_rules! skip_first {
|
|||
/// # use bumpalo::Bump;
|
||||
/// # let arena = Bump::new();
|
||||
/// # fn foo<'a>(arena: &'a Bump) {
|
||||
/// let parser = skip_second!(
|
||||
/// let parser = skip_second(
|
||||
/// lowercase_ident(),
|
||||
/// word(", world", |_| ())
|
||||
/// );
|
||||
|
@ -1439,19 +1439,21 @@ macro_rules! skip_first {
|
|||
/// # }
|
||||
/// # foo(&arena);
|
||||
/// ```
|
||||
#[macro_export]
|
||||
macro_rules! skip_second {
|
||||
($p1:expr, $p2:expr) => {
|
||||
move |arena, state: $crate::state::State<'a>, min_indent: u32| match $p1
|
||||
pub fn skip_second<'a, P1, First, P2, Second, E>(p1: P1, p2: P2) -> impl Parser<'a, First, E>
|
||||
where
|
||||
E: 'a,
|
||||
P1: Parser<'a, First, E>,
|
||||
P2: Parser<'a, Second, E>,
|
||||
{
|
||||
move |arena, state: crate::state::State<'a>, min_indent: u32| match p1
|
||||
.parse(arena, state, min_indent)
|
||||
{
|
||||
Ok((p1, out1, state)) => match $p2.parse(arena, state, min_indent) {
|
||||
Ok((p1, out1, state)) => match p2.parse(arena, state, min_indent) {
|
||||
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]
|
||||
|
@ -2535,7 +2537,10 @@ macro_rules! either {
|
|||
#[macro_export]
|
||||
macro_rules! between {
|
||||
($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)
|
||||
)
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
@ -2,8 +2,8 @@ use crate::ast::{EscapedChar, SingleQuoteLiteral, StrLiteral, StrSegment};
|
|||
use crate::expr;
|
||||
use crate::parser::Progress::{self, *};
|
||||
use crate::parser::{
|
||||
allocated, byte, loc, reset_min_indent, specialize_err_ref, then, BadInputError, ESingleQuote,
|
||||
EString, Parser,
|
||||
allocated, byte, loc, reset_min_indent, skip_second, specialize_err_ref, then, BadInputError,
|
||||
ESingleQuote, EString, Parser,
|
||||
};
|
||||
use crate::state::State;
|
||||
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
|
||||
// canonicalization error if that expression variant
|
||||
// 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(
|
||||
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)?;
|
||||
|
||||
|
@ -483,12 +483,12 @@ pub fn parse_str_like_literal<'a>() -> impl Parser<'a, StrLikeLiteral<'a>, EStri
|
|||
let original_byte_count = state.bytes().len();
|
||||
|
||||
// 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(
|
||||
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)?;
|
||||
|
||||
|
|
|
@ -9,7 +9,7 @@ use crate::expr::{record_field, FoundApplyValue};
|
|||
use crate::ident::{lowercase_ident, lowercase_ident_keyword_e};
|
||||
use crate::keyword;
|
||||
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::{
|
||||
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![
|
||||
map!(
|
||||
and!(
|
||||
skip_second!(
|
||||
skip_second(
|
||||
backtrackable(space0_e(EType::TIndentEnd)),
|
||||
crate::parser::keyword(keyword::AS, EType::TEnd)
|
||||
),
|
||||
|
@ -596,7 +596,7 @@ fn expression<'a>(
|
|||
]
|
||||
))
|
||||
.trace("type_annotation:expression:rest_args"),
|
||||
skip_second!(
|
||||
skip_second(
|
||||
space0_e(EType::TIndentStart),
|
||||
two_bytes(b'-', b'>', EType::TStart)
|
||||
)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue