--- /dev/null
+/// \file\r
+/// Definition of the ANTLR3 common tree node stream.\r
+///\r
+\r
+#ifndef _ANTLR3_COMMON_TREE_NODE_STREAM__H\r
+#define _ANTLR3_COMMON_TREE_NODE_STREAM__H\r
+\r
+// [The "BSD licence"]\r
+// Copyright (c) 2005-2009 Jim Idle, Temporal Wave LLC\r
+// http://www.temporal-wave.com\r
+// http://www.linkedin.com/in/jimidle\r
+//\r
+// All rights reserved.\r
+//\r
+// Redistribution and use in source and binary forms, with or without\r
+// modification, are permitted provided that the following conditions\r
+// are met:\r
+// 1. Redistributions of source code must retain the above copyright\r
+// notice, this list of conditions and the following disclaimer.\r
+// 2. Redistributions in binary form must reproduce the above copyright\r
+// notice, this list of conditions and the following disclaimer in the\r
+// documentation and/or other materials provided with the distribution.\r
+// 3. The name of the author may not be used to endorse or promote products\r
+// derived from this software without specific prior written permission.\r
+//\r
+// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR\r
+// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES\r
+// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.\r
+// IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,\r
+// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT\r
+// NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\r
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\r
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF\r
+// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
+\r
+#include <antlr3defs.h>\r
+#include <antlr3commontreeadaptor.h>\r
+#include <antlr3commontree.h>\r
+#include <antlr3collections.h>\r
+#include <antlr3intstream.h>\r
+#include <antlr3string.h>\r
+\r
+/// Token buffer initial size settings ( will auto increase)\r
+///\r
+#define DEFAULT_INITIAL_BUFFER_SIZE 100\r
+#define INITIAL_CALL_STACK_SIZE 10\r
+\r
+#ifdef __cplusplus\r
+extern "C" {\r
+#endif\r
+\r
+typedef struct ANTLR3_TREE_NODE_STREAM_struct\r
+{\r
+ /// Any interface that implements this interface (is a \r
+ /// super structure containing this structure), may store the pointer\r
+ /// to itself here in the super pointer, which is not used by \r
+ /// the tree node stream. This will point to an implementation\r
+ /// of ANTLR3_COMMON_TREE_NODE_STREAM in this case.\r
+ ///\r
+ pANTLR3_COMMON_TREE_NODE_STREAM ctns;\r
+\r
+ /// All input streams implement the ANTLR3_INT_STREAM interface...\r
+ ///\r
+ pANTLR3_INT_STREAM istream;\r
+\r
+ /// Get tree node at current input pointer + i ahead where i=1 is next node.\r
+ /// i<0 indicates nodes in the past. So LT(-1) is previous node, but\r
+ /// implementations are not required to provide results for k < -1.\r
+ /// LT(0) is undefined. For i>=n, return null.\r
+ /// Return NULL for LT(0) and any index that results in an absolute address\r
+ /// that is negative (beyond the start of the list).\r
+ ///\r
+ /// This is analogous to the LT() method of the TokenStream, but this\r
+ /// returns a tree node instead of a token. Makes code gen identical\r
+ /// for both parser and tree grammars. :)\r
+ ///\r
+ pANTLR3_BASE_TREE (*_LT) (struct ANTLR3_TREE_NODE_STREAM_struct * tns, ANTLR3_INT32 k);\r
+\r
+ /// Where is this stream pulling nodes from? This is not the name, but\r
+ /// the object that provides node objects.\r
+ ///\r
+ pANTLR3_BASE_TREE (*getTreeSource) (struct ANTLR3_TREE_NODE_STREAM_struct * tns);\r
+\r
+ /// What adaptor can tell me how to interpret/navigate nodes and\r
+ /// trees. E.g., get text of a node.\r
+ ///\r
+ pANTLR3_BASE_TREE_ADAPTOR (*getTreeAdaptor) (struct ANTLR3_TREE_NODE_STREAM_struct * tns);\r
+\r
+ /// As we flatten the tree, we use UP, DOWN nodes to represent\r
+ /// the tree structure. When debugging we need unique nodes\r
+ /// so we have to instantiate new ones. When doing normal tree\r
+ /// parsing, it's slow and a waste of memory to create unique\r
+ /// navigation nodes. Default should be false;\r
+ ///\r
+ void (*setUniqueNavigationNodes) (struct ANTLR3_TREE_NODE_STREAM_struct * tns, ANTLR3_BOOLEAN uniqueNavigationNodes);\r
+\r
+ pANTLR3_STRING (*toString) (struct ANTLR3_TREE_NODE_STREAM_struct * tns);\r
+\r
+ /// Return the text of all nodes from start to stop, inclusive.\r
+ /// If the stream does not buffer all the nodes then it can still\r
+ /// walk recursively from start until stop. You can always return\r
+ /// null or "" too, but users should not access $ruleLabel.text in\r
+ /// an action of course in that case.\r
+ ///\r
+ pANTLR3_STRING (*toStringSS) (struct ANTLR3_TREE_NODE_STREAM_struct * tns, pANTLR3_BASE_TREE start, pANTLR3_BASE_TREE stop);\r
+\r
+ /// Return the text of all nodes from start to stop, inclusive, into the\r
+ /// supplied buffer.\r
+ /// If the stream does not buffer all the nodes then it can still\r
+ /// walk recursively from start until stop. You can always return\r
+ /// null or "" too, but users should not access $ruleLabel.text in\r
+ /// an action of course in that case.\r
+ ///\r
+ void (*toStringWork) (struct ANTLR3_TREE_NODE_STREAM_struct * tns, pANTLR3_BASE_TREE start, pANTLR3_BASE_TREE stop, pANTLR3_STRING buf);\r
+\r
+ /// Release up any and all space the the interface allocate, including for this structure.\r
+ ///\r
+ void (*free) (struct ANTLR3_TREE_NODE_STREAM_struct * tns);\r
+\r
+ /// Get a tree node at an absolute index i; 0..n-1.\r
+ /// If you don't want to buffer up nodes, then this method makes no\r
+ /// sense for you.\r
+ ///\r
+ pANTLR3_BASE_TREE (*get) (struct ANTLR3_TREE_NODE_STREAM_struct * tns, ANTLR3_INT32 i);\r
+\r
+ // REWRITING TREES (used by tree parser)\r
+\r
+ /// Replace from start to stop child index of parent with t, which might\r
+ /// be a list. Number of children may be different\r
+ /// after this call. The stream is notified because it is walking the\r
+ /// tree and might need to know you are monkeying with the underlying\r
+ /// tree. Also, it might be able to modify the node stream to avoid\r
+ /// restreaming for future phases.\r
+ ///\r
+ /// If parent is null, don't do anything; must be at root of overall tree.\r
+ /// Can't replace whatever points to the parent externally. Do nothing.\r
+ ///\r
+ void (*replaceChildren) (struct ANTLR3_TREE_NODE_STREAM_struct * tns, pANTLR3_BASE_TREE parent, ANTLR3_INT32 startChildIndex, ANTLR3_INT32 stopChildIndex, pANTLR3_BASE_TREE t);\r
+\r
+}\r
+ ANTLR3_TREE_NODE_STREAM;\r
+\r
+typedef struct ANTLR3_COMMON_TREE_NODE_STREAM_struct\r
+{\r
+ /// Any interface that implements this interface (is a \r
+ /// super structure containing this structure), may store the pointer\r
+ /// to itself here in the super pointer, which is not used by \r
+ /// the common tree node stream.\r
+ ///\r
+ void * super;\r
+\r
+ /// Pointer to the tree node stream interface\r
+ ///\r
+ pANTLR3_TREE_NODE_STREAM tnstream;\r
+\r
+ /// String factory for use by anything that wishes to create strings\r
+ /// such as a tree representation or some copy of the text etc.\r
+ ///\r
+ pANTLR3_STRING_FACTORY stringFactory;\r
+\r
+ /// Dummy tree node that indicates a descent into a child\r
+ /// tree. Initialized by a call to create a new interface.\r
+ ///\r
+ ANTLR3_COMMON_TREE DOWN;\r
+\r
+ /// Dummy tree node that indicates a descent up to a parent\r
+ /// tree. Initialized by a call to create a new interface.\r
+ ///\r
+ ANTLR3_COMMON_TREE UP;\r
+\r
+ /// Dummy tree node that indicates the termination point of the\r
+ /// tree. Initialized by a call to create a new interface.\r
+ ///\r
+ ANTLR3_COMMON_TREE EOF_NODE;\r
+\r
+ /// Dummy node that is returned if we need to indicate an invalid node\r
+ /// for any reason.\r
+ ///\r
+ ANTLR3_COMMON_TREE INVALID_NODE;\r
+\r
+ /// The complete mapping from stream index to tree node.\r
+ /// This buffer includes pointers to DOWN, UP, and EOF nodes.\r
+ /// It is built upon ctor invocation. The elements are type\r
+ /// Object as we don't what the trees look like.\r
+ ///\r
+ /// Load upon first need of the buffer so we can set token types\r
+ /// of interest for reverseIndexing. Slows us down a wee bit to\r
+ /// do all of the if p==-1 testing everywhere though, though in C\r
+ /// you won't really be able to measure this.\r
+ ///\r
+ /// Must be freed when the tree node stream is torn down.\r
+ ///\r
+ pANTLR3_VECTOR nodes;\r
+\r
+ /// If set to ANTLR3_TRUE then the navigation nodes UP, DOWN are\r
+ /// duplicated rather than reused within the tree.\r
+ ///\r
+ ANTLR3_BOOLEAN uniqueNavigationNodes;\r
+\r
+ /// Which tree are we navigating ?\r
+ ///\r
+ pANTLR3_BASE_TREE root;\r
+\r
+ /// Pointer to tree adaptor interface that manipulates/builds\r
+ /// the tree.\r
+ ///\r
+ pANTLR3_BASE_TREE_ADAPTOR adaptor;\r
+\r
+ /// As we walk down the nodes, we must track parent nodes so we know\r
+ /// where to go after walking the last child of a node. When visiting\r
+ /// a child, push current node and current index (current index\r
+ /// is first stored in the tree node structure to avoid two stacks.\r
+ ///\r
+ pANTLR3_STACK nodeStack;\r
+\r
+ /// The current index into the nodes vector of the current tree\r
+ /// we are parsing and possibly rewriting.\r
+ ///\r
+ ANTLR3_INT32 p;\r
+\r
+ /// Which node are we currently visiting?\r
+ ///\r
+ pANTLR3_BASE_TREE currentNode;\r
+\r
+ /// Which node did we last visit? Used for LT(-1)\r
+ ///\r
+ pANTLR3_BASE_TREE previousNode;\r
+\r
+ /// Which child are we currently visiting? If -1 we have not visited\r
+ /// this node yet; next consume() request will set currentIndex to 0.\r
+ ///\r
+ ANTLR3_INT32 currentChildIndex;\r
+\r
+ /// What node index did we just consume? i=0..n-1 for n node trees.\r
+ /// IntStream.next is hence 1 + this value. Size will be same.\r
+ ///\r
+ ANTLR3_MARKER absoluteNodeIndex;\r
+\r
+ /// Buffer tree node stream for use with LT(i). This list grows\r
+ /// to fit new lookahead depths, but consume() wraps like a circular\r
+ /// buffer.\r
+ ///\r
+ pANTLR3_BASE_TREE * lookAhead;\r
+\r
+ /// Number of elements available in the lookahead buffer at any point in\r
+ /// time. This is the current size of the array.\r
+ ///\r
+ ANTLR3_UINT32 lookAheadLength;\r
+\r
+ /// lookAhead[head] is the first symbol of lookahead, LT(1). \r
+ ///\r
+ ANTLR3_UINT32 head;\r
+\r
+ /// Add new lookahead at lookahead[tail]. tail wraps around at the\r
+ /// end of the lookahead buffer so tail could be less than head.\r
+ ///\r
+ ANTLR3_UINT32 tail;\r
+\r
+ /// Calls to mark() may be nested so we have to track a stack of\r
+ /// them. The marker is an index into this stack. Index 0 is\r
+ /// the first marker. This is a List<TreeWalkState>\r
+ ///\r
+ pANTLR3_VECTOR markers;\r
+\r
+ // INTERFACE\r
+ //\r
+ void (*fill) (struct ANTLR3_COMMON_TREE_NODE_STREAM_struct * ctns, ANTLR3_INT32 k);\r
+\r
+ void (*addLookahead) (struct ANTLR3_COMMON_TREE_NODE_STREAM_struct * ctns, pANTLR3_BASE_TREE node);\r
+\r
+ ANTLR3_BOOLEAN (*hasNext) (struct ANTLR3_COMMON_TREE_NODE_STREAM_struct * ctns);\r
+\r
+ pANTLR3_BASE_TREE (*next) (struct ANTLR3_COMMON_TREE_NODE_STREAM_struct * ctns);\r
+\r
+ pANTLR3_BASE_TREE (*handleRootnode) (struct ANTLR3_COMMON_TREE_NODE_STREAM_struct * ctns);\r
+\r
+ pANTLR3_BASE_TREE (*visitChild) (struct ANTLR3_COMMON_TREE_NODE_STREAM_struct * ctns, ANTLR3_UINT32 child);\r
+\r
+ void (*addNavigationNode) (struct ANTLR3_COMMON_TREE_NODE_STREAM_struct * ctns, ANTLR3_UINT32 ttype);\r
+\r
+ pANTLR3_BASE_TREE (*newDownNode) (struct ANTLR3_COMMON_TREE_NODE_STREAM_struct * ctns);\r
+\r
+ pANTLR3_BASE_TREE (*newUpNode) (struct ANTLR3_COMMON_TREE_NODE_STREAM_struct * ctns);\r
+\r
+ void (*walkBackToMostRecentNodeWithUnvisitedChildren)\r
+ (struct ANTLR3_COMMON_TREE_NODE_STREAM_struct * ctns);\r
+\r
+ ANTLR3_BOOLEAN (*hasUniqueNavigationNodes) (struct ANTLR3_COMMON_TREE_NODE_STREAM_struct * ctns);\r
+\r
+ ANTLR3_UINT32 (*getLookaheadSize) (struct ANTLR3_COMMON_TREE_NODE_STREAM_struct * ctns);\r
+\r
+ void (*push) (struct ANTLR3_COMMON_TREE_NODE_STREAM_struct * ctns, ANTLR3_INT32 index);\r
+\r
+ ANTLR3_INT32 (*pop) (struct ANTLR3_COMMON_TREE_NODE_STREAM_struct * ctns);\r
+\r
+ void (*reset) (struct ANTLR3_COMMON_TREE_NODE_STREAM_struct * ctns);\r
+\r
+ void (*free) (struct ANTLR3_COMMON_TREE_NODE_STREAM_struct * ctns);\r
+\r
+ /// Indicates whether this node stream was derived from a prior\r
+ /// node stream to be used by a rewriting tree parser for instance.\r
+ /// If this flag is set to ANTLR3_TRUE, then when this stream is\r
+ /// closed it will not free the root tree as this tree always\r
+ /// belongs to the origniating node stream.\r
+ ///\r
+ ANTLR3_BOOLEAN isRewriter;\r
+\r
+}\r
+ ANTLR3_COMMON_TREE_NODE_STREAM;\r
+\r
+/** This structure is used to save the state information in the treenodestream\r
+ * when walking ahead with cyclic DFA or for syntactic predicates,\r
+ * we need to record the state of the tree node stream. This\r
+ * class wraps up the current state of the CommonTreeNodeStream.\r
+ * Calling mark() will push another of these on the markers stack.\r
+ */\r
+typedef struct ANTLR3_TREE_WALK_STATE_struct\r
+{\r
+ ANTLR3_UINT32 currentChildIndex;\r
+ ANTLR3_MARKER absoluteNodeIndex;\r
+ pANTLR3_BASE_TREE currentNode;\r
+ pANTLR3_BASE_TREE previousNode;\r
+ ANTLR3_UINT32 nodeStackSize;\r
+ pANTLR3_BASE_TREE * lookAhead;\r
+ ANTLR3_UINT32 lookAheadLength;\r
+ ANTLR3_UINT32 tail;\r
+ ANTLR3_UINT32 head;\r
+}\r
+ ANTLR3_TREE_WALK_STATE;\r
+\r
+#ifdef __cplusplus\r
+}\r
+#endif\r
+\r
+#endif\r