BailErrorStrategy.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /* Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
  2. * Use of this file is governed by the BSD 3-clause license that
  3. * can be found in the LICENSE.txt file in the project root.
  4. */
  5. #include "Exceptions.h"
  6. #include "ParserRuleContext.h"
  7. #include "InputMismatchException.h"
  8. #include "Parser.h"
  9. #include "BailErrorStrategy.h"
  10. using namespace antlr4;
  11. void BailErrorStrategy::recover(Parser *recognizer, std::exception_ptr e) {
  12. ParserRuleContext *context = recognizer->getContext();
  13. do {
  14. context->exception = e;
  15. if (context->parent == nullptr)
  16. break;
  17. context = static_cast<ParserRuleContext *>(context->parent);
  18. } while (true);
  19. try {
  20. std::rethrow_exception(e); // Throw the exception to be able to catch and rethrow nested.
  21. #if defined(_MSC_FULL_VER) && _MSC_FULL_VER < 190023026
  22. } catch (RecognitionException &inner) {
  23. throw ParseCancellationException(inner.what());
  24. #else
  25. } catch (RecognitionException & /*inner*/) {
  26. std::throw_with_nested(ParseCancellationException());
  27. #endif
  28. }
  29. }
  30. Token* BailErrorStrategy::recoverInline(Parser *recognizer) {
  31. InputMismatchException e(recognizer);
  32. std::exception_ptr exception = std::make_exception_ptr(e);
  33. ParserRuleContext *context = recognizer->getContext();
  34. do {
  35. context->exception = exception;
  36. if (context->parent == nullptr)
  37. break;
  38. context = static_cast<ParserRuleContext *>(context->parent);
  39. } while (true);
  40. try {
  41. throw e;
  42. #if defined(_MSC_FULL_VER) && _MSC_FULL_VER < 190023026
  43. } catch (InputMismatchException &inner) {
  44. throw ParseCancellationException(inner.what());
  45. #else
  46. } catch (InputMismatchException & /*inner*/) {
  47. std::throw_with_nested(ParseCancellationException());
  48. #endif
  49. }
  50. }
  51. void BailErrorStrategy::sync(Parser * /*recognizer*/) {
  52. }