]> gitweb.ps.run Git - toc/blob - antlr4-cpp-runtime-4.9.2-source/runtime/src/BailErrorStrategy.cpp
add antlr source code and ReadMe
[toc] / antlr4-cpp-runtime-4.9.2-source / runtime / src / BailErrorStrategy.cpp
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
6 #include "Exceptions.h"
7 #include "ParserRuleContext.h"
8 #include "InputMismatchException.h"
9 #include "Parser.h"
10
11 #include "BailErrorStrategy.h"
12
13 using namespace antlr4;
14
15 void BailErrorStrategy::recover(Parser *recognizer, std::exception_ptr e) {
16   ParserRuleContext *context = recognizer->getContext();
17   do {
18     context->exception = e;
19     if (context->parent == nullptr)
20       break;
21     context = static_cast<ParserRuleContext *>(context->parent);
22   } while (true);
23
24   try {
25     std::rethrow_exception(e); // Throw the exception to be able to catch and rethrow nested.
26 #if defined(_MSC_FULL_VER) && _MSC_FULL_VER < 190023026
27   } catch (RecognitionException &inner) {
28     throw ParseCancellationException(inner.what());
29 #else
30   } catch (RecognitionException & /*inner*/) {
31     std::throw_with_nested(ParseCancellationException());
32 #endif
33   }
34 }
35
36 Token* BailErrorStrategy::recoverInline(Parser *recognizer)  {
37   InputMismatchException e(recognizer);
38   std::exception_ptr exception = std::make_exception_ptr(e);
39
40   ParserRuleContext *context = recognizer->getContext();
41   do {
42     context->exception = exception;
43     if (context->parent == nullptr)
44       break;
45     context = static_cast<ParserRuleContext *>(context->parent);
46   } while (true);
47
48   try {
49     throw e;
50 #if defined(_MSC_FULL_VER) && _MSC_FULL_VER < 190023026
51   } catch (InputMismatchException &inner) {
52     throw ParseCancellationException(inner.what());
53 #else
54   } catch (InputMismatchException & /*inner*/) {
55     std::throw_with_nested(ParseCancellationException());
56 #endif
57   }
58 }
59
60 void BailErrorStrategy::sync(Parser * /*recognizer*/) {
61 }