forked from jasder/antlr
add more tests from v3
[git-p4: depot-paths = "//depot/code/antlr4/main/": change = 9073]
This commit is contained in:
parent
9c5f636bea
commit
149033fb2e
|
@ -0,0 +1,132 @@
|
|||
/*
|
||||
* [The "BSD license"]
|
||||
* Copyright (c) 2010 Terence Parr
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
package org.antlr.test;
|
||||
|
||||
import org.antlr.runtime.tree.*;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class TestTreeIterator {
|
||||
static final String[] tokens = new String[] {
|
||||
"<invalid>", "<EOR>", "<DOWN>", "<UP>", "A", "B", "C", "D", "E", "F", "G"
|
||||
};
|
||||
|
||||
@Test public void testNode() {
|
||||
TreeAdaptor adaptor = new CommonTreeAdaptor();
|
||||
TreeWizard wiz = new TreeWizard(adaptor, tokens);
|
||||
CommonTree t = (CommonTree)wiz.create("A");
|
||||
TreeIterator it = new TreeIterator(t);
|
||||
StringBuffer buf = toString(it);
|
||||
String expecting = "A EOF";
|
||||
String found = buf.toString();
|
||||
assertEquals(expecting, found);
|
||||
}
|
||||
|
||||
@Test public void testFlatAB() {
|
||||
TreeAdaptor adaptor = new CommonTreeAdaptor();
|
||||
TreeWizard wiz = new TreeWizard(adaptor, tokens);
|
||||
CommonTree t = (CommonTree)wiz.create("(nil A B)");
|
||||
TreeIterator it = new TreeIterator(t);
|
||||
StringBuffer buf = toString(it);
|
||||
String expecting = "nil DOWN A B UP EOF";
|
||||
String found = buf.toString();
|
||||
assertEquals(expecting, found);
|
||||
}
|
||||
|
||||
@Test public void testAB() {
|
||||
TreeAdaptor adaptor = new CommonTreeAdaptor();
|
||||
TreeWizard wiz = new TreeWizard(adaptor, tokens);
|
||||
CommonTree t = (CommonTree)wiz.create("(A B)");
|
||||
TreeIterator it = new TreeIterator(t);
|
||||
StringBuffer buf = toString(it);
|
||||
String expecting = "A DOWN B UP EOF";
|
||||
String found = buf.toString();
|
||||
assertEquals(expecting, found);
|
||||
}
|
||||
|
||||
@Test public void testABC() {
|
||||
TreeAdaptor adaptor = new CommonTreeAdaptor();
|
||||
TreeWizard wiz = new TreeWizard(adaptor, tokens);
|
||||
CommonTree t = (CommonTree)wiz.create("(A B C)");
|
||||
TreeIterator it = new TreeIterator(t);
|
||||
StringBuffer buf = toString(it);
|
||||
String expecting = "A DOWN B C UP EOF";
|
||||
String found = buf.toString();
|
||||
assertEquals(expecting, found);
|
||||
}
|
||||
|
||||
@Test public void testVerticalList() {
|
||||
TreeAdaptor adaptor = new CommonTreeAdaptor();
|
||||
TreeWizard wiz = new TreeWizard(adaptor, tokens);
|
||||
CommonTree t = (CommonTree)wiz.create("(A (B C))");
|
||||
TreeIterator it = new TreeIterator(t);
|
||||
StringBuffer buf = toString(it);
|
||||
String expecting = "A DOWN B DOWN C UP UP EOF";
|
||||
String found = buf.toString();
|
||||
assertEquals(expecting, found);
|
||||
}
|
||||
|
||||
@Test public void testComplex() {
|
||||
TreeAdaptor adaptor = new CommonTreeAdaptor();
|
||||
TreeWizard wiz = new TreeWizard(adaptor, tokens);
|
||||
CommonTree t = (CommonTree)wiz.create("(A (B (C D E) F) G)");
|
||||
TreeIterator it = new TreeIterator(t);
|
||||
StringBuffer buf = toString(it);
|
||||
String expecting = "A DOWN B DOWN C DOWN D E UP F UP G UP EOF";
|
||||
String found = buf.toString();
|
||||
assertEquals(expecting, found);
|
||||
}
|
||||
|
||||
@Test public void testReset() {
|
||||
TreeAdaptor adaptor = new CommonTreeAdaptor();
|
||||
TreeWizard wiz = new TreeWizard(adaptor, tokens);
|
||||
CommonTree t = (CommonTree)wiz.create("(A (B (C D E) F) G)");
|
||||
TreeIterator it = new TreeIterator(t);
|
||||
StringBuffer buf = toString(it);
|
||||
String expecting = "A DOWN B DOWN C DOWN D E UP F UP G UP EOF";
|
||||
String found = buf.toString();
|
||||
assertEquals(expecting, found);
|
||||
|
||||
it.reset();
|
||||
buf = toString(it);
|
||||
expecting = "A DOWN B DOWN C DOWN D E UP F UP G UP EOF";
|
||||
found = buf.toString();
|
||||
assertEquals(expecting, found);
|
||||
}
|
||||
|
||||
protected static StringBuffer toString(TreeIterator it) {
|
||||
StringBuffer buf = new StringBuffer();
|
||||
while ( it.hasNext() ) {
|
||||
CommonTree n = (CommonTree)it.next();
|
||||
buf.append(n);
|
||||
if ( it.hasNext() ) buf.append(" ");
|
||||
}
|
||||
return buf;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,376 @@
|
|||
/*
|
||||
* [The "BSD license"]
|
||||
* Copyright (c) 2010 Terence Parr
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
package org.antlr.test;
|
||||
|
||||
import org.antlr.runtime.CommonToken;
|
||||
import org.antlr.runtime.Token;
|
||||
import org.antlr.runtime.tree.*;
|
||||
import org.junit.Test;
|
||||
|
||||
/** Test the tree node stream. */
|
||||
public class TestTreeNodeStream extends BaseTest {
|
||||
|
||||
/** Build new stream; let's us override to test other streams. */
|
||||
public TreeNodeStream newStream(Object t) {
|
||||
return new CommonTreeNodeStream(t);
|
||||
}
|
||||
|
||||
public String toTokenTypeString(TreeNodeStream stream) {
|
||||
return ((CommonTreeNodeStream)stream).toTokenTypeString();
|
||||
}
|
||||
|
||||
@Test public void testSingleNode() throws Exception {
|
||||
Tree t = new CommonTree(new CommonToken(101));
|
||||
|
||||
TreeNodeStream stream = newStream(t);
|
||||
String expecting = " 101";
|
||||
String found = toNodesOnlyString(stream);
|
||||
assertEquals(expecting, found);
|
||||
|
||||
expecting = " 101";
|
||||
found = toTokenTypeString(stream);
|
||||
assertEquals(expecting, found);
|
||||
}
|
||||
|
||||
@Test public void test4Nodes() throws Exception {
|
||||
// ^(101 ^(102 103) 104)
|
||||
Tree t = new CommonTree(new CommonToken(101));
|
||||
t.addChild(new CommonTree(new CommonToken(102)));
|
||||
t.getChild(0).addChild(new CommonTree(new CommonToken(103)));
|
||||
t.addChild(new CommonTree(new CommonToken(104)));
|
||||
|
||||
TreeNodeStream stream = newStream(t);
|
||||
String expecting = " 101 102 103 104";
|
||||
String found = toNodesOnlyString(stream);
|
||||
assertEquals(expecting, found);
|
||||
|
||||
expecting = " 101 2 102 2 103 3 104 3";
|
||||
found = toTokenTypeString(stream);
|
||||
assertEquals(expecting, found);
|
||||
}
|
||||
|
||||
@Test public void testList() throws Exception {
|
||||
Tree root = new CommonTree((Token)null);
|
||||
|
||||
Tree t = new CommonTree(new CommonToken(101));
|
||||
t.addChild(new CommonTree(new CommonToken(102)));
|
||||
t.getChild(0).addChild(new CommonTree(new CommonToken(103)));
|
||||
t.addChild(new CommonTree(new CommonToken(104)));
|
||||
|
||||
Tree u = new CommonTree(new CommonToken(105));
|
||||
|
||||
root.addChild(t);
|
||||
root.addChild(u);
|
||||
|
||||
TreeNodeStream stream = newStream(root);
|
||||
String expecting = " 101 102 103 104 105";
|
||||
String found = toNodesOnlyString(stream);
|
||||
assertEquals(expecting, found);
|
||||
|
||||
expecting = " 101 2 102 2 103 3 104 3 105";
|
||||
found = toTokenTypeString(stream);
|
||||
assertEquals(expecting, found);
|
||||
}
|
||||
|
||||
@Test public void testFlatList() throws Exception {
|
||||
Tree root = new CommonTree((Token)null);
|
||||
|
||||
root.addChild(new CommonTree(new CommonToken(101)));
|
||||
root.addChild(new CommonTree(new CommonToken(102)));
|
||||
root.addChild(new CommonTree(new CommonToken(103)));
|
||||
|
||||
TreeNodeStream stream = newStream(root);
|
||||
String expecting = " 101 102 103";
|
||||
String found = toNodesOnlyString(stream);
|
||||
assertEquals(expecting, found);
|
||||
|
||||
expecting = " 101 102 103";
|
||||
found = toTokenTypeString(stream);
|
||||
assertEquals(expecting, found);
|
||||
}
|
||||
|
||||
@Test public void testListWithOneNode() throws Exception {
|
||||
Tree root = new CommonTree((Token)null);
|
||||
|
||||
root.addChild(new CommonTree(new CommonToken(101)));
|
||||
|
||||
TreeNodeStream stream = newStream(root);
|
||||
String expecting = " 101";
|
||||
String found = toNodesOnlyString(stream);
|
||||
assertEquals(expecting, found);
|
||||
|
||||
expecting = " 101";
|
||||
found = toTokenTypeString(stream);
|
||||
assertEquals(expecting, found);
|
||||
}
|
||||
|
||||
@Test public void testAoverB() throws Exception {
|
||||
Tree t = new CommonTree(new CommonToken(101));
|
||||
t.addChild(new CommonTree(new CommonToken(102)));
|
||||
|
||||
TreeNodeStream stream = newStream(t);
|
||||
String expecting = " 101 102";
|
||||
String found = toNodesOnlyString(stream);
|
||||
assertEquals(expecting, found);
|
||||
|
||||
expecting = " 101 2 102 3";
|
||||
found = toTokenTypeString(stream);
|
||||
assertEquals(expecting, found);
|
||||
}
|
||||
|
||||
@Test public void testLT() throws Exception {
|
||||
// ^(101 ^(102 103) 104)
|
||||
Tree t = new CommonTree(new CommonToken(101));
|
||||
t.addChild(new CommonTree(new CommonToken(102)));
|
||||
t.getChild(0).addChild(new CommonTree(new CommonToken(103)));
|
||||
t.addChild(new CommonTree(new CommonToken(104)));
|
||||
|
||||
TreeNodeStream stream = newStream(t);
|
||||
assertEquals(101, ((Tree)stream.LT(1)).getType());
|
||||
assertEquals(Token.DOWN, ((Tree)stream.LT(2)).getType());
|
||||
assertEquals(102, ((Tree)stream.LT(3)).getType());
|
||||
assertEquals(Token.DOWN, ((Tree)stream.LT(4)).getType());
|
||||
assertEquals(103, ((Tree)stream.LT(5)).getType());
|
||||
assertEquals(Token.UP, ((Tree)stream.LT(6)).getType());
|
||||
assertEquals(104, ((Tree)stream.LT(7)).getType());
|
||||
assertEquals(Token.UP, ((Tree)stream.LT(8)).getType());
|
||||
assertEquals(Token.EOF, ((Tree)stream.LT(9)).getType());
|
||||
// check way ahead
|
||||
assertEquals(Token.EOF, ((Tree)stream.LT(100)).getType());
|
||||
}
|
||||
|
||||
@Test public void testMarkRewindEntire() throws Exception {
|
||||
// ^(101 ^(102 103 ^(106 107) ) 104 105)
|
||||
// stream has 7 real + 6 nav nodes
|
||||
// Sequence of types: 101 DN 102 DN 103 106 DN 107 UP UP 104 105 UP EOF
|
||||
Tree r0 = new CommonTree(new CommonToken(101));
|
||||
Tree r1 = new CommonTree(new CommonToken(102));
|
||||
r0.addChild(r1);
|
||||
r1.addChild(new CommonTree(new CommonToken(103)));
|
||||
Tree r2 = new CommonTree(new CommonToken(106));
|
||||
r2.addChild(new CommonTree(new CommonToken(107)));
|
||||
r1.addChild(r2);
|
||||
r0.addChild(new CommonTree(new CommonToken(104)));
|
||||
r0.addChild(new CommonTree(new CommonToken(105)));
|
||||
|
||||
TreeNodeStream stream = newStream(r0);
|
||||
int m = stream.mark(); // MARK
|
||||
for (int k=1; k<=13; k++) { // consume til end
|
||||
stream.LT(1);
|
||||
stream.consume();
|
||||
}
|
||||
assertEquals(Token.EOF, ((Tree)stream.LT(1)).getType());
|
||||
stream.rewind(m); // REWIND
|
||||
|
||||
// consume til end again :)
|
||||
for (int k=1; k<=13; k++) { // consume til end
|
||||
stream.LT(1);
|
||||
stream.consume();
|
||||
}
|
||||
assertEquals(Token.EOF, ((Tree)stream.LT(1)).getType());
|
||||
}
|
||||
|
||||
@Test public void testMarkRewindInMiddle() throws Exception {
|
||||
// ^(101 ^(102 103 ^(106 107) ) 104 105)
|
||||
// stream has 7 real + 6 nav nodes
|
||||
// Sequence of types: 101 DN 102 DN 103 106 DN 107 UP UP 104 105 UP EOF
|
||||
Tree r0 = new CommonTree(new CommonToken(101));
|
||||
Tree r1 = new CommonTree(new CommonToken(102));
|
||||
r0.addChild(r1);
|
||||
r1.addChild(new CommonTree(new CommonToken(103)));
|
||||
Tree r2 = new CommonTree(new CommonToken(106));
|
||||
r2.addChild(new CommonTree(new CommonToken(107)));
|
||||
r1.addChild(r2);
|
||||
r0.addChild(new CommonTree(new CommonToken(104)));
|
||||
r0.addChild(new CommonTree(new CommonToken(105)));
|
||||
|
||||
TreeNodeStream stream = newStream(r0);
|
||||
for (int k=1; k<=7; k++) { // consume til middle
|
||||
//System.out.println(((Tree)stream.LT(1)).getType());
|
||||
stream.consume();
|
||||
}
|
||||
assertEquals(107, ((Tree)stream.LT(1)).getType());
|
||||
stream.mark(); // MARK
|
||||
stream.consume(); // consume 107
|
||||
stream.consume(); // consume UP
|
||||
stream.consume(); // consume UP
|
||||
stream.consume(); // consume 104
|
||||
stream.rewind(); // REWIND
|
||||
stream.mark(); // keep saving nodes though
|
||||
|
||||
assertEquals(107, ((Tree)stream.LT(1)).getType());
|
||||
stream.consume();
|
||||
assertEquals(Token.UP, ((Tree)stream.LT(1)).getType());
|
||||
stream.consume();
|
||||
assertEquals(Token.UP, ((Tree)stream.LT(1)).getType());
|
||||
stream.consume();
|
||||
assertEquals(104, ((Tree)stream.LT(1)).getType());
|
||||
stream.consume();
|
||||
// now we're past rewind position
|
||||
assertEquals(105, ((Tree)stream.LT(1)).getType());
|
||||
stream.consume();
|
||||
assertEquals(Token.UP, ((Tree)stream.LT(1)).getType());
|
||||
stream.consume();
|
||||
assertEquals(Token.EOF, ((Tree)stream.LT(1)).getType());
|
||||
assertEquals(Token.UP, ((Tree)stream.LT(-1)).getType());
|
||||
}
|
||||
|
||||
@Test public void testMarkRewindNested() throws Exception {
|
||||
// ^(101 ^(102 103 ^(106 107) ) 104 105)
|
||||
// stream has 7 real + 6 nav nodes
|
||||
// Sequence of types: 101 DN 102 DN 103 106 DN 107 UP UP 104 105 UP EOF
|
||||
Tree r0 = new CommonTree(new CommonToken(101));
|
||||
Tree r1 = new CommonTree(new CommonToken(102));
|
||||
r0.addChild(r1);
|
||||
r1.addChild(new CommonTree(new CommonToken(103)));
|
||||
Tree r2 = new CommonTree(new CommonToken(106));
|
||||
r2.addChild(new CommonTree(new CommonToken(107)));
|
||||
r1.addChild(r2);
|
||||
r0.addChild(new CommonTree(new CommonToken(104)));
|
||||
r0.addChild(new CommonTree(new CommonToken(105)));
|
||||
|
||||
TreeNodeStream stream = newStream(r0);
|
||||
int m = stream.mark(); // MARK at start
|
||||
stream.consume(); // consume 101
|
||||
stream.consume(); // consume DN
|
||||
int m2 = stream.mark(); // MARK on 102
|
||||
stream.consume(); // consume 102
|
||||
stream.consume(); // consume DN
|
||||
stream.consume(); // consume 103
|
||||
stream.consume(); // consume 106
|
||||
stream.rewind(m2); // REWIND to 102
|
||||
assertEquals(102, ((Tree)stream.LT(1)).getType());
|
||||
stream.consume();
|
||||
assertEquals(Token.DOWN, ((Tree)stream.LT(1)).getType());
|
||||
stream.consume();
|
||||
// stop at 103 and rewind to start
|
||||
stream.rewind(m); // REWIND to 101
|
||||
assertEquals(101, ((Tree)stream.LT(1)).getType());
|
||||
stream.consume();
|
||||
assertEquals(Token.DOWN, ((Tree)stream.LT(1)).getType());
|
||||
stream.consume();
|
||||
assertEquals(102, ((Tree)stream.LT(1)).getType());
|
||||
stream.consume();
|
||||
assertEquals(Token.DOWN, ((Tree)stream.LT(1)).getType());
|
||||
}
|
||||
|
||||
@Test public void testSeekFromStart() throws Exception {
|
||||
// ^(101 ^(102 103 ^(106 107) ) 104 105)
|
||||
// stream has 7 real + 6 nav nodes
|
||||
// Sequence of types: 101 DN 102 DN 103 106 DN 107 UP UP 104 105 UP EOF
|
||||
Tree r0 = new CommonTree(new CommonToken(101));
|
||||
Tree r1 = new CommonTree(new CommonToken(102));
|
||||
r0.addChild(r1);
|
||||
r1.addChild(new CommonTree(new CommonToken(103)));
|
||||
Tree r2 = new CommonTree(new CommonToken(106));
|
||||
r2.addChild(new CommonTree(new CommonToken(107)));
|
||||
r1.addChild(r2);
|
||||
r0.addChild(new CommonTree(new CommonToken(104)));
|
||||
r0.addChild(new CommonTree(new CommonToken(105)));
|
||||
|
||||
TreeNodeStream stream = newStream(r0);
|
||||
stream.seek(7); // seek to 107
|
||||
assertEquals(107, ((Tree)stream.LT(1)).getType());
|
||||
stream.consume(); // consume 107
|
||||
stream.consume(); // consume UP
|
||||
stream.consume(); // consume UP
|
||||
assertEquals(104, ((Tree)stream.LT(1)).getType());
|
||||
}
|
||||
|
||||
@Test public void testReset() throws Exception {
|
||||
// ^(101 ^(102 103 ^(106 107) ) 104 105)
|
||||
// stream has 7 real + 6 nav nodes
|
||||
// Sequence of types: 101 DN 102 DN 103 106 DN 107 UP UP 104 105 UP EOF
|
||||
Tree r0 = new CommonTree(new CommonToken(101));
|
||||
Tree r1 = new CommonTree(new CommonToken(102));
|
||||
r0.addChild(r1);
|
||||
r1.addChild(new CommonTree(new CommonToken(103)));
|
||||
Tree r2 = new CommonTree(new CommonToken(106));
|
||||
r2.addChild(new CommonTree(new CommonToken(107)));
|
||||
r1.addChild(r2);
|
||||
r0.addChild(new CommonTree(new CommonToken(104)));
|
||||
r0.addChild(new CommonTree(new CommonToken(105)));
|
||||
|
||||
TreeNodeStream stream = newStream(r0);
|
||||
String v = toNodesOnlyString(stream); // scan all
|
||||
stream.reset();
|
||||
String v2 = toNodesOnlyString(stream); // scan all
|
||||
assertEquals(v, v2);
|
||||
}
|
||||
|
||||
@Test public void testDeepTree() throws Exception {
|
||||
// ^(10 100 101 ^(20 ^(30 40 (50 (60 70)))) (80 90)))
|
||||
// stream has 8 real + 10 nav nodes
|
||||
int n = 9;
|
||||
CommonTree[] nodes = new CommonTree[n];
|
||||
for (int i=0; i< n; i++) {
|
||||
nodes[i] = new CommonTree(new CommonToken((i+1)*10));
|
||||
}
|
||||
Tree g = nodes[0];
|
||||
Tree rules = nodes[1];
|
||||
Tree rule1 = nodes[2];
|
||||
Tree id = nodes[3];
|
||||
Tree block = nodes[4];
|
||||
Tree alt = nodes[5];
|
||||
Tree s = nodes[6];
|
||||
Tree rule2 = nodes[7];
|
||||
Tree id2 = nodes[8];
|
||||
g.addChild(new CommonTree(new CommonToken(100)));
|
||||
g.addChild(new CommonTree(new CommonToken(101)));
|
||||
g.addChild(rules);
|
||||
rules.addChild(rule1);
|
||||
rule1.addChild(id);
|
||||
rule1.addChild(block);
|
||||
block.addChild(alt);
|
||||
alt.addChild(s);
|
||||
rules.addChild(rule2);
|
||||
rule2.addChild(id2);
|
||||
|
||||
TreeNodeStream stream = newStream(g);
|
||||
String expecting = " 10 2 100 101 20 2 30 2 40 50 2 60 2 70 3 3 3 80 2 90 3 3 3";
|
||||
String found = toTokenTypeString(stream);
|
||||
assertEquals(expecting, found);
|
||||
}
|
||||
|
||||
public String toNodesOnlyString(TreeNodeStream nodes) {
|
||||
TreeAdaptor adaptor = nodes.getTreeAdaptor();
|
||||
StringBuffer buf = new StringBuffer();
|
||||
Object o = nodes.LT(1);
|
||||
int type = adaptor.getType(o);
|
||||
while ( o!=null && type!=Token.EOF ) {
|
||||
if ( !(type==Token.DOWN||type==Token.UP) ) {
|
||||
buf.append(" ");
|
||||
buf.append(type);
|
||||
}
|
||||
nodes.consume();
|
||||
o = nodes.LT(1);
|
||||
type = adaptor.getType(o);
|
||||
}
|
||||
return buf.toString();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,340 @@
|
|||
/*
|
||||
* [The "BSD license"]
|
||||
* Copyright (c) 2010 Terence Parr
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
package org.antlr.test;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class TestTreeParsing extends BaseTest {
|
||||
@Test public void testFlatList() throws Exception {
|
||||
String grammar =
|
||||
"grammar T;\n" +
|
||||
"options {output=AST;}\n" +
|
||||
"a : ID INT;\n" +
|
||||
"ID : 'a'..'z'+ ;\n" +
|
||||
"INT : '0'..'9'+;\n" +
|
||||
"WS : (' '|'\\n') {$channel=HIDDEN;} ;\n";
|
||||
|
||||
String treeGrammar =
|
||||
"tree grammar TP; options {ASTLabelType=CommonTree;}\n" +
|
||||
"a : ID INT\n" +
|
||||
" {System.out.println($ID+\", \"+$INT);}\n" +
|
||||
" ;\n";
|
||||
|
||||
String found = execTreeParser("T.g", grammar, "TParser", "TP.g",
|
||||
treeGrammar, "TP", "TLexer", "a", "a", "abc 34");
|
||||
assertEquals("abc, 34\n", found);
|
||||
}
|
||||
|
||||
@Test public void testSimpleTree() throws Exception {
|
||||
String grammar =
|
||||
"grammar T;\n" +
|
||||
"options {output=AST;}\n" +
|
||||
"a : ID INT -> ^(ID INT);\n" +
|
||||
"ID : 'a'..'z'+ ;\n" +
|
||||
"INT : '0'..'9'+;\n" +
|
||||
"WS : (' '|'\\n') {$channel=HIDDEN;} ;\n";
|
||||
|
||||
String treeGrammar =
|
||||
"tree grammar TP; options {ASTLabelType=CommonTree;}\n" +
|
||||
"a : ^(ID INT)\n" +
|
||||
" {System.out.println($ID+\", \"+$INT);}\n" +
|
||||
" ;\n";
|
||||
|
||||
String found = execTreeParser("T.g", grammar, "TParser", "TP.g",
|
||||
treeGrammar, "TP", "TLexer", "a", "a", "abc 34");
|
||||
assertEquals("abc, 34\n", found);
|
||||
}
|
||||
|
||||
@Test public void testFlatVsTreeDecision() throws Exception {
|
||||
String grammar =
|
||||
"grammar T;\n" +
|
||||
"options {output=AST;}\n" +
|
||||
"a : b c ;\n" +
|
||||
"b : ID INT -> ^(ID INT);\n" +
|
||||
"c : ID INT;\n" +
|
||||
"ID : 'a'..'z'+ ;\n" +
|
||||
"INT : '0'..'9'+;\n" +
|
||||
"WS : (' '|'\\n') {$channel=HIDDEN;} ;\n";
|
||||
|
||||
String treeGrammar =
|
||||
"tree grammar TP; options {ASTLabelType=CommonTree;}\n" +
|
||||
"a : b b ;\n" +
|
||||
"b : ID INT {System.out.print($ID+\" \"+$INT);}\n" +
|
||||
" | ^(ID INT) {System.out.print(\"^(\"+$ID+\" \"+$INT+')');}\n" +
|
||||
" ;\n";
|
||||
|
||||
String found = execTreeParser("T.g", grammar, "TParser", "TP.g",
|
||||
treeGrammar, "TP", "TLexer", "a", "a", "a 1 b 2");
|
||||
assertEquals("^(a 1)b 2\n", found);
|
||||
}
|
||||
|
||||
@Test public void testFlatVsTreeDecision2() throws Exception {
|
||||
String grammar =
|
||||
"grammar T;\n" +
|
||||
"options {output=AST;}\n" +
|
||||
"a : b c ;\n" +
|
||||
"b : ID INT+ -> ^(ID INT+);\n" +
|
||||
"c : ID INT+;\n" +
|
||||
"ID : 'a'..'z'+ ;\n" +
|
||||
"INT : '0'..'9'+;\n" +
|
||||
"WS : (' '|'\\n') {$channel=HIDDEN;} ;\n";
|
||||
|
||||
String treeGrammar =
|
||||
"tree grammar TP; options {ASTLabelType=CommonTree;}\n" +
|
||||
"a : b b ;\n" +
|
||||
"b : ID INT+ {System.out.print($ID+\" \"+$INT);}\n" +
|
||||
" | ^(x=ID (y=INT)+) {System.out.print(\"^(\"+$x+' '+$y+')');}\n" +
|
||||
" ;\n";
|
||||
|
||||
String found = execTreeParser("T.g", grammar, "TParser", "TP.g",
|
||||
treeGrammar, "TP", "TLexer", "a", "a",
|
||||
"a 1 2 3 b 4 5");
|
||||
assertEquals("^(a 3)b 5\n", found);
|
||||
}
|
||||
|
||||
@Test public void testCyclicDFALookahead() throws Exception {
|
||||
String grammar =
|
||||
"grammar T;\n" +
|
||||
"options {output=AST;}\n" +
|
||||
"a : ID INT+ PERIOD;\n" +
|
||||
"ID : 'a'..'z'+ ;\n" +
|
||||
"INT : '0'..'9'+;\n" +
|
||||
"SEMI : ';' ;\n"+
|
||||
"PERIOD : '.' ;\n"+
|
||||
"WS : (' '|'\\n') {$channel=HIDDEN;} ;\n";
|
||||
|
||||
String treeGrammar =
|
||||
"tree grammar TP; options {ASTLabelType=CommonTree;}\n" +
|
||||
"a : ID INT+ PERIOD {System.out.print(\"alt 1\");}"+
|
||||
" | ID INT+ SEMI {System.out.print(\"alt 2\");}\n" +
|
||||
" ;\n";
|
||||
|
||||
String found = execTreeParser("T.g", grammar, "TParser", "TP.g",
|
||||
treeGrammar, "TP", "TLexer", "a", "a", "a 1 2 3.");
|
||||
assertEquals("alt 1\n", found);
|
||||
}
|
||||
|
||||
@Test public void testTemplateOutput() throws Exception {
|
||||
String grammar =
|
||||
"grammar T;\n" +
|
||||
"options {output=AST;}\n" +
|
||||
"a : ID INT;\n" +
|
||||
"ID : 'a'..'z'+ ;\n" +
|
||||
"INT : '0'..'9'+;\n" +
|
||||
"WS : (' '|'\\n') {$channel=HIDDEN;} ;\n";
|
||||
|
||||
String treeGrammar =
|
||||
"tree grammar TP;\n" +
|
||||
"options {output=template; ASTLabelType=CommonTree;}\n" +
|
||||
"s : a {System.out.println($a.st);};\n" +
|
||||
"a : ID INT -> {new StringTemplate($INT.text)}\n" +
|
||||
" ;\n";
|
||||
|
||||
String found = execTreeParser("T.g", grammar, "TParser", "TP.g",
|
||||
treeGrammar, "TP", "TLexer", "a", "s", "abc 34");
|
||||
assertEquals("34\n", found);
|
||||
}
|
||||
|
||||
@Test public void testNullableChildList() throws Exception {
|
||||
String grammar =
|
||||
"grammar T;\n" +
|
||||
"options {output=AST;}\n" +
|
||||
"a : ID INT? -> ^(ID INT?);\n" +
|
||||
"ID : 'a'..'z'+ ;\n" +
|
||||
"INT : '0'..'9'+;\n" +
|
||||
"WS : (' '|'\\n') {$channel=HIDDEN;} ;\n";
|
||||
|
||||
String treeGrammar =
|
||||
"tree grammar TP; options {ASTLabelType=CommonTree;}\n" +
|
||||
"a : ^(ID INT?)\n" +
|
||||
" {System.out.println($ID);}\n" +
|
||||
" ;\n";
|
||||
|
||||
String found = execTreeParser("T.g", grammar, "TParser", "TP.g",
|
||||
treeGrammar, "TP", "TLexer", "a", "a", "abc");
|
||||
assertEquals("abc\n", found);
|
||||
}
|
||||
|
||||
@Test public void testNullableChildList2() throws Exception {
|
||||
String grammar =
|
||||
"grammar T;\n" +
|
||||
"options {output=AST;}\n" +
|
||||
"a : ID INT? SEMI -> ^(ID INT?) SEMI ;\n" +
|
||||
"ID : 'a'..'z'+ ;\n" +
|
||||
"INT : '0'..'9'+;\n" +
|
||||
"SEMI : ';' ;\n"+
|
||||
"WS : (' '|'\\n') {$channel=HIDDEN;} ;\n";
|
||||
|
||||
String treeGrammar =
|
||||
"tree grammar TP; options {ASTLabelType=CommonTree;}\n" +
|
||||
"a : ^(ID INT?) SEMI\n" +
|
||||
" {System.out.println($ID);}\n" +
|
||||
" ;\n";
|
||||
|
||||
String found = execTreeParser("T.g", grammar, "TParser", "TP.g",
|
||||
treeGrammar, "TP", "TLexer", "a", "a", "abc;");
|
||||
assertEquals("abc\n", found);
|
||||
}
|
||||
|
||||
@Test public void testNullableChildList3() throws Exception {
|
||||
String grammar =
|
||||
"grammar T;\n" +
|
||||
"options {output=AST;}\n" +
|
||||
"a : x=ID INT? (y=ID)? SEMI -> ^($x INT? $y?) SEMI ;\n" +
|
||||
"ID : 'a'..'z'+ ;\n" +
|
||||
"INT : '0'..'9'+;\n" +
|
||||
"SEMI : ';' ;\n"+
|
||||
"WS : (' '|'\\n') {$channel=HIDDEN;} ;\n";
|
||||
|
||||
String treeGrammar =
|
||||
"tree grammar TP; options {ASTLabelType=CommonTree;}\n" +
|
||||
"a : ^(ID INT? b) SEMI\n" +
|
||||
" {System.out.println($ID+\", \"+$b.text);}\n" +
|
||||
" ;\n"+
|
||||
"b : ID? ;\n";
|
||||
|
||||
String found = execTreeParser("T.g", grammar, "TParser", "TP.g",
|
||||
treeGrammar, "TP", "TLexer", "a", "a", "abc def;");
|
||||
assertEquals("abc, def\n", found);
|
||||
}
|
||||
|
||||
@Test public void testActionsAfterRoot() throws Exception {
|
||||
String grammar =
|
||||
"grammar T;\n" +
|
||||
"options {output=AST;}\n" +
|
||||
"a : x=ID INT? SEMI -> ^($x INT?) ;\n" +
|
||||
"ID : 'a'..'z'+ ;\n" +
|
||||
"INT : '0'..'9'+;\n" +
|
||||
"SEMI : ';' ;\n"+
|
||||
"WS : (' '|'\\n') {$channel=HIDDEN;} ;\n";
|
||||
|
||||
String treeGrammar =
|
||||
"tree grammar TP; options {ASTLabelType=CommonTree;}\n" +
|
||||
"a @init {int x=0;} : ^(ID {x=1;} {x=2;} INT?)\n" +
|
||||
" {System.out.println($ID+\", \"+x);}\n" +
|
||||
" ;\n";
|
||||
|
||||
String found = execTreeParser("T.g", grammar, "TParser", "TP.g",
|
||||
treeGrammar, "TP", "TLexer", "a", "a", "abc;");
|
||||
assertEquals("abc, 2\n", found);
|
||||
}
|
||||
|
||||
@Test public void testWildcardLookahead() throws Exception {
|
||||
String grammar =
|
||||
"grammar T;\n" +
|
||||
"options {output=AST;}\n" +
|
||||
"a : ID '+'^ INT;\n" +
|
||||
"ID : 'a'..'z'+ ;\n" +
|
||||
"INT : '0'..'9'+;\n" +
|
||||
"SEMI : ';' ;\n"+
|
||||
"PERIOD : '.' ;\n"+
|
||||
"WS : (' '|'\\n') {$channel=HIDDEN;} ;\n";
|
||||
|
||||
String treeGrammar =
|
||||
"tree grammar TP; options {tokenVocab=T; ASTLabelType=CommonTree;}\n" +
|
||||
"a : ^('+' . INT) {System.out.print(\"alt 1\");}"+
|
||||
" ;\n";
|
||||
|
||||
String found = execTreeParser("T.g", grammar, "TParser", "TP.g",
|
||||
treeGrammar, "TP", "TLexer", "a", "a", "a + 2");
|
||||
assertEquals("alt 1\n", found);
|
||||
}
|
||||
|
||||
@Test public void testWildcardLookahead2() throws Exception {
|
||||
String grammar =
|
||||
"grammar T;\n" +
|
||||
"options {output=AST;}\n" +
|
||||
"a : ID '+'^ INT;\n" +
|
||||
"ID : 'a'..'z'+ ;\n" +
|
||||
"INT : '0'..'9'+;\n" +
|
||||
"SEMI : ';' ;\n"+
|
||||
"PERIOD : '.' ;\n"+
|
||||
"WS : (' '|'\\n') {$channel=HIDDEN;} ;\n";
|
||||
|
||||
String treeGrammar =
|
||||
"tree grammar TP; options {tokenVocab=T; ASTLabelType=CommonTree;}\n" +
|
||||
"a : ^('+' . INT) {System.out.print(\"alt 1\");}"+
|
||||
" | ^('+' . .) {System.out.print(\"alt 2\");}\n" +
|
||||
" ;\n";
|
||||
|
||||
// AMBIG upon '+' DOWN INT UP etc.. but so what.
|
||||
|
||||
String found = execTreeParser("T.g", grammar, "TParser", "TP.g",
|
||||
treeGrammar, "TP", "TLexer", "a", "a", "a + 2");
|
||||
assertEquals("alt 1\n", found);
|
||||
}
|
||||
|
||||
@Test public void testWildcardLookahead3() throws Exception {
|
||||
String grammar =
|
||||
"grammar T;\n" +
|
||||
"options {output=AST;}\n" +
|
||||
"a : ID '+'^ INT;\n" +
|
||||
"ID : 'a'..'z'+ ;\n" +
|
||||
"INT : '0'..'9'+;\n" +
|
||||
"SEMI : ';' ;\n"+
|
||||
"PERIOD : '.' ;\n"+
|
||||
"WS : (' '|'\\n') {$channel=HIDDEN;} ;\n";
|
||||
|
||||
String treeGrammar =
|
||||
"tree grammar TP; options {tokenVocab=T; ASTLabelType=CommonTree;}\n" +
|
||||
"a : ^('+' ID INT) {System.out.print(\"alt 1\");}"+
|
||||
" | ^('+' . .) {System.out.print(\"alt 2\");}\n" +
|
||||
" ;\n";
|
||||
|
||||
// AMBIG upon '+' DOWN INT UP etc.. but so what.
|
||||
|
||||
String found = execTreeParser("T.g", grammar, "TParser", "TP.g",
|
||||
treeGrammar, "TP", "TLexer", "a", "a", "a + 2");
|
||||
assertEquals("alt 1\n", found);
|
||||
}
|
||||
|
||||
@Test public void testWildcardPlusLookahead() throws Exception {
|
||||
String grammar =
|
||||
"grammar T;\n" +
|
||||
"options {output=AST;}\n" +
|
||||
"a : ID '+'^ INT;\n" +
|
||||
"ID : 'a'..'z'+ ;\n" +
|
||||
"INT : '0'..'9'+;\n" +
|
||||
"SEMI : ';' ;\n"+
|
||||
"PERIOD : '.' ;\n"+
|
||||
"WS : (' '|'\\n') {$channel=HIDDEN;} ;\n";
|
||||
|
||||
String treeGrammar =
|
||||
"tree grammar TP; options {tokenVocab=T; ASTLabelType=CommonTree;}\n" +
|
||||
"a : ^('+' INT INT ) {System.out.print(\"alt 1\");}"+
|
||||
" | ^('+' .+) {System.out.print(\"alt 2\");}\n" +
|
||||
" ;\n";
|
||||
|
||||
// AMBIG upon '+' DOWN INT UP etc.. but so what.
|
||||
|
||||
String found = execTreeParser("T.g", grammar, "TParser", "TP.g",
|
||||
treeGrammar, "TP", "TLexer", "a", "a", "a + 2");
|
||||
assertEquals("alt 2\n", found);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,402 @@
|
|||
/*
|
||||
* [The "BSD license"]
|
||||
* Copyright (c) 2010 Terence Parr
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
package org.antlr.test;
|
||||
|
||||
import org.antlr.runtime.tree.CommonTree;
|
||||
import org.antlr.runtime.tree.CommonTreeAdaptor;
|
||||
import org.antlr.runtime.tree.TreeAdaptor;
|
||||
import org.antlr.runtime.tree.TreeWizard;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
public class TestTreeWizard extends BaseTest {
|
||||
protected static final String[] tokens =
|
||||
new String[] {"", "", "", "", "", "A", "B", "C", "D", "E", "ID", "VAR"};
|
||||
protected static final TreeAdaptor adaptor = new CommonTreeAdaptor();
|
||||
|
||||
@Test public void testSingleNode() throws Exception {
|
||||
TreeWizard wiz = new TreeWizard(adaptor, tokens);
|
||||
CommonTree t = (CommonTree)wiz.create("ID");
|
||||
String found = t.toStringTree();
|
||||
String expecting = "ID";
|
||||
assertEquals(expecting, found);
|
||||
}
|
||||
|
||||
@Test public void testSingleNodeWithArg() throws Exception {
|
||||
TreeWizard wiz = new TreeWizard(adaptor, tokens);
|
||||
CommonTree t = (CommonTree)wiz.create("ID[foo]");
|
||||
String found = t.toStringTree();
|
||||
String expecting = "foo";
|
||||
assertEquals(expecting, found);
|
||||
}
|
||||
|
||||
@Test public void testSingleNodeTree() throws Exception {
|
||||
TreeWizard wiz = new TreeWizard(adaptor, tokens);
|
||||
CommonTree t = (CommonTree)wiz.create("(A)");
|
||||
String found = t.toStringTree();
|
||||
String expecting = "A";
|
||||
assertEquals(expecting, found);
|
||||
}
|
||||
|
||||
@Test public void testSingleLevelTree() throws Exception {
|
||||
TreeWizard wiz = new TreeWizard(adaptor, tokens);
|
||||
CommonTree t = (CommonTree)wiz.create("(A B C D)");
|
||||
String found = t.toStringTree();
|
||||
String expecting = "(A B C D)";
|
||||
assertEquals(expecting, found);
|
||||
}
|
||||
|
||||
@Test public void testListTree() throws Exception {
|
||||
TreeWizard wiz = new TreeWizard(adaptor, tokens);
|
||||
CommonTree t = (CommonTree)wiz.create("(nil A B C)");
|
||||
String found = t.toStringTree();
|
||||
String expecting = "A B C";
|
||||
assertEquals(expecting, found);
|
||||
}
|
||||
|
||||
@Test public void testInvalidListTree() throws Exception {
|
||||
TreeWizard wiz = new TreeWizard(adaptor, tokens);
|
||||
CommonTree t = (CommonTree)wiz.create("A B C");
|
||||
assertTrue(t==null);
|
||||
}
|
||||
|
||||
@Test public void testDoubleLevelTree() throws Exception {
|
||||
TreeWizard wiz = new TreeWizard(adaptor, tokens);
|
||||
CommonTree t = (CommonTree)wiz.create("(A (B C) (B D) E)");
|
||||
String found = t.toStringTree();
|
||||
String expecting = "(A (B C) (B D) E)";
|
||||
assertEquals(expecting, found);
|
||||
}
|
||||
|
||||
@Test public void testSingleNodeIndex() throws Exception {
|
||||
TreeWizard wiz = new TreeWizard(adaptor, tokens);
|
||||
CommonTree t = (CommonTree)wiz.create("ID");
|
||||
Map m = wiz.index(t);
|
||||
String found = m.toString();
|
||||
String expecting = "{10=[ID]}";
|
||||
assertEquals(expecting, found);
|
||||
}
|
||||
|
||||
@Test public void testNoRepeatsIndex() throws Exception {
|
||||
TreeWizard wiz = new TreeWizard(adaptor, tokens);
|
||||
CommonTree t = (CommonTree)wiz.create("(A B C D)");
|
||||
Map m = wiz.index(t);
|
||||
String found = sortMapToString(m);
|
||||
String expecting = "{5=[A], 6=[B], 7=[C], 8=[D]}";
|
||||
assertEquals(expecting, found);
|
||||
}
|
||||
|
||||
@Test public void testRepeatsIndex() throws Exception {
|
||||
TreeWizard wiz = new TreeWizard(adaptor, tokens);
|
||||
CommonTree t = (CommonTree)wiz.create("(A B (A C B) B D D)");
|
||||
Map m = wiz.index(t);
|
||||
String found = sortMapToString(m);
|
||||
String expecting = "{5=[A, A], 6=[B, B, B], 7=[C], 8=[D, D]}";
|
||||
assertEquals(expecting, found);
|
||||
}
|
||||
|
||||
@Test public void testNoRepeatsVisit() throws Exception {
|
||||
TreeWizard wiz = new TreeWizard(adaptor, tokens);
|
||||
CommonTree t = (CommonTree)wiz.create("(A B C D)");
|
||||
final List elements = new ArrayList();
|
||||
wiz.visit(t, wiz.getTokenType("B"), new TreeWizard.Visitor() {
|
||||
public void visit(Object t) {
|
||||
elements.add(t);
|
||||
}
|
||||
});
|
||||
String found = elements.toString();
|
||||
String expecting = "[B]";
|
||||
assertEquals(expecting, found);
|
||||
}
|
||||
|
||||
@Test public void testNoRepeatsVisit2() throws Exception {
|
||||
TreeWizard wiz = new TreeWizard(adaptor, tokens);
|
||||
CommonTree t = (CommonTree)wiz.create("(A B (A C B) B D D)");
|
||||
final List elements = new ArrayList();
|
||||
wiz.visit(t, wiz.getTokenType("C"),
|
||||
new TreeWizard.Visitor() {
|
||||
public void visit(Object t) {
|
||||
elements.add(t);
|
||||
}
|
||||
});
|
||||
String found = elements.toString();
|
||||
String expecting = "[C]";
|
||||
assertEquals(expecting, found);
|
||||
}
|
||||
|
||||
@Test public void testRepeatsVisit() throws Exception {
|
||||
TreeWizard wiz = new TreeWizard(adaptor, tokens);
|
||||
CommonTree t = (CommonTree)wiz.create("(A B (A C B) B D D)");
|
||||
final List elements = new ArrayList();
|
||||
wiz.visit(t, wiz.getTokenType("B"),
|
||||
new TreeWizard.Visitor() {
|
||||
public void visit(Object t) {
|
||||
elements.add(t);
|
||||
}
|
||||
});
|
||||
String found = elements.toString();
|
||||
String expecting = "[B, B, B]";
|
||||
assertEquals(expecting, found);
|
||||
}
|
||||
|
||||
@Test public void testRepeatsVisit2() throws Exception {
|
||||
TreeWizard wiz = new TreeWizard(adaptor, tokens);
|
||||
CommonTree t = (CommonTree)wiz.create("(A B (A C B) B D D)");
|
||||
final List elements = new ArrayList();
|
||||
wiz.visit(t, wiz.getTokenType("A"),
|
||||
new TreeWizard.Visitor() {
|
||||
public void visit(Object t) {
|
||||
elements.add(t);
|
||||
}
|
||||
});
|
||||
String found = elements.toString();
|
||||
String expecting = "[A, A]";
|
||||
assertEquals(expecting, found);
|
||||
}
|
||||
|
||||
@Test public void testRepeatsVisitWithContext() throws Exception {
|
||||
TreeWizard wiz = new TreeWizard(adaptor, tokens);
|
||||
CommonTree t = (CommonTree)wiz.create("(A B (A C B) B D D)");
|
||||
final List elements = new ArrayList();
|
||||
wiz.visit(t, wiz.getTokenType("B"),
|
||||
new TreeWizard.ContextVisitor() {
|
||||
public void visit(Object t, Object parent, int childIndex, Map labels) {
|
||||
elements.add(adaptor.getText(t)+"@"+
|
||||
(parent!=null?adaptor.getText(parent):"nil")+
|
||||
"["+childIndex+"]");
|
||||
}
|
||||
});
|
||||
String found = elements.toString();
|
||||
String expecting = "[B@A[0], B@A[1], B@A[2]]";
|
||||
assertEquals(expecting, found);
|
||||
}
|
||||
|
||||
@Test public void testRepeatsVisitWithNullParentAndContext() throws Exception {
|
||||
TreeWizard wiz = new TreeWizard(adaptor, tokens);
|
||||
CommonTree t = (CommonTree)wiz.create("(A B (A C B) B D D)");
|
||||
final List elements = new ArrayList();
|
||||
wiz.visit(t, wiz.getTokenType("A"),
|
||||
new TreeWizard.ContextVisitor() {
|
||||
public void visit(Object t, Object parent, int childIndex, Map labels) {
|
||||
elements.add(adaptor.getText(t)+"@"+
|
||||
(parent!=null?adaptor.getText(parent):"nil")+
|
||||
"["+childIndex+"]");
|
||||
}
|
||||
});
|
||||
String found = elements.toString();
|
||||
String expecting = "[A@nil[0], A@A[1]]";
|
||||
assertEquals(expecting, found);
|
||||
}
|
||||
|
||||
@Test public void testVisitPattern() throws Exception {
|
||||
TreeWizard wiz = new TreeWizard(adaptor, tokens);
|
||||
CommonTree t = (CommonTree)wiz.create("(A B C (A B) D)");
|
||||
final List elements = new ArrayList();
|
||||
wiz.visit(t, "(A B)",
|
||||
new TreeWizard.Visitor() {
|
||||
public void visit(Object t) {
|
||||
elements.add(t);
|
||||
}
|
||||
});
|
||||
String found = elements.toString();
|
||||
String expecting = "[A]"; // shouldn't match overall root, just (A B)
|
||||
assertEquals(expecting, found);
|
||||
}
|
||||
|
||||
@Test public void testVisitPatternMultiple() throws Exception {
|
||||
TreeWizard wiz = new TreeWizard(adaptor, tokens);
|
||||
CommonTree t = (CommonTree)wiz.create("(A B C (A B) (D (A B)))");
|
||||
final List elements = new ArrayList();
|
||||
wiz.visit(t, "(A B)",
|
||||
new TreeWizard.ContextVisitor() {
|
||||
public void visit(Object t, Object parent, int childIndex, Map labels) {
|
||||
elements.add(adaptor.getText(t)+"@"+
|
||||
(parent!=null?adaptor.getText(parent):"nil")+
|
||||
"["+childIndex+"]");
|
||||
}
|
||||
});
|
||||
String found = elements.toString();
|
||||
String expecting = "[A@A[2], A@D[0]]"; // shouldn't match overall root, just (A B)
|
||||
assertEquals(expecting, found);
|
||||
}
|
||||
|
||||
@Test public void testVisitPatternMultipleWithLabels() throws Exception {
|
||||
TreeWizard wiz = new TreeWizard(adaptor, tokens);
|
||||
CommonTree t = (CommonTree)wiz.create("(A B C (A[foo] B[bar]) (D (A[big] B[dog])))");
|
||||
final List elements = new ArrayList();
|
||||
wiz.visit(t, "(%a:A %b:B)",
|
||||
new TreeWizard.ContextVisitor() {
|
||||
public void visit(Object t, Object parent, int childIndex, Map labels) {
|
||||
elements.add(adaptor.getText(t)+"@"+
|
||||
(parent!=null?adaptor.getText(parent):"nil")+
|
||||
"["+childIndex+"]"+labels.get("a")+"&"+labels.get("b"));
|
||||
}
|
||||
});
|
||||
String found = elements.toString();
|
||||
String expecting = "[foo@A[2]foo&bar, big@D[0]big&dog]";
|
||||
assertEquals(expecting, found);
|
||||
}
|
||||
|
||||
@Test public void testParse() throws Exception {
|
||||
TreeWizard wiz = new TreeWizard(adaptor, tokens);
|
||||
CommonTree t = (CommonTree)wiz.create("(A B C)");
|
||||
boolean valid = wiz.parse(t, "(A B C)");
|
||||
assertTrue(valid);
|
||||
}
|
||||
|
||||
@Test public void testParseSingleNode() throws Exception {
|
||||
TreeWizard wiz = new TreeWizard(adaptor, tokens);
|
||||
CommonTree t = (CommonTree)wiz.create("A");
|
||||
boolean valid = wiz.parse(t, "A");
|
||||
assertTrue(valid);
|
||||
}
|
||||
|
||||
@Test public void testParseFlatTree() throws Exception {
|
||||
TreeWizard wiz = new TreeWizard(adaptor, tokens);
|
||||
CommonTree t = (CommonTree)wiz.create("(nil A B C)");
|
||||
boolean valid = wiz.parse(t, "(nil A B C)");
|
||||
assertTrue(valid);
|
||||
}
|
||||
|
||||
@Test public void testWildcard() throws Exception {
|
||||
TreeWizard wiz = new TreeWizard(adaptor, tokens);
|
||||
CommonTree t = (CommonTree)wiz.create("(A B C)");
|
||||
boolean valid = wiz.parse(t, "(A . .)");
|
||||
assertTrue(valid);
|
||||
}
|
||||
|
||||
@Test public void testParseWithText() throws Exception {
|
||||
TreeWizard wiz = new TreeWizard(adaptor, tokens);
|
||||
CommonTree t = (CommonTree)wiz.create("(A B[foo] C[bar])");
|
||||
// C pattern has no text arg so despite [bar] in t, no need
|
||||
// to match text--check structure only.
|
||||
boolean valid = wiz.parse(t, "(A B[foo] C)");
|
||||
assertTrue(valid);
|
||||
}
|
||||
|
||||
@Test public void testParseWithText2() throws Exception {
|
||||
TreeWizard wiz = new TreeWizard(adaptor, tokens);
|
||||
CommonTree t = (CommonTree)wiz.create("(A B[T__32] (C (D E[a])))");
|
||||
// C pattern has no text arg so despite [bar] in t, no need
|
||||
// to match text--check structure only.
|
||||
boolean valid = wiz.parse(t, "(A B[foo] C)");
|
||||
assertEquals("(A T__32 (C (D a)))", t.toStringTree());
|
||||
}
|
||||
|
||||
@Test public void testParseWithTextFails() throws Exception {
|
||||
TreeWizard wiz = new TreeWizard(adaptor, tokens);
|
||||
CommonTree t = (CommonTree)wiz.create("(A B C)");
|
||||
boolean valid = wiz.parse(t, "(A[foo] B C)");
|
||||
assertTrue(!valid); // fails
|
||||
}
|
||||
|
||||
@Test public void testParseLabels() throws Exception {
|
||||
TreeWizard wiz = new TreeWizard(adaptor, tokens);
|
||||
CommonTree t = (CommonTree)wiz.create("(A B C)");
|
||||
Map labels = new HashMap();
|
||||
boolean valid = wiz.parse(t, "(%a:A %b:B %c:C)", labels);
|
||||
assertTrue(valid);
|
||||
assertEquals("A", labels.get("a").toString());
|
||||
assertEquals("B", labels.get("b").toString());
|
||||
assertEquals("C", labels.get("c").toString());
|
||||
}
|
||||
|
||||
@Test public void testParseWithWildcardLabels() throws Exception {
|
||||
TreeWizard wiz = new TreeWizard(adaptor, tokens);
|
||||
CommonTree t = (CommonTree)wiz.create("(A B C)");
|
||||
Map labels = new HashMap();
|
||||
boolean valid = wiz.parse(t, "(A %b:. %c:.)", labels);
|
||||
assertTrue(valid);
|
||||
assertEquals("B", labels.get("b").toString());
|
||||
assertEquals("C", labels.get("c").toString());
|
||||
}
|
||||
|
||||
@Test public void testParseLabelsAndTestText() throws Exception {
|
||||
TreeWizard wiz = new TreeWizard(adaptor, tokens);
|
||||
CommonTree t = (CommonTree)wiz.create("(A B[foo] C)");
|
||||
Map labels = new HashMap();
|
||||
boolean valid = wiz.parse(t, "(%a:A %b:B[foo] %c:C)", labels);
|
||||
assertTrue(valid);
|
||||
assertEquals("A", labels.get("a").toString());
|
||||
assertEquals("foo", labels.get("b").toString());
|
||||
assertEquals("C", labels.get("c").toString());
|
||||
}
|
||||
|
||||
@Test public void testParseLabelsInNestedTree() throws Exception {
|
||||
TreeWizard wiz = new TreeWizard(adaptor, tokens);
|
||||
CommonTree t = (CommonTree)wiz.create("(A (B C) (D E))");
|
||||
Map labels = new HashMap();
|
||||
boolean valid = wiz.parse(t, "(%a:A (%b:B %c:C) (%d:D %e:E) )", labels);
|
||||
assertTrue(valid);
|
||||
assertEquals("A", labels.get("a").toString());
|
||||
assertEquals("B", labels.get("b").toString());
|
||||
assertEquals("C", labels.get("c").toString());
|
||||
assertEquals("D", labels.get("d").toString());
|
||||
assertEquals("E", labels.get("e").toString());
|
||||
}
|
||||
|
||||
@Test public void testEquals() throws Exception {
|
||||
TreeWizard wiz = new TreeWizard(adaptor, tokens);
|
||||
CommonTree t1 = (CommonTree)wiz.create("(A B C)");
|
||||
CommonTree t2 = (CommonTree)wiz.create("(A B C)");
|
||||
boolean same = TreeWizard.equals(t1, t2, adaptor);
|
||||
assertTrue(same);
|
||||
}
|
||||
|
||||
@Test public void testEqualsWithText() throws Exception {
|
||||
TreeWizard wiz = new TreeWizard(adaptor, tokens);
|
||||
CommonTree t1 = (CommonTree)wiz.create("(A B[foo] C)");
|
||||
CommonTree t2 = (CommonTree)wiz.create("(A B[foo] C)");
|
||||
boolean same = TreeWizard.equals(t1, t2, adaptor);
|
||||
assertTrue(same);
|
||||
}
|
||||
|
||||
@Test public void testEqualsWithMismatchedText() throws Exception {
|
||||
TreeWizard wiz = new TreeWizard(adaptor, tokens);
|
||||
CommonTree t1 = (CommonTree)wiz.create("(A B[foo] C)");
|
||||
CommonTree t2 = (CommonTree)wiz.create("(A B C)");
|
||||
boolean same = TreeWizard.equals(t1, t2, adaptor);
|
||||
assertTrue(!same);
|
||||
}
|
||||
|
||||
@Test public void testFindPattern() throws Exception {
|
||||
TreeWizard wiz = new TreeWizard(adaptor, tokens);
|
||||
CommonTree t = (CommonTree)wiz.create("(A B C (A[foo] B[bar]) (D (A[big] B[dog])))");
|
||||
final List subtrees = wiz.find(t, "(A B)");
|
||||
List elements = subtrees;
|
||||
String found = elements.toString();
|
||||
String expecting = "[foo, big]";
|
||||
assertEquals(expecting, found);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,408 @@
|
|||
/*
|
||||
* [The "BSD license"]
|
||||
* Copyright (c) 2010 Terence Parr
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
package org.antlr.test;
|
||||
|
||||
import org.antlr.runtime.CommonToken;
|
||||
import org.antlr.runtime.Token;
|
||||
import org.antlr.runtime.tree.CommonTree;
|
||||
import org.antlr.runtime.tree.CommonTreeAdaptor;
|
||||
import org.antlr.runtime.tree.Tree;
|
||||
import org.antlr.runtime.tree.TreeAdaptor;
|
||||
import org.junit.Test;
|
||||
|
||||
public class TestTrees extends BaseTest {
|
||||
TreeAdaptor adaptor = new CommonTreeAdaptor();
|
||||
protected boolean debug = false;
|
||||
|
||||
static class V extends CommonTree {
|
||||
public int x;
|
||||
public V(Token t) { this.token = t;}
|
||||
public V(int ttype, int x) { this.x=x; token=new CommonToken(ttype); }
|
||||
public V(int ttype, Token t, int x) { token=t; this.x=x;}
|
||||
public String toString() { return (token!=null?token.getText():"")+"<V>";}
|
||||
}
|
||||
|
||||
@Test public void testSingleNode() throws Exception {
|
||||
CommonTree t = new CommonTree(new CommonToken(101));
|
||||
assertNull(t.parent);
|
||||
assertEquals(-1, t.childIndex);
|
||||
}
|
||||
|
||||
@Test public void testTwoChildrenOfNilRoot() throws Exception {
|
||||
CommonTree root_0 = (CommonTree)adaptor.nil();
|
||||
CommonTree t = new V(101, 2);
|
||||
CommonTree u = new V(new CommonToken(102,"102"));
|
||||
adaptor.addChild(root_0, t);
|
||||
adaptor.addChild(root_0, u);
|
||||
assertNull(root_0.parent);
|
||||
assertEquals(-1, root_0.childIndex);
|
||||
assertEquals(0, t.childIndex);
|
||||
assertEquals(1, u.childIndex);
|
||||
}
|
||||
|
||||
@Test public void test4Nodes() throws Exception {
|
||||
// ^(101 ^(102 103) 104)
|
||||
CommonTree r0 = new CommonTree(new CommonToken(101));
|
||||
r0.addChild(new CommonTree(new CommonToken(102)));
|
||||
r0.getChild(0).addChild(new CommonTree(new CommonToken(103)));
|
||||
r0.addChild(new CommonTree(new CommonToken(104)));
|
||||
|
||||
assertNull(r0.parent);
|
||||
assertEquals(-1, r0.childIndex);
|
||||
}
|
||||
|
||||
@Test public void testList() throws Exception {
|
||||
// ^(nil 101 102 103)
|
||||
CommonTree r0 = new CommonTree((Token)null);
|
||||
CommonTree c0, c1, c2;
|
||||
r0.addChild(c0=new CommonTree(new CommonToken(101)));
|
||||
r0.addChild(c1=new CommonTree(new CommonToken(102)));
|
||||
r0.addChild(c2=new CommonTree(new CommonToken(103)));
|
||||
|
||||
assertNull(r0.parent);
|
||||
assertEquals(-1, r0.childIndex);
|
||||
assertEquals(r0, c0.parent);
|
||||
assertEquals(0, c0.childIndex);
|
||||
assertEquals(r0, c1.parent);
|
||||
assertEquals(1, c1.childIndex);
|
||||
assertEquals(r0, c2.parent);
|
||||
assertEquals(2, c2.childIndex);
|
||||
}
|
||||
|
||||
@Test public void testList2() throws Exception {
|
||||
// Add child ^(nil 101 102 103) to root 5
|
||||
// should pull 101 102 103 directly to become 5's child list
|
||||
CommonTree root = new CommonTree(new CommonToken(5));
|
||||
|
||||
// child tree
|
||||
CommonTree r0 = new CommonTree((Token)null);
|
||||
CommonTree c0, c1, c2;
|
||||
r0.addChild(c0=new CommonTree(new CommonToken(101)));
|
||||
r0.addChild(c1=new CommonTree(new CommonToken(102)));
|
||||
r0.addChild(c2=new CommonTree(new CommonToken(103)));
|
||||
|
||||
root.addChild(r0);
|
||||
|
||||
assertNull(root.parent);
|
||||
assertEquals(-1, root.childIndex);
|
||||
// check children of root all point at root
|
||||
assertEquals(root, c0.parent);
|
||||
assertEquals(0, c0.childIndex);
|
||||
assertEquals(root, c0.parent);
|
||||
assertEquals(1, c1.childIndex);
|
||||
assertEquals(root, c0.parent);
|
||||
assertEquals(2, c2.childIndex);
|
||||
}
|
||||
|
||||
@Test public void testAddListToExistChildren() throws Exception {
|
||||
// Add child ^(nil 101 102 103) to root ^(5 6)
|
||||
// should add 101 102 103 to end of 5's child list
|
||||
CommonTree root = new CommonTree(new CommonToken(5));
|
||||
root.addChild(new CommonTree(new CommonToken(6)));
|
||||
|
||||
// child tree
|
||||
CommonTree r0 = new CommonTree((Token)null);
|
||||
CommonTree c0, c1, c2;
|
||||
r0.addChild(c0=new CommonTree(new CommonToken(101)));
|
||||
r0.addChild(c1=new CommonTree(new CommonToken(102)));
|
||||
r0.addChild(c2=new CommonTree(new CommonToken(103)));
|
||||
|
||||
root.addChild(r0);
|
||||
|
||||
assertNull(root.parent);
|
||||
assertEquals(-1, root.childIndex);
|
||||
// check children of root all point at root
|
||||
assertEquals(root, c0.parent);
|
||||
assertEquals(1, c0.childIndex);
|
||||
assertEquals(root, c0.parent);
|
||||
assertEquals(2, c1.childIndex);
|
||||
assertEquals(root, c0.parent);
|
||||
assertEquals(3, c2.childIndex);
|
||||
}
|
||||
|
||||
@Test public void testDupTree() throws Exception {
|
||||
// ^(101 ^(102 103 ^(106 107) ) 104 105)
|
||||
CommonTree r0 = new CommonTree(new CommonToken(101));
|
||||
CommonTree r1 = new CommonTree(new CommonToken(102));
|
||||
r0.addChild(r1);
|
||||
r1.addChild(new CommonTree(new CommonToken(103)));
|
||||
Tree r2 = new CommonTree(new CommonToken(106));
|
||||
r2.addChild(new CommonTree(new CommonToken(107)));
|
||||
r1.addChild(r2);
|
||||
r0.addChild(new CommonTree(new CommonToken(104)));
|
||||
r0.addChild(new CommonTree(new CommonToken(105)));
|
||||
|
||||
CommonTree dup = (CommonTree)(new CommonTreeAdaptor()).dupTree(r0);
|
||||
|
||||
assertNull(dup.parent);
|
||||
assertEquals(-1, dup.childIndex);
|
||||
dup.sanityCheckParentAndChildIndexes();
|
||||
}
|
||||
|
||||
@Test public void testBecomeRoot() throws Exception {
|
||||
// 5 becomes new root of ^(nil 101 102 103)
|
||||
CommonTree newRoot = new CommonTree(new CommonToken(5));
|
||||
|
||||
CommonTree oldRoot = new CommonTree((Token)null);
|
||||
oldRoot.addChild(new CommonTree(new CommonToken(101)));
|
||||
oldRoot.addChild(new CommonTree(new CommonToken(102)));
|
||||
oldRoot.addChild(new CommonTree(new CommonToken(103)));
|
||||
|
||||
TreeAdaptor adaptor = new CommonTreeAdaptor();
|
||||
adaptor.becomeRoot(newRoot, oldRoot);
|
||||
newRoot.sanityCheckParentAndChildIndexes();
|
||||
}
|
||||
|
||||
@Test public void testBecomeRoot2() throws Exception {
|
||||
// 5 becomes new root of ^(101 102 103)
|
||||
CommonTree newRoot = new CommonTree(new CommonToken(5));
|
||||
|
||||
CommonTree oldRoot = new CommonTree(new CommonToken(101));
|
||||
oldRoot.addChild(new CommonTree(new CommonToken(102)));
|
||||
oldRoot.addChild(new CommonTree(new CommonToken(103)));
|
||||
|
||||
TreeAdaptor adaptor = new CommonTreeAdaptor();
|
||||
adaptor.becomeRoot(newRoot, oldRoot);
|
||||
newRoot.sanityCheckParentAndChildIndexes();
|
||||
}
|
||||
|
||||
@Test public void testBecomeRoot3() throws Exception {
|
||||
// ^(nil 5) becomes new root of ^(nil 101 102 103)
|
||||
CommonTree newRoot = new CommonTree((Token)null);
|
||||
newRoot.addChild(new CommonTree(new CommonToken(5)));
|
||||
|
||||
CommonTree oldRoot = new CommonTree((Token)null);
|
||||
oldRoot.addChild(new CommonTree(new CommonToken(101)));
|
||||
oldRoot.addChild(new CommonTree(new CommonToken(102)));
|
||||
oldRoot.addChild(new CommonTree(new CommonToken(103)));
|
||||
|
||||
TreeAdaptor adaptor = new CommonTreeAdaptor();
|
||||
adaptor.becomeRoot(newRoot, oldRoot);
|
||||
newRoot.sanityCheckParentAndChildIndexes();
|
||||
}
|
||||
|
||||
@Test public void testBecomeRoot5() throws Exception {
|
||||
// ^(nil 5) becomes new root of ^(101 102 103)
|
||||
CommonTree newRoot = new CommonTree((Token)null);
|
||||
newRoot.addChild(new CommonTree(new CommonToken(5)));
|
||||
|
||||
CommonTree oldRoot = new CommonTree(new CommonToken(101));
|
||||
oldRoot.addChild(new CommonTree(new CommonToken(102)));
|
||||
oldRoot.addChild(new CommonTree(new CommonToken(103)));
|
||||
|
||||
TreeAdaptor adaptor = new CommonTreeAdaptor();
|
||||
adaptor.becomeRoot(newRoot, oldRoot);
|
||||
newRoot.sanityCheckParentAndChildIndexes();
|
||||
}
|
||||
|
||||
@Test public void testBecomeRoot6() throws Exception {
|
||||
// emulates construction of ^(5 6)
|
||||
CommonTree root_0 = (CommonTree)adaptor.nil();
|
||||
CommonTree root_1 = (CommonTree)adaptor.nil();
|
||||
root_1 = (CommonTree)adaptor.becomeRoot(new CommonTree(new CommonToken(5)), root_1);
|
||||
|
||||
adaptor.addChild(root_1, new CommonTree(new CommonToken(6)));
|
||||
|
||||
adaptor.addChild(root_0, root_1);
|
||||
|
||||
root_0.sanityCheckParentAndChildIndexes();
|
||||
}
|
||||
|
||||
// Test replaceChildren
|
||||
|
||||
@Test public void testReplaceWithNoChildren() throws Exception {
|
||||
CommonTree t = new CommonTree(new CommonToken(101));
|
||||
CommonTree newChild = new CommonTree(new CommonToken(5));
|
||||
boolean error = false;
|
||||
try {
|
||||
t.replaceChildren(0, 0, newChild);
|
||||
}
|
||||
catch (IllegalArgumentException iae) {
|
||||
error = true;
|
||||
}
|
||||
assertTrue(error);
|
||||
}
|
||||
|
||||
@Test public void testReplaceWithOneChildren() throws Exception {
|
||||
// assume token type 99 and use text
|
||||
CommonTree t = new CommonTree(new CommonToken(99,"a"));
|
||||
CommonTree c0 = new CommonTree(new CommonToken(99, "b"));
|
||||
t.addChild(c0);
|
||||
|
||||
CommonTree newChild = new CommonTree(new CommonToken(99, "c"));
|
||||
t.replaceChildren(0, 0, newChild);
|
||||
String expecting = "(a c)";
|
||||
assertEquals(expecting, t.toStringTree());
|
||||
t.sanityCheckParentAndChildIndexes();
|
||||
}
|
||||
|
||||
@Test public void testReplaceInMiddle() throws Exception {
|
||||
CommonTree t = new CommonTree(new CommonToken(99, "a"));
|
||||
t.addChild(new CommonTree(new CommonToken(99, "b")));
|
||||
t.addChild(new CommonTree(new CommonToken(99, "c"))); // index 1
|
||||
t.addChild(new CommonTree(new CommonToken(99, "d")));
|
||||
|
||||
CommonTree newChild = new CommonTree(new CommonToken(99,"x"));
|
||||
t.replaceChildren(1, 1, newChild);
|
||||
String expecting = "(a b x d)";
|
||||
assertEquals(expecting, t.toStringTree());
|
||||
t.sanityCheckParentAndChildIndexes();
|
||||
}
|
||||
|
||||
@Test public void testReplaceAtLeft() throws Exception {
|
||||
CommonTree t = new CommonTree(new CommonToken(99, "a"));
|
||||
t.addChild(new CommonTree(new CommonToken(99, "b"))); // index 0
|
||||
t.addChild(new CommonTree(new CommonToken(99, "c")));
|
||||
t.addChild(new CommonTree(new CommonToken(99, "d")));
|
||||
|
||||
CommonTree newChild = new CommonTree(new CommonToken(99,"x"));
|
||||
t.replaceChildren(0, 0, newChild);
|
||||
String expecting = "(a x c d)";
|
||||
assertEquals(expecting, t.toStringTree());
|
||||
t.sanityCheckParentAndChildIndexes();
|
||||
}
|
||||
|
||||
@Test public void testReplaceAtRight() throws Exception {
|
||||
CommonTree t = new CommonTree(new CommonToken(99, "a"));
|
||||
t.addChild(new CommonTree(new CommonToken(99, "b")));
|
||||
t.addChild(new CommonTree(new CommonToken(99, "c")));
|
||||
t.addChild(new CommonTree(new CommonToken(99, "d"))); // index 2
|
||||
|
||||
CommonTree newChild = new CommonTree(new CommonToken(99,"x"));
|
||||
t.replaceChildren(2, 2, newChild);
|
||||
String expecting = "(a b c x)";
|
||||
assertEquals(expecting, t.toStringTree());
|
||||
t.sanityCheckParentAndChildIndexes();
|
||||
}
|
||||
|
||||
@Test public void testReplaceOneWithTwoAtLeft() throws Exception {
|
||||
CommonTree t = new CommonTree(new CommonToken(99, "a"));
|
||||
t.addChild(new CommonTree(new CommonToken(99, "b")));
|
||||
t.addChild(new CommonTree(new CommonToken(99, "c")));
|
||||
t.addChild(new CommonTree(new CommonToken(99, "d")));
|
||||
|
||||
CommonTree newChildren = (CommonTree)adaptor.nil();
|
||||
newChildren.addChild(new CommonTree(new CommonToken(99,"x")));
|
||||
newChildren.addChild(new CommonTree(new CommonToken(99,"y")));
|
||||
|
||||
t.replaceChildren(0, 0, newChildren);
|
||||
String expecting = "(a x y c d)";
|
||||
assertEquals(expecting, t.toStringTree());
|
||||
t.sanityCheckParentAndChildIndexes();
|
||||
}
|
||||
|
||||
@Test public void testReplaceOneWithTwoAtRight() throws Exception {
|
||||
CommonTree t = new CommonTree(new CommonToken(99, "a"));
|
||||
t.addChild(new CommonTree(new CommonToken(99, "b")));
|
||||
t.addChild(new CommonTree(new CommonToken(99, "c")));
|
||||
t.addChild(new CommonTree(new CommonToken(99, "d")));
|
||||
|
||||
CommonTree newChildren = (CommonTree)adaptor.nil();
|
||||
newChildren.addChild(new CommonTree(new CommonToken(99,"x")));
|
||||
newChildren.addChild(new CommonTree(new CommonToken(99,"y")));
|
||||
|
||||
t.replaceChildren(2, 2, newChildren);
|
||||
String expecting = "(a b c x y)";
|
||||
assertEquals(expecting, t.toStringTree());
|
||||
t.sanityCheckParentAndChildIndexes();
|
||||
}
|
||||
|
||||
@Test public void testReplaceOneWithTwoInMiddle() throws Exception {
|
||||
CommonTree t = new CommonTree(new CommonToken(99, "a"));
|
||||
t.addChild(new CommonTree(new CommonToken(99, "b")));
|
||||
t.addChild(new CommonTree(new CommonToken(99, "c")));
|
||||
t.addChild(new CommonTree(new CommonToken(99, "d")));
|
||||
|
||||
CommonTree newChildren = (CommonTree)adaptor.nil();
|
||||
newChildren.addChild(new CommonTree(new CommonToken(99,"x")));
|
||||
newChildren.addChild(new CommonTree(new CommonToken(99,"y")));
|
||||
|
||||
t.replaceChildren(1, 1, newChildren);
|
||||
String expecting = "(a b x y d)";
|
||||
assertEquals(expecting, t.toStringTree());
|
||||
t.sanityCheckParentAndChildIndexes();
|
||||
}
|
||||
|
||||
@Test public void testReplaceTwoWithOneAtLeft() throws Exception {
|
||||
CommonTree t = new CommonTree(new CommonToken(99, "a"));
|
||||
t.addChild(new CommonTree(new CommonToken(99, "b")));
|
||||
t.addChild(new CommonTree(new CommonToken(99, "c")));
|
||||
t.addChild(new CommonTree(new CommonToken(99, "d")));
|
||||
|
||||
CommonTree newChild = new CommonTree(new CommonToken(99,"x"));
|
||||
|
||||
t.replaceChildren(0, 1, newChild);
|
||||
String expecting = "(a x d)";
|
||||
assertEquals(expecting, t.toStringTree());
|
||||
t.sanityCheckParentAndChildIndexes();
|
||||
}
|
||||
|
||||
@Test public void testReplaceTwoWithOneAtRight() throws Exception {
|
||||
CommonTree t = new CommonTree(new CommonToken(99, "a"));
|
||||
t.addChild(new CommonTree(new CommonToken(99, "b")));
|
||||
t.addChild(new CommonTree(new CommonToken(99, "c")));
|
||||
t.addChild(new CommonTree(new CommonToken(99, "d")));
|
||||
|
||||
CommonTree newChild = new CommonTree(new CommonToken(99,"x"));
|
||||
|
||||
t.replaceChildren(1, 2, newChild);
|
||||
String expecting = "(a b x)";
|
||||
assertEquals(expecting, t.toStringTree());
|
||||
t.sanityCheckParentAndChildIndexes();
|
||||
}
|
||||
|
||||
@Test public void testReplaceAllWithOne() throws Exception {
|
||||
CommonTree t = new CommonTree(new CommonToken(99, "a"));
|
||||
t.addChild(new CommonTree(new CommonToken(99, "b")));
|
||||
t.addChild(new CommonTree(new CommonToken(99, "c")));
|
||||
t.addChild(new CommonTree(new CommonToken(99, "d")));
|
||||
|
||||
CommonTree newChild = new CommonTree(new CommonToken(99,"x"));
|
||||
|
||||
t.replaceChildren(0, 2, newChild);
|
||||
String expecting = "(a x)";
|
||||
assertEquals(expecting, t.toStringTree());
|
||||
t.sanityCheckParentAndChildIndexes();
|
||||
}
|
||||
|
||||
@Test public void testReplaceAllWithTwo() throws Exception {
|
||||
CommonTree t = new CommonTree(new CommonToken(99, "a"));
|
||||
t.addChild(new CommonTree(new CommonToken(99, "b")));
|
||||
t.addChild(new CommonTree(new CommonToken(99, "c")));
|
||||
t.addChild(new CommonTree(new CommonToken(99, "d")));
|
||||
|
||||
CommonTree newChildren = (CommonTree)adaptor.nil();
|
||||
newChildren.addChild(new CommonTree(new CommonToken(99,"x")));
|
||||
newChildren.addChild(new CommonTree(new CommonToken(99,"y")));
|
||||
|
||||
t.replaceChildren(0, 2, newChildren);
|
||||
String expecting = "(a x y)";
|
||||
assertEquals(expecting, t.toStringTree());
|
||||
t.sanityCheckParentAndChildIndexes();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue