• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /** \file
2  * Implementation of the base functionality for an ANTLR3 parser.
3  */
4 
5 // [The "BSD licence"]
6 // Copyright (c) 2005-2009 Jim Idle, Temporal Wave LLC
7 // http://www.temporal-wave.com
8 // http://www.linkedin.com/in/jimidle
9 //
10 // All rights reserved.
11 //
12 // Redistribution and use in source and binary forms, with or without
13 // modification, are permitted provided that the following conditions
14 // are met:
15 // 1. Redistributions of source code must retain the above copyright
16 //    notice, this list of conditions and the following disclaimer.
17 // 2. Redistributions in binary form must reproduce the above copyright
18 //    notice, this list of conditions and the following disclaimer in the
19 //    documentation and/or other materials provided with the distribution.
20 // 3. The name of the author may not be used to endorse or promote products
21 //    derived from this software without specific prior written permission.
22 //
23 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 
34 #include    <antlr3parser.h>
35 
36 /* Parser API
37  */
38 static void					setDebugListener	(pANTLR3_PARSER parser, pANTLR3_DEBUG_EVENT_LISTENER dbg);
39 static void					setTokenStream		(pANTLR3_PARSER parser, pANTLR3_TOKEN_STREAM);
40 static pANTLR3_TOKEN_STREAM	getTokenStream		(pANTLR3_PARSER parser);
41 static void					freeParser		    (pANTLR3_PARSER parser);
42 
43 ANTLR3_API pANTLR3_PARSER
antlr3ParserNewStreamDbg(ANTLR3_UINT32 sizeHint,pANTLR3_TOKEN_STREAM tstream,pANTLR3_DEBUG_EVENT_LISTENER dbg,pANTLR3_RECOGNIZER_SHARED_STATE state)44 antlr3ParserNewStreamDbg		(ANTLR3_UINT32 sizeHint, pANTLR3_TOKEN_STREAM tstream, pANTLR3_DEBUG_EVENT_LISTENER dbg, pANTLR3_RECOGNIZER_SHARED_STATE state)
45 {
46 	pANTLR3_PARSER	parser;
47 
48 	parser = antlr3ParserNewStream(sizeHint, tstream, state);
49 
50 	if	(parser == NULL)
51     {
52 		return	NULL;
53     }
54 
55 	parser->setDebugListener(parser, dbg);
56 
57 	return parser;
58 }
59 
60 ANTLR3_API pANTLR3_PARSER
antlr3ParserNew(ANTLR3_UINT32 sizeHint,pANTLR3_RECOGNIZER_SHARED_STATE state)61 antlr3ParserNew		(ANTLR3_UINT32 sizeHint, pANTLR3_RECOGNIZER_SHARED_STATE state)
62 {
63     pANTLR3_PARSER	parser;
64 
65     /* Allocate memory
66      */
67     parser	= (pANTLR3_PARSER) ANTLR3_MALLOC(sizeof(ANTLR3_PARSER));
68 
69     if	(parser == NULL)
70     {
71 		return	NULL;
72     }
73 
74     /* Install a base parser
75      */
76     parser->rec =  antlr3BaseRecognizerNew(ANTLR3_TYPE_PARSER, sizeHint, state);
77 
78     if	(parser->rec == NULL)
79     {
80 		parser->free(parser);
81 		return	NULL;
82     }
83 
84     parser->rec->super	= parser;
85 
86     /* Parser overrides
87      */
88     parser->rec->exConstruct	=  antlr3MTExceptionNew;
89 
90     /* Install the API
91      */
92 	parser->setDebugListener	=  setDebugListener;
93     parser->setTokenStream		=  setTokenStream;
94     parser->getTokenStream		=  getTokenStream;
95 
96     parser->free			=  freeParser;
97 
98     return parser;
99 }
100 
101 ANTLR3_API pANTLR3_PARSER
antlr3ParserNewStream(ANTLR3_UINT32 sizeHint,pANTLR3_TOKEN_STREAM tstream,pANTLR3_RECOGNIZER_SHARED_STATE state)102 antlr3ParserNewStream	(ANTLR3_UINT32 sizeHint, pANTLR3_TOKEN_STREAM tstream, pANTLR3_RECOGNIZER_SHARED_STATE state)
103 {
104     pANTLR3_PARSER	parser;
105 
106     parser  = antlr3ParserNew(sizeHint, state);
107 
108     if	(parser == NULL)
109     {
110 		return	NULL;
111     }
112 
113     /* Everything seems to be hunky dory so we can install the
114      * token stream.
115      */
116     parser->setTokenStream(parser, tstream);
117 
118     return parser;
119 }
120 
121 static void
freeParser(pANTLR3_PARSER parser)122 freeParser			    (pANTLR3_PARSER parser)
123 {
124     if	(parser->rec != NULL)
125     {
126 		// This may have ben a delegate or delegator parser, in which case the
127 		// state may already have been freed (and set to NULL therefore)
128 		// so we ignore the state if we don't have it.
129 		//
130 		if	(parser->rec->state != NULL)
131 		{
132 			if	(parser->rec->state->following != NULL)
133 			{
134 				parser->rec->state->following->free(parser->rec->state->following);
135 				parser->rec->state->following = NULL;
136 			}
137 		}
138 	    parser->rec->free(parser->rec);
139 	    parser->rec	= NULL;
140 
141     }
142     ANTLR3_FREE(parser);
143 }
144 
145 static void
setDebugListener(pANTLR3_PARSER parser,pANTLR3_DEBUG_EVENT_LISTENER dbg)146 setDebugListener		(pANTLR3_PARSER parser, pANTLR3_DEBUG_EVENT_LISTENER dbg)
147 {
148 	// Set the debug listener. There are no methods to override
149 	// because currently the only ones that notify the debugger
150 	// are error reporting and recovery. Hence we can afford to
151 	// check and see if the debugger interface is null or not
152 	// there. If there is ever an occasion for a performance
153 	// sensitive function to use the debugger interface, then
154 	// a replacement function for debug mode should be supplied
155 	// and installed here.
156 	//
157 	parser->rec->debugger	= dbg;
158 
159 	// If there was a tokenstream installed already
160 	// then we need to tell it about the debug interface
161 	//
162 	if	(parser->tstream != NULL)
163 	{
164 		parser->tstream->setDebugListener(parser->tstream, dbg);
165 	}
166 }
167 
168 static void
setTokenStream(pANTLR3_PARSER parser,pANTLR3_TOKEN_STREAM tstream)169 setTokenStream		    (pANTLR3_PARSER parser, pANTLR3_TOKEN_STREAM tstream)
170 {
171     parser->tstream = tstream;
172     parser->rec->reset(parser->rec);
173 }
174 
175 static pANTLR3_TOKEN_STREAM
getTokenStream(pANTLR3_PARSER parser)176 getTokenStream		    (pANTLR3_PARSER parser)
177 {
178     return  parser->tstream;
179 }
180 
181 
182 
183 
184 
185 
186 
187 
188 
189 
190 
191 
192 
193 
194