use super::Parser;
use crate::error::ParseResult;
use crate::token::TokenType;
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub(super) enum BracketProof {
RightBracketClosesTheComprehension,
RightBracketClosesWithTheMapOmitted,
InnerBracketOnly,
}
impl<'a> Parser<'a> {
pub(super) fn with_comprehension_where<T>(
&mut self,
context: Option<BracketProof>,
f: impl FnOnce(&mut Self) -> T,
) -> T {
let saved = self.comprehension_where;
self.comprehension_where = context;
let result = f(self);
self.comprehension_where = saved;
result
}
pub(super) fn ambiguous_pipe_continuation<T>(
&mut self,
parse: impl FnOnce(&mut Self) -> ParseResult<T>,
tail_ends_where: impl Fn(&Self, TokenType) -> bool,
) -> ParseResult<Option<T>> {
debug_assert_eq!(self.peek_type(), TokenType::Pipe);
let pipe_saved_pos = self.save_pos();
let pipe_saved_flags = self.side_effects;
self.next_token(); let continuation = parse(self);
let not_a_continuation = match &continuation {
Ok(_) => tail_ends_where(self, self.peek_type()),
Err(_) => true,
};
if self.comprehension_where.is_some() && not_a_continuation {
self.restore_pos(pipe_saved_pos);
self.side_effects = pipe_saved_flags;
return Ok(None);
}
Ok(Some(continuation?))
}
}