2 * Implementation of the base functionality for an ANTLR3 parser.
6 // Copyright (c) 2005-2009 Jim Idle, Temporal Wave LLC
7 // http://www.temporal-wave.com
8 // http://www.linkedin.com/in/jimidle
10 // All rights reserved.
12 // Redistribution and use in source and binary forms, with or without
13 // modification, are permitted provided that the following conditions
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.
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.
34 #include <antlr3parser.h>
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
);
43 ANTLR3_API pANTLR3_PARSER
44 antlr3ParserNewStreamDbg (ANTLR3_UINT32 sizeHint
, pANTLR3_TOKEN_STREAM tstream
, pANTLR3_DEBUG_EVENT_LISTENER dbg
, pANTLR3_RECOGNIZER_SHARED_STATE state
)
46 pANTLR3_PARSER parser
;
48 parser
= antlr3ParserNewStream(sizeHint
, tstream
, state
);
55 parser
->setDebugListener(parser
, dbg
);
60 ANTLR3_API pANTLR3_PARSER
61 antlr3ParserNew (ANTLR3_UINT32 sizeHint
, pANTLR3_RECOGNIZER_SHARED_STATE state
)
63 pANTLR3_PARSER parser
;
67 parser
= (pANTLR3_PARSER
) ANTLR3_MALLOC(sizeof(ANTLR3_PARSER
));
74 /* Install a base parser
76 parser
->rec
= antlr3BaseRecognizerNew(ANTLR3_TYPE_PARSER
, sizeHint
, state
);
78 if (parser
->rec
== NULL
)
84 parser
->rec
->super
= parser
;
88 parser
->rec
->exConstruct
= antlr3MTExceptionNew
;
92 parser
->setDebugListener
= setDebugListener
;
93 parser
->setTokenStream
= setTokenStream
;
94 parser
->getTokenStream
= getTokenStream
;
96 parser
->free
= freeParser
;
101 ANTLR3_API pANTLR3_PARSER
102 antlr3ParserNewStream (ANTLR3_UINT32 sizeHint
, pANTLR3_TOKEN_STREAM tstream
, pANTLR3_RECOGNIZER_SHARED_STATE state
)
104 pANTLR3_PARSER parser
;
106 parser
= antlr3ParserNew(sizeHint
, state
);
113 /* Everything seems to be hunky dory so we can install the
116 parser
->setTokenStream(parser
, tstream
);
122 freeParser (pANTLR3_PARSER parser
)
124 if (parser
->rec
!= NULL
)
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.
130 if (parser
->rec
->state
!= NULL
)
132 if (parser
->rec
->state
->following
!= NULL
)
134 stackFree(parser
->rec
->state
->following
);
135 parser
->rec
->state
->following
= NULL
;
138 parser
->rec
->free(parser
->rec
);
146 setDebugListener (pANTLR3_PARSER parser
, pANTLR3_DEBUG_EVENT_LISTENER dbg
)
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.
157 parser
->rec
->debugger
= dbg
;
159 // If there was a tokenstream installed already
160 // then we need to tell it about the debug interface
162 if (parser
->tstream
!= NULL
)
164 parser
->tstream
->setDebugListener(parser
->tstream
, dbg
);
169 setTokenStream (pANTLR3_PARSER parser
, pANTLR3_TOKEN_STREAM tstream
)
171 parser
->tstream
= tstream
;
172 parser
->rec
->reset(parser
->rec
);
175 static pANTLR3_TOKEN_STREAM
176 getTokenStream (pANTLR3_PARSER parser
)
178 return parser
->tstream
;