nescparser.java
来自「plugin for eclipse」· Java 代码 · 共 2,369 行 · 第 1/5 页
JAVA
2,369 行
// $ANTLR : "expandedNesCParser.g" -> "NesCParser.java"$
package isis.anp.nesc;
import antlr.TokenBuffer;
import antlr.TokenStreamException;
import antlr.TokenStreamIOException;
import antlr.ANTLRException;
import antlr.LLkParser;
import antlr.Token;
import antlr.TokenStream;
import antlr.RecognitionException;
import antlr.NoViableAltException;
import antlr.MismatchedTokenException;
import antlr.SemanticException;
import antlr.ParserSharedInputState;
import antlr.collections.impl.BitSet;
import antlr.collections.AST;
import java.util.Hashtable;
import antlr.ASTFactory;
import antlr.ASTPair;
import antlr.collections.impl.ASTArray;
import isis.anp.common.CToken;
import isis.anp.common.CodeLocation;
import isis.anp.common.ParserMessage;
import isis.anp.common.TNode;
import isis.anp.nesc.common.NesCParserContext;
import java.util.ArrayList;
import antlr.ASTFactory;
import antlr.ASTPair;
import antlr.MismatchedTokenException;
import antlr.NoViableAltException;
import antlr.ParserSharedInputState;
import antlr.RecognitionException;
import antlr.SemanticException;
import antlr.Token;
import antlr.TokenBuffer;
import antlr.TokenStream;
import antlr.TokenStreamException;
import antlr.collections.AST;
import antlr.collections.impl.ASTArray;
import antlr.collections.impl.BitSet;
public class NesCParser extends antlr.LLkParser implements NesCTokenTypes
{
// factor out parser state (symbol table, lists of parsed files, etc).
private NesCParserContext parserContext = null;
public NesCParser(TokenStream lexer, NesCParserContext ctx) {
this(lexer,2);
this.setParserContext(ctx);
}
public void setParserContext(NesCParserContext ctx) {
parserContext = ctx;
}
public NesCParserContext getParserContext() {
return parserContext;
}
public String getFilename() {
try {
return LT(1).getFilename();
} catch (TokenStreamException e) {
}
return null;
}
public boolean isTypedefName(String name) {
boolean returnValue = false;
TNode node = getParserContext().getSymbolTable().lookupNameInCurrentScope(name);
for (; node != null; node = (TNode) node.getNextSibling() ) {
if(node.getType() == LITERAL_typedef) {
returnValue = true;
break;
}
}
return returnValue;
}
public String getAScopeName() {
int cnt = getParserContext().getUnnamedScopeCounter();
getParserContext().setUnnamedScopeCounter(cnt+1);
return Integer.toString(cnt);
}
public void pushScope(String scopeName) {
getParserContext().getSymbolTable().pushScope(scopeName);
}
public void popScope() {
getParserContext().getSymbolTable().popScope();
}
int traceDepth = 0;
public void reportError(RecognitionException ex) {
if ( ex != null) {
parserContext.addMsg(new ParserMessage(ParserMessage.ERROR, ex.getMessage(), new CodeLocation(ex.getFilename(), ex.getLine(), ex.getColumn()), ex));
}
// try {
// System.err.println("ANTLR Parsing Error: "+ex + " token name:" + tokenNames[LA(1)]);
// ex.printStackTrace(System.err);
// }
// catch (TokenStreamException e) {
// System.err.println("ANTLR Parsing Error: "+ex);
// ex.printStackTrace(System.err);
// }
}
public void reportError(String s) {
//System.err.println("ANTLR Parsing Error from String: " + s);
parserContext.addMsg(new ParserMessage(ParserMessage.ERROR, s, null, null));
}
public void reportWarning(String s) {
//System.err.println("ANTLR Parsing Warning from String: " + s);
parserContext.addMsg(new ParserMessage(ParserMessage.WARNING, s, null, null));
}
public void match(int t) throws MismatchedTokenException {
boolean debugging = false;
if ( debugging ) {
for (int x=0; x<traceDepth; x++) System.out.print(" ");
try {
System.out.println("Match("+tokenNames[t]+") with LA(1)="+
tokenNames[LA(1)] + ((inputState.guessing>0)?" [inputState.guessing "+ inputState.guessing + "]":""));
}
catch (TokenStreamException e) {
System.out.println("Match("+tokenNames[t]+") " + ((inputState.guessing>0)?" [inputState.guessing "+ inputState.guessing + "]":""));
}
}
try {
if ( LA(1)!=t ) {
if ( debugging ){
for (int x=0; x<traceDepth; x++) System.out.print(" ");
System.out.println("token mismatch: "+tokenNames[LA(1)]
+ "!="+tokenNames[t]);
}
throw new MismatchedTokenException(tokenNames, LT(1), t, false, getFilename());
} else {
// mark token as consumed -- fetch next token deferred until LA/LT
consume();
}
}
catch (TokenStreamException e) {
}
}
public void traceIn(String rname) {
traceDepth += 1;
for (int x=0; x<traceDepth; x++) System.out.print(" ");
try {
System.out.println("> "+rname+"; LA(1)==("+ tokenNames[LT(1).getType()]
+ ") " + LT(1).getText() + " [inputState.guessing "+ inputState.guessing + "]");
}
catch (TokenStreamException e) {
}
}
public void traceOut(String rname) {
for (int x=0; x<traceDepth; x++) System.out.print(" ");
try {
System.out.println("< "+rname+"; LA(1)==("+ tokenNames[LT(1).getType()]
+ ") "+LT(1).getText() + " [inputState.guessing "+ inputState.guessing + "]");
}
catch (TokenStreamException e) {
}
traceDepth -= 1;
}
/*
public AST parseIncludeFile(String fileName) throws FileNotFoundException, RecognitionException, TokenStreamException {
AST ast = ast = (AST) getParserContext().getProcessedAST(fileName);
if(ast == null) {
// create parser
NesCParser parser = NesCParserFactory.create(fileName, getParserContext());
// start parsing
parser.includeFile();
ast = parser.getAST();
// add to hashmap containing processed include file names
getParserContext().addProcessedAST(fileName, ast);
}
return ast;
}
public AST parseInterfaceFile(String fileName) throws FileNotFoundException, RecognitionException, TokenStreamException {
AST ast = ast = (AST) getParserContext().getProcessedAST(fileName);
if(ast == null) {
// create parser
NesCParser parser = NesCParserFactory.create(fileName, getParserContext());
// start parsing
parser.interfaceFile();
ast = parser.getAST();
// add to hashmap containing processed include file names
getParserContext().addProcessedAST(fileName, ast);
}
return ast;
}
*/
ArrayList currentFileASTStack = new ArrayList();
public void pushCurrentFileAST(TNode root) {
currentFileASTStack.add(0,root);
}
public TNode popCurrentFileAST() {
return (TNode)currentFileASTStack.remove(0);
}
public TNode getCurrentFileAST() {
return (TNode)currentFileASTStack.get(0);
}
protected NesCParser(TokenBuffer tokenBuf, int k) {
super(tokenBuf,k);
tokenNames = _tokenNames;
buildTokenTypeASTClassMap();
astFactory = new ASTFactory(getTokenTypeToASTClassMap());
}
public NesCParser(TokenBuffer tokenBuf) {
this(tokenBuf,2);
}
protected NesCParser(TokenStream lexer, int k) {
super(lexer,k);
tokenNames = _tokenNames;
buildTokenTypeASTClassMap();
astFactory = new ASTFactory(getTokenTypeToASTClassMap());
}
public NesCParser(TokenStream lexer) {
this(lexer,2);
}
public NesCParser(ParserSharedInputState state) {
super(state,2);
tokenNames = _tokenNames;
buildTokenTypeASTClassMap();
astFactory = new ASTFactory(getTokenTypeToASTClassMap());
}
public final void translationUnit() throws RecognitionException, TokenStreamException {
returnAST = null;
ASTPair currentAST = new ASTPair();
TNode translationUnit_AST = null;
try { // for error handling
{
if ((_tokenSet_0.member(LA(1)))) {
includeFile();
astFactory.addASTChild(currentAST, returnAST);
}
else {
boolean synPredMatched2844 = false;
if (((LA(1)==LITERAL_module||LA(1)==LITERAL_includes) && (LA(2)==ID))) {
int _m2844 = mark();
synPredMatched2844 = true;
inputState.guessing++;
try {
{
{
_loop2843:
do {
if ((LA(1)==LITERAL_includes)) {
includes();
}
else {
break _loop2843;
}
} while (true);
}
match(LITERAL_module);
}
}
catch (RecognitionException pe) {
synPredMatched2844 = false;
}
rewind(_m2844);
inputState.guessing--;
}
if ( synPredMatched2844 ) {
moduleFile();
astFactory.addASTChild(currentAST, returnAST);
}
else {
boolean synPredMatched2848 = false;
if (((LA(1)==LITERAL_interface||LA(1)==LITERAL_includes) && (LA(2)==ID))) {
int _m2848 = mark();
synPredMatched2848 = true;
inputState.guessing++;
try {
{
{
_loop2847:
do {
if ((LA(1)==LITERAL_includes)) {
includes();
}
else {
break _loop2847;
}
} while (true);
}
match(LITERAL_interface);
}
}
catch (RecognitionException pe) {
synPredMatched2848 = false;
}
rewind(_m2848);
inputState.guessing--;
}
if ( synPredMatched2848 ) {
interfaceFile();
astFactory.addASTChild(currentAST, returnAST);
}
else {
boolean synPredMatched2852 = false;
if (((LA(1)==LITERAL_configuration||LA(1)==LITERAL_includes) && (LA(2)==ID))) {
int _m2852 = mark();
synPredMatched2852 = true;
inputState.guessing++;
try {
{
{
_loop2851:
do {
if ((LA(1)==LITERAL_includes)) {
includes();
}
else {
break _loop2851;
}
} while (true);
}
match(LITERAL_configuration);
}
}
catch (RecognitionException pe) {
synPredMatched2852 = false;
}
rewind(_m2852);
inputState.guessing--;
}
if ( synPredMatched2852 ) {
configurationFile();
astFactory.addASTChild(currentAST, returnAST);
}
else {
throw new NoViableAltException(LT(1), getFilename());
}
}}}
}
translationUnit_AST = (TNode)currentAST.root;
}
catch (RecognitionException ex) {
if (inputState.guessing==0) {
reportError(ex);
recover(ex,_tokenSet_1);
} else {
throw ex;
}
}
returnAST = translationUnit_AST;
}
public final void includeFile() throws RecognitionException, TokenStreamException {
returnAST = null;
ASTPair currentAST = new ASTPair();
TNode includeFile_AST = null;
try { // for error handling
{
switch ( LA(1)) {
case LITERAL_typedef:
case LITERAL_asm:
case LITERAL_volatile:
case SEMI:
case LITERAL_struct:
case LITERAL_union:
case LITERAL_enum:
case LITERAL_auto:
case LITERAL_register:
case LITERAL_extern:
case LITERAL_static:
case LITERAL_const:
case LITERAL_void:
case LITERAL_char:
case LITERAL_short:
case LITERAL_int:
case LITERAL_long:
case LITERAL_float:
case LITERAL_double:
case LITERAL_signed:
case LITERAL_unsigned:
case ID:
case STAR:
case LPAREN:
case LITERAL_default:
case LITERAL_inline:
case LITERAL_typeof:
case LITERAL___complex:
case LITERAL_norace:
case LITERAL_async:
case LITERAL_command:
case LITERAL_event:
case LITERAL_task:
{
externalList();
astFactory.addASTChild(currentAST, returnAST);
break;
}
case EOF:
{
break;
}
default:
{
throw new NoViableAltException(LT(1), getFilename());
}
}
}
if ( inputState.guessing==0 ) {
includeFile_AST = (TNode)currentAST.root;
includeFile_AST = (TNode)astFactory.make( (new ASTArray(2)).add((TNode)astFactory.create(NIncludeFile)).add(includeFile_AST));
currentAST.root = includeFile_AST;
currentAST.child = includeFile_AST!=null &&includeFile_AST.getFirstChild()!=null ?
includeFile_AST.getFirstChild() : includeFile_AST;
currentAST.advanceChildToEnd();
}
includeFile_AST = (TNode)currentAST.root;
}
catch (RecognitionException ex) {
if (inputState.guessing==0) {
reportError(ex);
recover(ex,_tokenSet_1);
} else {
throw ex;
}
}
returnAST = includeFile_AST;
}
public final void includes() throws RecognitionException, TokenStreamException {
returnAST = null;
ASTPair currentAST = new ASTPair();
TNode includes_AST = null;
try { // for error handling
TNode tmp1_AST = null;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?