...
No Format |
---|
public static final String[] tokenNames = new String[] { "<invalid>", "<EOR>", "<DOWN>", "<UP>", "A", "B", "C" }; |
in progress in another editor... Once you create a TreeWizard, you can keep calling create as it will know what kind of nodes to make, how to hook them up, and what the token types are for the token names. The wizard really only has write-once data in it...just a convenience so you don't have to keep passing stuff to each of the methods.
Tree Equality
If you want to compare to trees, you'd have to write your own method to do so right now. The equals() method in Java is not necessarily the right way to implement either because that is probably just matching to nodes not trees. The TreeWizard provides a method to check the equality two trees (one static and one instance method):
No Format |
---|
/** 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)).
public static boolean equals(Object t1, Object t2, TreeAdaptor adaptor);
/** Compare type, structure, and text of two trees, assuming adaptor
* in this instance of a TreeWizard.
*/
public boolean equals(Object t1, Object t2);
|
Indexing trees
Sometimes you want a list of all nodes with a certain type like VARDECL or METHOD:
No Format |
---|
List nodes = wiz.find(t, wiz.getTokenType("VARDECL"));
|
Sometimes you want all lists for each token type:
No Format |
---|
// from the unit tests
CommonTree t = (CommonTree)wiz.create("(A B (A C B) B D D)");
Map m = wiz.index(t);
String found = m.toString();
String expecting = "{8=[D, D], 6=[B, B, B], 7=[C], 5=[A, A]}";
|