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 "DefaultErrorStrategy.h"
13 * This implementation of {@link ANTLRErrorStrategy} responds to syntax errors
14 * by immediately canceling the parse operation with a
15 * {@link ParseCancellationException}. The implementation ensures that the
16 * {@link ParserRuleContext#exception} field is set for all parse tree nodes
17 * that were not completed prior to encountering the error.
20 * This error strategy is useful in the following scenarios.</p>
23 * <li><strong>Two-stage parsing:</strong> This error strategy allows the first
24 * stage of two-stage parsing to immediately terminate if an error is
25 * encountered, and immediately fall back to the second stage. In addition to
26 * avoiding wasted work by attempting to recover from errors here, the empty
27 * implementation of {@link BailErrorStrategy#sync} improves the performance of
28 * the first stage.</li>
29 * <li><strong>Silent validation:</strong> When syntax errors are not being
30 * reported or logged, and the parse result is simply ignored if errors occur,
31 * the {@link BailErrorStrategy} avoids wasting work on recovering from errors
32 * when the result will be ignored either way.</li>
36 * {@code myparser.setErrorHandler(new BailErrorStrategy());}</p>
38 * @see Parser#setErrorHandler(ANTLRErrorStrategy)
40 class ANTLR4CPP_PUBLIC BailErrorStrategy : public DefaultErrorStrategy {
42 /// Instead of recovering from exception {@code e}, re-throw it wrapped
43 /// in a <seealso cref="ParseCancellationException"/> so it is not caught by the
44 /// rule function catches. Use <seealso cref="Exception#getCause()"/> to get the
45 /// original <seealso cref="RecognitionException"/>.
48 virtual void recover(Parser *recognizer, std::exception_ptr e) override;
50 /// Make sure we don't attempt to recover inline; if the parser
51 /// successfully recovers, it won't throw an exception.
52 virtual Token* recoverInline(Parser *recognizer) override;
55 /// Make sure we don't attempt to recover from problems in subrules. </summary>
56 virtual void sync(Parser *recognizer) override;