got v3 tests working in v4
[git-p4: depot-paths = "//depot/code/antlr4/main/": change = 9074]
This commit is contained in:
parent
149033fb2e
commit
93581cfa98
|
@ -31,7 +31,7 @@ package org.antlr.v4.runtime.tree;
|
|||
|
||||
import java.util.*;
|
||||
|
||||
/** A generic tree implementation with no payload. You must subclass to
|
||||
/** A generic AST implementation with no payload. You must subclass to
|
||||
* actually have any user data. ANTLR v3 uses a list of children approach
|
||||
* instead of the child-sibling approach in v2. A flat tree (a list) is
|
||||
* an empty node whose children represent the list. An empty, but
|
||||
|
|
|
@ -32,6 +32,7 @@ package org.antlr.v4.runtime.tree;
|
|||
import org.antlr.v4.runtime.*;
|
||||
|
||||
/** A stream of tree nodes, accessing nodes from a tree of some kind */
|
||||
// TODO: rename to ASTNodeStream?
|
||||
public interface TreeNodeStream extends IntStream {
|
||||
/** Get a tree node at an absolute index i; 0..n-1.
|
||||
* If you don't want to buffer up nodes, then this method makes no
|
||||
|
|
|
@ -0,0 +1,530 @@
|
|||
/*
|
||||
[The "BSD license"]
|
||||
Copyright (c) 2011 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.v4.runtime.tree;
|
||||
|
||||
|
||||
import org.antlr.v4.runtime.Token;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/** Build and navigate trees with this object. Must know about the names
|
||||
* of tokens so you have to pass in a map or array of token names (from which
|
||||
* this class can build the map). I.e., Token DECL means nothing unless the
|
||||
* class can translate it to a token type.
|
||||
*
|
||||
* In order to create nodes and navigate, this class needs a ASTAdaptor.
|
||||
*
|
||||
* This class can build a token type -> node index for repeated use or for
|
||||
* iterating over the various nodes with a particular type.
|
||||
*
|
||||
* This class works in conjunction with the ASTAdaptor rather than moving
|
||||
* all this functionality into the adaptor. An adaptor helps build and
|
||||
* navigate trees using methods. This class helps you do it with string
|
||||
* patterns like "(A B C)". You can create a tree from that pattern or
|
||||
* match subtrees against it.
|
||||
*/
|
||||
public class TreeWizard {
|
||||
protected ASTAdaptor adaptor;
|
||||
protected Map tokenNameToTypeMap;
|
||||
|
||||
public interface ContextVisitor {
|
||||
// TODO: should this be called visit or something else?
|
||||
public void visit(Object t, Object parent, int childIndex, Map labels);
|
||||
}
|
||||
|
||||
public static abstract class Visitor implements ContextVisitor {
|
||||
public void visit(Object t, Object parent, int childIndex, Map labels) {
|
||||
visit(t);
|
||||
}
|
||||
public abstract void visit(Object t);
|
||||
}
|
||||
|
||||
/** When using %label:TOKENNAME in a tree for parse(), we must
|
||||
* track the label.
|
||||
*/
|
||||
public static class TreePattern extends CommonAST {
|
||||
public String label;
|
||||
public boolean hasTextArg;
|
||||
public TreePattern(Token payload) {
|
||||
super(payload);
|
||||
}
|
||||
public String toString() {
|
||||
if ( label!=null ) {
|
||||
return "%"+label+":"+super.toString();
|
||||
}
|
||||
else {
|
||||
return super.toString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class WildcardTreePattern extends TreePattern {
|
||||
public WildcardTreePattern(Token payload) {
|
||||
super(payload);
|
||||
}
|
||||
}
|
||||
|
||||
/** This adaptor creates TreePattern objects for use during scan() */
|
||||
public static class TreePatternASTAdaptor extends CommonASTAdaptor {
|
||||
public Object create(Token payload) {
|
||||
return new TreePattern(payload);
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: build indexes for the wizard
|
||||
|
||||
/** During fillBuffer(), we can make a reverse index from a set
|
||||
* of token types of interest to the list of indexes into the
|
||||
* node stream. This lets us convert a node pointer to a
|
||||
* stream index semi-efficiently for a list of interesting
|
||||
* nodes such as function definition nodes (you'll want to seek
|
||||
* to their bodies for an interpreter). Also useful for doing
|
||||
* dynamic searches; i.e., go find me all PLUS nodes.
|
||||
protected Map tokenTypeToStreamIndexesMap;
|
||||
|
||||
/** If tokenTypesToReverseIndex set to INDEX_ALL then indexing
|
||||
* occurs for all token types.
|
||||
public static final Set INDEX_ALL = new HashSet();
|
||||
|
||||
/** A set of token types user would like to index for faster lookup.
|
||||
* If this is INDEX_ALL, then all token types are tracked. If null,
|
||||
* then none are indexed.
|
||||
protected Set tokenTypesToReverseIndex = null;
|
||||
*/
|
||||
|
||||
public TreeWizard(ASTAdaptor adaptor) {
|
||||
this.adaptor = adaptor;
|
||||
}
|
||||
|
||||
public TreeWizard(ASTAdaptor adaptor, Map tokenNameToTypeMap) {
|
||||
this.adaptor = adaptor;
|
||||
this.tokenNameToTypeMap = tokenNameToTypeMap;
|
||||
}
|
||||
|
||||
public TreeWizard(ASTAdaptor adaptor, String[] tokenNames) {
|
||||
this.adaptor = adaptor;
|
||||
this.tokenNameToTypeMap = computeTokenTypes(tokenNames);
|
||||
}
|
||||
|
||||
public TreeWizard(String[] tokenNames) {
|
||||
this(new CommonASTAdaptor(), tokenNames);
|
||||
}
|
||||
|
||||
/** Compute a Map<String, Integer> that is an inverted index of
|
||||
* tokenNames (which maps int token types to names).
|
||||
*/
|
||||
public Map computeTokenTypes(String[] tokenNames) {
|
||||
Map m = new HashMap();
|
||||
if ( tokenNames==null ) {
|
||||
return m;
|
||||
}
|
||||
for (int ttype = Token.MIN_TOKEN_TYPE; ttype < tokenNames.length; ttype++) {
|
||||
String name = tokenNames[ttype];
|
||||
m.put(name, new Integer(ttype));
|
||||
}
|
||||
return m;
|
||||
}
|
||||
|
||||
/** Using the map of token names to token types, return the type. */
|
||||
public int getTokenType(String tokenName) {
|
||||
if ( tokenNameToTypeMap==null ) {
|
||||
return Token.INVALID_TYPE;
|
||||
}
|
||||
Integer ttypeI = (Integer)tokenNameToTypeMap.get(tokenName);
|
||||
if ( ttypeI!=null ) {
|
||||
return ttypeI.intValue();
|
||||
}
|
||||
return Token.INVALID_TYPE;
|
||||
}
|
||||
|
||||
/** Walk the entire tree and make a node name to nodes mapping.
|
||||
* For now, use recursion but later nonrecursive version may be
|
||||
* more efficient. Returns Map<Integer, List> where the List is
|
||||
* of your AST node type. The Integer is the token type of the node.
|
||||
*
|
||||
* TODO: save this index so that find and visit are faster
|
||||
*/
|
||||
public Map index(Object t) {
|
||||
Map m = new HashMap();
|
||||
_index(t, m);
|
||||
return m;
|
||||
}
|
||||
|
||||
/** Do the work for index */
|
||||
protected void _index(Object t, Map m) {
|
||||
if ( t==null ) {
|
||||
return;
|
||||
}
|
||||
int ttype = adaptor.getType(t);
|
||||
List elements = (List)m.get(new Integer(ttype));
|
||||
if ( elements==null ) {
|
||||
elements = new ArrayList();
|
||||
m.put(new Integer(ttype), elements);
|
||||
}
|
||||
elements.add(t);
|
||||
int n = adaptor.getChildCount(t);
|
||||
for (int i=0; i<n; i++) {
|
||||
Object child = adaptor.getChild(t, i);
|
||||
_index(child, m);
|
||||
}
|
||||
}
|
||||
|
||||
/** Return a List of tree nodes with token type ttype */
|
||||
public List find(Object t, int ttype) {
|
||||
final List nodes = new ArrayList();
|
||||
visit(t, ttype, new TreeWizard.Visitor() {
|
||||
public void visit(Object t) {
|
||||
nodes.add(t);
|
||||
}
|
||||
});
|
||||
return nodes;
|
||||
}
|
||||
|
||||
/** Return a List of subtrees matching pattern. */
|
||||
public List find(Object t, String pattern) {
|
||||
final List subtrees = new ArrayList();
|
||||
// Create a TreePattern from the pattern
|
||||
TreePatternLexer tokenizer = new TreePatternLexer(pattern);
|
||||
TreePatternParser parser =
|
||||
new TreePatternParser(tokenizer, this, new TreePatternASTAdaptor());
|
||||
final TreePattern tpattern = (TreePattern)parser.pattern();
|
||||
// don't allow invalid patterns
|
||||
if ( tpattern==null ||
|
||||
tpattern.isNil() ||
|
||||
tpattern.getClass()==WildcardTreePattern.class )
|
||||
{
|
||||
return null;
|
||||
}
|
||||
int rootTokenType = tpattern.getType();
|
||||
visit(t, rootTokenType, new TreeWizard.ContextVisitor() {
|
||||
public void visit(Object t, Object parent, int childIndex, Map labels) {
|
||||
if ( _parse(t, tpattern, null) ) {
|
||||
subtrees.add(t);
|
||||
}
|
||||
}
|
||||
});
|
||||
return subtrees;
|
||||
}
|
||||
|
||||
public Object findFirst(Object t, int ttype) {
|
||||
return null;
|
||||
}
|
||||
|
||||
public Object findFirst(Object t, String pattern) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/** Visit every ttype node in t, invoking the visitor. This is a quicker
|
||||
* version of the general visit(t, pattern) method. The labels arg
|
||||
* of the visitor action method is never set (it's null) since using
|
||||
* a token type rather than a pattern doesn't let us set a label.
|
||||
*/
|
||||
public void visit(Object t, int ttype, ContextVisitor visitor) {
|
||||
_visit(t, null, 0, ttype, visitor);
|
||||
}
|
||||
|
||||
/** Do the recursive work for visit */
|
||||
protected void _visit(Object t, Object parent, int childIndex, int ttype, ContextVisitor visitor) {
|
||||
if ( t==null ) {
|
||||
return;
|
||||
}
|
||||
if ( adaptor.getType(t)==ttype ) {
|
||||
visitor.visit(t, parent, childIndex, null);
|
||||
}
|
||||
int n = adaptor.getChildCount(t);
|
||||
for (int i=0; i<n; i++) {
|
||||
Object child = adaptor.getChild(t, i);
|
||||
_visit(child, t, i, ttype, visitor);
|
||||
}
|
||||
}
|
||||
|
||||
/** For all subtrees that match the pattern, execute the visit action.
|
||||
* The implementation uses the root node of the pattern in combination
|
||||
* with visit(t, ttype, visitor) so nil-rooted patterns are not allowed.
|
||||
* Patterns with wildcard roots are also not allowed.
|
||||
*/
|
||||
public void visit(Object t, final String pattern, final ContextVisitor visitor) {
|
||||
// Create a TreePattern from the pattern
|
||||
TreePatternLexer tokenizer = new TreePatternLexer(pattern);
|
||||
TreePatternParser parser =
|
||||
new TreePatternParser(tokenizer, this, new TreePatternASTAdaptor());
|
||||
final TreePattern tpattern = (TreePattern)parser.pattern();
|
||||
// don't allow invalid patterns
|
||||
if ( tpattern==null ||
|
||||
tpattern.isNil() ||
|
||||
tpattern.getClass()==WildcardTreePattern.class )
|
||||
{
|
||||
return;
|
||||
}
|
||||
final Map labels = new HashMap(); // reused for each _parse
|
||||
int rootTokenType = tpattern.getType();
|
||||
visit(t, rootTokenType, new TreeWizard.ContextVisitor() {
|
||||
public void visit(Object t, Object parent, int childIndex, Map unusedlabels) {
|
||||
// the unusedlabels arg is null as visit on token type doesn't set.
|
||||
labels.clear();
|
||||
if ( _parse(t, tpattern, labels) ) {
|
||||
visitor.visit(t, parent, childIndex, labels);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/** Given a pattern like (ASSIGN %lhs:ID %rhs:.) with optional labels
|
||||
* on the various nodes and '.' (dot) as the node/subtree wildcard,
|
||||
* return true if the pattern matches and fill the labels Map with
|
||||
* the labels pointing at the appropriate nodes. Return false if
|
||||
* the pattern is malformed or the tree does not match.
|
||||
*
|
||||
* If a node specifies a text arg in pattern, then that must match
|
||||
* for that node in t.
|
||||
*
|
||||
* TODO: what's a better way to indicate bad pattern? Exceptions are a hassle
|
||||
*/
|
||||
public boolean parse(Object t, String pattern, Map labels) {
|
||||
TreePatternLexer tokenizer = new TreePatternLexer(pattern);
|
||||
TreePatternParser parser =
|
||||
new TreePatternParser(tokenizer, this, new TreePatternASTAdaptor());
|
||||
TreePattern tpattern = (TreePattern)parser.pattern();
|
||||
/*
|
||||
System.out.println("t="+((Tree)t).toStringTree());
|
||||
System.out.println("scant="+tpattern.toStringTree());
|
||||
*/
|
||||
boolean matched = _parse(t, tpattern, labels);
|
||||
return matched;
|
||||
}
|
||||
|
||||
public boolean parse(Object t, String pattern) {
|
||||
return parse(t, pattern, null);
|
||||
}
|
||||
|
||||
/** Do the work for parse. Check to see if the t2 pattern fits the
|
||||
* structure and token types in t1. Check text if the pattern has
|
||||
* text arguments on nodes. Fill labels map with pointers to nodes
|
||||
* in tree matched against nodes in pattern with labels.
|
||||
*/
|
||||
protected boolean _parse(Object t1, TreePattern tpattern, Map labels) {
|
||||
// make sure both are non-null
|
||||
if ( t1==null || tpattern==null ) {
|
||||
return false;
|
||||
}
|
||||
// check roots (wildcard matches anything)
|
||||
if ( tpattern.getClass() != WildcardTreePattern.class ) {
|
||||
if ( adaptor.getType(t1) != tpattern.getType() ) return false;
|
||||
// if pattern has text, check node text
|
||||
if ( tpattern.hasTextArg && !adaptor.getText(t1).equals(tpattern.getText()) ) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if ( tpattern.label!=null && labels!=null ) {
|
||||
// map label in pattern to node in t1
|
||||
labels.put(tpattern.label, t1);
|
||||
}
|
||||
// check children
|
||||
int n1 = adaptor.getChildCount(t1);
|
||||
int n2 = tpattern.getChildCount();
|
||||
if ( n1 != n2 ) {
|
||||
return false;
|
||||
}
|
||||
for (int i=0; i<n1; i++) {
|
||||
Object child1 = adaptor.getChild(t1, i);
|
||||
TreePattern child2 = (TreePattern)tpattern.getChild(i);
|
||||
if ( !_parse(child1, child2, labels) ) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/** Create a tree or node from the indicated tree pattern that closely
|
||||
* follows ANTLR tree grammar tree element syntax:
|
||||
*
|
||||
* (root child1 ... child2).
|
||||
*
|
||||
* You can also just pass in a node: ID
|
||||
*
|
||||
* Any node can have a text argument: ID[foo]
|
||||
* (notice there are no quotes around foo--it's clear it's a string).
|
||||
*
|
||||
* nil is a special name meaning "give me a nil node". Useful for
|
||||
* making lists: (nil A B C) is a list of A B C.
|
||||
*/
|
||||
public Object create(String pattern) {
|
||||
TreePatternLexer tokenizer = new TreePatternLexer(pattern);
|
||||
TreePatternParser parser = new TreePatternParser(tokenizer, this, adaptor);
|
||||
Object t = parser.pattern();
|
||||
return t;
|
||||
}
|
||||
|
||||
/** Compare t1 and t2; return true if token types/text, structure match exactly.
|
||||
* The trees are examined in their entirety so that (A B) does not match
|
||||
* (A B C) nor (A (B C)).
|
||||
// TODO: allow them to pass in a comparator
|
||||
* TODO: have a version that is nonstatic so it can use instance adaptor
|
||||
*
|
||||
* I cannot rely on the tree node's equals() implementation as I make
|
||||
* no constraints at all on the node types nor interface etc...
|
||||
*/
|
||||
public static boolean equals(Object t1, Object t2, ASTAdaptor adaptor) {
|
||||
return _equals(t1, t2, adaptor);
|
||||
}
|
||||
|
||||
/** Compare type, structure, and text of two trees, assuming adaptor in
|
||||
* this instance of a TreeWizard.
|
||||
*/
|
||||
public boolean equals(Object t1, Object t2) {
|
||||
return _equals(t1, t2, adaptor);
|
||||
}
|
||||
|
||||
protected static boolean _equals(Object t1, Object t2, ASTAdaptor adaptor) {
|
||||
// make sure both are non-null
|
||||
if ( t1==null || t2==null ) {
|
||||
return false;
|
||||
}
|
||||
// check roots
|
||||
if ( adaptor.getType(t1) != adaptor.getType(t2) ) {
|
||||
return false;
|
||||
}
|
||||
if ( !adaptor.getText(t1).equals(adaptor.getText(t2)) ) {
|
||||
return false;
|
||||
}
|
||||
// check children
|
||||
int n1 = adaptor.getChildCount(t1);
|
||||
int n2 = adaptor.getChildCount(t2);
|
||||
if ( n1 != n2 ) {
|
||||
return false;
|
||||
}
|
||||
for (int i=0; i<n1; i++) {
|
||||
Object child1 = adaptor.getChild(t1, i);
|
||||
Object child2 = adaptor.getChild(t2, i);
|
||||
if ( !_equals(child1, child2, adaptor) ) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// TODO: next stuff taken from CommonASTNodeStream
|
||||
|
||||
/** Given a node, add this to the reverse index tokenTypeToStreamIndexesMap.
|
||||
* You can override this method to alter how indexing occurs. The
|
||||
* default is to create a
|
||||
*
|
||||
* Map<Integer token type,ArrayList<Integer stream index>>
|
||||
*
|
||||
* This data structure allows you to find all nodes with type INT in order.
|
||||
*
|
||||
* If you really need to find a node of type, say, FUNC quickly then perhaps
|
||||
*
|
||||
* Map<Integertoken type,Map<Object tree node,Integer stream index>>
|
||||
*
|
||||
* would be better for you. The interior maps map a tree node to
|
||||
* the index so you don't have to search linearly for a specific node.
|
||||
*
|
||||
* If you change this method, you will likely need to change
|
||||
* getNodeIndex(), which extracts information.
|
||||
protected void fillReverseIndex(Object node, int streamIndex) {
|
||||
//System.out.println("revIndex "+node+"@"+streamIndex);
|
||||
if ( tokenTypesToReverseIndex==null ) {
|
||||
return; // no indexing if this is empty (nothing of interest)
|
||||
}
|
||||
if ( tokenTypeToStreamIndexesMap==null ) {
|
||||
tokenTypeToStreamIndexesMap = new HashMap(); // first indexing op
|
||||
}
|
||||
int tokenType = adaptor.getType(node);
|
||||
Integer tokenTypeI = new Integer(tokenType);
|
||||
if ( !(tokenTypesToReverseIndex==INDEX_ALL ||
|
||||
tokenTypesToReverseIndex.contains(tokenTypeI)) )
|
||||
{
|
||||
return; // tokenType not of interest
|
||||
}
|
||||
Integer streamIndexI = new Integer(streamIndex);
|
||||
ArrayList indexes = (ArrayList)tokenTypeToStreamIndexesMap.get(tokenTypeI);
|
||||
if ( indexes==null ) {
|
||||
indexes = new ArrayList(); // no list yet for this token type
|
||||
indexes.add(streamIndexI); // not there yet, add
|
||||
tokenTypeToStreamIndexesMap.put(tokenTypeI, indexes);
|
||||
}
|
||||
else {
|
||||
if ( !indexes.contains(streamIndexI) ) {
|
||||
indexes.add(streamIndexI); // not there yet, add
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Track the indicated token type in the reverse index. Call this
|
||||
* repeatedly for each type or use variant with Set argument to
|
||||
* set all at once.
|
||||
* @param tokenType
|
||||
public void reverseIndex(int tokenType) {
|
||||
if ( tokenTypesToReverseIndex==null ) {
|
||||
tokenTypesToReverseIndex = new HashSet();
|
||||
}
|
||||
else if ( tokenTypesToReverseIndex==INDEX_ALL ) {
|
||||
return;
|
||||
}
|
||||
tokenTypesToReverseIndex.add(new Integer(tokenType));
|
||||
}
|
||||
|
||||
/** Track the indicated token types in the reverse index. Set
|
||||
* to INDEX_ALL to track all token types.
|
||||
public void reverseIndex(Set tokenTypes) {
|
||||
tokenTypesToReverseIndex = tokenTypes;
|
||||
}
|
||||
|
||||
/** Given a node pointer, return its index into the node stream.
|
||||
* This is not its Token stream index. If there is no reverse map
|
||||
* from node to stream index or the map does not contain entries
|
||||
* for node's token type, a linear search of entire stream is used.
|
||||
*
|
||||
* Return -1 if exact node pointer not in stream.
|
||||
public int getNodeIndex(Object node) {
|
||||
//System.out.println("get "+node);
|
||||
if ( tokenTypeToStreamIndexesMap==null ) {
|
||||
return getNodeIndexLinearly(node);
|
||||
}
|
||||
int tokenType = adaptor.getType(node);
|
||||
Integer tokenTypeI = new Integer(tokenType);
|
||||
ArrayList indexes = (ArrayList)tokenTypeToStreamIndexesMap.get(tokenTypeI);
|
||||
if ( indexes==null ) {
|
||||
//System.out.println("found linearly; stream index = "+getNodeIndexLinearly(node));
|
||||
return getNodeIndexLinearly(node);
|
||||
}
|
||||
for (int i = 0; i < indexes.size(); i++) {
|
||||
Integer streamIndexI = (Integer)indexes.get(i);
|
||||
Object n = get(streamIndexI.intValue());
|
||||
if ( n==node ) {
|
||||
//System.out.println("found in index; stream index = "+streamIndexI);
|
||||
return streamIndexI.intValue(); // found it!
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
*/
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
Actions/scopes
|
||||
|
||||
* no global scopes. no scope[n].
|
||||
|
||||
Trees
|
||||
|
||||
* moved methods to Trees
|
||||
* Tree->AST
|
||||
* added parse trees
|
|
@ -970,7 +970,7 @@ public abstract class BaseTest {
|
|||
" Trees.sanityCheckParentAndChildIndexes((CommonAST)r.tree);\n" +
|
||||
" }\n" +
|
||||
" <else>\n" +
|
||||
" CommonTreeNodeStream nodes = new CommonTreeNodeStream((Tree)r.tree);\n" +
|
||||
" CommonASTNodeStream nodes = new CommonASTNodeStream((Tree)r.tree);\n" +
|
||||
" nodes.setTokenStream(tokens);\n" +
|
||||
" <treeParserName> walker = new <treeParserName>(nodes);\n" +
|
||||
" walker.<treeParserStartRuleName>();\n" +
|
||||
|
@ -1021,7 +1021,7 @@ public abstract class BaseTest {
|
|||
" <createParser>\n"+
|
||||
" ParserRuleContext r = parser.<parserStartRuleName>();\n" +
|
||||
" Trees.sanityCheckParentAndChildIndexes((CommonAST)r.tree);\n" +
|
||||
" CommonTreeNodeStream nodes = new CommonTreeNodeStream((Tree)r.tree);\n" +
|
||||
" CommonASTNodeStream nodes = new CommonASTNodeStream((Tree)r.tree);\n" +
|
||||
" nodes.setTokenStream(tokens);\n" +
|
||||
" <treeParserName> walker = new <treeParserName>(nodes);\n" +
|
||||
" ParserRuleContext r2 = walker.<treeParserStartRuleName>();\n" +
|
||||
|
|
|
@ -1,33 +1,34 @@
|
|||
/*
|
||||
* [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;
|
||||
[The "BSD license"]
|
||||
Copyright (c) 2011 Terence Parr
|
||||
All rights reserved.
|
||||
|
||||
import org.antlr.runtime.tree.*;
|
||||
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.v4.test;
|
||||
|
||||
import org.antlr.v4.runtime.tree.*;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
@ -38,9 +39,9 @@ public class TestTreeIterator {
|
|||
};
|
||||
|
||||
@Test public void testNode() {
|
||||
TreeAdaptor adaptor = new CommonTreeAdaptor();
|
||||
ASTAdaptor adaptor = new CommonASTAdaptor();
|
||||
TreeWizard wiz = new TreeWizard(adaptor, tokens);
|
||||
CommonTree t = (CommonTree)wiz.create("A");
|
||||
CommonAST t = (CommonAST)wiz.create("A");
|
||||
TreeIterator it = new TreeIterator(t);
|
||||
StringBuffer buf = toString(it);
|
||||
String expecting = "A EOF";
|
||||
|
@ -49,9 +50,9 @@ public class TestTreeIterator {
|
|||
}
|
||||
|
||||
@Test public void testFlatAB() {
|
||||
TreeAdaptor adaptor = new CommonTreeAdaptor();
|
||||
ASTAdaptor adaptor = new CommonASTAdaptor();
|
||||
TreeWizard wiz = new TreeWizard(adaptor, tokens);
|
||||
CommonTree t = (CommonTree)wiz.create("(nil A B)");
|
||||
CommonAST t = (CommonAST)wiz.create("(nil A B)");
|
||||
TreeIterator it = new TreeIterator(t);
|
||||
StringBuffer buf = toString(it);
|
||||
String expecting = "nil DOWN A B UP EOF";
|
||||
|
@ -60,9 +61,9 @@ public class TestTreeIterator {
|
|||
}
|
||||
|
||||
@Test public void testAB() {
|
||||
TreeAdaptor adaptor = new CommonTreeAdaptor();
|
||||
ASTAdaptor adaptor = new CommonASTAdaptor();
|
||||
TreeWizard wiz = new TreeWizard(adaptor, tokens);
|
||||
CommonTree t = (CommonTree)wiz.create("(A B)");
|
||||
CommonAST t = (CommonAST)wiz.create("(A B)");
|
||||
TreeIterator it = new TreeIterator(t);
|
||||
StringBuffer buf = toString(it);
|
||||
String expecting = "A DOWN B UP EOF";
|
||||
|
@ -71,9 +72,9 @@ public class TestTreeIterator {
|
|||
}
|
||||
|
||||
@Test public void testABC() {
|
||||
TreeAdaptor adaptor = new CommonTreeAdaptor();
|
||||
ASTAdaptor adaptor = new CommonASTAdaptor();
|
||||
TreeWizard wiz = new TreeWizard(adaptor, tokens);
|
||||
CommonTree t = (CommonTree)wiz.create("(A B C)");
|
||||
CommonAST t = (CommonAST)wiz.create("(A B C)");
|
||||
TreeIterator it = new TreeIterator(t);
|
||||
StringBuffer buf = toString(it);
|
||||
String expecting = "A DOWN B C UP EOF";
|
||||
|
@ -82,9 +83,9 @@ public class TestTreeIterator {
|
|||
}
|
||||
|
||||
@Test public void testVerticalList() {
|
||||
TreeAdaptor adaptor = new CommonTreeAdaptor();
|
||||
ASTAdaptor adaptor = new CommonASTAdaptor();
|
||||
TreeWizard wiz = new TreeWizard(adaptor, tokens);
|
||||
CommonTree t = (CommonTree)wiz.create("(A (B C))");
|
||||
CommonAST t = (CommonAST)wiz.create("(A (B C))");
|
||||
TreeIterator it = new TreeIterator(t);
|
||||
StringBuffer buf = toString(it);
|
||||
String expecting = "A DOWN B DOWN C UP UP EOF";
|
||||
|
@ -93,9 +94,9 @@ public class TestTreeIterator {
|
|||
}
|
||||
|
||||
@Test public void testComplex() {
|
||||
TreeAdaptor adaptor = new CommonTreeAdaptor();
|
||||
ASTAdaptor adaptor = new CommonASTAdaptor();
|
||||
TreeWizard wiz = new TreeWizard(adaptor, tokens);
|
||||
CommonTree t = (CommonTree)wiz.create("(A (B (C D E) F) G)");
|
||||
CommonAST t = (CommonAST)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";
|
||||
|
@ -104,9 +105,9 @@ public class TestTreeIterator {
|
|||
}
|
||||
|
||||
@Test public void testReset() {
|
||||
TreeAdaptor adaptor = new CommonTreeAdaptor();
|
||||
ASTAdaptor adaptor = new CommonASTAdaptor();
|
||||
TreeWizard wiz = new TreeWizard(adaptor, tokens);
|
||||
CommonTree t = (CommonTree)wiz.create("(A (B (C D E) F) G)");
|
||||
CommonAST t = (CommonAST)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";
|
||||
|
@ -123,7 +124,7 @@ public class TestTreeIterator {
|
|||
protected static StringBuffer toString(TreeIterator it) {
|
||||
StringBuffer buf = new StringBuffer();
|
||||
while ( it.hasNext() ) {
|
||||
CommonTree n = (CommonTree)it.next();
|
||||
CommonAST n = (CommonAST)it.next();
|
||||
buf.append(n);
|
||||
if ( it.hasNext() ) buf.append(" ");
|
||||
}
|
||||
|
|
|
@ -11,7 +11,7 @@
|
|||
* 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
|
||||
* "+UP+". 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
|
||||
|
@ -25,27 +25,28 @@
|
|||
* (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;
|
||||
package org.antlr.v4.test;
|
||||
|
||||
import org.antlr.runtime.CommonToken;
|
||||
import org.antlr.runtime.Token;
|
||||
import org.antlr.runtime.tree.*;
|
||||
import org.antlr.v4.runtime.*;
|
||||
import org.antlr.v4.runtime.tree.*;
|
||||
import org.junit.Test;
|
||||
|
||||
/** Test the tree node stream. */
|
||||
public class TestTreeNodeStream extends BaseTest {
|
||||
public static final String DN = " "+Token.DOWN+" ";
|
||||
public static final String UP = " "+Token.UP;
|
||||
|
||||
/** Build new stream; let's us override to test other streams. */
|
||||
public TreeNodeStream newStream(Object t) {
|
||||
return new CommonTreeNodeStream(t);
|
||||
return new CommonASTNodeStream(t);
|
||||
}
|
||||
|
||||
public String toTokenTypeString(TreeNodeStream stream) {
|
||||
return ((CommonTreeNodeStream)stream).toTokenTypeString();
|
||||
return ((CommonASTNodeStream)stream).toTokenTypeString();
|
||||
}
|
||||
|
||||
@Test public void testSingleNode() throws Exception {
|
||||
Tree t = new CommonTree(new CommonToken(101));
|
||||
BaseAST t = new CommonAST(new CommonToken(101));
|
||||
|
||||
TreeNodeStream stream = newStream(t);
|
||||
String expecting = " 101";
|
||||
|
@ -59,30 +60,30 @@ public class TestTreeNodeStream extends BaseTest {
|
|||
|
||||
@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)));
|
||||
BaseAST t = new CommonAST(new CommonToken(101));
|
||||
t.addChild(new CommonAST(new CommonToken(102)));
|
||||
t.getChild(0).addChild(new CommonAST(new CommonToken(103)));
|
||||
t.addChild(new CommonAST(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";
|
||||
expecting = " 101"+DN+"102"+DN+"103"+UP+" 104"+UP+"";
|
||||
found = toTokenTypeString(stream);
|
||||
assertEquals(expecting, found);
|
||||
}
|
||||
|
||||
@Test public void testList() throws Exception {
|
||||
Tree root = new CommonTree((Token)null);
|
||||
BaseAST root = new CommonAST((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)));
|
||||
BaseAST t = new CommonAST(new CommonToken(101));
|
||||
t.addChild(new CommonAST(new CommonToken(102)));
|
||||
t.getChild(0).addChild(new CommonAST(new CommonToken(103)));
|
||||
t.addChild(new CommonAST(new CommonToken(104)));
|
||||
|
||||
Tree u = new CommonTree(new CommonToken(105));
|
||||
BaseAST u = new CommonAST(new CommonToken(105));
|
||||
|
||||
root.addChild(t);
|
||||
root.addChild(u);
|
||||
|
@ -92,17 +93,17 @@ public class TestTreeNodeStream extends BaseTest {
|
|||
String found = toNodesOnlyString(stream);
|
||||
assertEquals(expecting, found);
|
||||
|
||||
expecting = " 101 2 102 2 103 3 104 3 105";
|
||||
expecting = " 101"+DN+"102"+DN+"103"+UP+" 104"+UP+" 105";
|
||||
found = toTokenTypeString(stream);
|
||||
assertEquals(expecting, found);
|
||||
}
|
||||
|
||||
@Test public void testFlatList() throws Exception {
|
||||
Tree root = new CommonTree((Token)null);
|
||||
BaseAST root = new CommonAST((Token)null);
|
||||
|
||||
root.addChild(new CommonTree(new CommonToken(101)));
|
||||
root.addChild(new CommonTree(new CommonToken(102)));
|
||||
root.addChild(new CommonTree(new CommonToken(103)));
|
||||
root.addChild(new CommonAST(new CommonToken(101)));
|
||||
root.addChild(new CommonAST(new CommonToken(102)));
|
||||
root.addChild(new CommonAST(new CommonToken(103)));
|
||||
|
||||
TreeNodeStream stream = newStream(root);
|
||||
String expecting = " 101 102 103";
|
||||
|
@ -115,9 +116,9 @@ public class TestTreeNodeStream extends BaseTest {
|
|||
}
|
||||
|
||||
@Test public void testListWithOneNode() throws Exception {
|
||||
Tree root = new CommonTree((Token)null);
|
||||
BaseAST root = new CommonAST((Token)null);
|
||||
|
||||
root.addChild(new CommonTree(new CommonToken(101)));
|
||||
root.addChild(new CommonAST(new CommonToken(101)));
|
||||
|
||||
TreeNodeStream stream = newStream(root);
|
||||
String expecting = " 101";
|
||||
|
@ -130,53 +131,53 @@ public class TestTreeNodeStream extends BaseTest {
|
|||
}
|
||||
|
||||
@Test public void testAoverB() throws Exception {
|
||||
Tree t = new CommonTree(new CommonToken(101));
|
||||
t.addChild(new CommonTree(new CommonToken(102)));
|
||||
BaseAST t = new CommonAST(new CommonToken(101));
|
||||
t.addChild(new CommonAST(new CommonToken(102)));
|
||||
|
||||
TreeNodeStream stream = newStream(t);
|
||||
String expecting = " 101 102";
|
||||
String found = toNodesOnlyString(stream);
|
||||
assertEquals(expecting, found);
|
||||
|
||||
expecting = " 101 2 102 3";
|
||||
expecting = " 101"+DN+"102"+UP+"";
|
||||
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)));
|
||||
BaseAST t = new CommonAST(new CommonToken(101));
|
||||
t.addChild(new CommonAST(new CommonToken(102)));
|
||||
t.getChild(0).addChild(new CommonAST(new CommonToken(103)));
|
||||
t.addChild(new CommonAST(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());
|
||||
assertEquals(101, ((AST)stream.LT(1)).getType());
|
||||
assertEquals(Token.DOWN, ((AST)stream.LT(2)).getType());
|
||||
assertEquals(102, ((AST)stream.LT(3)).getType());
|
||||
assertEquals(Token.DOWN, ((AST)stream.LT(4)).getType());
|
||||
assertEquals(103, ((AST)stream.LT(5)).getType());
|
||||
assertEquals(Token.UP, ((AST)stream.LT(6)).getType());
|
||||
assertEquals(104, ((AST)stream.LT(7)).getType());
|
||||
assertEquals(Token.UP, ((AST)stream.LT(8)).getType());
|
||||
assertEquals(Token.EOF, ((AST)stream.LT(9)).getType());
|
||||
// check way ahead
|
||||
assertEquals(Token.EOF, ((Tree)stream.LT(100)).getType());
|
||||
assertEquals(Token.EOF, ((AST)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));
|
||||
BaseAST r0 = new CommonAST(new CommonToken(101));
|
||||
BaseAST r1 = new CommonAST(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(new CommonAST(new CommonToken(103)));
|
||||
BaseAST r2 = new CommonAST(new CommonToken(106));
|
||||
r2.addChild(new CommonAST(new CommonToken(107)));
|
||||
r1.addChild(r2);
|
||||
r0.addChild(new CommonTree(new CommonToken(104)));
|
||||
r0.addChild(new CommonTree(new CommonToken(105)));
|
||||
r0.addChild(new CommonAST(new CommonToken(104)));
|
||||
r0.addChild(new CommonAST(new CommonToken(105)));
|
||||
|
||||
TreeNodeStream stream = newStream(r0);
|
||||
int m = stream.mark(); // MARK
|
||||
|
@ -184,75 +185,77 @@ public class TestTreeNodeStream extends BaseTest {
|
|||
stream.LT(1);
|
||||
stream.consume();
|
||||
}
|
||||
assertEquals(Token.EOF, ((Tree)stream.LT(1)).getType());
|
||||
stream.rewind(m); // REWIND
|
||||
assertEquals(Token.EOF, ((AST)stream.LT(1)).getType());
|
||||
stream.release(m);
|
||||
stream.seek(m); // i know it's an index
|
||||
|
||||
// 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());
|
||||
assertEquals(Token.EOF, ((AST)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));
|
||||
BaseAST r0 = new CommonAST(new CommonToken(101));
|
||||
BaseAST r1 = new CommonAST(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(new CommonAST(new CommonToken(103)));
|
||||
BaseAST r2 = new CommonAST(new CommonToken(106));
|
||||
r2.addChild(new CommonAST(new CommonToken(107)));
|
||||
r1.addChild(r2);
|
||||
r0.addChild(new CommonTree(new CommonToken(104)));
|
||||
r0.addChild(new CommonTree(new CommonToken(105)));
|
||||
r0.addChild(new CommonAST(new CommonToken(104)));
|
||||
r0.addChild(new CommonAST(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());
|
||||
//System.out.println(((AST)stream.LT(1)).getType());
|
||||
stream.consume();
|
||||
}
|
||||
assertEquals(107, ((Tree)stream.LT(1)).getType());
|
||||
stream.mark(); // MARK
|
||||
assertEquals(107, ((AST)stream.LT(1)).getType());
|
||||
int m = stream.mark(); // MARK
|
||||
stream.consume(); // consume 107
|
||||
stream.consume(); // consume UP
|
||||
stream.consume(); // consume UP
|
||||
stream.consume(); // consume 104
|
||||
stream.rewind(); // REWIND
|
||||
stream.release(m); // REWIND
|
||||
stream.seek(m);
|
||||
stream.mark(); // keep saving nodes though
|
||||
|
||||
assertEquals(107, ((Tree)stream.LT(1)).getType());
|
||||
assertEquals(107, ((AST)stream.LT(1)).getType());
|
||||
stream.consume();
|
||||
assertEquals(Token.UP, ((Tree)stream.LT(1)).getType());
|
||||
assertEquals(Token.UP, ((AST)stream.LT(1)).getType());
|
||||
stream.consume();
|
||||
assertEquals(Token.UP, ((Tree)stream.LT(1)).getType());
|
||||
assertEquals(Token.UP, ((AST)stream.LT(1)).getType());
|
||||
stream.consume();
|
||||
assertEquals(104, ((Tree)stream.LT(1)).getType());
|
||||
assertEquals(104, ((AST)stream.LT(1)).getType());
|
||||
stream.consume();
|
||||
// now we're past rewind position
|
||||
assertEquals(105, ((Tree)stream.LT(1)).getType());
|
||||
assertEquals(105, ((AST)stream.LT(1)).getType());
|
||||
stream.consume();
|
||||
assertEquals(Token.UP, ((Tree)stream.LT(1)).getType());
|
||||
assertEquals(Token.UP, ((AST)stream.LT(1)).getType());
|
||||
stream.consume();
|
||||
assertEquals(Token.EOF, ((Tree)stream.LT(1)).getType());
|
||||
assertEquals(Token.UP, ((Tree)stream.LT(-1)).getType());
|
||||
assertEquals(Token.EOF, ((AST)stream.LT(1)).getType());
|
||||
assertEquals(Token.UP, ((AST)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));
|
||||
BaseAST r0 = new CommonAST(new CommonToken(101));
|
||||
BaseAST r1 = new CommonAST(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(new CommonAST(new CommonToken(103)));
|
||||
BaseAST r2 = new CommonAST(new CommonToken(106));
|
||||
r2.addChild(new CommonAST(new CommonToken(107)));
|
||||
r1.addChild(r2);
|
||||
r0.addChild(new CommonTree(new CommonToken(104)));
|
||||
r0.addChild(new CommonTree(new CommonToken(105)));
|
||||
r0.addChild(new CommonAST(new CommonToken(104)));
|
||||
r0.addChild(new CommonAST(new CommonToken(105)));
|
||||
|
||||
TreeNodeStream stream = newStream(r0);
|
||||
int m = stream.mark(); // MARK at start
|
||||
|
@ -263,58 +266,60 @@ public class TestTreeNodeStream extends BaseTest {
|
|||
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.release(m2); // REWIND to 102
|
||||
stream.seek(m2);
|
||||
assertEquals(102, ((AST)stream.LT(1)).getType());
|
||||
stream.consume();
|
||||
assertEquals(Token.DOWN, ((Tree)stream.LT(1)).getType());
|
||||
assertEquals(Token.DOWN, ((AST)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.release(m); // REWIND to 101
|
||||
stream.seek(m);
|
||||
assertEquals(101, ((AST)stream.LT(1)).getType());
|
||||
stream.consume();
|
||||
assertEquals(Token.DOWN, ((Tree)stream.LT(1)).getType());
|
||||
assertEquals(Token.DOWN, ((AST)stream.LT(1)).getType());
|
||||
stream.consume();
|
||||
assertEquals(102, ((Tree)stream.LT(1)).getType());
|
||||
assertEquals(102, ((AST)stream.LT(1)).getType());
|
||||
stream.consume();
|
||||
assertEquals(Token.DOWN, ((Tree)stream.LT(1)).getType());
|
||||
assertEquals(Token.DOWN, ((AST)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));
|
||||
BaseAST r0 = new CommonAST(new CommonToken(101));
|
||||
BaseAST r1 = new CommonAST(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(new CommonAST(new CommonToken(103)));
|
||||
BaseAST r2 = new CommonAST(new CommonToken(106));
|
||||
r2.addChild(new CommonAST(new CommonToken(107)));
|
||||
r1.addChild(r2);
|
||||
r0.addChild(new CommonTree(new CommonToken(104)));
|
||||
r0.addChild(new CommonTree(new CommonToken(105)));
|
||||
r0.addChild(new CommonAST(new CommonToken(104)));
|
||||
r0.addChild(new CommonAST(new CommonToken(105)));
|
||||
|
||||
TreeNodeStream stream = newStream(r0);
|
||||
stream.seek(7); // seek to 107
|
||||
assertEquals(107, ((Tree)stream.LT(1)).getType());
|
||||
assertEquals(107, ((AST)stream.LT(1)).getType());
|
||||
stream.consume(); // consume 107
|
||||
stream.consume(); // consume UP
|
||||
stream.consume(); // consume UP
|
||||
assertEquals(104, ((Tree)stream.LT(1)).getType());
|
||||
assertEquals(104, ((AST)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));
|
||||
BaseAST r0 = new CommonAST(new CommonToken(101));
|
||||
BaseAST r1 = new CommonAST(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(new CommonAST(new CommonToken(103)));
|
||||
BaseAST r2 = new CommonAST(new CommonToken(106));
|
||||
r2.addChild(new CommonAST(new CommonToken(107)));
|
||||
r1.addChild(r2);
|
||||
r0.addChild(new CommonTree(new CommonToken(104)));
|
||||
r0.addChild(new CommonTree(new CommonToken(105)));
|
||||
r0.addChild(new CommonAST(new CommonToken(104)));
|
||||
r0.addChild(new CommonAST(new CommonToken(105)));
|
||||
|
||||
TreeNodeStream stream = newStream(r0);
|
||||
String v = toNodesOnlyString(stream); // scan all
|
||||
|
@ -327,21 +332,21 @@ public class TestTreeNodeStream extends BaseTest {
|
|||
// ^(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];
|
||||
CommonAST[] nodes = new CommonAST[n];
|
||||
for (int i=0; i< n; i++) {
|
||||
nodes[i] = new CommonTree(new CommonToken((i+1)*10));
|
||||
nodes[i] = new CommonAST(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)));
|
||||
BaseAST g = nodes[0];
|
||||
BaseAST rules = nodes[1];
|
||||
BaseAST rule1 = nodes[2];
|
||||
BaseAST id = nodes[3];
|
||||
BaseAST block = nodes[4];
|
||||
BaseAST alt = nodes[5];
|
||||
BaseAST s = nodes[6];
|
||||
BaseAST rule2 = nodes[7];
|
||||
BaseAST id2 = nodes[8];
|
||||
g.addChild(new CommonAST(new CommonToken(100)));
|
||||
g.addChild(new CommonAST(new CommonToken(101)));
|
||||
g.addChild(rules);
|
||||
rules.addChild(rule1);
|
||||
rule1.addChild(id);
|
||||
|
@ -352,13 +357,13 @@ public class TestTreeNodeStream extends BaseTest {
|
|||
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 expecting = " 10"+DN+"100 101 20"+DN+"30"+DN+"40 50"+DN+"60"+DN+"70"+UP+""+UP+""+UP+" 80"+DN+"90"+UP+""+UP+""+UP+"";
|
||||
String found = toTokenTypeString(stream);
|
||||
assertEquals(expecting, found);
|
||||
}
|
||||
|
||||
public String toNodesOnlyString(TreeNodeStream nodes) {
|
||||
TreeAdaptor adaptor = nodes.getTreeAdaptor();
|
||||
ASTAdaptor adaptor = nodes.getTreeAdaptor();
|
||||
StringBuffer buf = new StringBuffer();
|
||||
Object o = nodes.LT(1);
|
||||
int type = adaptor.getType(o);
|
||||
|
|
|
@ -1,52 +1,47 @@
|
|||
/*
|
||||
* [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;
|
||||
[The "BSD license"]
|
||||
Copyright (c) 2011 Terence Parr
|
||||
All rights reserved.
|
||||
|
||||
import org.antlr.runtime.tree.CommonTree;
|
||||
import org.antlr.runtime.tree.CommonTreeAdaptor;
|
||||
import org.antlr.runtime.tree.TreeAdaptor;
|
||||
import org.antlr.runtime.tree.TreeWizard;
|
||||
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.v4.test;
|
||||
|
||||
import org.antlr.v4.runtime.tree.*;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
|
||||
|
||||
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();
|
||||
protected static final ASTAdaptor adaptor = new CommonASTAdaptor();
|
||||
|
||||
@Test public void testSingleNode() throws Exception {
|
||||
TreeWizard wiz = new TreeWizard(adaptor, tokens);
|
||||
CommonTree t = (CommonTree)wiz.create("ID");
|
||||
CommonAST t = (CommonAST)wiz.create("ID");
|
||||
String found = t.toStringTree();
|
||||
String expecting = "ID";
|
||||
assertEquals(expecting, found);
|
||||
|
@ -54,7 +49,7 @@ public class TestTreeWizard extends BaseTest {
|
|||
|
||||
@Test public void testSingleNodeWithArg() throws Exception {
|
||||
TreeWizard wiz = new TreeWizard(adaptor, tokens);
|
||||
CommonTree t = (CommonTree)wiz.create("ID[foo]");
|
||||
CommonAST t = (CommonAST)wiz.create("ID[foo]");
|
||||
String found = t.toStringTree();
|
||||
String expecting = "foo";
|
||||
assertEquals(expecting, found);
|
||||
|
@ -62,7 +57,7 @@ public class TestTreeWizard extends BaseTest {
|
|||
|
||||
@Test public void testSingleNodeTree() throws Exception {
|
||||
TreeWizard wiz = new TreeWizard(adaptor, tokens);
|
||||
CommonTree t = (CommonTree)wiz.create("(A)");
|
||||
CommonAST t = (CommonAST)wiz.create("(A)");
|
||||
String found = t.toStringTree();
|
||||
String expecting = "A";
|
||||
assertEquals(expecting, found);
|
||||
|
@ -70,7 +65,7 @@ public class TestTreeWizard extends BaseTest {
|
|||
|
||||
@Test public void testSingleLevelTree() throws Exception {
|
||||
TreeWizard wiz = new TreeWizard(adaptor, tokens);
|
||||
CommonTree t = (CommonTree)wiz.create("(A B C D)");
|
||||
CommonAST t = (CommonAST)wiz.create("(A B C D)");
|
||||
String found = t.toStringTree();
|
||||
String expecting = "(A B C D)";
|
||||
assertEquals(expecting, found);
|
||||
|
@ -78,7 +73,7 @@ public class TestTreeWizard extends BaseTest {
|
|||
|
||||
@Test public void testListTree() throws Exception {
|
||||
TreeWizard wiz = new TreeWizard(adaptor, tokens);
|
||||
CommonTree t = (CommonTree)wiz.create("(nil A B C)");
|
||||
CommonAST t = (CommonAST)wiz.create("(nil A B C)");
|
||||
String found = t.toStringTree();
|
||||
String expecting = "A B C";
|
||||
assertEquals(expecting, found);
|
||||
|
@ -86,13 +81,13 @@ public class TestTreeWizard extends BaseTest {
|
|||
|
||||
@Test public void testInvalidListTree() throws Exception {
|
||||
TreeWizard wiz = new TreeWizard(adaptor, tokens);
|
||||
CommonTree t = (CommonTree)wiz.create("A B C");
|
||||
CommonAST t = (CommonAST)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)");
|
||||
CommonAST t = (CommonAST)wiz.create("(A (B C) (B D) E)");
|
||||
String found = t.toStringTree();
|
||||
String expecting = "(A (B C) (B D) E)";
|
||||
assertEquals(expecting, found);
|
||||
|
@ -100,7 +95,7 @@ public class TestTreeWizard extends BaseTest {
|
|||
|
||||
@Test public void testSingleNodeIndex() throws Exception {
|
||||
TreeWizard wiz = new TreeWizard(adaptor, tokens);
|
||||
CommonTree t = (CommonTree)wiz.create("ID");
|
||||
CommonAST t = (CommonAST)wiz.create("ID");
|
||||
Map m = wiz.index(t);
|
||||
String found = m.toString();
|
||||
String expecting = "{10=[ID]}";
|
||||
|
@ -109,7 +104,7 @@ public class TestTreeWizard extends BaseTest {
|
|||
|
||||
@Test public void testNoRepeatsIndex() throws Exception {
|
||||
TreeWizard wiz = new TreeWizard(adaptor, tokens);
|
||||
CommonTree t = (CommonTree)wiz.create("(A B C D)");
|
||||
CommonAST t = (CommonAST)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]}";
|
||||
|
@ -118,7 +113,7 @@ public class TestTreeWizard extends BaseTest {
|
|||
|
||||
@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)");
|
||||
CommonAST t = (CommonAST)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]}";
|
||||
|
@ -127,7 +122,7 @@ public class TestTreeWizard extends BaseTest {
|
|||
|
||||
@Test public void testNoRepeatsVisit() throws Exception {
|
||||
TreeWizard wiz = new TreeWizard(adaptor, tokens);
|
||||
CommonTree t = (CommonTree)wiz.create("(A B C D)");
|
||||
CommonAST t = (CommonAST)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) {
|
||||
|
@ -141,7 +136,7 @@ public class TestTreeWizard extends BaseTest {
|
|||
|
||||
@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)");
|
||||
CommonAST t = (CommonAST)wiz.create("(A B (A C B) B D D)");
|
||||
final List elements = new ArrayList();
|
||||
wiz.visit(t, wiz.getTokenType("C"),
|
||||
new TreeWizard.Visitor() {
|
||||
|
@ -156,7 +151,7 @@ public class TestTreeWizard extends BaseTest {
|
|||
|
||||
@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)");
|
||||
CommonAST t = (CommonAST)wiz.create("(A B (A C B) B D D)");
|
||||
final List elements = new ArrayList();
|
||||
wiz.visit(t, wiz.getTokenType("B"),
|
||||
new TreeWizard.Visitor() {
|
||||
|
@ -171,7 +166,7 @@ public class TestTreeWizard extends BaseTest {
|
|||
|
||||
@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)");
|
||||
CommonAST t = (CommonAST)wiz.create("(A B (A C B) B D D)");
|
||||
final List elements = new ArrayList();
|
||||
wiz.visit(t, wiz.getTokenType("A"),
|
||||
new TreeWizard.Visitor() {
|
||||
|
@ -186,7 +181,7 @@ public class TestTreeWizard extends BaseTest {
|
|||
|
||||
@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)");
|
||||
CommonAST t = (CommonAST)wiz.create("(A B (A C B) B D D)");
|
||||
final List elements = new ArrayList();
|
||||
wiz.visit(t, wiz.getTokenType("B"),
|
||||
new TreeWizard.ContextVisitor() {
|
||||
|
@ -203,7 +198,7 @@ public class TestTreeWizard extends BaseTest {
|
|||
|
||||
@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)");
|
||||
CommonAST t = (CommonAST)wiz.create("(A B (A C B) B D D)");
|
||||
final List elements = new ArrayList();
|
||||
wiz.visit(t, wiz.getTokenType("A"),
|
||||
new TreeWizard.ContextVisitor() {
|
||||
|
@ -220,7 +215,7 @@ public class TestTreeWizard extends BaseTest {
|
|||
|
||||
@Test public void testVisitPattern() throws Exception {
|
||||
TreeWizard wiz = new TreeWizard(adaptor, tokens);
|
||||
CommonTree t = (CommonTree)wiz.create("(A B C (A B) D)");
|
||||
CommonAST t = (CommonAST)wiz.create("(A B C (A B) D)");
|
||||
final List elements = new ArrayList();
|
||||
wiz.visit(t, "(A B)",
|
||||
new TreeWizard.Visitor() {
|
||||
|
@ -235,7 +230,7 @@ public class TestTreeWizard extends BaseTest {
|
|||
|
||||
@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)))");
|
||||
CommonAST t = (CommonAST)wiz.create("(A B C (A B) (D (A B)))");
|
||||
final List elements = new ArrayList();
|
||||
wiz.visit(t, "(A B)",
|
||||
new TreeWizard.ContextVisitor() {
|
||||
|
@ -252,7 +247,7 @@ public class TestTreeWizard extends BaseTest {
|
|||
|
||||
@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])))");
|
||||
CommonAST t = (CommonAST)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() {
|
||||
|
@ -269,35 +264,35 @@ public class TestTreeWizard extends BaseTest {
|
|||
|
||||
@Test public void testParse() throws Exception {
|
||||
TreeWizard wiz = new TreeWizard(adaptor, tokens);
|
||||
CommonTree t = (CommonTree)wiz.create("(A B C)");
|
||||
CommonAST t = (CommonAST)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");
|
||||
CommonAST t = (CommonAST)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)");
|
||||
CommonAST t = (CommonAST)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)");
|
||||
CommonAST t = (CommonAST)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])");
|
||||
CommonAST t = (CommonAST)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)");
|
||||
|
@ -306,7 +301,7 @@ public class TestTreeWizard extends BaseTest {
|
|||
|
||||
@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])))");
|
||||
CommonAST t = (CommonAST)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)");
|
||||
|
@ -315,14 +310,14 @@ public class TestTreeWizard extends BaseTest {
|
|||
|
||||
@Test public void testParseWithTextFails() throws Exception {
|
||||
TreeWizard wiz = new TreeWizard(adaptor, tokens);
|
||||
CommonTree t = (CommonTree)wiz.create("(A B C)");
|
||||
CommonAST t = (CommonAST)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)");
|
||||
CommonAST t = (CommonAST)wiz.create("(A B C)");
|
||||
Map labels = new HashMap();
|
||||
boolean valid = wiz.parse(t, "(%a:A %b:B %c:C)", labels);
|
||||
assertTrue(valid);
|
||||
|
@ -333,7 +328,7 @@ public class TestTreeWizard extends BaseTest {
|
|||
|
||||
@Test public void testParseWithWildcardLabels() throws Exception {
|
||||
TreeWizard wiz = new TreeWizard(adaptor, tokens);
|
||||
CommonTree t = (CommonTree)wiz.create("(A B C)");
|
||||
CommonAST t = (CommonAST)wiz.create("(A B C)");
|
||||
Map labels = new HashMap();
|
||||
boolean valid = wiz.parse(t, "(A %b:. %c:.)", labels);
|
||||
assertTrue(valid);
|
||||
|
@ -343,7 +338,7 @@ public class TestTreeWizard extends BaseTest {
|
|||
|
||||
@Test public void testParseLabelsAndTestText() throws Exception {
|
||||
TreeWizard wiz = new TreeWizard(adaptor, tokens);
|
||||
CommonTree t = (CommonTree)wiz.create("(A B[foo] C)");
|
||||
CommonAST t = (CommonAST)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);
|
||||
|
@ -354,7 +349,7 @@ public class TestTreeWizard extends BaseTest {
|
|||
|
||||
@Test public void testParseLabelsInNestedTree() throws Exception {
|
||||
TreeWizard wiz = new TreeWizard(adaptor, tokens);
|
||||
CommonTree t = (CommonTree)wiz.create("(A (B C) (D E))");
|
||||
CommonAST t = (CommonAST)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);
|
||||
|
@ -367,36 +362,36 @@ public class TestTreeWizard extends BaseTest {
|
|||
|
||||
@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)");
|
||||
CommonAST t1 = (CommonAST)wiz.create("(A B C)");
|
||||
CommonAST t2 = (CommonAST)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)");
|
||||
CommonAST t1 = (CommonAST)wiz.create("(A B[foo] C)");
|
||||
CommonAST t2 = (CommonAST)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)");
|
||||
CommonAST t1 = (CommonAST)wiz.create("(A B[foo] C)");
|
||||
CommonAST t2 = (CommonAST)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])))");
|
||||
CommonAST t = (CommonAST)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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,45 +1,42 @@
|
|||
/*
|
||||
* [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;
|
||||
[The "BSD license"]
|
||||
Copyright (c) 2011 Terence Parr
|
||||
All rights reserved.
|
||||
|
||||
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;
|
||||
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.v4.test;
|
||||
|
||||
import org.antlr.v4.runtime.*;
|
||||
import org.antlr.v4.runtime.tree.*;
|
||||
import org.junit.Test;
|
||||
|
||||
public class TestTrees extends BaseTest {
|
||||
TreeAdaptor adaptor = new CommonTreeAdaptor();
|
||||
ASTAdaptor adaptor = new CommonASTAdaptor();
|
||||
protected boolean debug = false;
|
||||
|
||||
static class V extends CommonTree {
|
||||
static class V extends CommonAST {
|
||||
public int x;
|
||||
public V(Token t) { this.token = t;}
|
||||
public V(int ttype, int x) { this.x=x; token=new CommonToken(ttype); }
|
||||
|
@ -48,15 +45,15 @@ public class TestTrees extends BaseTest {
|
|||
}
|
||||
|
||||
@Test public void testSingleNode() throws Exception {
|
||||
CommonTree t = new CommonTree(new CommonToken(101));
|
||||
CommonAST t = new CommonAST(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"));
|
||||
CommonAST root_0 = (CommonAST)adaptor.nil();
|
||||
CommonAST t = new V(101, 2);
|
||||
CommonAST u = new V(new CommonToken(102,"102"));
|
||||
adaptor.addChild(root_0, t);
|
||||
adaptor.addChild(root_0, u);
|
||||
assertNull(root_0.parent);
|
||||
|
@ -67,10 +64,10 @@ public class TestTrees extends BaseTest {
|
|||
|
||||
@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)));
|
||||
CommonAST r0 = new CommonAST(new CommonToken(101));
|
||||
r0.addChild(new CommonAST(new CommonToken(102)));
|
||||
r0.getChild(0).addChild(new CommonAST(new CommonToken(103)));
|
||||
r0.addChild(new CommonAST(new CommonToken(104)));
|
||||
|
||||
assertNull(r0.parent);
|
||||
assertEquals(-1, r0.childIndex);
|
||||
|
@ -78,18 +75,18 @@ public class TestTrees extends BaseTest {
|
|||
|
||||
@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)));
|
||||
CommonAST r0 = new CommonAST((Token)null);
|
||||
CommonAST c0, c1, c2;
|
||||
r0.addChild(c0=new CommonAST(new CommonToken(101)));
|
||||
r0.addChild(c1=new CommonAST(new CommonToken(102)));
|
||||
r0.addChild(c2=new CommonAST(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(1, c1.childIndex);
|
||||
assertEquals(r0, c2.parent);
|
||||
assertEquals(2, c2.childIndex);
|
||||
}
|
||||
|
@ -97,14 +94,14 @@ public class TestTrees extends BaseTest {
|
|||
@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));
|
||||
CommonAST root = new CommonAST(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)));
|
||||
CommonAST r0 = new CommonAST((Token)null);
|
||||
CommonAST c0, c1, c2;
|
||||
r0.addChild(c0=new CommonAST(new CommonToken(101)));
|
||||
r0.addChild(c1=new CommonAST(new CommonToken(102)));
|
||||
r0.addChild(c2=new CommonAST(new CommonToken(103)));
|
||||
|
||||
root.addChild(r0);
|
||||
|
||||
|
@ -122,15 +119,15 @@ public class TestTrees extends BaseTest {
|
|||
@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)));
|
||||
CommonAST root = new CommonAST(new CommonToken(5));
|
||||
root.addChild(new CommonAST(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)));
|
||||
CommonAST r0 = new CommonAST((Token)null);
|
||||
CommonAST c0, c1, c2;
|
||||
r0.addChild(c0=new CommonAST(new CommonToken(101)));
|
||||
r0.addChild(c1=new CommonAST(new CommonToken(102)));
|
||||
r0.addChild(c2=new CommonAST(new CommonToken(103)));
|
||||
|
||||
root.addChild(r0);
|
||||
|
||||
|
@ -147,100 +144,100 @@ public class TestTrees extends BaseTest {
|
|||
|
||||
@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));
|
||||
CommonAST r0 = new CommonAST(new CommonToken(101));
|
||||
CommonAST r1 = new CommonAST(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(new CommonAST(new CommonToken(103)));
|
||||
BaseAST r2 = new CommonAST(new CommonToken(106));
|
||||
r2.addChild(new CommonAST(new CommonToken(107)));
|
||||
r1.addChild(r2);
|
||||
r0.addChild(new CommonTree(new CommonToken(104)));
|
||||
r0.addChild(new CommonTree(new CommonToken(105)));
|
||||
r0.addChild(new CommonAST(new CommonToken(104)));
|
||||
r0.addChild(new CommonAST(new CommonToken(105)));
|
||||
|
||||
CommonTree dup = (CommonTree)(new CommonTreeAdaptor()).dupTree(r0);
|
||||
CommonAST dup = (CommonAST)(new CommonASTAdaptor()).dupTree(r0);
|
||||
|
||||
assertNull(dup.parent);
|
||||
assertEquals(-1, dup.childIndex);
|
||||
dup.sanityCheckParentAndChildIndexes();
|
||||
Trees.sanityCheckParentAndChildIndexes(dup);
|
||||
}
|
||||
|
||||
@Test public void testBecomeRoot() throws Exception {
|
||||
// 5 becomes new root of ^(nil 101 102 103)
|
||||
CommonTree newRoot = new CommonTree(new CommonToken(5));
|
||||
CommonAST newRoot = new CommonAST(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)));
|
||||
CommonAST oldRoot = new CommonAST((Token)null);
|
||||
oldRoot.addChild(new CommonAST(new CommonToken(101)));
|
||||
oldRoot.addChild(new CommonAST(new CommonToken(102)));
|
||||
oldRoot.addChild(new CommonAST(new CommonToken(103)));
|
||||
|
||||
TreeAdaptor adaptor = new CommonTreeAdaptor();
|
||||
ASTAdaptor adaptor = new CommonASTAdaptor();
|
||||
adaptor.becomeRoot(newRoot, oldRoot);
|
||||
newRoot.sanityCheckParentAndChildIndexes();
|
||||
Trees.sanityCheckParentAndChildIndexes(newRoot);
|
||||
}
|
||||
|
||||
@Test public void testBecomeRoot2() throws Exception {
|
||||
// 5 becomes new root of ^(101 102 103)
|
||||
CommonTree newRoot = new CommonTree(new CommonToken(5));
|
||||
CommonAST newRoot = new CommonAST(new CommonToken(5));
|
||||
|
||||
CommonTree oldRoot = new CommonTree(new CommonToken(101));
|
||||
oldRoot.addChild(new CommonTree(new CommonToken(102)));
|
||||
oldRoot.addChild(new CommonTree(new CommonToken(103)));
|
||||
CommonAST oldRoot = new CommonAST(new CommonToken(101));
|
||||
oldRoot.addChild(new CommonAST(new CommonToken(102)));
|
||||
oldRoot.addChild(new CommonAST(new CommonToken(103)));
|
||||
|
||||
TreeAdaptor adaptor = new CommonTreeAdaptor();
|
||||
ASTAdaptor adaptor = new CommonASTAdaptor();
|
||||
adaptor.becomeRoot(newRoot, oldRoot);
|
||||
newRoot.sanityCheckParentAndChildIndexes();
|
||||
Trees.sanityCheckParentAndChildIndexes(newRoot);
|
||||
}
|
||||
|
||||
@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)));
|
||||
CommonAST newRoot = new CommonAST((Token)null);
|
||||
newRoot.addChild(new CommonAST(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)));
|
||||
CommonAST oldRoot = new CommonAST((Token)null);
|
||||
oldRoot.addChild(new CommonAST(new CommonToken(101)));
|
||||
oldRoot.addChild(new CommonAST(new CommonToken(102)));
|
||||
oldRoot.addChild(new CommonAST(new CommonToken(103)));
|
||||
|
||||
TreeAdaptor adaptor = new CommonTreeAdaptor();
|
||||
ASTAdaptor adaptor = new CommonASTAdaptor();
|
||||
adaptor.becomeRoot(newRoot, oldRoot);
|
||||
newRoot.sanityCheckParentAndChildIndexes();
|
||||
Trees.sanityCheckParentAndChildIndexes(newRoot);
|
||||
}
|
||||
|
||||
@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)));
|
||||
CommonAST newRoot = new CommonAST((Token)null);
|
||||
newRoot.addChild(new CommonAST(new CommonToken(5)));
|
||||
|
||||
CommonTree oldRoot = new CommonTree(new CommonToken(101));
|
||||
oldRoot.addChild(new CommonTree(new CommonToken(102)));
|
||||
oldRoot.addChild(new CommonTree(new CommonToken(103)));
|
||||
CommonAST oldRoot = new CommonAST(new CommonToken(101));
|
||||
oldRoot.addChild(new CommonAST(new CommonToken(102)));
|
||||
oldRoot.addChild(new CommonAST(new CommonToken(103)));
|
||||
|
||||
TreeAdaptor adaptor = new CommonTreeAdaptor();
|
||||
ASTAdaptor adaptor = new CommonASTAdaptor();
|
||||
adaptor.becomeRoot(newRoot, oldRoot);
|
||||
newRoot.sanityCheckParentAndChildIndexes();
|
||||
Trees.sanityCheckParentAndChildIndexes(newRoot);
|
||||
}
|
||||
|
||||
@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);
|
||||
CommonAST root_0 = (CommonAST)adaptor.nil();
|
||||
CommonAST root_1 = (CommonAST)adaptor.nil();
|
||||
root_1 = (CommonAST)adaptor.becomeRoot(new CommonAST(new CommonToken(5)), root_1);
|
||||
|
||||
adaptor.addChild(root_1, new CommonTree(new CommonToken(6)));
|
||||
adaptor.addChild(root_1, new CommonAST(new CommonToken(6)));
|
||||
|
||||
adaptor.addChild(root_0, root_1);
|
||||
|
||||
root_0.sanityCheckParentAndChildIndexes();
|
||||
Trees.sanityCheckParentAndChildIndexes(root_0);
|
||||
}
|
||||
|
||||
// Test replaceChildren
|
||||
|
||||
@Test public void testReplaceWithNoChildren() throws Exception {
|
||||
CommonTree t = new CommonTree(new CommonToken(101));
|
||||
CommonTree newChild = new CommonTree(new CommonToken(5));
|
||||
CommonAST t = new CommonAST(new CommonToken(101));
|
||||
CommonAST newChild = new CommonAST(new CommonToken(5));
|
||||
boolean error = false;
|
||||
try {
|
||||
t.replaceChildren(0, 0, newChild);
|
||||
Trees.replaceChildren(t, 0, 0, newChild);
|
||||
}
|
||||
catch (IllegalArgumentException iae) {
|
||||
error = true;
|
||||
|
@ -250,159 +247,159 @@ public class TestTrees extends BaseTest {
|
|||
|
||||
@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"));
|
||||
CommonAST t = new CommonAST(new CommonToken(99,"a"));
|
||||
CommonAST c0 = new CommonAST(new CommonToken(99, "b"));
|
||||
t.addChild(c0);
|
||||
|
||||
CommonTree newChild = new CommonTree(new CommonToken(99, "c"));
|
||||
t.replaceChildren(0, 0, newChild);
|
||||
CommonAST newChild = new CommonAST(new CommonToken(99, "c"));
|
||||
Trees.replaceChildren(t, 0, 0, newChild);
|
||||
String expecting = "(a c)";
|
||||
assertEquals(expecting, t.toStringTree());
|
||||
t.sanityCheckParentAndChildIndexes();
|
||||
Trees.sanityCheckParentAndChildIndexes(t);
|
||||
}
|
||||
|
||||
@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")));
|
||||
CommonAST t = new CommonAST(new CommonToken(99, "a"));
|
||||
t.addChild(new CommonAST(new CommonToken(99, "b")));
|
||||
t.addChild(new CommonAST(new CommonToken(99, "c"))); // index 1
|
||||
t.addChild(new CommonAST(new CommonToken(99, "d")));
|
||||
|
||||
CommonTree newChild = new CommonTree(new CommonToken(99,"x"));
|
||||
t.replaceChildren(1, 1, newChild);
|
||||
CommonAST newChild = new CommonAST(new CommonToken(99,"x"));
|
||||
Trees.replaceChildren(t, 1, 1, newChild);
|
||||
String expecting = "(a b x d)";
|
||||
assertEquals(expecting, t.toStringTree());
|
||||
t.sanityCheckParentAndChildIndexes();
|
||||
Trees.sanityCheckParentAndChildIndexes(t);
|
||||
}
|
||||
|
||||
@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")));
|
||||
CommonAST t = new CommonAST(new CommonToken(99, "a"));
|
||||
t.addChild(new CommonAST(new CommonToken(99, "b"))); // index 0
|
||||
t.addChild(new CommonAST(new CommonToken(99, "c")));
|
||||
t.addChild(new CommonAST(new CommonToken(99, "d")));
|
||||
|
||||
CommonTree newChild = new CommonTree(new CommonToken(99,"x"));
|
||||
t.replaceChildren(0, 0, newChild);
|
||||
CommonAST newChild = new CommonAST(new CommonToken(99,"x"));
|
||||
Trees.replaceChildren(t, 0, 0, newChild);
|
||||
String expecting = "(a x c d)";
|
||||
assertEquals(expecting, t.toStringTree());
|
||||
t.sanityCheckParentAndChildIndexes();
|
||||
Trees.sanityCheckParentAndChildIndexes(t);
|
||||
}
|
||||
|
||||
@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
|
||||
CommonAST t = new CommonAST(new CommonToken(99, "a"));
|
||||
t.addChild(new CommonAST(new CommonToken(99, "b")));
|
||||
t.addChild(new CommonAST(new CommonToken(99, "c")));
|
||||
t.addChild(new CommonAST(new CommonToken(99, "d"))); // index 2
|
||||
|
||||
CommonTree newChild = new CommonTree(new CommonToken(99,"x"));
|
||||
t.replaceChildren(2, 2, newChild);
|
||||
CommonAST newChild = new CommonAST(new CommonToken(99,"x"));
|
||||
Trees.replaceChildren(t, 2, 2, newChild);
|
||||
String expecting = "(a b c x)";
|
||||
assertEquals(expecting, t.toStringTree());
|
||||
t.sanityCheckParentAndChildIndexes();
|
||||
Trees.sanityCheckParentAndChildIndexes(t);
|
||||
}
|
||||
|
||||
@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")));
|
||||
CommonAST t = new CommonAST(new CommonToken(99, "a"));
|
||||
t.addChild(new CommonAST(new CommonToken(99, "b")));
|
||||
t.addChild(new CommonAST(new CommonToken(99, "c")));
|
||||
t.addChild(new CommonAST(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")));
|
||||
CommonAST newChildren = (CommonAST)adaptor.nil();
|
||||
newChildren.addChild(new CommonAST(new CommonToken(99,"x")));
|
||||
newChildren.addChild(new CommonAST(new CommonToken(99,"y")));
|
||||
|
||||
t.replaceChildren(0, 0, newChildren);
|
||||
Trees.replaceChildren(t, 0, 0, newChildren);
|
||||
String expecting = "(a x y c d)";
|
||||
assertEquals(expecting, t.toStringTree());
|
||||
t.sanityCheckParentAndChildIndexes();
|
||||
Trees.sanityCheckParentAndChildIndexes(t);
|
||||
}
|
||||
|
||||
@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")));
|
||||
CommonAST t = new CommonAST(new CommonToken(99, "a"));
|
||||
t.addChild(new CommonAST(new CommonToken(99, "b")));
|
||||
t.addChild(new CommonAST(new CommonToken(99, "c")));
|
||||
t.addChild(new CommonAST(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")));
|
||||
CommonAST newChildren = (CommonAST)adaptor.nil();
|
||||
newChildren.addChild(new CommonAST(new CommonToken(99,"x")));
|
||||
newChildren.addChild(new CommonAST(new CommonToken(99,"y")));
|
||||
|
||||
t.replaceChildren(2, 2, newChildren);
|
||||
Trees.replaceChildren(t, 2, 2, newChildren);
|
||||
String expecting = "(a b c x y)";
|
||||
assertEquals(expecting, t.toStringTree());
|
||||
t.sanityCheckParentAndChildIndexes();
|
||||
Trees.sanityCheckParentAndChildIndexes(t);
|
||||
}
|
||||
|
||||
@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")));
|
||||
CommonAST t = new CommonAST(new CommonToken(99, "a"));
|
||||
t.addChild(new CommonAST(new CommonToken(99, "b")));
|
||||
t.addChild(new CommonAST(new CommonToken(99, "c")));
|
||||
t.addChild(new CommonAST(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")));
|
||||
CommonAST newChildren = (CommonAST)adaptor.nil();
|
||||
newChildren.addChild(new CommonAST(new CommonToken(99,"x")));
|
||||
newChildren.addChild(new CommonAST(new CommonToken(99,"y")));
|
||||
|
||||
t.replaceChildren(1, 1, newChildren);
|
||||
Trees.replaceChildren(t, 1, 1, newChildren);
|
||||
String expecting = "(a b x y d)";
|
||||
assertEquals(expecting, t.toStringTree());
|
||||
t.sanityCheckParentAndChildIndexes();
|
||||
Trees.sanityCheckParentAndChildIndexes(t);
|
||||
}
|
||||
|
||||
@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")));
|
||||
CommonAST t = new CommonAST(new CommonToken(99, "a"));
|
||||
t.addChild(new CommonAST(new CommonToken(99, "b")));
|
||||
t.addChild(new CommonAST(new CommonToken(99, "c")));
|
||||
t.addChild(new CommonAST(new CommonToken(99, "d")));
|
||||
|
||||
CommonTree newChild = new CommonTree(new CommonToken(99,"x"));
|
||||
CommonAST newChild = new CommonAST(new CommonToken(99,"x"));
|
||||
|
||||
t.replaceChildren(0, 1, newChild);
|
||||
Trees.replaceChildren(t, 0, 1, newChild);
|
||||
String expecting = "(a x d)";
|
||||
assertEquals(expecting, t.toStringTree());
|
||||
t.sanityCheckParentAndChildIndexes();
|
||||
Trees.sanityCheckParentAndChildIndexes(t);
|
||||
}
|
||||
|
||||
@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")));
|
||||
CommonAST t = new CommonAST(new CommonToken(99, "a"));
|
||||
t.addChild(new CommonAST(new CommonToken(99, "b")));
|
||||
t.addChild(new CommonAST(new CommonToken(99, "c")));
|
||||
t.addChild(new CommonAST(new CommonToken(99, "d")));
|
||||
|
||||
CommonTree newChild = new CommonTree(new CommonToken(99,"x"));
|
||||
CommonAST newChild = new CommonAST(new CommonToken(99,"x"));
|
||||
|
||||
t.replaceChildren(1, 2, newChild);
|
||||
Trees.replaceChildren(t, 1, 2, newChild);
|
||||
String expecting = "(a b x)";
|
||||
assertEquals(expecting, t.toStringTree());
|
||||
t.sanityCheckParentAndChildIndexes();
|
||||
Trees.sanityCheckParentAndChildIndexes(t);
|
||||
}
|
||||
|
||||
@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")));
|
||||
CommonAST t = new CommonAST(new CommonToken(99, "a"));
|
||||
t.addChild(new CommonAST(new CommonToken(99, "b")));
|
||||
t.addChild(new CommonAST(new CommonToken(99, "c")));
|
||||
t.addChild(new CommonAST(new CommonToken(99, "d")));
|
||||
|
||||
CommonTree newChild = new CommonTree(new CommonToken(99,"x"));
|
||||
CommonAST newChild = new CommonAST(new CommonToken(99,"x"));
|
||||
|
||||
t.replaceChildren(0, 2, newChild);
|
||||
Trees.replaceChildren(t, 0, 2, newChild);
|
||||
String expecting = "(a x)";
|
||||
assertEquals(expecting, t.toStringTree());
|
||||
t.sanityCheckParentAndChildIndexes();
|
||||
Trees.sanityCheckParentAndChildIndexes(t);
|
||||
}
|
||||
|
||||
@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")));
|
||||
CommonAST t = new CommonAST(new CommonToken(99, "a"));
|
||||
t.addChild(new CommonAST(new CommonToken(99, "b")));
|
||||
t.addChild(new CommonAST(new CommonToken(99, "c")));
|
||||
t.addChild(new CommonAST(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")));
|
||||
CommonAST newChildren = (CommonAST)adaptor.nil();
|
||||
newChildren.addChild(new CommonAST(new CommonToken(99,"x")));
|
||||
newChildren.addChild(new CommonAST(new CommonToken(99,"y")));
|
||||
|
||||
t.replaceChildren(0, 2, newChildren);
|
||||
Trees.replaceChildren(t, 0, 2, newChildren);
|
||||
String expecting = "(a x y)";
|
||||
assertEquals(expecting, t.toStringTree());
|
||||
t.sanityCheckParentAndChildIndexes();
|
||||
Trees.sanityCheckParentAndChildIndexes(t);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue