1*16467b97STreehugger Robot #ifndef ANTLR3REWRITESTREAM_H 2*16467b97STreehugger Robot #define ANTLR3REWRITESTREAM_H 3*16467b97STreehugger Robot 4*16467b97STreehugger Robot // [The "BSD licence"] 5*16467b97STreehugger Robot // Copyright (c) 2005-2009 Jim Idle, Temporal Wave LLC 6*16467b97STreehugger Robot // http://www.temporal-wave.com 7*16467b97STreehugger Robot // http://www.linkedin.com/in/jimidle 8*16467b97STreehugger Robot // 9*16467b97STreehugger Robot // All rights reserved. 10*16467b97STreehugger Robot // 11*16467b97STreehugger Robot // Redistribution and use in source and binary forms, with or without 12*16467b97STreehugger Robot // modification, are permitted provided that the following conditions 13*16467b97STreehugger Robot // are met: 14*16467b97STreehugger Robot // 1. Redistributions of source code must retain the above copyright 15*16467b97STreehugger Robot // notice, this list of conditions and the following disclaimer. 16*16467b97STreehugger Robot // 2. Redistributions in binary form must reproduce the above copyright 17*16467b97STreehugger Robot // notice, this list of conditions and the following disclaimer in the 18*16467b97STreehugger Robot // documentation and/or other materials provided with the distribution. 19*16467b97STreehugger Robot // 3. The name of the author may not be used to endorse or promote products 20*16467b97STreehugger Robot // derived from this software without specific prior written permission. 21*16467b97STreehugger Robot // 22*16467b97STreehugger Robot // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 23*16467b97STreehugger Robot // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 24*16467b97STreehugger Robot // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 25*16467b97STreehugger Robot // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 26*16467b97STreehugger Robot // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 27*16467b97STreehugger Robot // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28*16467b97STreehugger Robot // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29*16467b97STreehugger Robot // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30*16467b97STreehugger Robot // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 31*16467b97STreehugger Robot // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32*16467b97STreehugger Robot 33*16467b97STreehugger Robot #include <antlr3defs.h> 34*16467b97STreehugger Robot #include <antlr3collections.h> 35*16467b97STreehugger Robot #include <antlr3commontreeadaptor.h> 36*16467b97STreehugger Robot #include <antlr3baserecognizer.h> 37*16467b97STreehugger Robot 38*16467b97STreehugger Robot #ifdef __cplusplus 39*16467b97STreehugger Robot extern "C" { 40*16467b97STreehugger Robot #endif 41*16467b97STreehugger Robot 42*16467b97STreehugger Robot /// A generic list of elements tracked in an alternative to be used in 43*16467b97STreehugger Robot /// a -> rewrite rule. 44*16467b97STreehugger Robot /// 45*16467b97STreehugger Robot /// In the C implementation, all tree oriented streams return a pointer to 46*16467b97STreehugger Robot /// the same type: pANTLR3_BASE_TREE. Anything that has subclassed from this 47*16467b97STreehugger Robot /// still passes this type, within which there is a super pointer, which points 48*16467b97STreehugger Robot /// to it's own data and methods. Hence we do not need to implement this as 49*16467b97STreehugger Robot /// the equivalent of an abstract class, but just fill in the appropriate interface 50*16467b97STreehugger Robot /// as usual with this model. 51*16467b97STreehugger Robot /// 52*16467b97STreehugger Robot /// Once you start next()ing, do not try to add more elements. It will 53*16467b97STreehugger Robot /// break the cursor tracking I believe. 54*16467b97STreehugger Robot /// 55*16467b97STreehugger Robot /// 56*16467b97STreehugger Robot /// \see #pANTLR3_REWRITE_RULE_NODE_STREAM 57*16467b97STreehugger Robot /// \see #pANTLR3_REWRITE_RULE_ELEMENT_STREAM 58*16467b97STreehugger Robot /// \see #pANTLR3_REWRITE_RULE_SUBTREE_STREAM 59*16467b97STreehugger Robot /// 60*16467b97STreehugger Robot /// TODO: add mechanism to detect/puke on modification after reading from stream 61*16467b97STreehugger Robot /// 62*16467b97STreehugger Robot typedef struct ANTLR3_REWRITE_RULE_ELEMENT_STREAM_struct 63*16467b97STreehugger Robot { 64*16467b97STreehugger Robot 65*16467b97STreehugger Robot /// Cursor 0..n-1. If singleElement!=NULL, cursor is 0 until you next(), 66*16467b97STreehugger Robot /// which bumps it to 1 meaning no more elements. 67*16467b97STreehugger Robot /// 68*16467b97STreehugger Robot ANTLR3_UINT32 cursor; 69*16467b97STreehugger Robot 70*16467b97STreehugger Robot /// Track single elements w/o creating a list. Upon 2nd add, alloc list 71*16467b97STreehugger Robot /// 72*16467b97STreehugger Robot void * singleElement; 73*16467b97STreehugger Robot 74*16467b97STreehugger Robot /// The list of tokens or subtrees we are tracking 75*16467b97STreehugger Robot /// 76*16467b97STreehugger Robot pANTLR3_VECTOR elements; 77*16467b97STreehugger Robot 78*16467b97STreehugger Robot /// Indicates whether we should free the vector or it was supplied to us 79*16467b97STreehugger Robot /// 80*16467b97STreehugger Robot ANTLR3_BOOLEAN freeElements; 81*16467b97STreehugger Robot 82*16467b97STreehugger Robot /// The element or stream description; usually has name of the token or 83*16467b97STreehugger Robot /// rule reference that this list tracks. Can include rulename too, but 84*16467b97STreehugger Robot /// the exception would track that info. 85*16467b97STreehugger Robot /// 86*16467b97STreehugger Robot void * elementDescription; 87*16467b97STreehugger Robot 88*16467b97STreehugger Robot /// Pointer to the tree adaptor in use for this stream 89*16467b97STreehugger Robot /// 90*16467b97STreehugger Robot pANTLR3_BASE_TREE_ADAPTOR adaptor; 91*16467b97STreehugger Robot 92*16467b97STreehugger Robot /// Once a node / subtree has been used in a stream, it must be dup'ed 93*16467b97STreehugger Robot /// from then on. Streams are reset after sub rules so that the streams 94*16467b97STreehugger Robot /// can be reused in future sub rules. So, reset must set a dirty bit. 95*16467b97STreehugger Robot /// If dirty, then next() always returns a dup. 96*16467b97STreehugger Robot /// 97*16467b97STreehugger Robot ANTLR3_BOOLEAN dirty; 98*16467b97STreehugger Robot 99*16467b97STreehugger Robot // Pointer to the recognizer shared state to which this stream belongs 100*16467b97STreehugger Robot // 101*16467b97STreehugger Robot pANTLR3_BASE_RECOGNIZER rec; 102*16467b97STreehugger Robot 103*16467b97STreehugger Robot // Methods 104*16467b97STreehugger Robot 105*16467b97STreehugger Robot /// Reset the condition of this stream so that it appears we have 106*16467b97STreehugger Robot /// not consumed any of its elements. Elements themselves are untouched. 107*16467b97STreehugger Robot /// 108*16467b97STreehugger Robot void (*reset) (struct ANTLR3_REWRITE_RULE_ELEMENT_STREAM_struct * stream); 109*16467b97STreehugger Robot 110*16467b97STreehugger Robot /// Add a new pANTLR3_BASE_TREE to this stream 111*16467b97STreehugger Robot /// 112*16467b97STreehugger Robot void (*add) (struct ANTLR3_REWRITE_RULE_ELEMENT_STREAM_struct * stream, void *el, void (ANTLR3_CDECL *freePtr)(void *)); 113*16467b97STreehugger Robot 114*16467b97STreehugger Robot /// Return the next element in the stream. If out of elements, throw 115*16467b97STreehugger Robot /// an exception unless size()==1. If size is 1, then return elements[0]. 116*16467b97STreehugger Robot /// 117*16467b97STreehugger Robot void * (*next) (struct ANTLR3_REWRITE_RULE_ELEMENT_STREAM_struct * stream); 118*16467b97STreehugger Robot pANTLR3_BASE_TREE (*nextTree) (struct ANTLR3_REWRITE_RULE_ELEMENT_STREAM_struct * stream); 119*16467b97STreehugger Robot void * (*nextToken) (struct ANTLR3_REWRITE_RULE_ELEMENT_STREAM_struct * stream); 120*16467b97STreehugger Robot void * (*_next) (struct ANTLR3_REWRITE_RULE_ELEMENT_STREAM_struct * stream); 121*16467b97STreehugger Robot 122*16467b97STreehugger Robot /// When constructing trees, sometimes we need to dup a token or AST 123*16467b97STreehugger Robot /// subtree. Dup'ing a token means just creating another AST node 124*16467b97STreehugger Robot /// around it. For trees, you must call the adaptor.dupTree(). 125*16467b97STreehugger Robot /// 126*16467b97STreehugger Robot void * (*dup) (struct ANTLR3_REWRITE_RULE_ELEMENT_STREAM_struct * stream, void * el); 127*16467b97STreehugger Robot 128*16467b97STreehugger Robot /// Ensure stream emits trees; tokens must be converted to AST nodes. 129*16467b97STreehugger Robot /// AST nodes can be passed through unmolested. 130*16467b97STreehugger Robot /// 131*16467b97STreehugger Robot pANTLR3_BASE_TREE (*toTree) (struct ANTLR3_REWRITE_RULE_ELEMENT_STREAM_struct * stream, void * el); 132*16467b97STreehugger Robot 133*16467b97STreehugger Robot /// Returns ANTLR3_TRUE if there is a next element available 134*16467b97STreehugger Robot /// 135*16467b97STreehugger Robot ANTLR3_BOOLEAN (*hasNext) (struct ANTLR3_REWRITE_RULE_ELEMENT_STREAM_struct * stream); 136*16467b97STreehugger Robot 137*16467b97STreehugger Robot /// Treat next element as a single node even if it's a subtree. 138*16467b97STreehugger Robot /// This is used instead of next() when the result has to be a 139*16467b97STreehugger Robot /// tree root node. Also prevents us from duplicating recently-added 140*16467b97STreehugger Robot /// children; e.g., ^(type ID)+ adds ID to type and then 2nd iteration 141*16467b97STreehugger Robot /// must dup the type node, but ID has been added. 142*16467b97STreehugger Robot /// 143*16467b97STreehugger Robot /// Referencing to a rule result twice is ok; dup entire tree as 144*16467b97STreehugger Robot /// we can't be adding trees; e.g., expr expr. 145*16467b97STreehugger Robot /// 146*16467b97STreehugger Robot pANTLR3_BASE_TREE (*nextNode) (struct ANTLR3_REWRITE_RULE_ELEMENT_STREAM_struct * stream); 147*16467b97STreehugger Robot 148*16467b97STreehugger Robot /// Number of elements available in the stream 149*16467b97STreehugger Robot /// 150*16467b97STreehugger Robot ANTLR3_UINT32 (*size) (struct ANTLR3_REWRITE_RULE_ELEMENT_STREAM_struct * stream); 151*16467b97STreehugger Robot 152*16467b97STreehugger Robot /// Returns the description string if there is one available (check for NULL). 153*16467b97STreehugger Robot /// 154*16467b97STreehugger Robot void * (*getDescription) (struct ANTLR3_REWRITE_RULE_ELEMENT_STREAM_struct * stream); 155*16467b97STreehugger Robot 156*16467b97STreehugger Robot void (*free) (struct ANTLR3_REWRITE_RULE_ELEMENT_STREAM_struct * stream); 157*16467b97STreehugger Robot 158*16467b97STreehugger Robot } 159*16467b97STreehugger Robot ANTLR3_REWRITE_RULE_ELEMENT_STREAM; 160*16467b97STreehugger Robot 161*16467b97STreehugger Robot /// This is an implementation of a token stream, which is basically an element 162*16467b97STreehugger Robot /// stream that deals with tokens only. 163*16467b97STreehugger Robot /// 164*16467b97STreehugger Robot typedef struct ANTLR3_REWRITE_RULE_ELEMENT_STREAM_struct ANTLR3_REWRITE_RULE_TOKEN_STREAM; 165*16467b97STreehugger Robot 166*16467b97STreehugger Robot /// This is an implementation of a subtree stream which is a set of trees 167*16467b97STreehugger Robot /// modelled as an element stream. 168*16467b97STreehugger Robot /// 169*16467b97STreehugger Robot typedef struct ANTLR3_REWRITE_RULE_ELEMENT_STREAM_struct ANTLR3_REWRITE_RULE_SUBTREE_STREAM; 170*16467b97STreehugger Robot 171*16467b97STreehugger Robot /// This is an implementation of a node stream, which is basically an element 172*16467b97STreehugger Robot /// stream that deals with tree nodes only. 173*16467b97STreehugger Robot /// 174*16467b97STreehugger Robot typedef struct ANTLR3_REWRITE_RULE_ELEMENT_STREAM_struct ANTLR3_REWRITE_RULE_NODE_STREAM; 175*16467b97STreehugger Robot 176*16467b97STreehugger Robot #ifdef __cplusplus 177*16467b97STreehugger Robot } 178*16467b97STreehugger Robot #endif 179*16467b97STreehugger Robot 180*16467b97STreehugger Robot #endif 181