]> gitweb.ps.run Git - toc/blob - antlr4-cpp-runtime-4.9.2-source/runtime/src/ANTLRErrorStrategy.h
add antlr source code and ReadMe
[toc] / antlr4-cpp-runtime-4.9.2-source / runtime / src / ANTLRErrorStrategy.h
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 #pragma once
7
8 #include "Token.h"
9
10 namespace antlr4 {
11
12   /// <summary>
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:
16   ///
17   /// <ul>
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>
22   /// </ul>
23   ///
24   /// Implementations of this interface report syntax errors by calling
25   /// <seealso cref="Parser#notifyErrorListeners"/>.
26   /// <p/>
27   /// TODO: what to do about lexers
28   /// </summary>
29   class ANTLR4CPP_PUBLIC ANTLRErrorStrategy {
30   public:
31
32     /// <summary>
33     /// Reset the error handler state for the specified {@code recognizer}. </summary>
34     /// <param name="recognizer"> the parser instance </param>
35     virtual ~ANTLRErrorStrategy();
36
37     virtual void reset(Parser *recognizer) = 0;
38
39     /**
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.
45      *
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>
48      *
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>
52      *
53      * @param recognizer the parser instance
54      * @throws RecognitionException if the error strategy was not able to
55      * recover from the unexpected input symbol
56      */
57     virtual Token* recoverInline(Parser *recognizer) = 0;
58
59     /// <summary>
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.
63     /// </summary>
64     /// <seealso cref= #reportError
65     /// </seealso>
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;
71
72     /// <summary>
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"/>.
76     /// <p/>
77     /// The generated code currently contains calls to <seealso cref="#sync"/> after
78     /// entering the decision state of a closure block ({@code (...)*} or
79     /// {@code (...)+}).
80     /// <p/>
81     /// For an implementation based on Jim Idle's "magic sync" mechanism, see
82     /// <seealso cref="DefaultErrorStrategy#sync"/>.
83     /// </summary>
84     /// <seealso cref= DefaultErrorStrategy#sync
85     /// </seealso>
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;
91
92     /// <summary>
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)}.
99     /// </summary>
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;
104
105     /// <summary>
106     /// This method is called by when the parser successfully matches an input
107     /// symbol.
108     /// </summary>
109     /// <param name="recognizer"> the parser instance </param>
110     virtual void reportMatch(Parser *recognizer) = 0;
111
112     /// <summary>
113     /// Report any kind of <seealso cref="RecognitionException"/>. This method is called by
114     /// the default exception handler generated for a rule method.
115     /// </summary>
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;
119   };
120
121 } // namespace antlr4