• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * [The "BSD license"]
3  *  Copyright (c) 2010 Terence Parr
4  *  All rights reserved.
5  *
6  *  Redistribution and use in source and binary forms, with or without
7  *  modification, are permitted provided that the following conditions
8  *  are met:
9  *  1. Redistributions of source code must retain the above copyright
10  *      notice, this list of conditions and the following disclaimer.
11  *  2. Redistributions in binary form must reproduce the above copyright
12  *      notice, this list of conditions and the following disclaimer in the
13  *      documentation and/or other materials provided with the distribution.
14  *  3. The name of the author may not be used to endorse or promote products
15  *      derived from this software without specific prior written permission.
16  *
17  *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 package org.antlr.test;
29 
30 import org.antlr.runtime.CommonToken;
31 import org.antlr.runtime.tree.BufferedTreeNodeStream;
32 import org.antlr.runtime.tree.CommonTree;
33 import org.antlr.runtime.tree.Tree;
34 import org.antlr.runtime.tree.TreeNodeStream;
35 import org.junit.Test;
36 
37 import static org.junit.Assert.*;
38 
39 public class TestBufferedTreeNodeStream extends TestTreeNodeStream {
40     // inherits tests; these methods make it use a new buffer
41 
42 	@Override
newStream(Object t)43 	public TreeNodeStream newStream(Object t) {
44 		return new BufferedTreeNodeStream(t);
45 	}
46 
47 	@Override
toTokenTypeString(TreeNodeStream stream)48     public String toTokenTypeString(TreeNodeStream stream) {
49         return ((BufferedTreeNodeStream)stream).toTokenTypeString();
50     }
51 
testSeek()52     @Test public void testSeek() throws Exception {
53         // ^(101 ^(102 103 ^(106 107) ) 104 105)
54         // stream has 7 real + 6 nav nodes
55         // Sequence of types: 101 DN 102 DN 103 106 DN 107 UP UP 104 105 UP EOF
56         Tree r0 = new CommonTree(new CommonToken(101));
57         Tree r1 = new CommonTree(new CommonToken(102));
58         r0.addChild(r1);
59         r1.addChild(new CommonTree(new CommonToken(103)));
60         Tree r2 = new CommonTree(new CommonToken(106));
61         r2.addChild(new CommonTree(new CommonToken(107)));
62         r1.addChild(r2);
63         r0.addChild(new CommonTree(new CommonToken(104)));
64         r0.addChild(new CommonTree(new CommonToken(105)));
65 
66         TreeNodeStream stream = newStream(r0);
67         stream.consume(); // consume 101
68         stream.consume(); // consume DN
69         stream.consume(); // consume 102
70         stream.seek(7);   // seek to 107
71         assertEquals(107, ((Tree)stream.LT(1)).getType());
72         stream.consume(); // consume 107
73         stream.consume(); // consume UP
74         stream.consume(); // consume UP
75         assertEquals(104, ((Tree)stream.LT(1)).getType());
76     }
77 }
78