001: /*
002: [The "BSD licence"]
003: Copyright (c) 2005-2006 Terence Parr
004: All rights reserved.
005:
006: Redistribution and use in source and binary forms, with or without
007: modification, are permitted provided that the following conditions
008: are met:
009: 1. Redistributions of source code must retain the above copyright
010: notice, this list of conditions and the following disclaimer.
011: 2. Redistributions in binary form must reproduce the above copyright
012: notice, this list of conditions and the following disclaimer in the
013: documentation and/or other materials provided with the distribution.
014: 3. The name of the author may not be used to endorse or promote products
015: derived from this software without specific prior written permission.
016:
017: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
018: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
019: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
020: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
021: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
022: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
023: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
024: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
026: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: */
028: package org.antlr.test;
029:
030: import org.antlr.runtime.CommonToken;
031: import org.antlr.runtime.Token;
032: import org.antlr.runtime.tree.CommonTree;
033: import org.antlr.runtime.tree.CommonTreeNodeStream;
034: import org.antlr.runtime.tree.Tree;
035:
036: /** Tests specific to CommonTreeNodeStream */
037: public class TestCommonTreeNodeStream extends TestTreeNodeStream {
038: public void testPushPop() throws Exception {
039: // ^(101 ^(102 103) ^(104 105) ^(106 107) 108 109)
040: // stream has 9 real + 8 nav nodes
041: // Sequence of types: 101 DN 102 DN 103 UP 104 DN 105 UP 106 DN 107 UP 108 109 UP
042: Tree r0 = new CommonTree(new CommonToken(101));
043: Tree r1 = new CommonTree(new CommonToken(102));
044: r1.addChild(new CommonTree(new CommonToken(103)));
045: r0.addChild(r1);
046: Tree r2 = new CommonTree(new CommonToken(104));
047: r2.addChild(new CommonTree(new CommonToken(105)));
048: r0.addChild(r2);
049: Tree r3 = new CommonTree(new CommonToken(106));
050: r3.addChild(new CommonTree(new CommonToken(107)));
051: r0.addChild(r3);
052: r0.addChild(new CommonTree(new CommonToken(108)));
053: r0.addChild(new CommonTree(new CommonToken(109)));
054:
055: CommonTreeNodeStream stream = new CommonTreeNodeStream(r0);
056: String expecting = " 101 2 102 2 103 3 104 2 105 3 106 2 107 3 108 109 3";
057: String found = stream.toString();
058: assertEquals(expecting, found);
059:
060: // Assume we want to hit node 107 and then "call 102" then return
061:
062: int indexOf102 = 2;
063: int indexOf107 = 12;
064: for (int k = 1; k <= indexOf107; k++) { // consume til 107 node
065: stream.consume();
066: }
067: // CALL 102
068: assertEquals(107, ((Tree) stream.LT(1)).getType());
069: stream.push(indexOf102);
070: assertEquals(102, ((Tree) stream.LT(1)).getType());
071: stream.consume(); // consume 102
072: assertEquals(Token.DOWN, ((Tree) stream.LT(1)).getType());
073: stream.consume(); // consume DN
074: assertEquals(103, ((Tree) stream.LT(1)).getType());
075: stream.consume(); // consume 103
076: assertEquals(Token.UP, ((Tree) stream.LT(1)).getType());
077: // RETURN
078: stream.pop();
079: assertEquals(107, ((Tree) stream.LT(1)).getType());
080: }
081:
082: public void testNestedPushPop() throws Exception {
083: // ^(101 ^(102 103) ^(104 105) ^(106 107) 108 109)
084: // stream has 9 real + 8 nav nodes
085: // Sequence of types: 101 DN 102 DN 103 UP 104 DN 105 UP 106 DN 107 UP 108 109 UP
086: Tree r0 = new CommonTree(new CommonToken(101));
087: Tree r1 = new CommonTree(new CommonToken(102));
088: r1.addChild(new CommonTree(new CommonToken(103)));
089: r0.addChild(r1);
090: Tree r2 = new CommonTree(new CommonToken(104));
091: r2.addChild(new CommonTree(new CommonToken(105)));
092: r0.addChild(r2);
093: Tree r3 = new CommonTree(new CommonToken(106));
094: r3.addChild(new CommonTree(new CommonToken(107)));
095: r0.addChild(r3);
096: r0.addChild(new CommonTree(new CommonToken(108)));
097: r0.addChild(new CommonTree(new CommonToken(109)));
098:
099: CommonTreeNodeStream stream = new CommonTreeNodeStream(r0);
100:
101: // Assume we want to hit node 107 and then "call 102", which
102: // calls 104, then return
103:
104: int indexOf102 = 2;
105: int indexOf107 = 12;
106: for (int k = 1; k <= indexOf107; k++) { // consume til 107 node
107: stream.consume();
108: }
109: assertEquals(107, ((Tree) stream.LT(1)).getType());
110: // CALL 102
111: stream.push(indexOf102);
112: assertEquals(102, ((Tree) stream.LT(1)).getType());
113: stream.consume(); // consume 102
114: assertEquals(Token.DOWN, ((Tree) stream.LT(1)).getType());
115: stream.consume(); // consume DN
116: assertEquals(103, ((Tree) stream.LT(1)).getType());
117: stream.consume(); // consume 103
118:
119: // CALL 104
120: int indexOf104 = 6;
121: stream.push(indexOf104);
122: assertEquals(104, ((Tree) stream.LT(1)).getType());
123: stream.consume(); // consume 102
124: assertEquals(Token.DOWN, ((Tree) stream.LT(1)).getType());
125: stream.consume(); // consume DN
126: assertEquals(105, ((Tree) stream.LT(1)).getType());
127: stream.consume(); // consume 103
128: assertEquals(Token.UP, ((Tree) stream.LT(1)).getType());
129: // RETURN (to UP node in 102 subtree)
130: stream.pop();
131:
132: assertEquals(Token.UP, ((Tree) stream.LT(1)).getType());
133: // RETURN (to empty stack)
134: stream.pop();
135: assertEquals(107, ((Tree) stream.LT(1)).getType());
136: }
137:
138: public void testPushPopFromEOF() throws Exception {
139: // ^(101 ^(102 103) ^(104 105) ^(106 107) 108 109)
140: // stream has 9 real + 8 nav nodes
141: // Sequence of types: 101 DN 102 DN 103 UP 104 DN 105 UP 106 DN 107 UP 108 109 UP
142: Tree r0 = new CommonTree(new CommonToken(101));
143: Tree r1 = new CommonTree(new CommonToken(102));
144: r1.addChild(new CommonTree(new CommonToken(103)));
145: r0.addChild(r1);
146: Tree r2 = new CommonTree(new CommonToken(104));
147: r2.addChild(new CommonTree(new CommonToken(105)));
148: r0.addChild(r2);
149: Tree r3 = new CommonTree(new CommonToken(106));
150: r3.addChild(new CommonTree(new CommonToken(107)));
151: r0.addChild(r3);
152: r0.addChild(new CommonTree(new CommonToken(108)));
153: r0.addChild(new CommonTree(new CommonToken(109)));
154:
155: CommonTreeNodeStream stream = new CommonTreeNodeStream(r0);
156:
157: while (stream.LA(1) != Token.EOF) {
158: stream.consume();
159: }
160: int indexOf102 = 2;
161: int indexOf104 = 6;
162: assertEquals(Token.EOF, ((Tree) stream.LT(1)).getType());
163:
164: // CALL 102
165: stream.push(indexOf102);
166: assertEquals(102, ((Tree) stream.LT(1)).getType());
167: stream.consume(); // consume 102
168: assertEquals(Token.DOWN, ((Tree) stream.LT(1)).getType());
169: stream.consume(); // consume DN
170: assertEquals(103, ((Tree) stream.LT(1)).getType());
171: stream.consume(); // consume 103
172: assertEquals(Token.UP, ((Tree) stream.LT(1)).getType());
173: // RETURN (to empty stack)
174: stream.pop();
175: assertEquals(Token.EOF, ((Tree) stream.LT(1)).getType());
176:
177: // CALL 104
178: stream.push(indexOf104);
179: assertEquals(104, ((Tree) stream.LT(1)).getType());
180: stream.consume(); // consume 102
181: assertEquals(Token.DOWN, ((Tree) stream.LT(1)).getType());
182: stream.consume(); // consume DN
183: assertEquals(105, ((Tree) stream.LT(1)).getType());
184: stream.consume(); // consume 103
185: assertEquals(Token.UP, ((Tree) stream.LT(1)).getType());
186: // RETURN (to empty stack)
187: stream.pop();
188: assertEquals(Token.EOF, ((Tree) stream.LT(1)).getType());
189: }
190:
191: public void testStackStretch() throws Exception {
192: // make more than INITIAL_CALL_STACK_SIZE pushes
193: Tree r0 = new CommonTree(new CommonToken(101));
194: CommonTreeNodeStream stream = new CommonTreeNodeStream(r0);
195: // go 1 over initial size
196: for (int i = 1; i <= CommonTreeNodeStream.INITIAL_CALL_STACK_SIZE + 1; i++) {
197: stream.push(i);
198: }
199: assertEquals(10, stream.pop());
200: assertEquals(9, stream.pop());
201: }
202:
203: }
|