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.
13 /// The interface for defining strategies to deal with syntax errors encountered
14 /// during a parse by ANTLR-generated parsers. We distinguish between three
15 /// different kinds of errors:
18 /// <li>The parser could not figure out which path to take in the ATN (none of
19 /// the available alternatives could possibly match)</li>
20 /// <li>The current input does not match what we were looking for</li>
21 /// <li>A predicate evaluated to false</li>
24 /// Implementations of this interface report syntax errors by calling
25 /// <seealso cref="Parser#notifyErrorListeners"/>.
27 /// TODO: what to do about lexers
29 class ANTLR4CPP_PUBLIC ANTLRErrorStrategy {
33 /// Reset the error handler state for the specified {@code recognizer}. </summary>
34 /// <param name="recognizer"> the parser instance </param>
35 virtual ~ANTLRErrorStrategy();
37 virtual void reset(Parser *recognizer) = 0;
40 * This method is called when an unexpected symbol is encountered during an
41 * inline match operation, such as {@link Parser#match}. If the error
42 * strategy successfully recovers from the match failure, this method
43 * returns the {@link Token} instance which should be treated as the
44 * successful result of the match.
46 * <p>This method handles the consumption of any tokens - the caller should
47 * <b>not</b> call {@link Parser#consume} after a successful recovery.</p>
49 * <p>Note that the calling code will not report an error if this method
50 * returns successfully. The error strategy implementation is responsible
51 * for calling {@link Parser#notifyErrorListeners} as appropriate.</p>
53 * @param recognizer the parser instance
54 * @throws RecognitionException if the error strategy was not able to
55 * recover from the unexpected input symbol
57 virtual Token* recoverInline(Parser *recognizer) = 0;
60 /// This method is called to recover from exception {@code e}. This method is
61 /// called after <seealso cref="#reportError"/> by the default exception handler
62 /// generated for a rule method.
64 /// <seealso cref= #reportError
66 /// <param name="recognizer"> the parser instance </param>
67 /// <param name="e"> the recognition exception to recover from </param>
68 /// <exception cref="RecognitionException"> if the error strategy could not recover from
69 /// the recognition exception </exception>
70 virtual void recover(Parser *recognizer, std::exception_ptr e) = 0;
73 /// This method provides the error handler with an opportunity to handle
74 /// syntactic or semantic errors in the input stream before they result in a
75 /// <seealso cref="RecognitionException"/>.
77 /// The generated code currently contains calls to <seealso cref="#sync"/> after
78 /// entering the decision state of a closure block ({@code (...)*} or
81 /// For an implementation based on Jim Idle's "magic sync" mechanism, see
82 /// <seealso cref="DefaultErrorStrategy#sync"/>.
84 /// <seealso cref= DefaultErrorStrategy#sync
86 /// <param name="recognizer"> the parser instance </param>
87 /// <exception cref="RecognitionException"> if an error is detected by the error
88 /// strategy but cannot be automatically recovered at the current state in
89 /// the parsing process </exception>
90 virtual void sync(Parser *recognizer) = 0;
93 /// Tests whether or not {@code recognizer} is in the process of recovering
94 /// from an error. In error recovery mode, <seealso cref="Parser#consume"/> adds
95 /// symbols to the parse tree by calling
96 /// {@link Parser#createErrorNode(ParserRuleContext, Token)} then
97 /// {@link ParserRuleContext#addErrorNode(ErrorNode)} instead of
98 /// {@link Parser#createTerminalNode(ParserRuleContext, Token)}.
100 /// <param name="recognizer"> the parser instance </param>
101 /// <returns> {@code true} if the parser is currently recovering from a parse
102 /// error, otherwise {@code false} </returns>
103 virtual bool inErrorRecoveryMode(Parser *recognizer) = 0;
106 /// This method is called by when the parser successfully matches an input
109 /// <param name="recognizer"> the parser instance </param>
110 virtual void reportMatch(Parser *recognizer) = 0;
113 /// Report any kind of <seealso cref="RecognitionException"/>. This method is called by
114 /// the default exception handler generated for a rule method.
116 /// <param name="recognizer"> the parser instance </param>
117 /// <param name="e"> the recognition exception to report </param>
118 virtual void reportError(Parser *recognizer, const RecognitionException &e) = 0;
121 } // namespace antlr4