]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/cpp/DataBoardTest/libantlr3c-3.2/src/antlr3commontree.c
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.databoard / cpp / DataBoardTest / libantlr3c-3.2 / src / antlr3commontree.c
1 // \file
2 //
3 // Implementation of ANTLR3 CommonTree, which you can use as a
4 // starting point for your own tree. Though it is often easier just to tag things on
5 // to the user pointer in the tree unless you are building a different type
6 // of structure.
7 //
8
9 // [The "BSD licence"]
10 // Copyright (c) 2005-2009 Jim Idle, Temporal Wave LLC
11 // http://www.temporal-wave.com
12 // http://www.linkedin.com/in/jimidle
13 //
14 // All rights reserved.
15 //
16 // Redistribution and use in source and binary forms, with or without
17 // modification, are permitted provided that the following conditions
18 // are met:
19 // 1. Redistributions of source code must retain the above copyright
20 //    notice, this list of conditions and the following disclaimer.
21 // 2. Redistributions in binary form must reproduce the above copyright
22 //    notice, this list of conditions and the following disclaimer in the
23 //    documentation and/or other materials provided with the distribution.
24 // 3. The name of the author may not be used to endorse or promote products
25 //    derived from this software without specific prior written permission.
26 //
27 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
28 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
29 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
30 // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
31 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
32 // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
36 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37
38 #include    <antlr3commontree.h>
39
40
41 static pANTLR3_COMMON_TOKEN getToken                            (pANTLR3_BASE_TREE tree);
42 static pANTLR3_BASE_TREE    dupNode                                     (pANTLR3_BASE_TREE tree);
43 static ANTLR3_BOOLEAN       isNilNode                                   (pANTLR3_BASE_TREE tree);
44 static ANTLR3_UINT32        getType                                     (pANTLR3_BASE_TREE tree);
45 static pANTLR3_STRING       getText                                     (pANTLR3_BASE_TREE tree);
46 static ANTLR3_UINT32        getLine                                     (pANTLR3_BASE_TREE tree);
47 static ANTLR3_UINT32        getCharPositionInLine       (pANTLR3_BASE_TREE tree);
48 static pANTLR3_STRING       toString                            (pANTLR3_BASE_TREE tree);
49 static pANTLR3_BASE_TREE        getParent                               (pANTLR3_BASE_TREE tree);
50 static void                                     setParent                               (pANTLR3_BASE_TREE tree, pANTLR3_BASE_TREE parent);
51 static void                             setChildIndex                   (pANTLR3_BASE_TREE tree, ANTLR3_INT32 i);
52 static ANTLR3_INT32                     getChildIndex                   (pANTLR3_BASE_TREE tree);
53 static void                                     createChildrenList              (pANTLR3_BASE_TREE tree);
54 static void                 reuse                   (pANTLR3_BASE_TREE tree);
55
56 // Factory functions for the Arboretum
57 //
58 static void                                     newPool                         (pANTLR3_ARBORETUM factory);
59 static pANTLR3_BASE_TREE    newPoolTree                 (pANTLR3_ARBORETUM factory);
60 static pANTLR3_BASE_TREE    newFromTree                 (pANTLR3_ARBORETUM factory, pANTLR3_COMMON_TREE tree);
61 static pANTLR3_BASE_TREE    newFromToken                (pANTLR3_ARBORETUM factory, pANTLR3_COMMON_TOKEN token);
62 static void                                     factoryClose            (pANTLR3_ARBORETUM factory);
63
64 ANTLR3_API pANTLR3_ARBORETUM
65 antlr3ArboretumNew(pANTLR3_STRING_FACTORY strFactory)
66 {
67     pANTLR3_ARBORETUM   factory;
68
69     // Allocate memory
70     //
71     factory     = (pANTLR3_ARBORETUM) ANTLR3_MALLOC((size_t)sizeof(ANTLR3_ARBORETUM));
72     if  (factory == NULL)
73     {
74                 return  NULL;
75     }
76
77         // Install a vector factory to create, track and free() any child
78         // node lists.
79         //
80         factory->vFactory                                       = antlr3VectorFactoryNew(0);
81         if      (factory->vFactory == NULL)
82         {
83                 free(factory);
84                 return  NULL;
85         }
86
87     // We also keep a reclaim stack, so that any Nil nodes that are
88     // orphaned are not just left in the pool but are reused, other wise
89     // we create 6 times as many nilNodes as ordinary nodes and use loads of
90     // memory. Perhaps at some point, the analysis phase will generate better
91     // code and we won't need to do this here.
92     //
93     factory->nilStack       =  antlr3StackNew(0);
94
95     // Install factory API
96     //
97     factory->newTree        =  newPoolTree;
98     factory->newFromTree    =  newFromTree;
99     factory->newFromToken   =  newFromToken;
100     factory->close                      =  factoryClose;
101
102     // Allocate the initial pool
103     //
104     factory->thisPool   = -1;
105     factory->pools              = NULL;
106     newPool(factory);
107
108     // Factory space is good, we now want to initialize our cheating token
109     // which one it is initialized is the model for all tokens we manufacture
110     //
111     antlr3SetCTAPI(&factory->unTruc);
112
113     // Set some initial variables for future copying, including a string factory
114     // that we can use later for converting trees to strings.
115     //
116         factory->unTruc.factory                         = factory;
117     factory->unTruc.baseTree.strFactory = strFactory;
118
119     return  factory;
120
121 }
122
123 static void
124 newPool(pANTLR3_ARBORETUM factory)
125 {
126     // Increment factory count
127     //
128     factory->thisPool++;
129
130     // Ensure we have enough pointers allocated
131     //
132     factory->pools = (pANTLR3_COMMON_TREE *)
133                                         ANTLR3_REALLOC( (void *)factory->pools,                                                                         // Current pools pointer (starts at NULL)
134                                         (ANTLR3_UINT32)((factory->thisPool + 1) * sizeof(pANTLR3_COMMON_TREE *))        // Memory for new pool pointers
135                                         );
136
137     // Allocate a new pool for the factory
138     //
139     factory->pools[factory->thisPool]   =
140                             (pANTLR3_COMMON_TREE) 
141                                 ANTLR3_MALLOC((size_t)(sizeof(ANTLR3_COMMON_TREE) * ANTLR3_FACTORY_POOL_SIZE));
142
143
144     // Reset the counters
145     //
146     factory->nextTree   = 0;
147   
148     // Done
149     //
150     return;
151 }
152
153 static  pANTLR3_BASE_TREE    
154 newPoolTree         (pANTLR3_ARBORETUM factory)
155 {
156         pANTLR3_COMMON_TREE    tree;
157
158     // If we have anything on the re claim stack, reuse that sucker first
159     //
160     tree = factory->nilStack->peek(factory->nilStack);
161
162     if  (tree != NULL)
163     {
164         // Cool we got something we could reuse, it will have been cleaned up by
165         // whatever put it back on the stack (for instance if it had a child vector,
166         // that will have been cleared to hold zero entries and that vector will get reused too.
167         // It is the basetree pointer that is placed on the stack of course
168         //
169         factory->nilStack->pop(factory->nilStack);
170         return (pANTLR3_BASE_TREE)tree;
171
172     }
173         // See if we need a new tree pool before allocating a new tree
174         //
175         if      (factory->nextTree >= ANTLR3_FACTORY_POOL_SIZE)
176         {
177                 // We ran out of tokens in the current pool, so we need a new pool
178                 //
179                 newPool(factory);
180         }
181
182         // Assuming everything went well - we are trying for performance here so doing minimal
183         // error checking - then we can work out what the pointer is to the next commontree.
184         //
185         tree   = factory->pools[factory->thisPool] + factory->nextTree;
186         factory->nextTree++;
187
188         // We have our token pointer now, so we can initialize it to the predefined model.
189         //
190     antlr3SetCTAPI(tree);
191
192     // Set some initial variables for future copying, including a string factory
193     // that we can use later for converting trees to strings.
194     //
195         tree->factory                           = factory;
196     tree->baseTree.strFactory   = factory->unTruc.baseTree.strFactory;
197
198         // The super points to the common tree so we must override the one used by
199         // by the pre-built tree as otherwise we will always poitn to the same initial
200         // common tree and we might spend 3 hours trying to debug why - this would never
201         // happen to me of course! :-(
202         //
203         tree->baseTree.super    = tree;
204
205         // And we are done
206         //
207         return  &(tree->baseTree);
208 }
209
210
211 static pANTLR3_BASE_TREE            
212 newFromTree(pANTLR3_ARBORETUM factory, pANTLR3_COMMON_TREE tree)
213 {
214         pANTLR3_BASE_TREE       newTree;
215
216         newTree = factory->newTree(factory);
217
218         if      (newTree == NULL)
219         {
220                 return  NULL;
221         }
222
223         // Pick up the payload we had in the supplied tree
224         //
225         ((pANTLR3_COMMON_TREE)(newTree->super))->token   = tree->token;
226         newTree->u                  = tree->baseTree.u;                                                 // Copy any user pointer
227
228         return  newTree;
229 }
230
231 static pANTLR3_BASE_TREE            
232 newFromToken(pANTLR3_ARBORETUM factory, pANTLR3_COMMON_TOKEN token)
233 {
234         pANTLR3_BASE_TREE       newTree;
235
236         newTree = factory->newTree(factory);
237
238         if      (newTree == NULL)
239         {
240                 return  NULL;
241         }
242
243         // Pick up the payload we had in the supplied tree
244         //
245         ((pANTLR3_COMMON_TREE)(newTree->super))->token = token;
246
247         return newTree;
248 }
249
250 static  void
251 factoryClose        (pANTLR3_ARBORETUM factory)
252 {
253         ANTLR3_INT32        poolCount;
254
255         // First close the vector factory that supplied all the child pointer
256         // vectors.
257         //
258         factory->vFactory->close(factory->vFactory);
259
260     if  (factory->nilStack !=  NULL)
261     {
262         factory->nilStack->free(factory->nilStack);
263     }
264
265         // We now JUST free the pools because the C runtime CommonToken based tree
266         // cannot contain anything that was not made by this factory.
267         //
268         for     (poolCount = 0; poolCount <= factory->thisPool; poolCount++)
269         {
270                 // We can now free this pool allocation
271                 //
272                 ANTLR3_FREE(factory->pools[poolCount]);
273                 factory->pools[poolCount] = NULL;
274         }
275
276         // All the pools are deallocated we can free the pointers to the pools
277         // now.
278         //
279         ANTLR3_FREE(factory->pools);
280
281         // Finally, we can free the space for the factory itself
282         //
283         ANTLR3_FREE(factory);
284 }
285
286
287 ANTLR3_API void 
288 antlr3SetCTAPI(pANTLR3_COMMON_TREE tree)
289 {
290     // Init base tree
291     //
292     antlr3BaseTreeNew(&(tree->baseTree));
293
294     // We need a pointer to ourselves for 
295     // the payload and few functions that we
296     // provide.
297     //
298     tree->baseTree.super    =  tree;
299
300     // Common tree overrides
301
302     tree->baseTree.isNilNode                                    = isNilNode;
303     tree->baseTree.toString                                     = toString;
304     tree->baseTree.dupNode                                      = (void *(*)(pANTLR3_BASE_TREE))(dupNode);
305     tree->baseTree.getLine                                      = getLine;
306     tree->baseTree.getCharPositionInLine        = getCharPositionInLine;
307     tree->baseTree.toString                                     = toString;
308     tree->baseTree.getType                                      = getType;
309     tree->baseTree.getText                                      = getText;
310     tree->baseTree.getToken                                     = getToken;
311         tree->baseTree.getParent                                = getParent;
312         tree->baseTree.setParent                                = setParent;
313         tree->baseTree.setChildIndex                    = setChildIndex;
314         tree->baseTree.getChildIndex                    = getChildIndex;
315         tree->baseTree.createChildrenList               = createChildrenList;
316     tree->baseTree.reuse                    = reuse;
317         tree->baseTree.free                                             = NULL; // Factory trees have no free function
318         
319         tree->baseTree.children = NULL;
320
321     tree->token                         = NULL; // No token as yet
322     tree->startIndex            = 0;
323     tree->stopIndex                     = 0;
324         tree->parent                    = NULL; // No parent yet
325         tree->childIndex                = -1;
326
327     return;
328 }
329
330 // --------------------------------------
331 // Non factory node constructors.
332 //
333
334 ANTLR3_API pANTLR3_COMMON_TREE
335 antlr3CommonTreeNew()
336 {
337         pANTLR3_COMMON_TREE     tree;
338         tree    = ANTLR3_MALLOC(sizeof(ANTLR3_COMMON_TREE));
339
340         if      (tree == NULL)
341         {
342                 return NULL;
343         }
344
345         antlr3SetCTAPI(tree);
346
347         return tree;
348 }
349
350 ANTLR3_API pANTLR3_COMMON_TREE      
351 antlr3CommonTreeNewFromToken(pANTLR3_COMMON_TOKEN token)
352 {
353         pANTLR3_COMMON_TREE     newTree;
354
355         newTree = antlr3CommonTreeNew();
356
357         if      (newTree == NULL)
358         {
359                 return  NULL;
360         }
361
362         //Pick up the payload we had in the supplied tree
363         //
364         newTree->token = token;
365         return newTree;
366 }
367
368 /// Create a new vector for holding child nodes using the inbuilt
369 /// vector factory.
370 ///
371 static void
372 createChildrenList  (pANTLR3_BASE_TREE tree)
373 {
374         tree->children = ((pANTLR3_COMMON_TREE)(tree->super))->factory->vFactory->newVector(((pANTLR3_COMMON_TREE)(tree->super))->factory->vFactory);
375 }
376
377
378 static pANTLR3_COMMON_TOKEN 
379 getToken                        (pANTLR3_BASE_TREE tree)
380 {
381     // The token is the payload of the common tree or other implementor
382     // so it is stored within ourselves, which is the super pointer.Note 
383         // that whatever the actual token is, it is passed around by its pointer
384         // to the common token implementation, which it may of course surround
385         // with its own super structure.
386     //
387     return  ((pANTLR3_COMMON_TREE)(tree->super))->token;
388 }
389
390 static pANTLR3_BASE_TREE    
391 dupNode                 (pANTLR3_BASE_TREE tree)
392 {
393     // The node we are duplicating is in fact the common tree (that's why we are here)
394     // so we use the super pointer to duplicate.
395     //
396     pANTLR3_COMMON_TREE     theOld;
397     
398         theOld  = (pANTLR3_COMMON_TREE)(tree->super);
399
400         // The pointer we return is the base implementation of course
401     //
402         return  theOld->factory->newFromTree(theOld->factory, theOld);
403 }
404
405 static ANTLR3_BOOLEAN       
406 isNilNode                       (pANTLR3_BASE_TREE tree)
407 {
408         // This is a Nil tree if it has no payload (Token in our case)
409         //
410         if      (((pANTLR3_COMMON_TREE)(tree->super))->token == NULL)
411         {
412                 return ANTLR3_TRUE;
413         }
414         else
415         {
416                 return ANTLR3_FALSE;
417         }
418 }
419
420 static ANTLR3_UINT32        
421 getType                 (pANTLR3_BASE_TREE tree)
422 {
423         pANTLR3_COMMON_TREE    theTree;
424
425         theTree = (pANTLR3_COMMON_TREE)(tree->super);
426
427         if      (theTree->token == NULL)
428         {
429                 return  0;
430         }
431         else
432         {
433                 return  theTree->token->getType(theTree->token);
434         }
435 }
436
437 static pANTLR3_STRING       
438 getText                 (pANTLR3_BASE_TREE tree)
439 {
440         return  tree->toString(tree);
441 }
442
443 static ANTLR3_UINT32        getLine                     (pANTLR3_BASE_TREE tree)
444 {
445         pANTLR3_COMMON_TREE         cTree;
446         pANTLR3_COMMON_TOKEN    token;
447
448         cTree   = (pANTLR3_COMMON_TREE)(tree->super);
449
450         token   = cTree->token;
451
452         if      (token == NULL || token->getLine(token) == 0)
453         {
454                 if  (tree->getChildCount(tree) > 0)
455                 {
456                         pANTLR3_BASE_TREE       child;
457
458                         child   = (pANTLR3_BASE_TREE)tree->getChild(tree, 0);
459                         return child->getLine(child);
460                 }
461                 return 0;
462         }
463         return  token->getLine(token);
464 }
465
466 static ANTLR3_UINT32        getCharPositionInLine       (pANTLR3_BASE_TREE tree)
467 {
468         pANTLR3_COMMON_TOKEN    token;
469
470         token   = ((pANTLR3_COMMON_TREE)(tree->super))->token;
471
472         if      (token == NULL || token->getCharPositionInLine(token) == -1)
473         {
474                 if  (tree->getChildCount(tree) > 0)
475                 {
476                         pANTLR3_BASE_TREE       child;
477
478                         child   = (pANTLR3_BASE_TREE)tree->getChild(tree, 0);
479
480                         return child->getCharPositionInLine(child);
481                 }
482                 return 0;
483         }
484         return  token->getCharPositionInLine(token);
485 }
486
487 static pANTLR3_STRING       toString                    (pANTLR3_BASE_TREE tree)
488 {
489         if  (tree->isNilNode(tree) == ANTLR3_TRUE)
490         {
491                 pANTLR3_STRING  nilNode;
492
493                 nilNode = tree->strFactory->newPtr(tree->strFactory, (pANTLR3_UINT8)"nil", 3);
494
495                 return nilNode;
496         }
497
498         return  ((pANTLR3_COMMON_TREE)(tree->super))->token->getText(((pANTLR3_COMMON_TREE)(tree->super))->token);
499 }
500
501 static pANTLR3_BASE_TREE        
502 getParent                               (pANTLR3_BASE_TREE tree)
503 {
504         return & (((pANTLR3_COMMON_TREE)(tree->super))->parent->baseTree);
505 }
506
507 static void                                     
508 setParent                               (pANTLR3_BASE_TREE tree, pANTLR3_BASE_TREE parent)
509 {
510         ((pANTLR3_COMMON_TREE)(tree->super))->parent = parent == NULL ? NULL : ((pANTLR3_COMMON_TREE)(parent->super))->parent;
511 }
512
513 static void                             
514 setChildIndex                   (pANTLR3_BASE_TREE tree, ANTLR3_INT32 i)
515 {
516         ((pANTLR3_COMMON_TREE)(tree->super))->childIndex = i;
517 }
518 static  ANTLR3_INT32                    
519 getChildIndex                   (pANTLR3_BASE_TREE tree )
520 {
521         return ((pANTLR3_COMMON_TREE)(tree->super))->childIndex;
522 }
523
524 /** Clean up any child vector that the tree might have, so it can be reused,
525  *  then add it into the reuse stack.
526  */
527 static void
528 reuse                   (pANTLR3_BASE_TREE tree)
529 {
530     pANTLR3_COMMON_TREE     cTree;
531
532         cTree   = (pANTLR3_COMMON_TREE)(tree->super);
533
534     if  (cTree->factory != NULL)
535     {
536
537         if  (cTree->baseTree.children != NULL)
538         {
539             
540             cTree->baseTree.children->clear(cTree->baseTree.children);
541         }
542        cTree->factory->nilStack->push(cTree->factory->nilStack, tree, NULL);
543        
544     }
545 }