⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 parsercontext.java

📁 plugin for eclipse
💻 JAVA
字号:
/*
 * Created on Apr 27, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package isis.anp.common;

import isis.anp.preprocessor.Preprocessor;
import isis.commons.fs.CanonicalPath;
import isis.commons.fs.SearchPath;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Stack;

import antlr.collections.AST;

/**
 * Provides context for the parser.
 * 
 * @author sallai
 */
public abstract class ParserContext {
	/**
	 * Suppport C++-style single-line comments?
	 */
	private static boolean CPPComments = true;

	/**
	 * Access to symbol table.
	 */
	private CSymbolTable symbolTable = new CSymbolTable();

	/**
	 * Source for names to unnamed scopes.
	 */
	private int unnamedScopeCounter = 0;

	/**
	 * The parser configuration (search path, defines, etc).
	 */
	ParserConfiguration parserConfig;

	/**
	 * The preprocessor instance.
	 */
	private Preprocessor preprocessor = null;

	/**
	 * Processed file record.
	 */
	class ProcessedRec {
		CanonicalPath cp;

		AST ast;

		CSymbolTable symbolTable;

		ProcessedRec(CanonicalPath cp, AST ast, CSymbolTable st) {
			this.cp = cp;
			this.ast = ast;
			this.symbolTable = st;
		}
	};

	/**
	 * Map of processed file records (by canonical file name).
	 */
	HashMap processed = new HashMap();

	/**
	 * Stack containing parser messages/containers.
	 */
	private Stack messageContainerStack = new Stack();

	public ParserContext(ParserConfiguration parserConfig) {

		// set config
		setParserConfiguration(parserConfig);

		// create preprocessor
		preprocessor = new Preprocessor(parserConfig);
	}

	/**
	 * @return Returns the symbolTable.
	 */
	public CSymbolTable getSymbolTable() {
		return symbolTable;
	}

	/**
	 * @param symbolTable
	 *            The symbolTable to set.
	 */
	public void setSymbolTable(CSymbolTable symbolTable) {
		this.symbolTable = symbolTable;
	}

	/**
	 * @return Returns the unnamedScopeCounter.
	 */
	public int getUnnamedScopeCounter() {
		return unnamedScopeCounter;
	}

	/**
	 * @param unnamedScopeCounter
	 *            The unnamedScopeCounter to set.
	 */
	public void setUnnamedScopeCounter(int unnamedScopeCounter) {
		this.unnamedScopeCounter = unnamedScopeCounter;
	}

	/**
	 * @return Returns the cPPComments.
	 */
	public static boolean isCPPComments() {
		return CPPComments;
	}

	/**
	 * @param comments
	 *            The cPPComments to set.
	 */
	public static void setCPPComments(boolean comments) {
		CPPComments = comments;
	}

	/**
	 * @return Returns the preprocessor.
	 */
	public Preprocessor getPreprocessor() {
		return preprocessor;
	}

	/**
	 * @param preprocessor
	 *            The preprocessor to set.
	 */
	public void setPreprocessor(Preprocessor preprocessor) {
		this.preprocessor = preprocessor;
	}

	/**
	 * Parses tos include files or does nothing if they had already been parsed.
	 * 
	 * @throws ParserMessage
	 *             s
	 */
	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 (getProcessedAST(cp) == null) {
				parseFile(cp);
			}
		}
	}

	public AST getAST(CanonicalPath cp) throws ParserMessage {

		// make sure nesC includes (tos.h, etc.) are processed before everything
		// else
		parseNesCIncludes();

		AST ast = getProcessedAST(cp);

		if (ast == null) {
			ast = parseFile(cp);
		}

		return ast;
	}

	public AST getAST(String fileName) throws ParserMessage {
		// use the search path list to canonize the file path
		try {
			return this.getAST(this.getSearchPathList().getCanonicalPath(
					fileName));
		} catch (IOException e) {
			throw new ParserMessage(ParserMessage.ERROR, e.getMessage(), null,
					e);
		}
	}

	public AST getProcessedAST(CanonicalPath cp) {
		if (cp == null)
			return null;
		ProcessedRec rec = (ProcessedRec) processed.get(cp.getText());
		if (rec == null)
			return null;
		else
			return rec.ast;
	}

	public List getProcessedASTs() {
		ArrayList rval = new ArrayList();
		Iterator i = processed.values().iterator();
		while (i.hasNext()) {
			ProcessedRec r = (ProcessedRec) i.next();
			rval.add(r.ast);
		}
		return rval;
	}

	/**
	 * @param fileName
	 * @param ast
	 */
	public void addProcessedAST(CanonicalPath cp, AST ast) {
		if (cp == null)
			return;
		ProcessedRec r = null;
		r = new ProcessedRec(cp, ast, null);

		processed.put(cp.getText(), r);
	}

	public AST parseFile(String fileName) throws ParserMessage {
		// use the search path list to canonize the file path
		try {
			return this.parseFile(this.getSearchPathList().getCanonicalPath(
					fileName));
		} catch (IOException e) {
			throw new ParserMessage(ParserMessage.ERROR, e.getMessage(), null,
					e);
		}
	}

	public abstract AST parseFile(CanonicalPath cp) throws ParserMessage;

	/**
	 * @return Returns the messages.
	 */
	public ParserMessage getMessages() {
		return peekMessages();
	}

	/**
	 * @param messages
	 *            The messages to set.
	 */
	public void addMsg(ParserMessage msg) {
		this.peekMessages().addException(msg);
	}

	/**
	 * @return Returns the searchPathList.
	 */
	public SearchPath getSearchPathList() {
		return parserConfig.getSearchPath();
	}

	/**
	 * @param defines
	 */
	public void setDefines(HashMap defines) {
		parserConfig.setDefines(defines);
	}

	public void addDefine(String name, String value) {
		parserConfig.getDefines().put(name, value);
	}

	/**
	 * Get the temp dir.
	 */
	public String getTempDir() {
		return preprocessor.getTempDir();
	}

	/**
	 * Set the temp dir.
	 */
	public void setTempDir(String tempDir) {
		preprocessor.setTempDir(tempDir);
	}

	/**
	 * @return Returns the parserConfig.
	 */
	public ParserConfiguration getParserConfiguration() {
		return parserConfig;
	}

	/**
	 * @param parserConfig
	 *            The parserConfig 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 + -