declaration.java
来自「plugin for eclipse」· Java 代码 · 共 312 行
JAVA
312 行
/*
* Created on May 9, 2005
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package isis.anp.nesc.ot;
import isis.anp.common.CodeLocation;
import isis.anp.common.ObjectTreeBuilderContext;
import isis.anp.common.ParserMessage;
import isis.anp.common.TNode;
import isis.anp.nesc.NesCEmitter;
import isis.anp.nesc.ot.scope.Scope;
import isis.anp.nesc.ot.scope.Symbol;
import isis.anp.nesc.ot.types.Type;
import isis.anp.nesc.ot.types.TypeReference;
import isis.anp.nesc.tinydoc.TinyDoc;
import isis.anp.nesc.tinydoc.TinyDocFactory;
import java.util.ArrayList;
import java.util.Iterator;
import antlr.CommonHiddenStreamToken;
import antlr.RecognitionException;
/**
* @author sallai
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class Declaration implements Symbol {
TNode defNode;
TNode nameNode;
String name;
Scope parentScope;
TNode initializer;
ArrayList storageClassSpecifiers = new ArrayList();
ArrayList attributeList = new ArrayList();
Type type;
Object parent;
/**
* @return Returns the declName.
*/
public String getName() {
return name;
}
/**
* @param declName The declName to set.
*/
public void setName(String declName) {
this.name = declName;
this.getNameNode().setText(declName);
}
/**
* @return Returns the declNameNode.
*/
public TNode getNameNode() {
return nameNode;
}
/**
* @param declNameNode The declNameNode to set.
*/
public void setNameNode(TNode declNameNode) {
this.nameNode = declNameNode;
if(declNameNode!=null) {
this.name = declNameNode.getText();
} else {
System.out.println("Declaration name node is null");
}
}
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append(this.getType().toString());
if(this.getName()!=null) {
sb.append(" ");
sb.append(this.getName());
}
if(isTypedef()) {
sb.append(" (typedef declaration)");
} else {
sb.append(" (variable declaration)");
}
return sb.toString();
}
/**
* @return Returns the type.
*/
public Type getType() {
return type;
}
/**
* @param type The type to set.
*/
public void setType(Type type) {
this.type = type;
}
/**
* @return Returns the storageClassSpecifiers.
*/
public ArrayList getStorageClassSpecifiers() {
return storageClassSpecifiers;
}
public void addStorageClassSpecifier(StorageClassSpecifier scs) {
storageClassSpecifiers.add(scs);
}
public Object clone() {
Declaration rval = new Declaration();
rval.defNode = this.defNode;
rval.name = this.name;
rval.nameNode = this.nameNode;
rval.initializer = this.initializer;
rval.storageClassSpecifiers.addAll(this.storageClassSpecifiers);
// FIXME: deep copy of type might be needed...
rval.type = this.type;
return rval;
}
/**
* @param iv
*/
public void setInitializer(TNode iv) {
initializer = iv;
}
public TNode getDefNode() {
return defNode;
}
public void setDefNode(TNode defNode) {
this.defNode = defNode;
}
private boolean isTypedef() {
boolean isTypedef = false;
// check if this declaration is a typedef
Iterator i = storageClassSpecifiers.iterator();
while (i.hasNext()) {
StorageClassSpecifier scs = (StorageClassSpecifier) i.next();
if(scs.getDefNode().getText().equals("typedef")) {
isTypedef = true;
break;
}
}
return isTypedef;
}
public void addToScope(ObjectTreeBuilderContext ctx) {
Symbol prevSymbol = ctx.getCurrentScope().findSymbolInCurrentScope(getName());
Symbol newSymbol;
if(isTypedef()) {
newSymbol = new TypeReference(getType(), getNameNode(), getDefNode());
} else {
newSymbol = this;
}
if(prevSymbol==null) {
ctx.getCurrentScope().addSymbol(newSymbol);
} else {
NameConflictException ex = new NameConflictException(prevSymbol, newSymbol);
// ctx.addMsg(new ParserMessage(ex, ex.getMessage(), new CodeLocation(newSymbol.getNameNode()))) ;
ctx.addMsg(new ParserMessage(ParserMessage.ERROR, ex.getMessage(), new CodeLocation(newSymbol.getNameNode()),ex)) ;
}
}
public void outline(Outline o) {
// String doc = getTinyDocText();
String doc = null;
TinyDoc tinyDoc = getTinyDoc();
if(tinyDoc !=null) doc = tinyDoc.toString();
if(doc!=null) {
o.addTabs();
o.append(doc);
o.addNewLine();
}
o.addTabs();
if(getDefNode()!=null) {
// o.append("/* "+getDefNode().getFileName()+":"+getDefNode().getLine()+":"+getDefNode().getColumn()+" */>");
} else {
o.append("/* -*-*-*-*-*-*-*-* DEFNODE IS NULL */");
}
o.ensureSpace();
Iterator i = storageClassSpecifiers.iterator();
while (i.hasNext()) {
StorageClassSpecifier scs = (StorageClassSpecifier) i.next();
o.append(scs.getDefNode().getText());
o.append(" ");
}
if(getType()!=null) {
getType().outline(o);
} else {
o.append("/* unknown type */>");
}
o.ensureSpace();
if(getName()!=null) {
o.append(getName());
} else {
o.append("/* no declarator */");
}
Iterator j = getAttributeList().iterator();
while (j.hasNext()) {
Attribute a = (Attribute) j.next();
o.ensureSpace();
a.outline(o);
}
if(this.initializer!=null) {
o.ensureSpace();
o.append('=');
o.ensureSpace();
NesCEmitter e = new NesCEmitter(o);
try {
e.initializer(this.initializer);
} catch(RecognitionException ex) {
o.append("/* Exception while printing out initializer: ");
//ex.printStackTrace(new PrintWriter(o));
o.append(" */");
}
}
}
/**
* @return Returns the parent.
*/
public Object getParent() {
return parent;
}
/**
* @param parent The parent to set.
*/
public void setParent(Object parent) {
this.parent = parent;
}
public void setParentScope(Scope parentScope) {
this.parentScope = parentScope;
}
public Scope getParentScope() {
return this.parentScope;
}
public String getHierarchicalName() {
StringBuffer sb = new StringBuffer();
if (getParentScope() != null) {
sb.append(getParentScope().getHierarchicalName());
}
sb.append("::");
sb.append(getName());
return sb.toString();
}
/**
* @param attrObj
*/
public void addAttribute(Attribute attr) {
this.attributeList.add(attr);
}
/**
* @return Returns the attributeList.
*/
public ArrayList getAttributeList() {
return attributeList;
}
public String getTinyDocText() {
if(this.defNode==null) return null;
TNode nextRealNode = this.defNode.getNextRealNode();
if(nextRealNode==null) return null;
// return the TinyDoc comment preceding the first real node
CommonHiddenStreamToken tinyDocTok = nextRealNode.getHiddenBefore();
if(tinyDocTok==null) return null;
return tinyDocTok.getText();
}
public TinyDoc getTinyDoc() {
String tinyDocText = getTinyDocText();
if(tinyDocText == null) return null;
try {
return TinyDocFactory.parse(tinyDocText);
} catch (RecognitionException e) {
System.out.println("Error parsing TinyDoc: " + tinyDocText);
return null;
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?