2 * Contains default functions for creating and destroying as well as
\r
3 * otherwise handling ANTLR3 standard exception structures.
\r
6 // [The "BSD licence"]
\r
7 // Copyright (c) 2005-2009 Jim Idle, Temporal Wave LLC
\r
8 // http://www.temporal-wave.com
\r
9 // http://www.linkedin.com/in/jimidle
\r
11 // All rights reserved.
\r
13 // Redistribution and use in source and binary forms, with or without
\r
14 // modification, are permitted provided that the following conditions
\r
16 // 1. Redistributions of source code must retain the above copyright
\r
17 // notice, this list of conditions and the following disclaimer.
\r
18 // 2. Redistributions in binary form must reproduce the above copyright
\r
19 // notice, this list of conditions and the following disclaimer in the
\r
20 // documentation and/or other materials provided with the distribution.
\r
21 // 3. The name of the author may not be used to endorse or promote products
\r
22 // derived from this software without specific prior written permission.
\r
24 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
\r
25 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
\r
26 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
\r
27 // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
\r
28 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
\r
29 // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
\r
30 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
\r
31 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
\r
32 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
\r
33 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\r
35 #include <antlr3exception.h>
\r
37 static void antlr3ExceptionPrint(pANTLR3_EXCEPTION ex);
\r
38 static void antlr3ExceptionFree (pANTLR3_EXCEPTION ex);
\r
42 * Creates a new ANTLR3 exception structure
\r
44 * \param[in] exception
\r
45 * One of the ANTLR3_xxx_EXCEPTION indicators such as #ANTLR3_RECOGNITION_EXCEPTION
\r
47 * \param[in] message
\r
48 * Pointer to message string
\r
50 * \param[in] freeMessage
\r
51 * Set to ANTLR3_TRUE if the message parameter should be freed by a call to
\r
52 * ANTLR3_FREE() when the exception is destroyed.
\r
55 * Pointer to newly initialized exception structure, or an ANTLR3_ERR_xx defined value
\r
58 * An exception is 'thrown' by a recognizer when input is seen that is not predicted by
\r
59 * the grammar productions or when some other error condition occurs. In C we do not have
\r
60 * the luxury of try and catch blocks, so exceptions are added in the order they occur to
\r
61 * a list in the baserecognizer structure. The last one to be thrown is inserted at the head of
\r
62 * the list and the one currently installed is pointed to by the newly installed exception.
\r
65 * After an exception is created, you may add a pointer to your own structure and a pointer
\r
66 * to a function to free this structure when the exception is destroyed.
\r
72 antlr3ExceptionNew(ANTLR3_UINT32 exception, void * name, void * message, ANTLR3_BOOLEAN freeMessage)
\r
74 pANTLR3_EXCEPTION ex;
\r
76 /* Allocate memory for the structure
\r
78 ex = (pANTLR3_EXCEPTION) ANTLR3_CALLOC(1, sizeof(ANTLR3_EXCEPTION));
\r
80 /* Check for memory allocation
\r
87 ex->name = name; /* Install exception name */
\r
88 ex->type = exception; /* Install the exception number */
\r
89 ex->message = message; /* Install message string */
\r
91 /* Indicate whether the string should be freed if exception is destroyed
\r
93 ex->freeMessage = freeMessage;
\r
97 ex->print = antlr3ExceptionPrint;
\r
98 ex->freeEx = antlr3ExceptionFree;
\r
105 * Prints out the message in all the exceptions in the supplied chain.
\r
108 * Pointer to the exception structure to print.
\r
111 * You may wish to override this function by installing a pointer to a new function
\r
112 * in the base recognizer context structure.
\r
115 * ANTLR3_BASE_RECOGNIZER
\r
118 antlr3ExceptionPrint(pANTLR3_EXCEPTION ex)
\r
120 /* Ensure valid pointer
\r
124 /* Number if no message, else the message
\r
126 if (ex->message == NULL)
\r
128 ANTLR3_FPRINTF(stderr, "ANTLR3_EXCEPTION number %d (%08X).\n", ex->type, ex->type);
\r
132 ANTLR3_FPRINTF(stderr, "ANTLR3_EXCEPTION: %s\n", (char *)(ex->message));
\r
135 /* Move to next in the chain (if any)
\r
137 ex = ex->nextException;
\r
145 * Frees up a chain of ANTLR3 exceptions
\r
148 * Pointer to the first exception in the chain to free.
\r
154 antlr3ExceptionFree(pANTLR3_EXCEPTION ex)
\r
156 pANTLR3_EXCEPTION next;
\r
158 /* Ensure valid pointer
\r
162 /* Pick up anythign following now, before we free the
\r
163 * current memory block.
\r
165 next = ex->nextException;
\r
167 /* Free the message pointer if advised to
\r
169 if (ex->freeMessage == ANTLR3_TRUE)
\r
171 ANTLR3_FREE(ex->message);
\r
174 /* Call the programmer's custom free routine if advised to
\r
176 if (ex->freeCustom != NULL)
\r
178 ex->freeCustom(ex->custom);
\r
181 /* Free the actual structure itself
\r