scope.java
来自「plugin for eclipse」· Java 代码 · 共 249 行
JAVA
249 行
package isis.anp.nesc.ot.scope;
import isis.anp.common.TNode;
import isis.anp.nesc.ot.NameConflictException;
import isis.anp.nesc.ot.types.CompoundType;
import isis.anp.nesc.ot.types.EnumType;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
/*
* Created on Apr 6, 2005
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
/**
* @author sallai
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class Scope implements Symbol {
ArrayList symbolList = new ArrayList();
TNode defNode;
SymbolTraits symbol = new SymbolTraits();
/**
* @param newScopeName
*/
public Scope(String name, Scope parent) {
setName(name);
// System.out.println("Creating scope "+getHierarchicalName());
if(parent!=null) {
parent.addSymbol(this);
}
}
/**
* @return Returns the children.
*/
public List getSymbolList() {
return symbolList;
}
public String toString() {
StringBuffer rval = new StringBuffer();
rval.append("scope ");
rval.append(getHierarchicalName());
rval.append("\n");
Iterator j = symbolList.iterator();
while(j.hasNext()) {
Symbol s = (Symbol) j.next();
rval.append(s.toString());
rval.append("\n");
}
return rval.toString();
}
public String getHierarchicalName() {
return symbol.getHierarchicalName();
}
public Scope getRootScope() {
if(getParentScope()==null) {
return this;
} else {
return getParentScope().getRootScope();
}
}
public void addSymbol(Symbol s) {
// symbol has no name: we don't put it into the scope
if(s.getName()==null) return;
s.setParentScope(this);
// System.out.println("Adding Symbol: "+s.toString());
symbolList.add(s);
}
public void removeSymbol(Symbol s) {
symbolList.remove(s);
}
public void addSymbols(Collection symbols) {
Iterator i = symbols.iterator();
while (i.hasNext()) {
Symbol s = (Symbol) i.next();
addSymbol(s);
}
}
/**
* Find a symbol recursively.
* @param symbolName the name of the symbol.
* @return The symbol object, or null if not found.
*/
public Symbol findSymbol(String symbolName) {
Symbol position = null;
if(!symbolList.isEmpty()) {
position = (Symbol)this.symbolList.get(symbolList.size()-1);
}
// if no position is given, we start searching backwards from the last symbol
return findSymbol(symbolName, position);
}
/**
* Find a symbol recursively.
* @param symbolName The name of the symbol.
* @param position The position in the currect scope at which to start the search.
* If position is null, search starts in parent scope (at the position of the current scope)
* @return The symbol object, or null if not found.
*/
public Symbol findSymbol(String symbolName, Symbol position) {
Symbol rval = null;
// try to look it up in the current scope
rval = findSymbolInCurrentScope(symbolName, position);
if(rval == null) {
// recursively search in parent scope
if(getParentScope()!=null) {
rval = getParentScope().findSymbol(symbolName, this);
}
}
return rval;
}
/**
* Search for the symbol in the current scope looking backwards.
* @param symbol The name of the symbol.
* @return The symbol object, or null if not found.
*/
public Symbol findSymbolInCurrentScope(String symbol) {
if(symbolList.isEmpty()) return null;
// if no position is given, we start searching backwards from the last symbol
return findSymbolInCurrentScope(symbol, (Symbol)this.symbolList.get(symbolList.size()-1));
};
/**
* Search for the symbol in the current scope looking backwards, starting from
* the last index of the symbol given in position.
* @param symbol The name of the symbol.
* @param position The position to start search at.
* @return The symbol object, or null if not found.
*/
public Symbol findSymbolInCurrentScope(String symbol, Symbol position) {
boolean positionReached = false;
if(position == null) return null;
for(int i=symbolList.lastIndexOf(position); i>=0; --i) {
Symbol s = (Symbol) symbolList.get(i);
if(s == position) {
positionReached = true;
}
if(positionReached && s.getName().equals(symbol)) return s;
}
return null;
}
public void setNameNode(TNode nameNode) {
this.symbol.setNameNode(nameNode);
}
public TNode getNameNode() {
return this.symbol.getNameNode();
}
public void setName(String name) {
this.symbol.setName(name);
}
public String getName() {
return this.symbol.getName();
}
public void setParentScope(Scope parentScope) {
this.symbol.setParentScope(parentScope);
}
public Scope getParentScope() {
return this.symbol.getParentScope();
}
public void add(CompoundType ct) {
// complete declaration with name given
if(ct.getNameNode()!=null && ct.isComplete()) {
// look for the symbol in current scope
CompoundType prev = (CompoundType)this.findSymbolInCurrentScope(ct.getName());
if(prev!=null && prev.isComplete()) {
// error: this type is already defined
NameConflictException ex = new NameConflictException(prev, this);
// ctx.addError(new Error(ex, ex.getMessage(), new CodeLocation(ct.getNameNode()))) ;
} else {
// add it to the scope
this.addSymbol(ct);
if(ct instanceof EnumType) {
this.addSymbols(((EnumType)ct).getEnumeratorList());
}
}
return;
}
// complete declaration with no name given
if(ct.getNameNode()==null && ct.isComplete()) {
if(ct instanceof EnumType) {
this.addSymbols(((EnumType)ct).getEnumeratorList());
}
return;
}
// incomplete declaration
if(ct.getNameNode()!=null && !ct.isComplete()) {
// look for the symbol in scope
CompoundType prev = (CompoundType) this.findSymbol(ct.getName());
if(prev == null) {
// this type is declared here first
this.addSymbol(ct);
} else {
// error: this type is already defined
NameConflictException ex = new NameConflictException(prev, this);
// ctx.addError(new Error(ex, ex.getMessage(), new CodeLocation(ct.getNameNode()))) ;
}
return;
}
// we should never get here...
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?