📄 tnode.java
字号:
/*
Copyright (c) 1998-2000, Non, Inc.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright
notice, this list of conditions, and the following disclaimer.
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.
All advertising materials mentioning features or use of this
software must display the following acknowledgement:
This product includes software developed by Non, Inc. and
its contributors.
Neither name of the company nor the names of its contributors
may be used to endorse or promote products derived from this
software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``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 COMPANY OR CONTRIBUTORS 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 isis.anp.common;
import java.lang.reflect.Field;
import java.util.Enumeration;
import java.util.Hashtable;
import antlr.CommonASTWithHiddenTokens;
import antlr.Token;
import antlr.collections.AST;
//import CToken;
/**
* Class TNode is an implementation of the AST interface and adds many useful
* features:
*
* It is double-linked for reverse searching. (this is currently incomplete, in
* that method doubleLink() must be called after any changes to the tree to
* maintain the reverse links).
*
* It can store a definition node (defNode), so that nodes such as scoped names
* can refer to the node that defines the name.
*
* It stores line numbers for nodes.
*
* Searches for parents and children of a tree can be done based on their type.
*
* The tree can be printed to System.out using a lisp-style syntax.
*
*
*
*/
public class TNode extends CommonASTWithHiddenTokens {
private static final long serialVersionUID = 1L;
protected int ttype;
protected String text;
protected int lineNum = 0;
protected TNode defNode;
protected TNode up;
protected TNode left;
protected boolean marker = false;
protected Hashtable attributes = null;
static String tokenVocabulary;
private int columnNum = 0;
private int tokenLength = 0;
/**
* Set the token vocabulary to a tokentypes class generated by antlr.
*/
public static void setTokenVocabulary(String s) {
tokenVocabulary = s;
}
public void initialize(Token token) {
CToken tok = (CToken) token;
setText(tok.getText());
setType(tok.getType());
setLineNum(tok.getLine());
setColumnNum(tok.getColumn());
setTokenLength(tok.getLength());
setAttribute("source", tok.getSource());
setAttribute("tokenNumber", new Integer(tok.getTokenNumber()));
hiddenBefore = tok.getHiddenBefore();
hiddenAfter = tok.getHiddenAfter();
}
public void initialize(AST tr) {
TNode t = (TNode) tr;
setText(t.getText());
setType(t.getType());
setLineNum(t.getLineNum());
setColumnNum(t.getColumnNum());
setTokenLength(t.getTokenLength());
setDefNode(t.getDefNode());
this.attributes = t.getAttributesTable();
hiddenBefore = t.getHiddenBefore();
hiddenAfter = t.getHiddenAfter();
}
/** Get the token type for this node */
public int getType() {
return ttype;
}
/** Set the token type for this node */
public void setType(int ttype_) {
ttype = ttype_;
}
/**
* Get the marker value for this node. This member is a general-use marker.
*/
public boolean getMarker() {
return marker;
}
/**
* Set the marker value for this node. This property is a general-use
* boolean marker.
*/
public void setMarker(boolean marker_) {
marker = marker_;
}
/**
* get the hashtable that holds attribute values.
*/
public Hashtable getAttributesTable() {
if (attributes == null)
attributes = new Hashtable(7);
return attributes;
}
/**
* set an attribute in the attribute table.
*/
public void setAttribute(String attrName, Object value) {
if (attributes == null)
attributes = new Hashtable(7);
attributes.put(attrName, value);
}
/**
* lookup the attribute name in the attribute table. If the value does not
* exist, it returns null.
*/
public Object getAttribute(String attrName) {
if (attributes == null)
return null;
else
return attributes.get(attrName);
}
/**
* Get the line number for this node. If the line number is 0, search for a
* non-zero line num among children
*/
public int getLineNum() {
if (lineNum != 0)
return lineNum;
else if (down == null)
return lineNum;
else
return ((TNode) down).getLocalLineNum();
}
/**
* Get the column number for this node. If the line number is 0, search for
* a non-zero line num among children
*/
public int getColumnNum() {
if (columnNum != 0) {
return columnNum;
} else if (down == null)
return columnNum;
else
return ((TNode) down).getLocalColumnNum();
}
/**
* Get the length of the token belonging to this node. If the length is 0,
* search for a non-zero token length among children
*/
public int getTokenLength() {
if (tokenLength != 0) {
return tokenLength;
} else if (down == null)
return tokenLength;
else
return ((TNode) down).getLocalTokenLength();
}
public int getLocalLineNum() {
if (lineNum != 0)
return lineNum;
else if (down == null)
if (right == null)
return lineNum;
else
return ((TNode) right).getLocalLineNum();
else
return ((TNode) down).getLocalLineNum();
}
public int getLocalColumnNum() {
if (columnNum != 0)
return columnNum;
else if (down == null)
if (right == null)
return columnNum;
else
return ((TNode) right).getLocalColumnNum();
else
return ((TNode) down).getLocalColumnNum();
}
public int getLocalTokenLength() {
if (tokenLength != 0)
return tokenLength;
else if (down == null)
if (right == null)
return tokenLength;
else
return ((TNode) right).getLocalTokenLength();
else
return ((TNode) down).getLocalTokenLength();
}
public String getFileName() {
String fileName = (String)getAttribute("source");
if (fileName != null)
return fileName;
else if (down == null)
if (right == null)
return fileName;
else
return ((TNode) right).getFileName();
else
return ((TNode) down).getFileName();
}
/**
* Returns the next node that is created from a token.
* @return the node if such node exists, null otherwise
*/
public TNode getNextRealNode() {
if (this.getText()!=null && !this.getText().equals(""))
return this;
else if (down == null)
if (right == null)
return null;
else
return ((TNode) right).getNextRealNode();
else
return ((TNode) down).getNextRealNode();
}
/** Set the line number for this node */
public void setLineNum(int lineNum_) {
lineNum = lineNum_;
}
/** Set the columnt number for this node */
public void setColumnNum(int colNum_) {
columnNum = colNum_;
}
/** Set the token lenth for this node */
public void setTokenLength(int l_) {
tokenLength = l_;
}
/** Get the token text for this node */
public String getText() {
return text;
}
/** Set the token text for this node */
public void setText(String text_) {
text = text_;
}
/** return the last child of this node, or null if there is none */
public TNode getLastChild() {
TNode down = (TNode) getFirstChild();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -