2 * This is the standard tree adaptor used by the C runtime unless the grammar
3 * source file says to use anything different. It embeds a BASE_TREE to which
4 * it adds its own implementation of anything that the base tree is not
5 * good for, plus a number of methods that any other adaptor type
6 * needs to implement too.
7 * \ingroup pANTLR3_COMMON_TREE_ADAPTOR
10 // [The "BSD licence"]
11 // Copyright (c) 2005-2009 Jim Idle, Temporal Wave LLC
12 // http://www.temporal-wave.com
13 // http://www.linkedin.com/in/jimidle
15 // All rights reserved.
17 // Redistribution and use in source and binary forms, with or without
18 // modification, are permitted provided that the following conditions
20 // 1. Redistributions of source code must retain the above copyright
21 // notice, this list of conditions and the following disclaimer.
22 // 2. Redistributions in binary form must reproduce the above copyright
23 // notice, this list of conditions and the following disclaimer in the
24 // documentation and/or other materials provided with the distribution.
25 // 3. The name of the author may not be used to endorse or promote products
26 // derived from this software without specific prior written permission.
28 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
29 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
30 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
31 // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
32 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
33 // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
37 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #include <antlr3commontreeadaptor.h>
42 #pragma warning( disable : 4100 )
45 /* BASE_TREE_ADAPTOR overrides... */
46 static pANTLR3_BASE_TREE dupNode (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE treeNode);
47 static pANTLR3_BASE_TREE create (pANTLR3_BASE_TREE_ADAPTOR adpator, pANTLR3_COMMON_TOKEN payload);
48 static pANTLR3_BASE_TREE dbgCreate (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_COMMON_TOKEN payload);
49 static pANTLR3_COMMON_TOKEN createToken (pANTLR3_BASE_TREE_ADAPTOR adaptor, ANTLR3_UINT32 tokenType, pANTLR3_UINT8 text);
50 static pANTLR3_COMMON_TOKEN createTokenFromToken (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_COMMON_TOKEN fromToken);
51 static pANTLR3_COMMON_TOKEN getToken (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t);
52 static pANTLR3_STRING getText (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t);
53 static ANTLR3_UINT32 getType (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t);
54 static pANTLR3_BASE_TREE getChild (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t, ANTLR3_UINT32 i);
55 static ANTLR3_UINT32 getChildCount (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t);
56 static void replaceChildren (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE parent, ANTLR3_INT32 startChildIndex, ANTLR3_INT32 stopChildIndex, pANTLR3_BASE_TREE t);
57 static void setDebugEventListener (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_DEBUG_EVENT_LISTENER debugger);
58 static void setChildIndex (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t, ANTLR3_INT32 i);
59 static ANTLR3_INT32 getChildIndex (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t);
60 static void setParent (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE child, pANTLR3_BASE_TREE parent);
61 static pANTLR3_BASE_TREE getParent (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE child);
62 static void setChild (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t, ANTLR3_UINT32 i, pANTLR3_BASE_TREE child);
63 static void deleteChild (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t, ANTLR3_UINT32 i);
64 static pANTLR3_BASE_TREE errorNode (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_TOKEN_STREAM ctnstream, pANTLR3_COMMON_TOKEN startToken, pANTLR3_COMMON_TOKEN stopToken, pANTLR3_EXCEPTION e);
65 /* Methods specific to each tree adaptor
67 static void setTokenBoundaries (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t, pANTLR3_COMMON_TOKEN startToken, pANTLR3_COMMON_TOKEN stopToken);
68 static void dbgSetTokenBoundaries (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t, pANTLR3_COMMON_TOKEN startToken, pANTLR3_COMMON_TOKEN stopToken);
69 static ANTLR3_MARKER getTokenStartIndex (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t);
70 static ANTLR3_MARKER getTokenStopIndex (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t);
72 static void ctaFree (pANTLR3_BASE_TREE_ADAPTOR adaptor);
74 /** Create a new tree adaptor. Note that despite the fact that this is
75 * creating a new COMMON_TREE adaptor, we return the address of the
76 * BASE_TREE interface, as should any other adaptor that wishes to be
77 * used as the tree element of a tree parse/build. It needs to be given the
78 * address of a valid string factory as we do not know what the originating
79 * input stream encoding type was. This way we can rely on just using
80 * the original input stream's string factory or one of the correct type
81 * which the user supplies us.
83 ANTLR3_API pANTLR3_BASE_TREE_ADAPTOR
84 ANTLR3_TREE_ADAPTORNew(pANTLR3_STRING_FACTORY strFactory)
86 pANTLR3_COMMON_TREE_ADAPTOR cta;
88 // First job is to create the memory we need for the tree adaptor interface.
90 cta = (pANTLR3_COMMON_TREE_ADAPTOR) ANTLR3_MALLOC((size_t)(sizeof(ANTLR3_COMMON_TREE_ADAPTOR)));
97 // Memory is initialized, so initialize the base tree adaptor
99 antlr3BaseTreeAdaptorInit(&(cta->baseAdaptor), NULL);
101 // Install our interface overrides. Strangeness is to allow generated code to treat them
102 // as returning void *
104 cta->baseAdaptor.dupNode = (void * (*) (pANTLR3_BASE_TREE_ADAPTOR, void *))
106 cta->baseAdaptor.create = (void * (*) (pANTLR3_BASE_TREE_ADAPTOR, pANTLR3_COMMON_TOKEN))
108 cta->baseAdaptor.createToken =
110 cta->baseAdaptor.createTokenFromToken =
111 createTokenFromToken;
112 cta->baseAdaptor.setTokenBoundaries = (void (*) (pANTLR3_BASE_TREE_ADAPTOR, void *, pANTLR3_COMMON_TOKEN, pANTLR3_COMMON_TOKEN))
114 cta->baseAdaptor.getTokenStartIndex = (ANTLR3_MARKER (*) (pANTLR3_BASE_TREE_ADAPTOR, void *))
116 cta->baseAdaptor.getTokenStopIndex = (ANTLR3_MARKER (*) (pANTLR3_BASE_TREE_ADAPTOR, void *))
118 cta->baseAdaptor.getText = (pANTLR3_STRING (*) (pANTLR3_BASE_TREE_ADAPTOR, void *))
120 cta->baseAdaptor.getType = (ANTLR3_UINT32 (*) (pANTLR3_BASE_TREE_ADAPTOR, void *))
122 cta->baseAdaptor.getChild = (void * (*) (pANTLR3_BASE_TREE_ADAPTOR, void *, ANTLR3_UINT32))
124 cta->baseAdaptor.setChild = (void (*) (pANTLR3_BASE_TREE_ADAPTOR, void *, ANTLR3_UINT32, void *))
126 cta->baseAdaptor.setParent = (void (*) (pANTLR3_BASE_TREE_ADAPTOR, void *, void *))
128 cta->baseAdaptor.getParent = (void * (*) (pANTLR3_BASE_TREE_ADAPTOR, void *))
130 cta->baseAdaptor.setChildIndex = (void (*) (pANTLR3_BASE_TREE_ADAPTOR, void *, ANTLR3_UINT32))
132 cta->baseAdaptor.deleteChild = (void (*) (pANTLR3_BASE_TREE_ADAPTOR, void *, ANTLR3_UINT32))
134 cta->baseAdaptor.getChildCount = (ANTLR3_UINT32 (*) (pANTLR3_BASE_TREE_ADAPTOR, void *))
136 cta->baseAdaptor.getChildIndex = (ANTLR3_INT32 (*) (pANTLR3_BASE_TREE_ADAPTOR, void *))
138 cta->baseAdaptor.free = (void (*) (pANTLR3_BASE_TREE_ADAPTOR))
140 cta->baseAdaptor.setDebugEventListener =
141 setDebugEventListener;
142 cta->baseAdaptor.replaceChildren = (void (*) (pANTLR3_BASE_TREE_ADAPTOR, void *, ANTLR3_INT32, ANTLR3_INT32, void *))
144 cta->baseAdaptor.errorNode = (void * (*) (pANTLR3_BASE_TREE_ADAPTOR, pANTLR3_TOKEN_STREAM, pANTLR3_COMMON_TOKEN, pANTLR3_COMMON_TOKEN, pANTLR3_EXCEPTION))
147 // Install the super class pointer
149 cta->baseAdaptor.super = cta;
151 // Install a tree factory for creating new tree nodes
153 cta->arboretum = antlr3ArboretumNew(strFactory);
155 // Install a token factory for imaginary tokens, these imaginary
156 // tokens do not require access to the input stream so we can
157 // dummy the creation of it, but they will need a string factory.
159 cta->baseAdaptor.tokenFactory = antlr3TokenFactoryNew(NULL);
160 cta->baseAdaptor.tokenFactory->unTruc.strFactory = strFactory;
162 // Allow the base tree adaptor to share the tree factory's string factory.
164 cta->baseAdaptor.strFactory = strFactory;
166 // Return the address of the base adaptor interface.
168 return &(cta->baseAdaptor);
171 /// Debugging version of the tree adaptor (not normally called as generated code
172 /// calls setDebugEventListener instead which changes a normal token stream to
173 /// a debugging stream and means that a user's instantiation code does not need
174 /// to be changed just to debug with AW.
176 ANTLR3_API pANTLR3_BASE_TREE_ADAPTOR
177 ANTLR3_TREE_ADAPTORDebugNew(pANTLR3_STRING_FACTORY strFactory, pANTLR3_DEBUG_EVENT_LISTENER debugger)
179 pANTLR3_BASE_TREE_ADAPTOR ta;
181 // Create a normal one first
183 ta = ANTLR3_TREE_ADAPTORNew(strFactory);
187 // Reinitialize as a debug version
189 antlr3BaseTreeAdaptorInit(ta, debugger);
190 ta->create = (void * (*) (pANTLR3_BASE_TREE_ADAPTOR, pANTLR3_COMMON_TOKEN))
192 ta->setTokenBoundaries = (void (*) (pANTLR3_BASE_TREE_ADAPTOR, void *, pANTLR3_COMMON_TOKEN, pANTLR3_COMMON_TOKEN))
193 dbgSetTokenBoundaries;
199 /// Causes an existing common tree adaptor to become a debug version
202 setDebugEventListener (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_DEBUG_EVENT_LISTENER debugger)
204 // Reinitialize as a debug version
206 antlr3BaseTreeAdaptorInit(adaptor, debugger);
208 adaptor->create = (void * (*) (pANTLR3_BASE_TREE_ADAPTOR, pANTLR3_COMMON_TOKEN))
210 adaptor->setTokenBoundaries = (void (*) (pANTLR3_BASE_TREE_ADAPTOR, void *, pANTLR3_COMMON_TOKEN, pANTLR3_COMMON_TOKEN))
211 dbgSetTokenBoundaries;
216 ctaFree(pANTLR3_BASE_TREE_ADAPTOR adaptor)
218 pANTLR3_COMMON_TREE_ADAPTOR cta;
220 cta = (pANTLR3_COMMON_TREE_ADAPTOR)(adaptor->super);
222 /* Free the tree factory we created
224 cta->arboretum->close(((pANTLR3_COMMON_TREE_ADAPTOR)(adaptor->super))->arboretum);
226 /* Free the token factory we created
228 adaptor->tokenFactory->close(adaptor->tokenFactory);
230 /* Free the super pointer, as it is this that was allocated
231 * and is the common tree structure.
233 ANTLR3_FREE(adaptor->super);
236 /* BASE_TREE_ADAPTOR overrides */
238 static pANTLR3_BASE_TREE
239 errorNode (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_TOKEN_STREAM ctnstream, pANTLR3_COMMON_TOKEN startToken, pANTLR3_COMMON_TOKEN stopToken, pANTLR3_EXCEPTION e)
241 // Use the supplied common tree node stream to get another tree from the factory
242 // TODO: Look at creating the erronode as in Java, but this is complicated by the
243 // need to track and free the memory allocated to it, so for now, we just
244 // want something in the tree that isn't a NULL pointer.
246 return adaptor->createTypeText(adaptor, ANTLR3_TOKEN_INVALID, (pANTLR3_UINT8)"Tree Error Node");
250 /** Duplicate the supplied node.
252 static pANTLR3_BASE_TREE
253 dupNode (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE treeNode)
255 return treeNode == NULL ? NULL : treeNode->dupNode(treeNode);
258 static pANTLR3_BASE_TREE
259 create (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_COMMON_TOKEN payload)
261 pANTLR3_BASE_TREE ct;
263 /* Create a new common tree as this is what this adaptor deals with
265 ct = ((pANTLR3_COMMON_TREE_ADAPTOR)(adaptor->super))->arboretum->newFromToken(((pANTLR3_COMMON_TREE_ADAPTOR)(adaptor->super))->arboretum, payload);
267 /* But all adaptors return the pointer to the base interface.
271 static pANTLR3_BASE_TREE
272 dbgCreate (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_COMMON_TOKEN payload)
274 pANTLR3_BASE_TREE ct;
276 ct = create(adaptor, payload);
277 adaptor->debugger->createNode(adaptor->debugger, ct);
282 /** Tell me how to create a token for use with imaginary token nodes.
283 * For example, there is probably no input symbol associated with imaginary
284 * token DECL, but you need to create it as a payload or whatever for
285 * the DECL node as in ^(DECL type ID).
287 * If you care what the token payload objects' type is, you should
288 * override this method and any other createToken variant.
290 static pANTLR3_COMMON_TOKEN
291 createToken (pANTLR3_BASE_TREE_ADAPTOR adaptor, ANTLR3_UINT32 tokenType, pANTLR3_UINT8 text)
293 pANTLR3_COMMON_TOKEN newToken;
295 newToken = adaptor->tokenFactory->newToken(adaptor->tokenFactory);
297 if (newToken != NULL)
299 newToken->textState = ANTLR3_TEXT_CHARP;
300 newToken->tokText.chars = (pANTLR3_UCHAR)text;
301 newToken->setType(newToken, tokenType);
302 newToken->input = adaptor->tokenFactory->input;
303 newToken->strFactory = adaptor->strFactory;
308 /** Tell me how to create a token for use with imaginary token nodes.
309 * For example, there is probably no input symbol associated with imaginary
310 * token DECL, but you need to create it as a payload or whatever for
311 * the DECL node as in ^(DECL type ID).
313 * This is a variant of createToken where the new token is derived from
314 * an actual real input token. Typically this is for converting '{'
315 * tokens to BLOCK etc... You'll see
317 * r : lc='{' ID+ '}' -> ^(BLOCK[$lc] ID+) ;
319 * If you care what the token payload objects' type is, you should
320 * override this method and any other createToken variant.
322 * NB: this being C it is not so easy to extend the types of creaeteToken.
323 * We will have to see if anyone needs to do this and add any variants to
326 static pANTLR3_COMMON_TOKEN
327 createTokenFromToken (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_COMMON_TOKEN fromToken)
329 pANTLR3_COMMON_TOKEN newToken;
331 newToken = adaptor->tokenFactory->newToken(adaptor->tokenFactory);
333 if (newToken != NULL)
335 // Create the text using our own string factory to avoid complicating
340 newToken->toString = fromToken->toString;
342 if (fromToken->textState == ANTLR3_TEXT_CHARP)
344 newToken->textState = ANTLR3_TEXT_CHARP;
345 newToken->tokText.chars = fromToken->tokText.chars;
349 text = fromToken->getText(fromToken);
350 newToken->textState = ANTLR3_TEXT_STRING;
351 newToken->tokText.text = adaptor->strFactory->newPtr(adaptor->strFactory, text->chars, text->len);
354 newToken->setLine (newToken, fromToken->getLine(fromToken));
355 newToken->setTokenIndex (newToken, fromToken->getTokenIndex(fromToken));
356 newToken->setCharPositionInLine (newToken, fromToken->getCharPositionInLine(fromToken));
357 newToken->setChannel (newToken, fromToken->getChannel(fromToken));
358 newToken->setType (newToken, fromToken->getType(fromToken));
364 /* Specific methods for a TreeAdaptor */
366 /** Track start/stop token for subtree root created for a rule.
367 * Only works with CommonTree nodes. For rules that match nothing,
368 * seems like this will yield start=i and stop=i-1 in a nil node.
369 * Might be useful info so I'll not force to be i..i.
372 setTokenBoundaries (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t, pANTLR3_COMMON_TOKEN startToken, pANTLR3_COMMON_TOKEN stopToken)
377 pANTLR3_COMMON_TREE ct;
384 if ( startToken != NULL)
386 start = startToken->getTokenIndex(startToken);
393 if ( stopToken != NULL)
395 stop = stopToken->getTokenIndex(stopToken);
402 ct = (pANTLR3_COMMON_TREE)(t->super);
404 ct->startIndex = start;
405 ct->stopIndex = stop;
409 dbgSetTokenBoundaries (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t, pANTLR3_COMMON_TOKEN startToken, pANTLR3_COMMON_TOKEN stopToken)
411 setTokenBoundaries(adaptor, t, startToken, stopToken);
413 if (t != NULL && startToken != NULL && stopToken != NULL)
415 adaptor->debugger->setTokenBoundaries(adaptor->debugger, t, startToken->getTokenIndex(startToken), stopToken->getTokenIndex(stopToken));
420 getTokenStartIndex (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t)
422 return ((pANTLR3_COMMON_TREE)(t->super))->startIndex;
426 getTokenStopIndex (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t)
428 return ((pANTLR3_COMMON_TREE)(t->super))->stopIndex;
431 static pANTLR3_STRING
432 getText (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t)
434 return t->getText(t);
438 getType (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t)
440 return t->getType(t);
445 (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE parent, ANTLR3_INT32 startChildIndex, ANTLR3_INT32 stopChildIndex, pANTLR3_BASE_TREE t)
449 parent->replaceChildren(parent, startChildIndex, stopChildIndex, t);
453 static pANTLR3_BASE_TREE
454 getChild (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t, ANTLR3_UINT32 i)
456 return t->getChild(t, i);
459 setChild (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t, ANTLR3_UINT32 i, pANTLR3_BASE_TREE child)
461 t->setChild(t, i, child);
465 deleteChild (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t, ANTLR3_UINT32 i)
467 t->deleteChild(t, i);
471 getChildCount (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t)
473 return t->getChildCount(t);
477 setChildIndex (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t, ANTLR3_INT32 i)
479 t->setChildIndex(t, i);
483 getChildIndex (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE t)
485 return t->getChildIndex(t);
488 setParent (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE child, pANTLR3_BASE_TREE parent)
490 child->setParent(child, parent);
492 static pANTLR3_BASE_TREE
493 getParent (pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE child)
495 return child->getParent(child);