Skip to content

Commit

Permalink
Merge pull request #539 from zyansheep/select_pr
Browse files Browse the repository at this point in the history
Allow select! and select_ref! to use MapExtra
  • Loading branch information
zesterer authored Oct 10, 2023
2 parents 76d28a7 + e0d4812 commit e02f8de
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 18 deletions.
2 changes: 1 addition & 1 deletion examples/nested_spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn parser<'a>() -> impl Parser<'a, TokenTreeInput<'a>, i64> {
let parens = expr
// Here we specify how the parser should come up with the nested tokens
.nested_in(select_ref! {
Token::Parens(xs) = span => xs.as_slice().spanned(SimpleSpan::to_end(&span)),
Token::Parens(xs) = extra => xs.as_slice().spanned(SimpleSpan::to_end(&extra.span())),
});

let atom = num.or(parens);
Expand Down
4 changes: 2 additions & 2 deletions src/combinator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,8 +332,8 @@ where

/// See [`Parser::map_with`].
pub struct MapExtra<'a, 'b, 'inv, I: Input<'a>, E: ParserExtra<'a, I>> {
before: Offset<'a, 'inv, I>,
inp: &'b mut InputRef<'a, 'inv, I, E>,
pub(crate) before: Offset<'a, 'inv, I>,
pub(crate) inp: &'b mut InputRef<'a, 'inv, I, E>,
}

impl<'a, 'b, 'inv, I: Input<'a>, E: ParserExtra<'a, I>> MapExtra<'a, 'b, 'inv, I, E> {
Expand Down
16 changes: 8 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2726,8 +2726,8 @@ where
///
/// # let _: chumsky::primitive::Select<_, &[Token], (Expr, Span), extra::Default> =
/// select! {
/// Token::Num(x) = span => Expr::Num(x).spanned(span),
/// Token::Str(s) = span => Expr::Str(s).spanned(span),
/// Token::Num(x) = extra => Expr::Num(x).spanned(extra.span()),
/// Token::Str(s) = extra => Expr::Str(s).spanned(extra.span()),
/// }
/// # ;
/// ```
Expand Down Expand Up @@ -2780,10 +2780,10 @@ where
/// ```
#[macro_export]
macro_rules! select {
($($p:pat $(= $span:ident)? $(if $guard:expr)? $(=> $out:expr)?),+ $(,)?) => ({
($($p:pat $(= $extra:ident)? $(if $guard:expr)? $(=> $out:expr)?),+ $(,)?) => ({
$crate::primitive::select(
move |x, span| match (x, span) {
$(($p $(,$span)?, ..) $(if $guard)? => ::core::option::Option::Some({ () $(;$out)? })),+,
move |x, extra| match (x, extra) {
$(($p $(,$extra)?, ..) $(if $guard)? => ::core::option::Option::Some({ () $(;$out)? })),+,
_ => ::core::option::Option::None,
}
)
Expand All @@ -2797,10 +2797,10 @@ macro_rules! select {
/// `select_ref` requires that the parser input implements [`BorrowInput`].
#[macro_export]
macro_rules! select_ref {
($($p:pat $(= $span:ident)? $(if $guard:expr)? $(=> $out:expr)?),+ $(,)?) => ({
($($p:pat $(= $extra:ident)? $(if $guard:expr)? $(=> $out:expr)?),+ $(,)?) => ({
$crate::primitive::select_ref(
move |x, span| match (x, span) {
$(($p $(,$span)?, ..) $(if $guard)? => ::core::option::Option::Some({ () $(;$out)? })),+,
move |x, extra| match (x, extra) {
$(($p $(,$extra)?, ..) $(if $guard)? => ::core::option::Option::Some({ () $(;$out)? })),+,
_ => ::core::option::Option::None,
}
)
Expand Down
13 changes: 6 additions & 7 deletions src/primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ where
I: Input<'a>,
I::Token: Clone + 'a,
E: ParserExtra<'a, I>,
F: Fn(I::Token, I::Span) -> Option<O>,
F: Fn(I::Token, &mut MapExtra<'a, '_, '_, I, E>) -> Option<O>,
{
Select {
filter,
Expand All @@ -452,15 +452,15 @@ where
I: ValueInput<'a>,
I::Token: Clone + 'a,
E: ParserExtra<'a, I>,
F: Fn(I::Token, I::Span) -> Option<O>,
F: Fn(I::Token, &mut MapExtra<'a, '_, '_, I, E>) -> Option<O>,
{
#[inline]
fn go<M: Mode>(&self, inp: &mut InputRef<'a, '_, I, E>) -> PResult<M, O> {
let before = inp.offset();
let next = inp.next_inner();
let err_span = inp.span_since(before);
let (at, found) = match next {
(at, Some(tok)) => match (self.filter)(tok.clone(), inp.span_since(before)) {
(at, Some(tok)) => match (self.filter)(tok.clone(), &mut MapExtra { before, inp }) {
Some(out) => return Ok(M::bind(|| out)),
None => (at, Some(tok.into())),
},
Expand Down Expand Up @@ -496,7 +496,7 @@ where
I: BorrowInput<'a>,
I::Token: 'a,
E: ParserExtra<'a, I>,
F: Fn(&'a I::Token, I::Span) -> Option<O>,
F: Fn(&'a I::Token, &mut MapExtra<'a, '_, '_, I, E>) -> Option<O>,
{
SelectRef {
filter,
Expand All @@ -509,16 +509,15 @@ where
I: BorrowInput<'a>,
I::Token: 'a,
E: ParserExtra<'a, I>,
F: Fn(&'a I::Token, I::Span) -> Option<O>,
F: Fn(&'a I::Token, &mut MapExtra<'a, '_, '_, I, E>) -> Option<O>,
{
#[inline]
fn go<M: Mode>(&self, inp: &mut InputRef<'a, '_, I, E>) -> PResult<M, O> {
let before = inp.offset();
let next = inp.next_ref_inner();
let span = inp.span_since(before);
let err_span = inp.span_since(before);
let (at, found) = match next {
(at, Some(tok)) => match (self.filter)(tok, span) {
(at, Some(tok)) => match (self.filter)(tok, &mut MapExtra { before, inp }) {
Some(out) => return Ok(M::bind(|| out)),
None => (at, Some(tok.into())),
},
Expand Down

0 comments on commit e02f8de

Please sign in to comment.