use super::*;
impl<'source, 'ast, 'name, 'names> Parser<'source, 'ast, 'name, 'names>
where
'name: 'ast,
{
pub(in crate::parser) fn recover_match_on_line(
&mut self,
expected: Token<'source, 'ast>,
) -> bool {
let expected_kind = expected.kind();
let line = self.previous_token_end_position().line;
while self.current.kind() != TokenKind::Eof
&& self.current_token_location().begin.line == line
{
if self.current.kind() == expected_kind {
self.advance();
return true;
}
if self.match_recovery_stops_at_current() {
return false;
}
self.advance();
}
false
}
pub(in crate::parser) fn with_match_recovery_stop<T>(
&mut self,
stop: MatchRecoveryStop,
parse: impl FnOnce(&mut Self) -> Result<T>,
) -> Result<T> {
self.push_match_recovery_stop(stop);
let result = parse(self);
self.pop_match_recovery_stop(stop);
result
}
pub(in crate::parser) fn push_match_recovery_stop(&mut self, stop: MatchRecoveryStop) {
self.recovery.match_recovery_stops[stop.index()] += 1;
}
pub(in crate::parser) fn pop_match_recovery_stop(&mut self, stop: MatchRecoveryStop) {
let slot = &mut self.recovery.match_recovery_stops[stop.index()];
debug_assert!(*slot != 0, "parser match recovery stop is balanced");
*slot -= 1;
}
pub(in crate::parser) fn match_recovery_stops_at_current(&self) -> bool {
match self.current.kind() {
TokenKind::Equal => {
self.recovery.match_recovery_stops[MatchRecoveryStop::Equal.index()] != 0
}
TokenKind::RightParen => {
self.recovery.match_recovery_stops[MatchRecoveryStop::RightParen.index()] != 0
}
TokenKind::Reserved(R::End) => {
self.recovery.match_recovery_stops[MatchRecoveryStop::ReservedEnd.index()] != 0
}
TokenKind::SkinnyArrow => {
self.recovery.match_recovery_stops[MatchRecoveryStop::SkinnyArrow.index()] != 0
}
_ => false,
}
}
}