📄 expandednescparser.g
字号:
header {
package isis.anp.nesc;}
{
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;
}class NesCParser extends Parser;
options {
k= 2;
exportVocab= NesC;
buildAST= true;
ASTLabelType= "TNode";
codeGenMakeSwitchThreshold= 2;
codeGenBitsetTestThreshold= 3;
importVocab=GnuC;
}
{
// 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);
}
}
translationUnit :(
includeFile
|
( (includes)* "module") =>
moduleFile
|
( (includes)* "interface") =>
interfaceFile
|
( (includes)* "configuration") =>
configurationFile
)
;
includeFile :(externalList)?
{ ## = #( #[NIncludeFile], ## ); }
;
interfaceFile :{
// create the NInterfaceFile node now, so that the ASTs
// of the include files can be attached to it an attribute
TNode newRoot = #[NInterfaceFile];
ArrayList includeFileASTs = new ArrayList();
newRoot.setAttribute("includeFileASTs", includeFileASTs);
pushCurrentFileAST(newRoot);
/*
// parse tos.h if it hasn't been parsed beforee
String inputFileName = "tos.h";
if(!getParserContext().getProcessedIncludes().containsKey(inputFileName)) {
try {
// parse include file
AST includeFileAST = parseIncludeFile(inputFileName);
// add its AST to the includeFileASTs attribute of the nesC file from where
// this include was included
includeFileASTs.add(includeFileAST);
} catch (java.io.FileNotFoundException e) {
// print File not found
System.err.println("Include file not found: " + inputFileName);
e.printStackTrace(System.err);
}
}
*/
}
(includes)*
interfaceDeclaration
{
## = #( popCurrentFileAST(), ## );
// ## = #( #[NInterfaceFile], ## );
}
;
includes :"includes"^
includeFileNameList
(SEMI!)+
{ ##.setType(NIncludes); }
;
includeFileNameList :includeFileName
( COMMA! includeFileName )*
;
includeFileName :i:ID
{
String inputFileName = i.getText()+".h";
// try {
// parse include file
// AST includeFileAST = parseIncludeFile(inputFileName);
AST includeFileAST = null;
ParserMessage container = new ParserMessage(ParserMessage.INFO, "Include file: "+inputFileName, new CodeLocation((CToken)i), null);
getParserContext().addMsg(container);
getParserContext().pushMessages(container);
try {
includeFileAST = getParserContext().getAST(inputFileName);
// add its AST as an attribute to the file name ID node
##.setAttribute("includeFileAST", includeFileAST);
// add its AST to the includeFileASTs attribute of the nesC file from where
// this include was included
TNode currentFileAST = getCurrentFileAST();
ArrayList includeFileASTs = (ArrayList)currentFileAST.getAttribute("includeFileASTs");
includeFileASTs.add(includeFileAST);
} catch(ParserMessage m) {
getParserContext().addMsg(m);
} finally {
getParserContext().popMessages();
}
// ##.addSibling(parseIncludeFile(inputFileName));
// } catch (java.io.FileNotFoundException e) {
// print File not found
// System.err.println("Include file not found: " + inputFileName);
// e.printStackTrace(System.err);
// }
}
{ ## = #( #[NIncludeFileName], ## ); }
;
interfaceDeclaration :"interface" i:ID
LCURLY^ declarationList RCURLY
{ ##.setType(NInterface); }
;
configurationFile :{
// create the NConfigurationFile node now, so that the ASTs
// of the include files can be attached to it an attribute
TNode newRoot = #[NConfigurationFile];
ArrayList includeFileASTs = new ArrayList();
newRoot.setAttribute("includeFileASTs", includeFileASTs);
pushCurrentFileAST(newRoot);
}
(includes)*
configuration
{
## = #( popCurrentFileAST(), ## );
}
;
moduleFile :{
// create the NModuleFile node now, so that the ASTs
// of the include files can be attached to it an attribute
TNode newRoot = #[NModuleFile];
ArrayList includeFileASTs = new ArrayList();
newRoot.setAttribute("includeFileASTs", includeFileASTs);
pushCurrentFileAST(newRoot);
/*
// parse tos.h if it hasn't been parsed beforeee
String inputFileName = "tos.h";
if(!getParserContext().getProcessedIncludes().containsKey(inputFileName)) {
try {
// parse include file
AST includeFileAST = parseIncludeFile(inputFileName);
// add its AST to the includeFileASTs attribute of the nesC file from where
// this include was included
includeFileASTs.add(includeFileAST);
} catch (java.io.FileNotFoundException e) {
// print File not found
System.err.println("Include file not found: " + inputFileName);
e.printStackTrace(System.err);
}
}
*/
}
(includes)*
module
{
## = #( popCurrentFileAST(), ## );
// ## = #( #[NModuleFile], ## );
}
;
module :"module"^ m:ID
interfaceDescription
moduleImplementation
{ ##.setType(NModule); }
;
configuration :"configuration"^ c:ID
interfaceDescription
configurationImplementation
{ ##.setType(NConfiguration); }
;
interfaceDescription :LCURLY^
(
uses
| provides
)*
RCURLY
{ ##.setType(NInterfaceDescription); }
;
uses :"uses"^
interfaceDescriptionItemList
{ ##.setType(NUses); }
;
provides :"provides"^
interfaceDescriptionItemList
{ ##.setType(NProvides); }
;
interfaceDescriptionItemList :interfaceDescriptionItem
| LCURLY (interfaceDescriptionItem)+ RCURLY
;
interfaceDescriptionItem :declaration
| "interface" /*{ ## = #( #[NInterface], ## ); }*/
interfaceIdentifier (interfaceParameters)? (SEMI!)+
;
interfaceIdentifier :i:ID^ ("as" r:ID)?
{
String inputFileName = i.getText()+".nc";
AST interfaceFileAST = null;
ParserMessage container = new ParserMessage(ParserMessage.INFO, "Interface file: "+inputFileName, new CodeLocation((CToken)i), null);
getParserContext().addMsg(container);
getParserContext().pushMessages(container);
try {
interfaceFileAST = getParserContext().getAST(inputFileName);
// add its AST as an attribute to the file name ID node
##.setAttribute("interfaceFileAST", interfaceFileAST);
} catch(ParserMessage m) {
getParserContext().addMsg(m);
} finally {
getParserContext().popMessages();
}
}
;
interfaceParameters :LBRACKET parameterTypeList RBRACKET
;
moduleImplementation :"implementation"^
LCURLY externalList RCURLY
{ ##.setType(NImplementation); }
;
configurationImplementation :"implementation"^
LCURLY
(componentList)*
connectionList
RCURLY
{ ##.setType(NImplementation); }
;
componentList :"components"^
aliasedComponentName (COMMA! aliasedComponentName)*
(SEMI)+
{ ##.setType(NComponentList); }
;
aliasedComponentName :i:ID
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -