1 /*
2  * Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
3  * Use of this file is governed by the BSD 3-clause license that
4  * can be found in the LICENSE.txt file in the project root.
5  */
6 
7 module antlr.v4.runtime.RecognitionException;
8 
9 import antlr.v4.runtime.RuntimeException;
10 import antlr.v4.runtime.InterfaceRecognizer;
11 import antlr.v4.runtime.RuleContext;
12 import antlr.v4.runtime.Token;
13 import antlr.v4.runtime.IntStream;
14 import antlr.v4.runtime.ParserRuleContext;
15 import antlr.v4.runtime.misc.IntervalSet;
16 
17 // Class RecognitionException
18 /**
19  * The root of the ANTLR exception hierarchy. In general, ANTLR tracks just
20  * 3 kinds of errors: prediction errors, failed predicate errors, and
21  * mismatched input errors. In each case, the parser knows where it is
22  * in the input, where it is in the ATN, the rule invocation stack,
23  * and what kind of problem occurred.
24  */
25 class RecognitionException : RuntimeException
26 {
27 
28     /**
29      * The {@link Recognizer} where this exception originated.
30      */
31     public InterfaceRecognizer recognizer;
32 
33     private RuleContext ctx;
34 
35     private IntStream input;
36 
37     /**
38      * The current {@link Token} when an error occurred. Since not all streams
39      * support accessing symbols by index, we have to track the {@link Token}
40      * instance itself.
41      */
42     private Token offendingToken;
43 
44     private int offendingState = -1;
45 
46     public this(InterfaceRecognizer recognizer, IntStream input, ParserRuleContext ctx)
47     {
48         this.recognizer = recognizer;
49         this.input = input;
50         this.ctx = ctx;
51         if (recognizer)
52             this.offendingState = recognizer.getState;
53     }
54 
55     public this(string message, InterfaceRecognizer recognizer, IntStream input, ParserRuleContext ctx)
56     {
57         super(message);
58         this.recognizer = recognizer;
59         this.input = input;
60         this.ctx = ctx;
61         if (recognizer)
62             this.offendingState = recognizer.getState;
63     }
64 
65     /**
66      * Get the ATN state number the parser was in at the time the error
67      * occurred. For {@link NoViableAltException} and
68      * {@link LexerNoViableAltException} exceptions, this is the
69      * {@link DecisionState} number. For others, it is the state whose outgoing
70      * edge we couldn't match.
71      *
72      * <p>If the state number is not known, this method returns -1.</p>
73      * @uml
74      * Get the ATN state number the parser was in at the time the error
75      * occurred. For {@link NoViableAltException} and
76      * {@link LexerNoViableAltException} exceptions, this is the
77      * {@link DecisionState} number. For others, it is the state whose outgoing
78      * edge we couldn't match.
79      *
80      * <p>If the state number is not known, this method returns -1.</p>
81      */
82     public int getOffendingState()
83     {
84         return offendingState;
85     }
86 
87     protected void setOffendingState(int offendingState)
88     {
89         this.offendingState = offendingState;
90     }
91 
92     /**
93      * Gets the set of input symbols which could potentially follow the
94      * previously matched symbol at the time this exception was thrown.
95      *
96      * <p>If the set of expected tokens is not known and could not be computed,
97      * this method returns {@code null}.</p>
98      *
99      *  @return The set of token types that could potentially follow the current
100      * state in the ATN, or {@code null} if the information is not available.
101      * @uml
102      * Gets the set of input symbols which could potentially follow the
103      * previously matched symbol at the time this exception was thrown.
104      *
105      * <p>If the set of expected tokens is not known and could not be computed,
106      * this method returns {@code null}.</p>
107      *
108      *  @return The set of token types that could potentially follow the current
109      * state in the ATN, or {@code null} if the information is not available.
110      */
111     public IntervalSet getExpectedTokens()
112     {
113         if (recognizer) {
114             return recognizer.getATN().getExpectedTokens(offendingState, ctx);
115         }
116         return null;
117     }
118 
119     /**
120      * Gets the {@link RuleContext} at the time this exception was thrown.
121      *
122      * <p>If the context is not available, this method returns {@code null}.</p>
123      *
124      *  @return The {@link RuleContext} at the time this exception was thrown.
125      * If the context is not available, this method returns {@code null}.
126      * @uml
127      * Gets the {@link RuleContext} at the time this exception was thrown.
128      *
129      * <p>If the context is not available, this method returns {@code null}.</p>
130      *
131      *  @return The {@link RuleContext} at the time this exception was thrown.
132      * If the context is not available, this method returns {@code null}.
133      */
134     public RuleContext getCtx()
135     {
136         return ctx;
137     }
138 
139     public IntStream getInputStream()
140     {
141         return input;
142     }
143 
144     public Token getOffendingToken()
145     {
146         return offendingToken;
147     }
148 
149     public void setOffendingToken(Token offendingToken)
150     {
151         this.offendingToken = offendingToken;
152     }
153 
154     /**
155      * Gets the {@link Recognizer} where this exception occurred.
156      *
157      * <p>If the recognizer is not available, this method returns {@code null}.</p>
158      *
159      *  @return The recognizer where this exception occurred, or {@code null} if
160      *  the recognizer is not available.
161      * @uml
162      * Gets the {@link Recognizer} where this exception occurred.
163      *
164      * <p>If the recognizer is not available, this method returns {@code null}.</p>
165      *
166      *  @return The recognizer where this exception occurred, or {@code null} if
167      *  the recognizer is not available.
168      */
169     public InterfaceRecognizer getRecognizer()
170     {
171         return recognizer;
172     }
173 
174 }