nescparsercontext.java

来自「plugin for eclipse」· Java 代码 · 共 112 行

JAVA
112
字号
/*
 * 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.nesc.common;

import isis.anp.common.CodeLocation;
import isis.anp.common.ParserConfiguration;
import isis.anp.common.ParserContext;
import isis.anp.common.ParserMessage;
import isis.anp.common.TNode;
import isis.anp.nesc.NesCLexer;
import isis.anp.nesc.NesCLexerTokenTypes;
import isis.anp.nesc.NesCParser;
import isis.anp.preprocessor.PreprocessorException;
import isis.commons.fs.CanonicalPath;

import java.io.DataInputStream;
import java.io.IOException;

import antlr.RecognitionException;
import antlr.TokenStreamException;
import antlr.TokenStreamHiddenTokenFilter;
import antlr.collections.AST;

/**
 * Context of the nesC parser.
 * 
 * @author sallai
 */
public class NesCParserContext extends ParserContext {

	public NesCParserContext(ParserConfiguration parserConfig) {
		super(parserConfig);
	}

	/**
	 * Parses a file. 
	 */
	public AST parseFile(CanonicalPath cp) throws ParserMessage {
		AST ast = null;

		// add a placeholder AST to the processed AST map to resolve circular
		// references
		// TNode will have an invalid type
		this.addProcessedAST(cp, new TNode());
		
		// parse file
		NesCParser parser = null;
		DataInputStream preprocessedStream = null;

		// get preprocessed stream
		try {

			preprocessedStream = new DataInputStream(this.getPreprocessor()
					.preprocess(cp.getText()));

		} catch (PreprocessorException e1) {
			throw new ParserMessage(ParserMessage.ERROR, e1.getMessage(), new CodeLocation(cp.getText(),0,0), e1);
		}
			
		// create a nesC lexer on the preprocessed stream
		NesCLexer lexer = new NesCLexer(preprocessedStream);
		lexer.setTokenObjectClass(isis.anp.common.CToken.class.getName());
		lexer.initialize(cp.getText());

		// create a filter to hide TinyDoc comments
		TokenStreamHiddenTokenFilter filter = new TokenStreamHiddenTokenFilter(
				lexer);
		filter.hide(NesCLexerTokenTypes.TinyDoc);

		// create parser
		parser = new NesCParser(filter, this);
		parser.setASTNodeClass(TNode.class.getName());
		TNode.setTokenVocabulary(isis.anp.nesc.NesCLexerTokenTypes.class
				.getName());


		// parse
		System.out.println("Parsing " + cp.getText());

		try {
			parser.translationUnit();
		} catch (TokenStreamException e1) {
			throw new ParserMessage(ParserMessage.ERROR, e1.getMessage(), new CodeLocation(cp.getText(),0,0), e1);
		} catch (RecognitionException e2) {
			throw new ParserMessage(ParserMessage.ERROR, e2.getMessage(), new CodeLocation(e2.getFilename(), e2.getLine(), e2.getColumn()), e2);
		} finally {
			// close the input stream
			if (preprocessedStream != null)
				try {
					preprocessedStream.close();
				} catch (IOException e) {
					// do nothing
				}
		}
		
		ast = parser.getAST();

		// add file name to root node of AST
		if (((TNode) ast).getAttribute("source") == null) {
			((TNode) ast).setAttribute("source", cp.getText());
		}

		// add to list of processed files
		this.addProcessedAST(cp, ast);

		return ast;
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?