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.
8 #include "Exceptions.h"
12 /// The root of the ANTLR exception hierarchy. In general, ANTLR tracks just
13 /// 3 kinds of errors: prediction errors, failed predicate errors, and
14 /// mismatched input errors. In each case, the parser knows where it is
15 /// in the input, where it is in the ATN, the rule invocation stack,
16 /// and what kind of problem occurred.
17 class ANTLR4CPP_PUBLIC RecognitionException : public RuntimeException {
19 /// The Recognizer where this exception originated.
20 Recognizer *_recognizer;
22 ParserRuleContext *_ctx;
24 /// The current Token when an error occurred. Since not all streams
25 /// support accessing symbols by index, we have to track the Token
27 Token *_offendingToken;
29 size_t _offendingState;
32 RecognitionException(Recognizer *recognizer, IntStream *input, ParserRuleContext *ctx,
33 Token *offendingToken = nullptr);
34 RecognitionException(const std::string &message, Recognizer *recognizer, IntStream *input,
35 ParserRuleContext *ctx, Token *offendingToken = nullptr);
36 RecognitionException(RecognitionException const&) = default;
37 ~RecognitionException();
38 RecognitionException& operator=(RecognitionException const&) = default;
40 /// Get the ATN state number the parser was in at the time the error
41 /// occurred. For NoViableAltException and
42 /// LexerNoViableAltException exceptions, this is the
43 /// DecisionState number. For others, it is the state whose outgoing
44 /// edge we couldn't match.
46 /// If the state number is not known, this method returns -1.
47 virtual size_t getOffendingState() const;
50 void setOffendingState(size_t offendingState);
52 /// Gets the set of input symbols which could potentially follow the
53 /// previously matched symbol at the time this exception was thrown.
55 /// If the set of expected tokens is not known and could not be computed,
56 /// this method returns an empty set.
58 /// @returns The set of token types that could potentially follow the current
59 /// state in the ATN, or an empty set if the information is not available.
61 virtual misc::IntervalSet getExpectedTokens() const;
64 /// Gets the <seealso cref="RuleContext"/> at the time this exception was thrown.
66 /// If the context is not available, this method returns {@code null}.
68 /// <returns> The <seealso cref="RuleContext"/> at the time this exception was thrown.
69 /// If the context is not available, this method returns {@code null}. </returns>
70 virtual RuleContext* getCtx() const;
73 /// Gets the input stream which is the symbol source for the recognizer where
74 /// this exception was thrown.
76 /// If the input stream is not available, this method returns {@code null}.
78 /// <returns> The input stream which is the symbol source for the recognizer
79 /// where this exception was thrown, or {@code null} if the stream is not
80 /// available. </returns>
81 virtual IntStream* getInputStream() const;
83 virtual Token* getOffendingToken() const;
86 /// Gets the <seealso cref="Recognizer"/> where this exception occurred.
88 /// If the recognizer is not available, this method returns {@code null}.
90 /// <returns> The recognizer where this exception occurred, or {@code null} if
91 /// the recognizer is not available. </returns>
92 virtual Recognizer* getRecognizer() const;
95 void InitializeInstanceFields();