]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/cpp/DataBoardTest/libantlr3c-3.2/src/antlr3exception.c
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.databoard / cpp / DataBoardTest / libantlr3c-3.2 / src / antlr3exception.c
1 /** \file\r
2  * Contains default functions for creating and destroying as well as\r
3  * otherwise handling ANTLR3 standard exception structures.\r
4  */\r
5 \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
10 //\r
11 // All rights reserved.\r
12 //\r
13 // Redistribution and use in source and binary forms, with or without\r
14 // modification, are permitted provided that the following conditions\r
15 // are met:\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
23 //\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
34 \r
35 #include    <antlr3exception.h>\r
36 \r
37 static    void  antlr3ExceptionPrint(pANTLR3_EXCEPTION ex);\r
38 static    void  antlr3ExceptionFree (pANTLR3_EXCEPTION ex);\r
39 \r
40 /**\r
41  * \brief\r
42  * Creates a new ANTLR3 exception structure\r
43  * \r
44  * \param[in] exception\r
45  * One of the ANTLR3_xxx_EXCEPTION indicators such as #ANTLR3_RECOGNITION_EXCEPTION\r
46  * \r
47  * \param[in] message\r
48  * Pointer to message string \r
49  * \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
53  * \r
54  * \returns\r
55  * Pointer to newly initialized exception structure, or an ANTLR3_ERR_xx defined value\r
56  * upon failure.\r
57  * \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
63  * \r
64  * \remarks\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
67  * \r
68  * \see\r
69  * ANTLR3_EXCEPTION\r
70  */\r
71 pANTLR3_EXCEPTION\r
72 antlr3ExceptionNew(ANTLR3_UINT32 exception, void * name, void * message, ANTLR3_BOOLEAN freeMessage)\r
73 {\r
74         pANTLR3_EXCEPTION       ex;\r
75 \r
76         /* Allocate memory for the structure\r
77         */\r
78         ex      = (pANTLR3_EXCEPTION) ANTLR3_CALLOC(1, sizeof(ANTLR3_EXCEPTION));\r
79 \r
80         /* Check for memory allocation\r
81         */\r
82         if      (ex == NULL)\r
83         {\r
84                 return  NULL;\r
85         }\r
86 \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
90 \r
91         /* Indicate whether the string should be freed if exception is destroyed    \r
92         */\r
93         ex->freeMessage = freeMessage;\r
94 \r
95         /* Install the API\r
96         */\r
97         ex->print           =  antlr3ExceptionPrint;\r
98         ex->freeEx          =  antlr3ExceptionFree;\r
99 \r
100         return ex;\r
101 }\r
102 \r
103 /**\r
104  * \brief\r
105  * Prints out the message in all the exceptions in the supplied chain.\r
106  * \r
107  * \param[in] ex\r
108  * Pointer to the exception structure to print.\r
109  * \r
110  * \remarks\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
113  * \r
114  * \see\r
115  * ANTLR3_BASE_RECOGNIZER\r
116  */\r
117 static void\r
118 antlr3ExceptionPrint(pANTLR3_EXCEPTION ex)\r
119 {\r
120     /* Ensure valid pointer\r
121      */\r
122     while   (ex != NULL)\r
123     {\r
124         /* Number if no message, else the message\r
125          */\r
126         if  (ex->message == NULL)\r
127         {\r
128             ANTLR3_FPRINTF(stderr, "ANTLR3_EXCEPTION number %d (%08X).\n", ex->type, ex->type);\r
129         }\r
130         else\r
131         {\r
132             ANTLR3_FPRINTF(stderr, "ANTLR3_EXCEPTION: %s\n", (char *)(ex->message));\r
133         }\r
134 \r
135         /* Move to next in the chain (if any)\r
136          */\r
137         ex = ex->nextException;\r
138     }\r
139 \r
140     return;\r
141 }\r
142 \r
143 /**\r
144  * \brief\r
145  * Frees up a chain of ANTLR3 exceptions\r
146  * \r
147  * \param[in] ex\r
148  * Pointer to the first exception in the chain to free.\r
149  * \r
150  * \see\r
151  * ANTLR3_EXCEPTION\r
152  */\r
153 static void\r
154 antlr3ExceptionFree(pANTLR3_EXCEPTION ex)\r
155 {\r
156     pANTLR3_EXCEPTION next;\r
157 \r
158     /* Ensure valid pointer\r
159      */\r
160     while   (ex != NULL)\r
161     {\r
162         /* Pick up anythign following now, before we free the\r
163          * current memory block.\r
164          */\r
165         next    = ex->nextException;\r
166 \r
167         /* Free the message pointer if advised to\r
168          */\r
169         if  (ex->freeMessage == ANTLR3_TRUE)\r
170         {\r
171             ANTLR3_FREE(ex->message);\r
172         }\r
173 \r
174         /* Call the programmer's custom free routine if advised to\r
175          */\r
176         if  (ex->freeCustom != NULL)\r
177         {\r
178             ex->freeCustom(ex->custom);\r
179         }\r
180 \r
181         /* Free the actual structure itself\r
182          */\r
183         ANTLR3_FREE(ex);\r
184 \r
185         ex = next;\r
186     }\r
187 \r
188     return;\r
189 }\r
190 \r