xref: /plugin/combo/vendor/antlr/antlr4-php-runtime/src/Error/ANTLRErrorStrategy.php (revision 37748cd8654635afbeca80942126742f0f4cc346)
1*37748cd8SNickeau<?php
2*37748cd8SNickeau
3*37748cd8SNickeaudeclare(strict_types=1);
4*37748cd8SNickeau
5*37748cd8SNickeaunamespace Antlr\Antlr4\Runtime\Error;
6*37748cd8SNickeau
7*37748cd8SNickeauuse Antlr\Antlr4\Runtime\Error\Exceptions\RecognitionException;
8*37748cd8SNickeauuse Antlr\Antlr4\Runtime\Parser;
9*37748cd8SNickeauuse Antlr\Antlr4\Runtime\Token;
10*37748cd8SNickeau
11*37748cd8SNickeau/**
12*37748cd8SNickeau * The interface for defining strategies to deal with syntax errors encountered
13*37748cd8SNickeau * during a parse by ANTLR-generated parsers. We distinguish between three
14*37748cd8SNickeau * different kinds of errors:
15*37748cd8SNickeau *
16*37748cd8SNickeau * - The parser could not figure out which path to take in the ATN (none of
17*37748cd8SNickeau *    the available alternatives could possibly match)
18*37748cd8SNickeau * - The current input does not match what we were looking for
19*37748cd8SNickeau * - A predicate evaluated to false
20*37748cd8SNickeau *
21*37748cd8SNickeau * Implementations of this interface report syntax errors by calling
22*37748cd8SNickeau * {@see Parser::notifyErrorListeners()}.
23*37748cd8SNickeau *
24*37748cd8SNickeau * TODO: what to do about lexers
25*37748cd8SNickeau */
26*37748cd8SNickeauinterface ANTLRErrorStrategy
27*37748cd8SNickeau{
28*37748cd8SNickeau    /**
29*37748cd8SNickeau     * Reset the error handler state for the specified `recognizer`.
30*37748cd8SNickeau     *
31*37748cd8SNickeau     * @param Parser $recognizer the parser instance
32*37748cd8SNickeau     */
33*37748cd8SNickeau    public function reset(Parser $recognizer) : void;
34*37748cd8SNickeau
35*37748cd8SNickeau    /**
36*37748cd8SNickeau     * This method is called when an unexpected symbol is encountered during an
37*37748cd8SNickeau     * inline match operation, such as {@see Parser::match()}. If the error
38*37748cd8SNickeau     * strategy successfully recovers from the match failure, this method
39*37748cd8SNickeau     * returns the {@see Token} instance which should be treated as the
40*37748cd8SNickeau     * successful result of the match.
41*37748cd8SNickeau     *
42*37748cd8SNickeau     * This method handles the consumption of any tokens - the caller should
43*37748cd8SNickeau     * not call {@see Parser::consume()} after a successful recovery.
44*37748cd8SNickeau     *
45*37748cd8SNickeau     * Note that the calling code will not report an error if this method
46*37748cd8SNickeau     * returns successfully. The error strategy implementation is responsible
47*37748cd8SNickeau     * for calling {@see Parser::notifyErrorListeners()} as appropriate.
48*37748cd8SNickeau     *
49*37748cd8SNickeau     * @param Parser $recognizer The parser instance
50*37748cd8SNickeau     *
51*37748cd8SNickeau     * @throws RecognitionException If the error strategy was not able to
52*37748cd8SNickeau     *                              recover from the unexpected input symbol.
53*37748cd8SNickeau     */
54*37748cd8SNickeau    public function recoverInline(Parser $recognizer) : Token;
55*37748cd8SNickeau
56*37748cd8SNickeau    /**
57*37748cd8SNickeau     * This method is called to recover from exception `e`. This method is
58*37748cd8SNickeau     * called after {@see ANTLRErrorStrategy::reportError()} by the default
59*37748cd8SNickeau     * exception handler generated for a rule method.
60*37748cd8SNickeau     *
61*37748cd8SNickeau     * @param Parser               $recognizer The parser instance
62*37748cd8SNickeau     * @param RecognitionException $e          The recognition exception to
63*37748cd8SNickeau     *                                         recover from
64*37748cd8SNickeau     *
65*37748cd8SNickeau     * @throws RecognitionException If the error strategy could not recover
66*37748cd8SNickeau     *                              from the recognition exception.
67*37748cd8SNickeau     *
68*37748cd8SNickeau     * @see ANTLRErrorStrategy::reportError
69*37748cd8SNickeau     */
70*37748cd8SNickeau    public function recover(Parser $recognizer, RecognitionException $e) : void;
71*37748cd8SNickeau
72*37748cd8SNickeau    /**
73*37748cd8SNickeau     * This method provides the error handler with an opportunity to handle
74*37748cd8SNickeau     * syntactic or semantic errors in the input stream before they result in a
75*37748cd8SNickeau     * {@see RecognitionException}.
76*37748cd8SNickeau     *
77*37748cd8SNickeau     * The generated code currently contains calls to
78*37748cd8SNickeau     * {@see ANTLRErrorStrategy::sync()} after entering the decision state
79*37748cd8SNickeau     * of a closure block (`(...)*` or `(...)+`).
80*37748cd8SNickeau     *
81*37748cd8SNickeau     * For an implementation based on Jim Idle's "magic sync" mechanism, see
82*37748cd8SNickeau     * {@see DefaultErrorStrategy::sync()}.
83*37748cd8SNickeau     *
84*37748cd8SNickeau     * @param Parser $recognizer the parser instance
85*37748cd8SNickeau     *
86*37748cd8SNickeau     * @throws RecognitionException If an error is detected by the error strategy
87*37748cd8SNickeau     *                              but cannot be automatically recovered at the
88*37748cd8SNickeau     *                              current state in the parsing process.
89*37748cd8SNickeau     *
90*37748cd8SNickeau     * @see DefaultErrorStrategy::sync()
91*37748cd8SNickeau     */
92*37748cd8SNickeau    public function sync(Parser $recognizer) : void;
93*37748cd8SNickeau
94*37748cd8SNickeau    /**
95*37748cd8SNickeau     * Tests whether or not `recognizer` is in the process of recovering
96*37748cd8SNickeau     * from an error. In error recovery mode, {@see Parser::consume()} adds
97*37748cd8SNickeau     * symbols to the parse tree by calling {@see Parser::createErrorNode()}
98*37748cd8SNickeau     * then {@see ParserRuleContext::addErrorNode()} instead of
99*37748cd8SNickeau     * {@see Parser::createTerminalNode()}.
100*37748cd8SNickeau     *
101*37748cd8SNickeau     * @param Parser $recognizer The parser instance.
102*37748cd8SNickeau     *
103*37748cd8SNickeau     * @return bool `true` if the parser is currently recovering from a parse
104*37748cd8SNickeau     *               error, otherwise `false`.
105*37748cd8SNickeau     */
106*37748cd8SNickeau    public function inErrorRecoveryMode(Parser $recognizer) : bool;
107*37748cd8SNickeau
108*37748cd8SNickeau    /**
109*37748cd8SNickeau     * This method is called by when the parser successfully matches an input symbol.
110*37748cd8SNickeau     *
111*37748cd8SNickeau     * @param Parser $recognizer The parser instance.
112*37748cd8SNickeau     */
113*37748cd8SNickeau    public function reportMatch(Parser $recognizer) : void;
114*37748cd8SNickeau
115*37748cd8SNickeau    /**
116*37748cd8SNickeau     * Report any kind of {@see RecognitionException}. This method is called by
117*37748cd8SNickeau     * the default exception handler generated for a rule method.
118*37748cd8SNickeau     *
119*37748cd8SNickeau     * @param Parser               $recognizer The parser instance.
120*37748cd8SNickeau     * @param RecognitionException $e          The recognition exception to report.
121*37748cd8SNickeau     */
122*37748cd8SNickeau    public function reportError(Parser $recognizer, RecognitionException $e) : void;
123*37748cd8SNickeau}
124