2 * Contains the definition of a basic ANTLR3 exception structure created
3 * by a recognizer when errors are found/predicted.
5 #ifndef _ANTLR3_EXCEPTION_H
6 #define _ANTLR3_EXCEPTION_H
9 // Copyright (c) 2005-2009 Jim Idle, Temporal Wave LLC
10 // http://www.temporal-wave.com
11 // http://www.linkedin.com/in/jimidle
13 // All rights reserved.
15 // Redistribution and use in source and binary forms, with or without
16 // modification, are permitted provided that the following conditions
18 // 1. Redistributions of source code must retain the above copyright
19 // notice, this list of conditions and the following disclaimer.
20 // 2. Redistributions in binary form must reproduce the above copyright
21 // notice, this list of conditions and the following disclaimer in the
22 // documentation and/or other materials provided with the distribution.
23 // 3. The name of the author may not be used to endorse or promote products
24 // derived from this software without specific prior written permission.
26 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
27 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29 // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
30 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31 // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 #include <antlr3defs.h>
39 /** Indicates that the recognizer received a token
40 * in the input that was not predicted.
42 #define ANTLR3_RECOGNITION_EXCEPTION 1
44 /** Name of exception #ANTLR3_RECOGNITION_EXCEPTION
46 #define ANTLR3_RECOGNITION_EX_NAME "Recognition Exception"
48 /** Indicates that the recognizer was expecting one token and found a
51 #define ANTLR3_MISMATCHED_TOKEN_EXCEPTION 2
53 /** Name of #ANTLR3_MISMATCHED_TOKEN_EXCEPTION
55 #define ANTLR3_MISMATCHED_EX_NAME "Mismatched Token Exception"
57 /** Recognizer could not find a valid alternative from the input
59 #define ANTLR3_NO_VIABLE_ALT_EXCEPTION 3
61 /** Name of #ANTLR3_NO_VIABLE_ALT_EXCEPTION
63 #define ANTLR3_NO_VIABLE_ALT_NAME "No Viable Alt"
65 /* Character in a set was not found
67 #define ANTLR3_MISMATCHED_SET_EXCEPTION 4
69 /* Name of #ANTLR3_MISMATCHED_SET_EXCEPTION
71 #define ANTLR3_MISMATCHED_SET_NAME "Mismatched set"
73 /* A rule predicting at least n elements found less than that,
76 #define ANTLR3_EARLY_EXIT_EXCEPTION 5
78 /* Name of #ANTLR3_EARLY_EXIT_EXCEPTION
80 #define ANTLR3_EARLY_EXIT_NAME "Early exit"
82 #define ANTLR3_FAILED_PREDICATE_EXCEPTION 6
83 #define ANTLR3_FAILED_PREDICATE_NAME "Predicate failed!"
85 #define ANTLR3_MISMATCHED_TREE_NODE_EXCEPTION 7
86 #define ANTLR3_MISMATCHED_TREE_NODE_NAME "Mismatched tree node!"
88 #define ANTLR3_REWRITE_EARLY_EXCEPTION 8
89 #define ANTLR3_REWRITE_EARLY_EXCEPTION_NAME "Mismatched tree node!"
91 #define ANTLR3_UNWANTED_TOKEN_EXCEPTION 9
92 #define ANTLR3_UNWANTED_TOKEN_EXCEPTION_NAME "Extraneous token"
94 #define ANTLR3_MISSING_TOKEN_EXCEPTION 10
95 #define ANTLR3_MISSING_TOKEN_EXCEPTION_NAME "Missing token"
101 /** Base structure for an ANTLR3 exception tracker
103 typedef struct ANTLR3_EXCEPTION_struct
105 /// Set to one of the exception type defines:
107 /// - #ANTLR3_RECOGNITION_EXCEPTION
108 /// - #ANTLR3_MISMATCHED_TOKEN_EXCEPTION
109 /// - #ANTLR3_NO_VIABLE_ALT_EXCEPTION
110 /// - #ANTLR3_MISMATCHED_SET_EXCEPTION
111 /// - #ANTLR3_EARLY_EXIT_EXCEPTION
112 /// - #ANTLR3_FAILED_PREDICATE_EXCEPTION
113 /// - #ANTLR3_EARLY_EXIT_EXCEPTION
117 /** The string name of the exception
121 /** The printable message that goes with this exception, in your preferred
122 * encoding format. ANTLR just uses ASCII by default but you can ignore these
123 * messages or convert them to another format or whatever of course. They are
124 * really internal messages that you then decide how to print out in a form that
125 * the users of your product will understand, as they are unlikely to know what
126 * to do with "Recognition exception at: [[TOK_GERUND..... " ;-)
130 /** Name of the file/input source for reporting. Note that this may be NULL!!
132 pANTLR3_STRING streamName
;
134 /** If set to ANTLR3_TRUE, this indicates that the message element of this structure
135 * should be freed by calling ANTLR3_FREE() when the exception is destroyed.
137 ANTLR3_BOOLEAN freeMessage
;
139 /** Indicates the index of the 'token' we were looking at when the
140 * exception occurred.
144 /** Indicates what the current token/tree was when the error occurred. Since not
145 * all input streams will be able to retrieve the nth token, we track it here
146 * instead. This is for parsers, and even tree parsers may set this.
150 /** Indicates the token we were expecting to see next when the error occurred
152 ANTLR3_UINT32 expecting
;
154 /** Indicates a set of tokens that we were expecting to see one of when the
155 * error occurred. It is a following bitset list, so you can use load it and use ->toIntList() on it
156 * to generate an array of integer tokens that it represents.
158 pANTLR3_BITSET_LIST expectingSet
;
160 /** If this is a tree parser exception then the node is set to point to the node
161 * that caused the issue.
165 /** The current character when an error occurred - for lexers.
169 /** Track the line at which the error occurred in case this is
170 * generated from a lexer. We need to track this since the
171 * unexpected char doesn't carry the line info.
175 /** Character position in the line where the error occurred.
177 ANTLR3_INT32 charPositionInLine
;
179 /** decision number for NVE
181 ANTLR3_UINT32 decisionNum
;
187 /** Rule name for failed predicate exception
191 /** Pointer to the next exception in the chain (if any)
193 struct ANTLR3_EXCEPTION_struct
* nextException
;
195 /** Pointer to the input stream that this exception occurred in.
197 pANTLR3_INT_STREAM input
;
199 /** Pointer for you, the programmer to add anything you like to an exception.
203 /** Pointer to a routine that is called to free the custom exception structure
204 * when the exception is destroyed. Set to NULL if nothing should be done.
206 void (*freeCustom
) (void * custom
);
207 void (*print
) (struct ANTLR3_EXCEPTION_struct
* ex
);
208 void (*freeEx
) (struct ANTLR3_EXCEPTION_struct
* ex
);