* * `$myparser->setErrorHandler(new BailErrorStrategy());` * * @see Parser::setErrorHandler() */ class BailErrorStrategy extends DefaultErrorStrategy { /** * Instead of recovering from exception `e`, re-throw it wrapped * in a {@see ParseCancellationException} so it is not caught by the * rule function catches. Use {@see Exception::getCause()} to get the * original {@see RecognitionException}. */ public function recover(Parser $recognizer, RecognitionException $e) : void { $context = $recognizer->getContext(); while ($context !== null) { if (!$context instanceof ParserRuleContext) { throw new \RuntimeException('Unexpected context type.'); } $context->exception = $e; $context = $context->getParent(); } throw ParseCancellationException::from($e); } /** * Make sure we don't attempt to recover inline; if the parser successfully * recovers, it won't throw an exception. * * @throws ParseCancellationException */ public function recoverInline(Parser $recognizer) : Token { $e = new InputMismatchException($recognizer); for ($context = $recognizer->getContext(); $context; $context = $context->getParent()) { if (!$context instanceof ParserRuleContext) { throw new \RuntimeException('Unexpected context type.'); } $context->exception = $e; } throw ParseCancellationException::from($e); } /** * Make sure we don't attempt to recover from problems in subrules. */ public function sync(Parser $recognizer) : void { } }