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