📄 objecttreebuildercontext.java
字号:
/*
* Created on May 9, 2005
*
*/
package isis.anp.common;
import isis.anp.nesc.ot.Module;
import isis.anp.nesc.ot.NesCFile;
import isis.anp.nesc.ot.scope.Scope;
import isis.commons.fs.CanonicalPath;
import isis.commons.fs.SearchPath;
import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Stack;
import antlr.TokenStreamException;
/**
* Provides context for the object tree builder.
*
* @author sallai
*/
public abstract class ObjectTreeBuilderContext {
/**
* Contains NesCFile objects associated with the corresponding canonized
* file names.
*/
HashMap objectTreeMap = new HashMap();
/**
* Scopes are stored on a stack.
*/
Stack scopeStack = new Stack();
/**
* The module being processed.
*/
Module currentModule;
/**
* The context of the underlying parser.
*/
ParserContext parserCtx;
/**
* Used for unique naming of anonymous scopes.
*/
int anonymousScopeCounter = 0;
/**
* The parser configuration (search path, defines, etc.).
*/
ParserConfiguration parserConfig;
/**
* Stack containing parser messages/containers.
*/
private Stack messageContainerStack = new Stack();
/**
* Creates an object tree builder context.
*
* @param parserConfig
* parser configuration.
*/
public ObjectTreeBuilderContext(ParserConfiguration parserConfig) {
// set configuration
setParserConfiguration(parserConfig);
// set up root scope
Scope rootScope = new Scope("/", null);
pushScope(rootScope);
}
/**
* Returns the object tree corresponding to the given canonical file name.
*
* @param cp
* path in canonical form.
* @return the object tree.
*/
public NesCFile getProcessedObjectTree(CanonicalPath cp) {
if (cp == null)
return null;
return (NesCFile) objectTreeMap.get(cp.getText());
}
/**
* Returns the object tree corresponding to the given file name.
*
* @param fileName
* file name relative to the search path. The file name is
* converted to a canonical path, and the canonical path is used
* as a key in the hash table that indexes the object trees.
* @return the object tree, or null if not found.
* @throws ParserMessage is thrown if the file path can't be resolved.
*/
public NesCFile getProcessedObjectTree(String fileName) throws ParserMessage {
CanonicalPath cp = null;
try {
cp = getSearchPath().getCanonicalPath(fileName);
} catch (IOException e) {
throw new ParserMessage(ParserMessage.ERROR, e.getMessage(), null,
e);
}
return getProcessedObjectTree(cp);
}
/**
* Add an object tree to the map of parsed object trees. Called by the
* object tree builder.
*
* @param cp
* the canonical file name of the parsed file.
* @param nesCFile
* the root object of file's the object network.
*/
public void addProcessedObjectTree(CanonicalPath cp, NesCFile nesCFile) {
if (cp == null)
return;
objectTreeMap.put(cp.getText(), nesCFile);
}
/**
* Push a scope to the scope stack.
*
* @param scope
* the scope to push.
*/
public void pushScope(Scope scope) {
scopeStack.push(scope);
}
/**
* Get the current scope at the top of the scope stack.
*
* @return the scope.
*/
public Scope getCurrentScope() {
return (Scope) scopeStack.peek();
}
/**
* Pop the scope from the top of the scope stack.
*
* @return the scope.
*/
public Scope popScope() {
return (Scope) scopeStack.pop();
}
/**
* Set the current module being processed. This is used to avoid passing
* module objects between rules.
*
* @param moduleObj
* the module.
*/
public void setCurrentModule(Module moduleObj) {
currentModule = moduleObj;
}
/**
* Get the current module being parsed.
*
* @return the module.
*/
public Module getCurrentModule() {
return currentModule;
}
/**
* Get the parsed object trees.
*
* @return collection of parsed object trees.
*/
public Collection getObjectTrees() {
return objectTreeMap.values();
}
/**
* Parses tos include files or does nothing if they had already been parsed.
* If there are file access/preprocessing/parsing errors, the errors are
* placed into the message container.
* @throws ParserMessage
*/
private void parseNesCIncludes() throws ParserMessage {
Iterator i = null;
try {
i = getParserConfiguration().getNesCIncludesCanonicalPaths()
.iterator();
} catch (IOException e) {
throw new ParserMessage(ParserMessage.ERROR,
"Cannot find nesC include(s)", null, e);
}
while (i.hasNext()) {
CanonicalPath cp = (CanonicalPath) i.next();
if (getProcessedObjectTree(cp) == null) {
parseFile(cp);
}
}
}
/**
* Get object tree by canonical path of its source file.
*
* @param cp
* canonical path of the file.
* @return the object tree.
* @throws ParserMessage
*/
public NesCFile getObjectTree(CanonicalPath cp) throws ParserMessage {
parseNesCIncludes();
NesCFile ot = getProcessedObjectTree(cp);
if (ot == null) {
ot = parseFile(cp);
}
return ot;
}
public NesCFile getObjectTree(String fileName) throws ParserMessage {
// use the search path list to canonize the file path
try {
return this.getObjectTree(this.getSearchPath().getCanonicalPath(
fileName));
} catch (IOException e) {
throw new ParserMessage(ParserMessage.ERROR, e.getMessage(), null,
e);
}
}
public abstract NesCFile parseFile(CanonicalPath cp) throws ParserMessage;
/**
* Parses a file and generates an object tree.
*
* @param fileName
* the name of the file to parse (relative to the search path
* list)
*/
public NesCFile parseFile(String fileName) throws ParserMessage,
TokenStreamException {
// use the search path list to canonize the file path
try {
return this.parseFile(this.getSearchPath().getCanonicalPath(
fileName));
} catch (IOException e) {
throw new ParserMessage(ParserMessage.ERROR, e.getMessage(), null,
e);
}
}
/**
* Return a unique name for an anonymous scope.
*
* @return unique scope name.
*/
public String getAScopeName() {
anonymousScopeCounter++;
return "anonymousScope" + anonymousScopeCounter;
}
/**
* Add a message (error, warning, etc) to the massage list.
*
* @param msg
* the message.
*/
public void addMsg(ParserMessage msg) {
this.peekMessages().addException(msg);
}
/**
* Get parser messages (errors, warnings, etc).
*
* @return list of messages.
*/
public ParserMessage getMessages() {
return peekMessages();
}
/**
* @return Returns the parserCtx.
*/
public ParserContext getParserCtx() {
return parserCtx;
}
/**
* Set the parser context.
*
* @param parserCtx
* The parserCtx to set.
*/
public void setParserCtx(ParserContext parserCtx) {
this.parserCtx = parserCtx;
}
/**
* Get the search path.
*
* @return the search path.
*/
public SearchPath getSearchPath() {
return getParserConfiguration().getSearchPath();
}
/**
* Get the defines.
*
* @return the defines.
*/
public Map getDefines() {
return getParserConfiguration().getDefines();
}
/**
* Get the temp dir.
*
* @return the temp dir.
*/
public String getTempDir() {
return parserCtx.getTempDir();
}
/**
* Set the temp dir.
*
* @param the
* temp dir to set.
*/
public void setTempDir(String tempDir) {
parserCtx.setTempDir(tempDir);
}
/**
* Get the parser configuration.
*
* @return the parser configuration.
*/
public ParserConfiguration getParserConfiguration() {
return this.parserConfig;
}
/**
* @param parserConfig
* The parser configuration to set.
*/
public void setParserConfiguration(ParserConfiguration parserConfig) {
this.parserConfig = parserConfig;
}
/**
* Check if message/container stack is empty.
*/
public boolean isMessageContainerStackEmpty() {
return messageContainerStack.isEmpty();
}
/**
* Get the parser message/container on the top of the stack.
*/
public ParserMessage peekMessages() {
return (ParserMessage) messageContainerStack.peek();
}
/**
* Pop the message/container from the stack.
*/
public ParserMessage popMessages() {
return (ParserMessage) messageContainerStack.pop();
}
/**
* Push the message/container to the stack.
*/
public ParserMessage pushMessages(ParserMessage arg0) {
return (ParserMessage) messageContainerStack.push(arg0);
}
/**
* @return Returns the messageContainerStack.
*/
public Stack getMessageContainerStack() {
return messageContainerStack;
}
/**
* @param messageContainerStack The messageContainerStack to set.
*/
public void setMessageContainerStack(Stack messageContainerStack) {
this.messageContainerStack = messageContainerStack;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -