org.antlr.runtime.tree.TreeWizard Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of antlr-complete Show documentation
Show all versions of antlr-complete Show documentation
Complete distribution for ANTLR 3
/*
[The "BSD license"]
Copyright (c) 2005-2009 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.runtime.tree;
import org.antlr.runtime.Token;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/** 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 TreeAdaptor.
*
* 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 TreeAdaptor 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 TreeAdaptor 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 {
@Override
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 CommonTree {
public String label;
public boolean hasTextArg;
public TreePattern(Token payload) {
super(payload);
}
@Override
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 TreePatternTreeAdaptor extends CommonTreeAdaptor {
@Override
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(TreeAdaptor adaptor) {
this.adaptor = adaptor;
}
public TreeWizard(TreeAdaptor adaptor, Map tokenNameToTypeMap) {
this.adaptor = adaptor;
this.tokenNameToTypeMap = tokenNameToTypeMap;
}
public TreeWizard(TreeAdaptor adaptor, String[] tokenNames) {
this.adaptor = adaptor;
this.tokenNameToTypeMap = computeTokenTypes(tokenNames);
}
public TreeWizard(String[] tokenNames) {
this(new CommonTreeAdaptor(), tokenNames);
}
/** Compute a Map 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, 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_TOKEN_TYPE;
}
Integer ttypeI = tokenNameToTypeMap.get(tokenName);
if ( ttypeI!=null ) {
return ttypeI;
}
return Token.INVALID_TOKEN_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 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
© 2015 - 2025 Weber Informatics LLC | Privacy Policy