📄 iclast.java
字号:
package com.sri.oaa2.icl;
import java.util.ArrayList;
import java.util.Iterator;
import antlr_oaa.collections.AST;
import antlr_oaa.collections.ASTEnumeration;
import antlr_oaa.Token;
public abstract class IclAST implements AST
{
/// IclTerm that this AST corresponds to
protected IclTerm term;
/// Children of this node
protected ArrayList children = new ArrayList();
/// Siblings of this node--used only when we don't have a parent
protected ArrayList siblings = null;
/// sibling number of this node--used only by the parser
protected int astSibNum = 0;
/// parent of this node--used only by parser
protected IclAST astParent = null;
/// oldest sibling of this node--used only by parser
protected IclAST oldestSib = null;
/// token text
protected String astText;
/// token type
protected int astType = 0;
public IclTerm getIclTerm()
{
return this.term;
}
ArrayList getChildren()
{
return this.children;
}
public int getType()
{
return this.astType;
}
public void setType(int t)
{
this.astType = t;
}
void setIclTerm(IclTerm t)
{
this.term = t;
}
/// Begin AST implementation
/**
* add a child to this IclAST
*/
void add(IclAST c)
{
this.getChildren().add(c);
this.getIclTerm().add(c.getIclTerm());
}
/**
* Set the list of children of this IclAST
*/
void setChildren(ArrayList c)
{
this.children = c;
ArrayList termChildren = new ArrayList();
for(Iterator it = c.iterator(); it.hasNext(); ) {
IclAST ia = (IclAST)it.next();
termChildren.add(ia.getIclTerm());
}
this.getIclTerm().setChildren(termChildren);
}
/**
* Set the first child of a node.
*/
public final void setFirstChild(AST child)
{
IclAST c = (IclAST)child;
c.setParent(this);
c.setSibNum(0);
// Check if the given node has any record of its siblings,
// and use them as the children if it does.
ArrayList sibs = c.getSiblings();
if(sibs != null) {
this.setChildren(sibs);
return;
}
// Otherwise, we can create a new siblings ArrayList and
// use it for the children
sibs = new ArrayList(2);
sibs.add(c);
setChildren(sibs);
}
/**
* Set the next sibling after this one.
*/
public final void setNextSibling(AST n)
{
IclAST next = (IclAST)n;
// If we don't know who our parent is, then we
// need to get the oldest sibling and add to its
// siblings list. If we don't know who the oldest
// sibling is, then we assume that we are the oldest
// and set the first two siblings as ourselves and
// the next one.
int nextSibNum = this.getSibNum() + 1;
IclAST parent = this.getParent();
if(parent == null) {
IclAST oldest = getOldestSib();
if(oldest == null) {
setSiblings(new ArrayList(4));
getSiblings().add(this);
getSiblings().add(next);
next.setSibNum(nextSibNum);
setOldestSib(this);
next.setOldestSib(this);
}
else {
oldest.getSiblings().add(next);
next.setSibNum(nextSibNum);
next.setOldestSib(oldest);
}
return;
}
// Here, we know who our parent is, so we can just
// add to the parent's list of children.
int parentSize = parent.getChildren().size();
next.setParent(parent);
next.setSibNum(nextSibNum);
if(parentSize == nextSibNum) {
parent.add(next);
}
else if(parentSize > nextSibNum) {
for(int i = nextSibNum; i < parent.getChildren().size(); ++i) {
((IclAST)parent.getChildren().get(i)).setSibNum(i + 1);
}
parent.getChildren().add(next);
}
else {
throw new RuntimeException("setNextSibling() parentSize < nextSibNum");
}
}
/**
* Add child to this node.
*/
public final void addChild(AST child)
{
IclAST c =(IclAST)child;
if(c == null) {
return;
}
c.setParent(this);
c.setSibNum(this.getChildren().size());
this.add(c);
}
/**
* Set parent for AST
*/
protected final void setParent(IclAST t)
{
astParent = t;
}
/**
* Get parent for AST or null if unknown
*/
protected final IclAST getParent()
{
return astParent;
}
/**
* Get oldest sibling for AST, or null if unknown
*/
protected final IclAST getOldestSib()
{
return oldestSib;
}
/**
* Set the oldest sibling
*/
protected final void setOldestSib(IclAST t)
{
oldestSib = t;
}
/**
* For the oldest sibling, this is used to keep track of the other siblings.
*/
protected final void setSiblings(ArrayList l)
{
siblings = l;
}
/**
* Get the list of siblings. This is valid only for the oldest sibling.
*/
protected final ArrayList getSiblings()
{
return siblings;
}
/**
* Set sibling number. Oldest child is 0, next oldest is 1, and so on.
*/
protected final void setSibNum(int s)
{
astSibNum = s;
}
/**
* Get the sibling number.
*/
protected final int getSibNum()
{
return astSibNum;
}
/**
* Is node t equal to this in terms of token type and text?
*/
public boolean equals(AST t)
{
System.out.println("equals(AST) called");
RuntimeException re = new RuntimeException();
re.printStackTrace();
if(t == null) return false;
return this.getText().equals(t.getText()) &&
this.getType() == t.getType();
}
/**
* Is t an exact structural and equals() match of this tree. The 'this'
* reference is considered the start of a sibling list.
*/
public boolean equalsList(AST t)
{
AST sibling;
// the empty tree is not a match of any non-null tree.
if(t == null) {
return false;
}
// Otherwise, start walking sibling lists. First mismatch, return false.
for(sibling = this; sibling != null && t != null; sibling = sibling
.getNextSibling(), t = t.getNextSibling()) {
// as a quick optimization, check roots first.
if(!sibling.equals(t)) {
return false;
}
// if roots match, do full list match test on children.
if(sibling.getFirstChild() != null) {
if(!sibling.getFirstChild().equalsList(t.getFirstChild())) {
return false;
}
}
// sibling has no kids, make sure t doesn't either
else if(t.getFirstChild() != null) {
return false;
}
}
if(sibling == null && t == null) {
return true;
}
// one sibling list has more than the other
return false;
}
/**
* Checks if 'sub' a subtree of this list. The siblings of the root are NOT
* ignored.
*/
public boolean equalsListPartial(AST sub)
{
AST sibling;
// the empty tree is always a subset of any tree.
if(sub == null) {
return true;
}
// Otherwise, start walking sibling lists. First mismatch, return false.
for(sibling = this;
sibling != null && sub != null;
sibling = sibling.getNextSibling(), sub = sub.getNextSibling())
{
// as a quick optimization, check roots first.
if(!sibling.equals(sub)) return false;
// if roots match, do partial list match test on children.
if(sibling.getFirstChild() != null) {
if(!sibling.getFirstChild().equalsListPartial(sub.getFirstChild()))
return false;
}
}
if(sibling == null && sub != null) {
// nothing left to match in this tree, but subtree has more
return false;
}
// either both are null or sibling has more, but subtree doesn't
return true;
}
/**
* Checks if tree rooted at 'this' equal to 't'. The siblings of 'this' are
* ignored.
*/
public boolean equalsTree(AST t)
{
// check roots first.
if(!this.equals(t)) return false;
// if roots match, do full list match test on children.
if(this.getFirstChild() != null) {
if(!this.getFirstChild().equalsList(t.getFirstChild())) return false;
}
// sibling has no kids, make sure t doesn't either
else if(t.getFirstChild() != null) {
return false;
}
return true;
}
/**
* Checks if 't' a subtree of the tree rooted at 'this'. The siblings of
* 'this' are ignored.
*/
public boolean equalsTreePartial(AST sub)
{
// the empty tree is always a subset of any tree.
if(sub == null) {
return true;
}
// check roots first.
if(!this.equals(sub)) return false;
// if roots match, do full list partial match test on children.
if(this.getFirstChild() != null) {
if(!this.getFirstChild().equalsListPartial(sub.getFirstChild()))
return false;
}
return true;
}
/**
* @deprecated unimplemented
*/
public ASTEnumeration findAll(AST tree)
{
throw new UnsupportedOperationException("IclTerm.findAll not supported");
}
/**
* @deprecated unimplemented
*/
public ASTEnumeration findAllPartial(AST subtree)
{
throw new UnsupportedOperationException(
"IclTerm.findAllPartial not supported");
}
/**
* Get the first child of this node; null if no children .
*/
public final AST getFirstChild()
{
if(this.getChildren().size() > 0) {
return (AST)this.getChildren().get(0);
}
else {
return null;
}
}
/**
* Get the next sibling in line after this one.
*/
public final AST getNextSibling()
{
int mySibNum = this.getSibNum();
if(getParent() == null) {
// Don't know our parent.
// If we don't know the oldest sib, return null.
if(getOldestSib() == null) {
return null;
}
else {
// Know the oldest sib, so get the next sibling
if(getOldestSib().getSiblings().size() > mySibNum + 1) {
return (AST)(getOldestSib().getSiblings().get(mySibNum + 1));
}
else {
return null;
}
}
}
// If we know our parent, then we can use it to get the
// next sibling
if(this.getParent().getChildren().size() > mySibNum + 1) {
return (AST)getParent().getChildren().get(mySibNum + 1);
}
else {
return null;
}
}
/**
* Get the token text for this node.
*/
public String getText()
{
return astText;
}
/**
* Initialize from an integer type and text.
*/
public final void initialize(int t, String txt)
{
setType(t);
setText(txt);
}
/**
* Initialize from another AST.
*/
public final void initialize(AST t)
{
setType(t.getType());
setText(t.getText());
}
/**
* Initialize from a token.
*/
public final void initialize(Token t)
{
setType(t.getType());
setText(t.getText());
}
/**
* Set the token text for this node.
*/
public void setText(String text)
{
astText = text;
}
public String toString()
{
return this.getIclTerm().toString();
}
/**
* Print out a child-sibling tree in LISP notation.
*/
public final String toStringList()
{
AST t = this;
String ts = "";
if(t.getFirstChild() != null) {
ts += " (";
}
ts += " " + this.toString();
if(t.getFirstChild() != null) {
ts += ((IclAST)t.getFirstChild()).toStringList();
}
if(t.getFirstChild() != null) {
ts += " )";
}
if(t.getNextSibling() != null) {
ts += ((IclAST)t.getNextSibling()).toStringList();
}
return ts;
}
/**
* Print out the tree.
*/
public final String toStringTree()
{
AST t = this;
String ts = "";
if(t.getFirstChild() != null) ts += " (";
ts += " " + this.toString();
if(t.getFirstChild() != null) {
ts += ((IclAST)t.getFirstChild()).toStringList();
}
if(t.getFirstChild() != null) ts += " )";
return ts;
}
/// End AST implementation
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -