2005-09-25 16:11:32 +02:00
|
|
|
header {
|
|
|
|
package processing.app.preproc;
|
|
|
|
|
|
|
|
import java.io.*;
|
|
|
|
|
|
|
|
import antlr.CommonAST;
|
|
|
|
import antlr.DumpASTVisitor;
|
2005-10-06 19:18:26 +02:00
|
|
|
}
|
|
|
|
class WParser extends Parser;
|
|
|
|
|
|
|
|
options {
|
|
|
|
k= 2;
|
|
|
|
exportVocab= W;
|
|
|
|
buildAST= true;
|
|
|
|
ASTLabelType= "TNode";
|
|
|
|
defaultErrorHandler= false;
|
|
|
|
codeGenMakeSwitchThreshold= 2;
|
|
|
|
codeGenBitsetTestThreshold= 3;
|
|
|
|
importVocab=STDC;
|
|
|
|
}
|
|
|
|
|
2005-09-25 16:11:32 +02:00
|
|
|
{
|
|
|
|
// Suppport C++-style single-line comments?
|
|
|
|
public static boolean CPPComments = true;
|
|
|
|
|
|
|
|
// access to symbol table
|
|
|
|
public CSymbolTable symbolTable = new CSymbolTable();
|
|
|
|
|
|
|
|
// source for names to unnamed scopes
|
|
|
|
protected int unnamedScopeCounter = 0;
|
|
|
|
|
|
|
|
public boolean isTypedefName(String name) {
|
|
|
|
boolean returnValue = false;
|
|
|
|
TNode node = symbolTable.lookupNameInCurrentScope(name);
|
|
|
|
for (; node != null; node = (TNode) node.getNextSibling() ) {
|
|
|
|
if(node.getType() == LITERAL_typedef) {
|
|
|
|
returnValue = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return returnValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public String getAScopeName() {
|
|
|
|
return "" + (unnamedScopeCounter++);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void pushScope(String scopeName) {
|
|
|
|
symbolTable.pushScope(scopeName);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void popScope() {
|
|
|
|
symbolTable.popScope();
|
|
|
|
}
|
|
|
|
|
|
|
|
int traceDepth = 0;
|
|
|
|
public void reportError(RecognitionException 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);
|
|
|
|
}
|
|
|
|
public void reportWarning(String s) {
|
|
|
|
System.err.println("ANTLR Parsing Warning from String: " + s);
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2005-10-06 19:18:26 +02:00
|
|
|
}
|
2005-09-25 16:11:32 +02:00
|
|
|
translationUnit :( externalList )? /* Empty source files are allowed. */
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
2005-09-25 16:11:32 +02:00
|
|
|
asm_expr :"asm"^
|
|
|
|
("volatile")? LCURLY expr RCURLY ( SEMI )+
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
2005-09-25 16:11:32 +02:00
|
|
|
idList :ID ( options{warnWhenFollowAmbig=false;}: COMMA ID )*
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
2005-09-25 16:11:32 +02:00
|
|
|
externalDef :( "typedef" | declaration )=> declaration
|
|
|
|
| ( functionPrefix )=> functionDef
|
|
|
|
| typelessDeclaration
|
|
|
|
| asm_expr
|
|
|
|
| SEMI
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
|
|
|
functionPrefix { String declName; }
|
2005-09-25 16:11:32 +02:00
|
|
|
:( (functionDeclSpecifiers)=> ds:functionDeclSpecifiers
|
|
|
|
| //epsilon
|
|
|
|
)
|
|
|
|
declName = d:declarator[true]
|
|
|
|
( declaration )* (VARARGS)? ( SEMI )*
|
|
|
|
LCURLY
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
|
|
|
typelessDeclaration { AST typeMissing = #[NTypeMissing]; }
|
2005-09-25 16:11:32 +02:00
|
|
|
:initDeclList[typeMissing] SEMI { ## = #( #[NTypeMissing], ##); }
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
2005-09-25 16:11:32 +02:00
|
|
|
initializer :( ( ( (initializerElementLabel)=> initializerElementLabel )?
|
|
|
|
( assignExpr | lcurlyInitializer ) { ## = #( #[NInitializer], ## ); }
|
|
|
|
)
|
|
|
|
| lcurlyInitializer
|
|
|
|
)
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
2005-09-25 16:11:32 +02:00
|
|
|
initializerElementLabel :( ( LBRACKET ((constExpr VARARGS)=> rangeExpr | constExpr) RBRACKET (ASSIGN)? )
|
|
|
|
| ID COLON
|
|
|
|
| DOT ID ASSIGN
|
|
|
|
)
|
|
|
|
{ ## = #( #[NInitializerElementLabel], ##) ; }
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
2005-09-25 16:11:32 +02:00
|
|
|
lcurlyInitializer :LCURLY^ (initializerList ( COMMA! )? )? RCURLY
|
|
|
|
{ ##.setType( NLcurlyInitializer ); }
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
2005-09-25 16:11:32 +02:00
|
|
|
initializerList :initializer ( options{warnWhenFollowAmbig=false;}:COMMA! initializer )*
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
|
|
|
declarator[boolean isFunctionDefinition] returns [String declName]{ declName = ""; }
|
2005-09-25 16:11:32 +02:00
|
|
|
:( pointerGroup )?
|
|
|
|
|
|
|
|
( id:ID { declName = id.getText(); }
|
|
|
|
| LPAREN declName = declarator[false] RPAREN
|
|
|
|
)
|
|
|
|
|
|
|
|
( declaratorParamaterList[isFunctionDefinition, declName]
|
|
|
|
| LBRACKET ( expr )? RBRACKET
|
|
|
|
)*
|
|
|
|
{ ## = #( #[NDeclarator], ## ); }
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
2005-09-25 16:11:32 +02:00
|
|
|
declaratorParamaterList[boolean isFunctionDefinition, String declName] :LPAREN^
|
|
|
|
{
|
|
|
|
if (isFunctionDefinition) {
|
|
|
|
pushScope(declName);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
pushScope("!"+declName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(
|
|
|
|
(declSpecifiers)=> parameterTypeList
|
|
|
|
| (idList)?
|
|
|
|
)
|
|
|
|
{
|
|
|
|
popScope();
|
|
|
|
}
|
|
|
|
( COMMA! )?
|
|
|
|
RPAREN
|
|
|
|
{ ##.setType(NParameterTypeList); }
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
2005-09-25 16:11:32 +02:00
|
|
|
parameterTypeList :parameterDeclaration
|
|
|
|
( options {
|
|
|
|
warnWhenFollowAmbig = false;
|
|
|
|
} :
|
|
|
|
( COMMA | SEMI )
|
|
|
|
parameterDeclaration
|
|
|
|
)*
|
|
|
|
( ( COMMA | SEMI )
|
|
|
|
VARARGS
|
|
|
|
)?
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
2005-09-25 16:11:32 +02:00
|
|
|
declarationList :( options { // this loop properly aborts when
|
|
|
|
// it finds a non-typedefName ID MBZ
|
|
|
|
warnWhenFollowAmbig = false;
|
|
|
|
} :
|
|
|
|
|
|
|
|
localLabelDeclaration
|
|
|
|
| ( declarationPredictor )=> declaration
|
|
|
|
)+
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
2005-09-25 16:11:32 +02:00
|
|
|
localLabelDeclaration :( //GNU note: any __label__ declarations must come before regular declarations.
|
|
|
|
"__label__"^ ID (options{warnWhenFollowAmbig=false;}: COMMA! ID)* ( COMMA! )? ( SEMI! )+
|
|
|
|
)
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
|
|
|
declaration { AST ds1 = null; }
|
2005-09-25 16:11:32 +02:00
|
|
|
:ds:declSpecifiers { ds1 = astFactory.dupList(#ds); }
|
|
|
|
(
|
|
|
|
initDeclList[ds1]
|
|
|
|
)?
|
|
|
|
( SEMI )+
|
|
|
|
{ ## = #( #[NDeclaration], ##); }
|
|
|
|
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
2005-09-25 16:11:32 +02:00
|
|
|
functionStorageClassSpecifier :"extern"
|
|
|
|
| "static"
|
|
|
|
| "inline"
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
|
|
|
typeSpecifier[int specCount] returns [int retSpecCount]{ retSpecCount = specCount + 1; }
|
2005-09-25 16:11:32 +02:00
|
|
|
:( "void"
|
|
|
|
| "char"
|
|
|
|
| "short"
|
|
|
|
| "int"
|
|
|
|
| "long"
|
|
|
|
| "float"
|
|
|
|
| "double"
|
|
|
|
| "signed"
|
|
|
|
| "unsigned"
|
|
|
|
| "byte"
|
|
|
|
| "boolean"
|
|
|
|
| "Servo"
|
|
|
|
| "Wire"
|
|
|
|
| structOrUnionSpecifier ( options{warnWhenFollowAmbig=false;}: attributeDecl )*
|
|
|
|
| enumSpecifier
|
|
|
|
| { specCount==0 }? typedefName
|
|
|
|
| "typeof"^ LPAREN
|
|
|
|
( ( typeName )=> typeName
|
|
|
|
| expr
|
|
|
|
)
|
|
|
|
RPAREN
|
|
|
|
| "__complex"
|
|
|
|
)
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
|
|
|
structOrUnionSpecifier { String scopeName; }
|
2005-09-25 16:11:32 +02:00
|
|
|
:sou:structOrUnion!
|
|
|
|
( ( ID LCURLY )=> i:ID l:LCURLY
|
|
|
|
{
|
|
|
|
scopeName = #sou.getText() + " " + #i.getText();
|
|
|
|
#l.setText(scopeName);
|
|
|
|
pushScope(scopeName);
|
|
|
|
}
|
|
|
|
( structDeclarationList )?
|
|
|
|
{ popScope();}
|
|
|
|
RCURLY
|
|
|
|
| l1:LCURLY
|
|
|
|
{
|
|
|
|
scopeName = getAScopeName();
|
|
|
|
#l1.setText(scopeName);
|
|
|
|
pushScope(scopeName);
|
|
|
|
}
|
|
|
|
( structDeclarationList )?
|
|
|
|
{ popScope(); }
|
|
|
|
RCURLY
|
|
|
|
| ID
|
|
|
|
)
|
|
|
|
{
|
|
|
|
## = #( #sou, ## );
|
|
|
|
}
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
2005-09-25 16:11:32 +02:00
|
|
|
structDeclaration :specifierQualifierList structDeclaratorList ( COMMA! )? ( SEMI! )+
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
2005-09-25 16:11:32 +02:00
|
|
|
structDeclaratorList :structDeclarator ( options{warnWhenFollowAmbig=false;}: COMMA! structDeclarator )*
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
2005-09-25 16:11:32 +02:00
|
|
|
structDeclarator :( declarator[false] )?
|
|
|
|
( COLON constExpr )?
|
|
|
|
( attributeDecl )*
|
|
|
|
{ ## = #( #[NStructDeclarator], ##); }
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
2005-09-25 16:11:32 +02:00
|
|
|
enumSpecifier :"enum"^
|
|
|
|
( ( ID LCURLY )=> i:ID LCURLY enumList[i.getText()] RCURLY
|
|
|
|
| LCURLY enumList["anonymous"] RCURLY
|
|
|
|
| ID
|
|
|
|
)
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
2005-09-25 16:11:32 +02:00
|
|
|
enumList[String enumName] :enumerator[enumName] ( options{warnWhenFollowAmbig=false;}: COMMA! enumerator[enumName] )* ( COMMA! )?
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
2005-09-25 16:11:32 +02:00
|
|
|
initDeclList[AST declarationSpecifiers] :initDecl[declarationSpecifiers]
|
|
|
|
( options{warnWhenFollowAmbig=false;}: COMMA! initDecl[declarationSpecifiers] )*
|
|
|
|
( COMMA! )?
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
|
|
|
initDecl[AST declarationSpecifiers] { String declName = ""; }
|
2005-09-25 16:11:32 +02:00
|
|
|
:declName = d:declarator[false]
|
|
|
|
{ AST ds1, d1;
|
|
|
|
ds1 = astFactory.dupList(declarationSpecifiers);
|
|
|
|
d1 = astFactory.dupList(#d);
|
|
|
|
symbolTable.add(declName, #(null, ds1, d1) );
|
|
|
|
}
|
|
|
|
( attributeDecl )*
|
|
|
|
( ASSIGN initializer
|
|
|
|
| COLON expr
|
|
|
|
)?
|
|
|
|
{ ## = #( #[NInitDecl], ## ); }
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
2005-09-25 16:11:32 +02:00
|
|
|
attributeDecl :"__attribute"^ LPAREN LPAREN attributeList RPAREN RPAREN
|
|
|
|
| "asm"^ LPAREN stringConst RPAREN { ##.setType( NAsmAttribute ); }
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
2005-09-25 16:11:32 +02:00
|
|
|
attributeList :attribute ( options{warnWhenFollowAmbig=false;}: COMMA attribute)* ( COMMA )?
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
2005-09-25 16:11:32 +02:00
|
|
|
attribute :( ~(LPAREN | RPAREN | COMMA)
|
|
|
|
| LPAREN attributeList RPAREN
|
|
|
|
)*
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
2005-09-25 16:11:32 +02:00
|
|
|
compoundStatement[String scopeName] :LCURLY^
|
|
|
|
|
|
|
|
{
|
|
|
|
pushScope(scopeName);
|
|
|
|
}
|
|
|
|
( //this ambiguity is ok, declarationList and nestedFunctionDef end properly
|
|
|
|
options {
|
|
|
|
warnWhenFollowAmbig = false;
|
|
|
|
} :
|
|
|
|
( "typedef" | "__label__" | declaration )=> declarationList
|
|
|
|
| (nestedFunctionDef)=> nestedFunctionDef
|
|
|
|
)*
|
|
|
|
( statementList )?
|
|
|
|
{ popScope(); }
|
|
|
|
RCURLY
|
|
|
|
{ ##.setType( NCompoundStatement ); ##.setAttribute( "scopeName", scopeName ); }
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
|
|
|
nestedFunctionDef { String declName; }
|
2005-09-25 16:11:32 +02:00
|
|
|
:( "auto" )? //only for nested functions
|
|
|
|
( (functionDeclSpecifiers)=> ds:functionDeclSpecifiers
|
|
|
|
)?
|
|
|
|
declName = d:declarator[false]
|
|
|
|
{
|
|
|
|
AST d2, ds2;
|
|
|
|
d2 = astFactory.dupList(#d);
|
|
|
|
ds2 = astFactory.dupList(#ds);
|
|
|
|
symbolTable.add(declName, #(null, ds2, d2));
|
|
|
|
pushScope(declName);
|
|
|
|
}
|
|
|
|
( declaration )*
|
|
|
|
{ popScope(); }
|
|
|
|
compoundStatement[declName]
|
|
|
|
{ ## = #( #[NFunctionDef], ## );}
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
2005-09-25 16:11:32 +02:00
|
|
|
statement :SEMI // Empty statements
|
|
|
|
|
|
|
|
| compoundStatement[getAScopeName()] // Group of statements
|
|
|
|
|
|
|
|
| expr SEMI! { ## = #( #[NStatementExpr], ## );} // Expressions
|
|
|
|
|
|
|
|
// Iteration statements:
|
|
|
|
|
|
|
|
| "while"^ LPAREN! expr RPAREN! statement
|
|
|
|
| "do"^ statement "while"! LPAREN! expr RPAREN! SEMI!
|
|
|
|
|! "for"
|
|
|
|
LPAREN ( e1:expr )? SEMI ( e2:expr )? SEMI ( e3:expr )? RPAREN
|
|
|
|
s:statement
|
|
|
|
{
|
|
|
|
if ( #e1 == null) { #e1 = #[ NEmptyExpression ]; }
|
|
|
|
if ( #e2 == null) { #e2 = #[ NEmptyExpression ]; }
|
|
|
|
if ( #e3 == null) { #e3 = #[ NEmptyExpression ]; }
|
|
|
|
## = #( #[LITERAL_for, "for"], #e1, #e2, #e3, #s );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Jump statements:
|
|
|
|
|
|
|
|
| "goto"^ expr SEMI!
|
|
|
|
| "continue" SEMI!
|
|
|
|
| "break" SEMI!
|
|
|
|
| "return"^ ( expr )? SEMI!
|
|
|
|
|
|
|
|
|
|
|
|
| ID COLON! (options {warnWhenFollowAmbig=false;}: statement)? { ## = #( #[NLabel], ## ); }
|
|
|
|
// GNU allows range expressions in case statements
|
|
|
|
| "case"^ ((constExpr VARARGS)=> rangeExpr | constExpr) COLON! ( options{warnWhenFollowAmbig=false;}:statement )?
|
|
|
|
| "default"^ COLON! ( options{warnWhenFollowAmbig=false;}: statement )?
|
|
|
|
|
|
|
|
// Selection statements:
|
|
|
|
|
|
|
|
| "if"^
|
|
|
|
LPAREN! expr RPAREN! statement
|
|
|
|
( //standard if-else ambiguity
|
|
|
|
options {
|
|
|
|
warnWhenFollowAmbig = false;
|
|
|
|
} :
|
|
|
|
"else" statement )?
|
|
|
|
| "switch"^ LPAREN! expr RPAREN! statement
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
2005-09-25 16:11:32 +02:00
|
|
|
conditionalExpr :logicalOrExpr
|
|
|
|
( QUESTION^ (expr)? COLON conditionalExpr )?
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
2005-09-25 16:11:32 +02:00
|
|
|
rangeExpr :constExpr VARARGS constExpr
|
|
|
|
{ ## = #(#[NRangeExpr], ##); }
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
2005-09-25 16:11:32 +02:00
|
|
|
castExpr :( LPAREN typeName RPAREN )=>
|
|
|
|
LPAREN^ typeName RPAREN ( castExpr | lcurlyInitializer )
|
|
|
|
{ ##.setType(NCast); }
|
|
|
|
|
|
|
|
| unaryExpr
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
2005-09-25 16:11:32 +02:00
|
|
|
nonemptyAbstractDeclarator :(
|
|
|
|
pointerGroup
|
|
|
|
( (LPAREN
|
|
|
|
( nonemptyAbstractDeclarator
|
|
|
|
| parameterTypeList
|
|
|
|
)?
|
|
|
|
( COMMA! )?
|
|
|
|
RPAREN)
|
|
|
|
| (LBRACKET (expr)? RBRACKET)
|
|
|
|
)*
|
|
|
|
|
|
|
|
| ( (LPAREN
|
|
|
|
( nonemptyAbstractDeclarator
|
|
|
|
| parameterTypeList
|
|
|
|
)?
|
|
|
|
( COMMA! )?
|
|
|
|
RPAREN)
|
|
|
|
| (LBRACKET (expr)? RBRACKET)
|
|
|
|
)+
|
|
|
|
)
|
|
|
|
{ ## = #( #[NNonemptyAbstractDeclarator], ## ); }
|
|
|
|
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
2005-09-25 16:11:32 +02:00
|
|
|
unaryExpr :postfixExpr
|
|
|
|
| INC^ castExpr
|
|
|
|
| DEC^ castExpr
|
|
|
|
| u:unaryOperator castExpr { ## = #( #[NUnaryExpr], ## ); }
|
|
|
|
|
|
|
|
| "sizeof"^
|
|
|
|
( ( LPAREN typeName )=> LPAREN typeName RPAREN
|
|
|
|
| unaryExpr
|
|
|
|
)
|
|
|
|
| "__alignof"^
|
|
|
|
( ( LPAREN typeName )=> LPAREN typeName RPAREN
|
|
|
|
| unaryExpr
|
|
|
|
)
|
|
|
|
| gnuAsmExpr
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
2005-09-25 16:11:32 +02:00
|
|
|
unaryOperator :BAND
|
|
|
|
| STAR
|
|
|
|
| PLUS
|
|
|
|
| MINUS
|
|
|
|
| BNOT //also stands for complex conjugation
|
|
|
|
| LNOT
|
|
|
|
| LAND //for label dereference (&&label)
|
|
|
|
| "__real"
|
|
|
|
| "__imag"
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
2005-09-25 16:11:32 +02:00
|
|
|
gnuAsmExpr :"asm"^ ("volatile")?
|
|
|
|
LPAREN stringConst
|
|
|
|
( options { warnWhenFollowAmbig = false; }:
|
|
|
|
COLON (strOptExprPair ( COMMA strOptExprPair)* )?
|
|
|
|
( options { warnWhenFollowAmbig = false; }:
|
|
|
|
COLON (strOptExprPair ( COMMA strOptExprPair)* )?
|
|
|
|
)?
|
|
|
|
)?
|
|
|
|
( COLON stringConst ( COMMA stringConst)* )?
|
|
|
|
RPAREN
|
|
|
|
{ ##.setType(NGnuAsmExpr); }
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
2005-09-25 16:11:32 +02:00
|
|
|
strOptExprPair :stringConst ( LPAREN expr RPAREN )?
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
2005-09-25 16:11:32 +02:00
|
|
|
primaryExpr :ID
|
|
|
|
| Number
|
|
|
|
| charConst
|
|
|
|
| stringConst
|
|
|
|
// JTC:
|
|
|
|
// ID should catch the enumerator
|
|
|
|
// leaving it in gives ambiguous err
|
|
|
|
// | enumerator
|
|
|
|
| (LPAREN LCURLY) => LPAREN^ compoundStatement[getAScopeName()] RPAREN
|
|
|
|
| LPAREN^ expr RPAREN { ##.setType(NExpressionGroup); }
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
|
|
|
// inherited from grammar StdCParser
|
2005-09-25 16:11:32 +02:00
|
|
|
externalList :( externalDef )+
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
|
|
|
// inherited from grammar StdCParser
|
|
|
|
declSpecifiers { int specCount=0; }
|
2005-09-25 16:11:32 +02:00
|
|
|
:( options { // this loop properly aborts when
|
|
|
|
// it finds a non-typedefName ID MBZ
|
|
|
|
warnWhenFollowAmbig = false;
|
|
|
|
} :
|
|
|
|
s:storageClassSpecifier
|
|
|
|
| typeQualifier
|
|
|
|
| ( "struct" | "union" | "enum" | typeSpecifier[specCount] )=>
|
|
|
|
specCount = typeSpecifier[specCount]
|
|
|
|
)+
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
|
|
|
// inherited from grammar StdCParser
|
2005-09-25 16:11:32 +02:00
|
|
|
storageClassSpecifier :"auto"
|
|
|
|
| "register"
|
|
|
|
| "typedef"
|
|
|
|
| functionStorageClassSpecifier
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
|
|
|
// inherited from grammar StdCParser
|
2005-09-25 16:11:32 +02:00
|
|
|
typeQualifier :"const"
|
|
|
|
| "volatile"
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
|
|
|
// inherited from grammar StdCParser
|
2005-09-25 16:11:32 +02:00
|
|
|
typedefName :{ isTypedefName ( LT(1).getText() ) }?
|
|
|
|
i:ID { ## = #(#[NTypedefName], #i); }
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
|
|
|
// inherited from grammar StdCParser
|
2005-09-25 16:11:32 +02:00
|
|
|
structOrUnion :"struct"
|
|
|
|
| "union"
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
|
|
|
// inherited from grammar StdCParser
|
2005-09-25 16:11:32 +02:00
|
|
|
structDeclarationList :( structDeclaration )+
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
|
|
|
// inherited from grammar StdCParser
|
|
|
|
specifierQualifierList { int specCount = 0; }
|
2005-09-25 16:11:32 +02:00
|
|
|
:( options { // this loop properly aborts when
|
|
|
|
// it finds a non-typedefName ID MBZ
|
|
|
|
warnWhenFollowAmbig = false;
|
|
|
|
} :
|
|
|
|
( "struct" | "union" | "enum" | typeSpecifier[specCount] )=>
|
|
|
|
specCount = typeSpecifier[specCount]
|
|
|
|
| typeQualifier
|
|
|
|
)+
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
|
|
|
// inherited from grammar StdCParser
|
2005-09-25 16:11:32 +02:00
|
|
|
enumerator[String enumName] :i:ID { symbolTable.add( i.getText(),
|
|
|
|
#( null,
|
|
|
|
#[LITERAL_enum, "enum"],
|
|
|
|
#[ ID, enumName]
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
(ASSIGN constExpr)?
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
|
|
|
// inherited from grammar StdCParser
|
2005-09-25 16:11:32 +02:00
|
|
|
pointerGroup :( STAR ( typeQualifier )* )+ { ## = #( #[NPointerGroup], ##); }
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
|
|
|
// inherited from grammar StdCParser
|
|
|
|
parameterDeclaration { String declName; }
|
2005-09-25 16:11:32 +02:00
|
|
|
:ds:declSpecifiers
|
|
|
|
( ( declarator[false] )=> declName = d:declarator[false]
|
|
|
|
{
|
|
|
|
AST d2, ds2;
|
|
|
|
d2 = astFactory.dupList(#d);
|
|
|
|
ds2 = astFactory.dupList(#ds);
|
|
|
|
symbolTable.add(declName, #(null, ds2, d2));
|
|
|
|
}
|
|
|
|
| nonemptyAbstractDeclarator
|
|
|
|
)?
|
|
|
|
{
|
|
|
|
## = #( #[NParameterDeclaration], ## );
|
|
|
|
}
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
|
|
|
// inherited from grammar StdCParser
|
|
|
|
functionDef { String declName; }
|
2005-09-25 16:11:32 +02:00
|
|
|
:( (functionDeclSpecifiers)=> ds:functionDeclSpecifiers
|
|
|
|
| //epsilon
|
|
|
|
)
|
|
|
|
declName = d:declarator[true]
|
|
|
|
{
|
|
|
|
AST d2, ds2;
|
|
|
|
d2 = astFactory.dupList(#d);
|
|
|
|
ds2 = astFactory.dupList(#ds);
|
|
|
|
symbolTable.add(declName, #(null, ds2, d2));
|
|
|
|
pushScope(declName);
|
|
|
|
}
|
|
|
|
( declaration )* (VARARGS)? ( SEMI! )*
|
|
|
|
{ popScope(); }
|
|
|
|
compoundStatement[declName]
|
|
|
|
{ ## = #( #[NFunctionDef], ## );}
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
|
|
|
// inherited from grammar StdCParser
|
|
|
|
functionDeclSpecifiers { int specCount = 0; }
|
2005-09-25 16:11:32 +02:00
|
|
|
:( options { // this loop properly aborts when
|
|
|
|
// it finds a non-typedefName ID MBZ
|
|
|
|
warnWhenFollowAmbig = false;
|
|
|
|
} :
|
|
|
|
functionStorageClassSpecifier
|
|
|
|
| typeQualifier
|
|
|
|
| ( "struct" | "union" | "enum" | typeSpecifier[specCount] )=>
|
|
|
|
specCount = typeSpecifier[specCount]
|
|
|
|
)+
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
|
|
|
// inherited from grammar StdCParser
|
2005-09-25 16:11:32 +02:00
|
|
|
declarationPredictor :(options { //only want to look at declaration if I don't see typedef
|
|
|
|
warnWhenFollowAmbig = false;
|
|
|
|
}:
|
|
|
|
"typedef"
|
|
|
|
| declaration
|
|
|
|
)
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
|
|
|
// inherited from grammar StdCParser
|
2005-09-25 16:11:32 +02:00
|
|
|
statementList :( statement )+
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
|
|
|
// inherited from grammar StdCParser
|
2005-09-25 16:11:32 +02:00
|
|
|
expr :assignExpr (options {
|
|
|
|
/* MBZ:
|
|
|
|
COMMA is ambiguous between comma expressions and
|
|
|
|
argument lists. argExprList should get priority,
|
|
|
|
and it does by being deeper in the expr rule tree
|
|
|
|
and using (COMMA assignExpr)*
|
|
|
|
*/
|
|
|
|
warnWhenFollowAmbig = false;
|
|
|
|
} :
|
|
|
|
c:COMMA^ { #c.setType(NCommaExpr); } assignExpr
|
|
|
|
)*
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
|
|
|
// inherited from grammar StdCParser
|
2005-09-25 16:11:32 +02:00
|
|
|
assignExpr :conditionalExpr ( a:assignOperator! assignExpr { ## = #( #a, ## );} )?
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
|
|
|
// inherited from grammar StdCParser
|
2005-09-25 16:11:32 +02:00
|
|
|
assignOperator :ASSIGN
|
|
|
|
| DIV_ASSIGN
|
|
|
|
| PLUS_ASSIGN
|
|
|
|
| MINUS_ASSIGN
|
|
|
|
| STAR_ASSIGN
|
|
|
|
| MOD_ASSIGN
|
|
|
|
| RSHIFT_ASSIGN
|
|
|
|
| LSHIFT_ASSIGN
|
|
|
|
| BAND_ASSIGN
|
|
|
|
| BOR_ASSIGN
|
|
|
|
| BXOR_ASSIGN
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
|
|
|
// inherited from grammar StdCParser
|
2005-09-25 16:11:32 +02:00
|
|
|
constExpr :conditionalExpr
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
|
|
|
// inherited from grammar StdCParser
|
2005-09-25 16:11:32 +02:00
|
|
|
logicalOrExpr :logicalAndExpr ( LOR^ logicalAndExpr )*
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
|
|
|
// inherited from grammar StdCParser
|
2005-09-25 16:11:32 +02:00
|
|
|
logicalAndExpr :inclusiveOrExpr ( LAND^ inclusiveOrExpr )*
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
|
|
|
// inherited from grammar StdCParser
|
2005-09-25 16:11:32 +02:00
|
|
|
inclusiveOrExpr :exclusiveOrExpr ( BOR^ exclusiveOrExpr )*
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
|
|
|
// inherited from grammar StdCParser
|
2005-09-25 16:11:32 +02:00
|
|
|
exclusiveOrExpr :bitAndExpr ( BXOR^ bitAndExpr )*
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
|
|
|
// inherited from grammar StdCParser
|
2005-09-25 16:11:32 +02:00
|
|
|
bitAndExpr :equalityExpr ( BAND^ equalityExpr )*
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
|
|
|
// inherited from grammar StdCParser
|
2005-09-25 16:11:32 +02:00
|
|
|
equalityExpr :relationalExpr
|
|
|
|
( ( EQUAL^ | NOT_EQUAL^ ) relationalExpr )*
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
|
|
|
// inherited from grammar StdCParser
|
2005-09-25 16:11:32 +02:00
|
|
|
relationalExpr :shiftExpr
|
|
|
|
( ( LT^ | LTE^ | GT^ | GTE^ ) shiftExpr )*
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
|
|
|
// inherited from grammar StdCParser
|
2005-09-25 16:11:32 +02:00
|
|
|
shiftExpr :additiveExpr
|
|
|
|
( ( LSHIFT^ | RSHIFT^ ) additiveExpr )*
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
|
|
|
// inherited from grammar StdCParser
|
2005-09-25 16:11:32 +02:00
|
|
|
additiveExpr :multExpr
|
|
|
|
( ( PLUS^ | MINUS^ ) multExpr )*
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
|
|
|
// inherited from grammar StdCParser
|
2005-09-25 16:11:32 +02:00
|
|
|
multExpr :castExpr
|
|
|
|
( ( STAR^ | DIV^ | MOD^ ) castExpr )*
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
|
|
|
// inherited from grammar StdCParser
|
2005-09-25 16:11:32 +02:00
|
|
|
typeName :specifierQualifierList (nonemptyAbstractDeclarator)?
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
|
|
|
// inherited from grammar StdCParser
|
2005-09-25 16:11:32 +02:00
|
|
|
postfixExpr :primaryExpr
|
|
|
|
(
|
|
|
|
postfixSuffix {## = #( #[NPostfixExpr], ## );}
|
|
|
|
)?
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
|
|
|
// inherited from grammar StdCParser
|
2005-09-25 16:11:32 +02:00
|
|
|
postfixSuffix :( PTR ID
|
|
|
|
| DOT ID
|
|
|
|
| functionCall
|
|
|
|
| LBRACKET expr RBRACKET
|
|
|
|
| INC
|
|
|
|
| DEC
|
|
|
|
)+
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
|
|
|
// inherited from grammar StdCParser
|
2005-09-25 16:11:32 +02:00
|
|
|
functionCall :LPAREN^ (a:argExprList)? RPAREN
|
|
|
|
{
|
|
|
|
##.setType( NFunctionCallArgs );
|
|
|
|
}
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
|
|
|
// inherited from grammar StdCParser
|
2005-09-25 16:11:32 +02:00
|
|
|
argExprList :assignExpr ( COMMA! assignExpr )*
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
|
|
|
// inherited from grammar StdCParser
|
2005-09-25 16:11:32 +02:00
|
|
|
protected charConst :CharLiteral
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
|
|
|
// inherited from grammar StdCParser
|
2005-09-25 16:11:32 +02:00
|
|
|
protected stringConst :(StringLiteral)+ { ## = #(#[NStringSeq], ##); }
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
|
|
|
// inherited from grammar StdCParser
|
2005-09-25 16:11:32 +02:00
|
|
|
protected intConst :IntOctalConst
|
|
|
|
| LongOctalConst
|
|
|
|
| UnsignedOctalConst
|
|
|
|
| IntIntConst
|
|
|
|
| LongIntConst
|
|
|
|
| UnsignedIntConst
|
|
|
|
| IntHexConst
|
|
|
|
| LongHexConst
|
|
|
|
| UnsignedHexConst
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
|
|
|
// inherited from grammar StdCParser
|
2005-09-25 16:11:32 +02:00
|
|
|
protected floatConst :FloatDoubleConst
|
|
|
|
| DoubleDoubleConst
|
|
|
|
| LongDoubleConst
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
|
|
|
// inherited from grammar StdCParser
|
2005-09-25 16:11:32 +02:00
|
|
|
dummy :NTypedefName
|
|
|
|
| NInitDecl
|
|
|
|
| NDeclarator
|
|
|
|
| NStructDeclarator
|
|
|
|
| NDeclaration
|
|
|
|
| NCast
|
|
|
|
| NPointerGroup
|
|
|
|
| NExpressionGroup
|
|
|
|
| NFunctionCallArgs
|
|
|
|
| NNonemptyAbstractDeclarator
|
|
|
|
| NInitializer
|
|
|
|
| NStatementExpr
|
|
|
|
| NEmptyExpression
|
|
|
|
| NParameterTypeList
|
|
|
|
| NFunctionDef
|
|
|
|
| NCompoundStatement
|
|
|
|
| NParameterDeclaration
|
|
|
|
| NCommaExpr
|
|
|
|
| NUnaryExpr
|
|
|
|
| NLabel
|
|
|
|
| NPostfixExpr
|
|
|
|
| NRangeExpr
|
|
|
|
| NStringSeq
|
|
|
|
| NInitializerElementLabel
|
|
|
|
| NLcurlyInitializer
|
|
|
|
| NAsmAttribute
|
|
|
|
| NGnuAsmExpr
|
|
|
|
| NTypeMissing
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
2005-09-25 16:11:32 +02:00
|
|
|
{
|
|
|
|
// import CToken;
|
|
|
|
import java.io.*;
|
|
|
|
// import LineObject;
|
|
|
|
import antlr.*;
|
2005-10-06 19:18:26 +02:00
|
|
|
}class WLexer extends Lexer;
|
|
|
|
|
|
|
|
options {
|
|
|
|
k= 3;
|
|
|
|
importVocab= W;
|
|
|
|
testLiterals= false;
|
|
|
|
}
|
|
|
|
|
2005-09-25 16:11:32 +02:00
|
|
|
tokens {
|
|
|
|
LITERAL___extension__ = "__extension__";
|
|
|
|
}
|
|
|
|
{
|
|
|
|
public void initialize(String src)
|
|
|
|
{
|
|
|
|
setOriginalSource(src);
|
|
|
|
initialize();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void initialize()
|
|
|
|
{
|
|
|
|
literals.put(new ANTLRHashString("__alignof__", this), new Integer(LITERAL___alignof));
|
|
|
|
literals.put(new ANTLRHashString("__asm", this), new Integer(LITERAL_asm));
|
|
|
|
literals.put(new ANTLRHashString("__asm__", this), new Integer(LITERAL_asm));
|
|
|
|
literals.put(new ANTLRHashString("__attribute__", this), new Integer(LITERAL___attribute));
|
|
|
|
literals.put(new ANTLRHashString("__complex__", this), new Integer(LITERAL___complex));
|
|
|
|
literals.put(new ANTLRHashString("__const", this), new Integer(LITERAL_const));
|
|
|
|
literals.put(new ANTLRHashString("__const__", this), new Integer(LITERAL_const));
|
|
|
|
literals.put(new ANTLRHashString("__imag__", this), new Integer(LITERAL___imag));
|
|
|
|
literals.put(new ANTLRHashString("__inline", this), new Integer(LITERAL_inline));
|
|
|
|
literals.put(new ANTLRHashString("__inline__", this), new Integer(LITERAL_inline));
|
|
|
|
literals.put(new ANTLRHashString("__real__", this), new Integer(LITERAL___real));
|
|
|
|
literals.put(new ANTLRHashString("__signed", this), new Integer(LITERAL_signed));
|
|
|
|
literals.put(new ANTLRHashString("__signed__", this), new Integer(LITERAL_signed));
|
|
|
|
literals.put(new ANTLRHashString("__typeof", this), new Integer(LITERAL_typeof));
|
|
|
|
literals.put(new ANTLRHashString("__typeof__", this), new Integer(LITERAL_typeof));
|
|
|
|
literals.put(new ANTLRHashString("__volatile", this), new Integer(LITERAL_volatile));
|
|
|
|
literals.put(new ANTLRHashString("__volatile__", this), new Integer(LITERAL_volatile));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
LineObject lineObject = new LineObject();
|
|
|
|
String originalSource = "";
|
|
|
|
PreprocessorInfoChannel preprocessorInfoChannel = new PreprocessorInfoChannel();
|
|
|
|
int tokenNumber = 0;
|
|
|
|
boolean countingTokens = true;
|
|
|
|
int deferredLineCount = 0;
|
|
|
|
|
|
|
|
public void setCountingTokens(boolean ct)
|
|
|
|
{
|
|
|
|
countingTokens = ct;
|
|
|
|
if ( countingTokens ) {
|
|
|
|
tokenNumber = 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
tokenNumber = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setOriginalSource(String src)
|
|
|
|
{
|
|
|
|
originalSource = src;
|
|
|
|
lineObject.setSource(src);
|
|
|
|
}
|
|
|
|
public void setSource(String src)
|
|
|
|
{
|
|
|
|
lineObject.setSource(src);
|
|
|
|
}
|
|
|
|
|
|
|
|
public PreprocessorInfoChannel getPreprocessorInfoChannel()
|
|
|
|
{
|
|
|
|
return preprocessorInfoChannel;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setPreprocessingDirective(String pre)
|
|
|
|
{
|
|
|
|
preprocessorInfoChannel.addLineForTokenNumber( pre, new Integer(tokenNumber) );
|
|
|
|
}
|
|
|
|
|
|
|
|
protected Token makeToken(int t)
|
|
|
|
{
|
|
|
|
if ( t != Token.SKIP && countingTokens) {
|
|
|
|
tokenNumber++;
|
|
|
|
}
|
|
|
|
CToken tok = (CToken) super.makeToken(t);
|
|
|
|
tok.setLine(lineObject.line);
|
|
|
|
tok.setSource(lineObject.source);
|
|
|
|
tok.setTokenNumber(tokenNumber);
|
|
|
|
|
|
|
|
lineObject.line += deferredLineCount;
|
|
|
|
deferredLineCount = 0;
|
|
|
|
return tok;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void deferredNewline() {
|
|
|
|
deferredLineCount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void newline() {
|
|
|
|
lineObject.newline();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2005-10-06 19:18:26 +02:00
|
|
|
}
|
2005-09-25 16:11:32 +02:00
|
|
|
Whitespace :( ( ' ' | '\t' | '\014')
|
|
|
|
| "\r\n" { newline(); }
|
|
|
|
| ( '\n' | '\r' ) { newline(); }
|
|
|
|
) { _ttype = Token.SKIP; }
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
2005-09-25 16:11:32 +02:00
|
|
|
protected Escape :'\\'
|
|
|
|
( options{warnWhenFollowAmbig=false;}:
|
|
|
|
~('0'..'7' | 'x')
|
|
|
|
| ('0'..'3') ( options{warnWhenFollowAmbig=false;}: Digit )*
|
|
|
|
| ('4'..'7') ( options{warnWhenFollowAmbig=false;}: Digit )*
|
|
|
|
| 'x' ( options{warnWhenFollowAmbig=false;}: Digit | 'a'..'f' | 'A'..'F' )+
|
|
|
|
)
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
2005-09-25 16:11:32 +02:00
|
|
|
protected IntSuffix :'L'
|
|
|
|
| 'l'
|
|
|
|
| 'U'
|
|
|
|
| 'u'
|
|
|
|
| 'I'
|
|
|
|
| 'i'
|
|
|
|
| 'J'
|
|
|
|
| 'j'
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
2005-09-25 16:11:32 +02:00
|
|
|
protected NumberSuffix :IntSuffix
|
|
|
|
| 'F'
|
|
|
|
| 'f'
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
2005-09-25 16:11:32 +02:00
|
|
|
Number :( ( Digit )+ ( '.' | 'e' | 'E' ) )=> ( Digit )+
|
|
|
|
( '.' ( Digit )* ( Exponent )?
|
|
|
|
| Exponent
|
|
|
|
)
|
|
|
|
( NumberSuffix
|
|
|
|
)*
|
|
|
|
|
|
|
|
| ( "..." )=> "..." { _ttype = VARARGS; }
|
|
|
|
|
|
|
|
| '.' { _ttype = DOT; }
|
|
|
|
( ( Digit )+ ( Exponent )?
|
|
|
|
{ _ttype = Number; }
|
|
|
|
( NumberSuffix
|
|
|
|
)*
|
|
|
|
)?
|
|
|
|
|
|
|
|
| '0' ( '0'..'7' )*
|
|
|
|
( NumberSuffix
|
|
|
|
)*
|
|
|
|
|
|
|
|
| '1'..'9' ( Digit )*
|
|
|
|
( NumberSuffix
|
|
|
|
)*
|
|
|
|
|
|
|
|
| '0' ( 'x' | 'X' ) ( 'a'..'f' | 'A'..'F' | Digit )+
|
|
|
|
( IntSuffix
|
|
|
|
)*
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
2005-09-25 16:11:32 +02:00
|
|
|
IDMEAT :i:ID {
|
|
|
|
|
|
|
|
if ( i.getType() == LITERAL___extension__ ) {
|
|
|
|
$setType(Token.SKIP);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$setType(i.getType());
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
|
|
|
protected ID
|
|
|
|
options {
|
|
|
|
testLiterals= true;
|
|
|
|
}
|
2005-09-25 16:11:32 +02:00
|
|
|
:( 'a'..'z' | 'A'..'Z' | '_' | '$')
|
|
|
|
( 'a'..'z' | 'A'..'Z' | '_' | '$' | '0'..'9' )*
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
2005-09-25 16:11:32 +02:00
|
|
|
WideCharLiteral :'L' CharLiteral
|
|
|
|
{ $setType(CharLiteral); }
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
2005-09-25 16:11:32 +02:00
|
|
|
WideStringLiteral :'L' StringLiteral
|
|
|
|
{ $setType(StringLiteral); }
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
2005-09-25 16:11:32 +02:00
|
|
|
StringLiteral :'"'
|
|
|
|
( ('\\' ~('\n'))=> Escape
|
|
|
|
| ( '\r' { newline(); }
|
|
|
|
| '\n' {
|
|
|
|
newline();
|
|
|
|
}
|
|
|
|
| '\\' '\n' {
|
|
|
|
newline();
|
|
|
|
}
|
|
|
|
)
|
|
|
|
| ~( '"' | '\r' | '\n' | '\\' )
|
|
|
|
)*
|
|
|
|
'"'
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
|
|
|
// inherited from grammar StdCLexer
|
2005-09-25 16:11:32 +02:00
|
|
|
protected Vocabulary :'\3'..'\377'
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
|
|
|
// inherited from grammar StdCLexer
|
|
|
|
ASSIGN :'=' ;
|
|
|
|
|
|
|
|
// inherited from grammar StdCLexer
|
|
|
|
COLON :':' ;
|
|
|
|
|
|
|
|
// inherited from grammar StdCLexer
|
|
|
|
COMMA :',' ;
|
|
|
|
|
|
|
|
// inherited from grammar StdCLexer
|
|
|
|
QUESTION :'?' ;
|
|
|
|
|
|
|
|
// inherited from grammar StdCLexer
|
|
|
|
SEMI :';' ;
|
|
|
|
|
|
|
|
// inherited from grammar StdCLexer
|
|
|
|
PTR :"->" ;
|
|
|
|
|
|
|
|
// inherited from grammar StdCLexer
|
|
|
|
protected DOT :;
|
|
|
|
|
|
|
|
// inherited from grammar StdCLexer
|
|
|
|
protected VARARGS :;
|
|
|
|
|
|
|
|
// inherited from grammar StdCLexer
|
|
|
|
LPAREN :'(' ;
|
|
|
|
|
|
|
|
// inherited from grammar StdCLexer
|
|
|
|
RPAREN :')' ;
|
|
|
|
|
|
|
|
// inherited from grammar StdCLexer
|
|
|
|
LBRACKET :'[' ;
|
|
|
|
|
|
|
|
// inherited from grammar StdCLexer
|
|
|
|
RBRACKET :']' ;
|
|
|
|
|
|
|
|
// inherited from grammar StdCLexer
|
|
|
|
LCURLY :'{' ;
|
|
|
|
|
|
|
|
// inherited from grammar StdCLexer
|
|
|
|
RCURLY :'}' ;
|
|
|
|
|
|
|
|
// inherited from grammar StdCLexer
|
|
|
|
EQUAL :"==" ;
|
|
|
|
|
|
|
|
// inherited from grammar StdCLexer
|
|
|
|
NOT_EQUAL :"!=" ;
|
|
|
|
|
|
|
|
// inherited from grammar StdCLexer
|
|
|
|
LTE :"<=" ;
|
|
|
|
|
|
|
|
// inherited from grammar StdCLexer
|
|
|
|
LT :"<" ;
|
|
|
|
|
|
|
|
// inherited from grammar StdCLexer
|
|
|
|
GTE :">=" ;
|
|
|
|
|
|
|
|
// inherited from grammar StdCLexer
|
|
|
|
GT :">" ;
|
|
|
|
|
|
|
|
// inherited from grammar StdCLexer
|
|
|
|
DIV :'/' ;
|
|
|
|
|
|
|
|
// inherited from grammar StdCLexer
|
|
|
|
DIV_ASSIGN :"/=" ;
|
|
|
|
|
|
|
|
// inherited from grammar StdCLexer
|
|
|
|
PLUS :'+' ;
|
|
|
|
|
|
|
|
// inherited from grammar StdCLexer
|
|
|
|
PLUS_ASSIGN :"+=" ;
|
|
|
|
|
|
|
|
// inherited from grammar StdCLexer
|
|
|
|
INC :"++" ;
|
|
|
|
|
|
|
|
// inherited from grammar StdCLexer
|
|
|
|
MINUS :'-' ;
|
|
|
|
|
|
|
|
// inherited from grammar StdCLexer
|
|
|
|
MINUS_ASSIGN :"-=" ;
|
|
|
|
|
|
|
|
// inherited from grammar StdCLexer
|
|
|
|
DEC :"--" ;
|
|
|
|
|
|
|
|
// inherited from grammar StdCLexer
|
|
|
|
STAR :'*' ;
|
|
|
|
|
|
|
|
// inherited from grammar StdCLexer
|
|
|
|
STAR_ASSIGN :"*=" ;
|
|
|
|
|
|
|
|
// inherited from grammar StdCLexer
|
|
|
|
MOD :'%' ;
|
|
|
|
|
|
|
|
// inherited from grammar StdCLexer
|
|
|
|
MOD_ASSIGN :"%=" ;
|
|
|
|
|
|
|
|
// inherited from grammar StdCLexer
|
|
|
|
RSHIFT :">>" ;
|
|
|
|
|
|
|
|
// inherited from grammar StdCLexer
|
|
|
|
RSHIFT_ASSIGN :">>=" ;
|
|
|
|
|
|
|
|
// inherited from grammar StdCLexer
|
|
|
|
LSHIFT :"<<" ;
|
|
|
|
|
|
|
|
// inherited from grammar StdCLexer
|
|
|
|
LSHIFT_ASSIGN :"<<=" ;
|
|
|
|
|
|
|
|
// inherited from grammar StdCLexer
|
|
|
|
LAND :"&&" ;
|
|
|
|
|
|
|
|
// inherited from grammar StdCLexer
|
|
|
|
LNOT :'!' ;
|
|
|
|
|
|
|
|
// inherited from grammar StdCLexer
|
|
|
|
LOR :"||" ;
|
|
|
|
|
|
|
|
// inherited from grammar StdCLexer
|
|
|
|
BAND :'&' ;
|
|
|
|
|
|
|
|
// inherited from grammar StdCLexer
|
|
|
|
BAND_ASSIGN :"&=" ;
|
|
|
|
|
|
|
|
// inherited from grammar StdCLexer
|
|
|
|
BNOT :'~' ;
|
|
|
|
|
|
|
|
// inherited from grammar StdCLexer
|
|
|
|
BOR :'|' ;
|
|
|
|
|
|
|
|
// inherited from grammar StdCLexer
|
|
|
|
BOR_ASSIGN :"|=" ;
|
|
|
|
|
|
|
|
// inherited from grammar StdCLexer
|
|
|
|
BXOR :'^' ;
|
|
|
|
|
|
|
|
// inherited from grammar StdCLexer
|
|
|
|
BXOR_ASSIGN :"^=" ;
|
|
|
|
|
|
|
|
// inherited from grammar StdCLexer
|
2005-09-25 16:11:32 +02:00
|
|
|
Comment :"/*"
|
|
|
|
( { LA(2) != '/' }? '*'
|
|
|
|
| "\r\n" { deferredNewline(); }
|
|
|
|
| ( '\r' | '\n' ) { deferredNewline(); }
|
|
|
|
| ~( '*'| '\r' | '\n' )
|
|
|
|
)*
|
|
|
|
"*/" { _ttype = Token.SKIP;
|
|
|
|
}
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
|
|
|
// inherited from grammar StdCLexer
|
2005-09-25 16:11:32 +02:00
|
|
|
CPPComment :"//" ( ~('\n') )*
|
|
|
|
{
|
|
|
|
_ttype = Token.SKIP;
|
|
|
|
}
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
|
|
|
// inherited from grammar StdCLexer
|
|
|
|
PREPROC_DIRECTIVE
|
|
|
|
options {
|
|
|
|
paraphrase= "a line directive";
|
|
|
|
}
|
2005-09-25 16:11:32 +02:00
|
|
|
:'#'
|
|
|
|
( ( "line" || (( ' ' | '\t' | '\014')+ '0'..'9')) => LineDirective
|
|
|
|
| (~'\n')* { setPreprocessingDirective(getText()); }
|
|
|
|
)
|
|
|
|
{
|
|
|
|
_ttype = Token.SKIP;
|
|
|
|
}
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
|
|
|
// inherited from grammar StdCLexer
|
2005-09-25 16:11:32 +02:00
|
|
|
protected Space :( ' ' | '\t' | '\014')
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
|
|
|
// inherited from grammar StdCLexer
|
2005-09-25 16:11:32 +02:00
|
|
|
protected LineDirective {
|
|
|
|
boolean oldCountingTokens = countingTokens;
|
|
|
|
countingTokens = false;
|
2005-10-06 19:18:26 +02:00
|
|
|
}
|
2005-09-25 16:11:32 +02:00
|
|
|
:{
|
|
|
|
lineObject = new LineObject();
|
|
|
|
deferredLineCount = 0;
|
|
|
|
}
|
|
|
|
("line")? //this would be for if the directive started "#line", but not there for GNU directives
|
|
|
|
(Space)+
|
|
|
|
n:Number { lineObject.setLine(Integer.parseInt(n.getText())); }
|
|
|
|
(Space)+
|
|
|
|
( fn:StringLiteral { try {
|
|
|
|
lineObject.setSource(fn.getText().substring(1,fn.getText().length()-1));
|
|
|
|
}
|
|
|
|
catch (StringIndexOutOfBoundsException e) { /*not possible*/ }
|
|
|
|
}
|
|
|
|
| fi:ID { lineObject.setSource(fi.getText()); }
|
|
|
|
)?
|
|
|
|
(Space)*
|
|
|
|
("1" { lineObject.setEnteringFile(true); } )?
|
|
|
|
(Space)*
|
|
|
|
("2" { lineObject.setReturningToFile(true); } )?
|
|
|
|
(Space)*
|
|
|
|
("3" { lineObject.setSystemHeader(true); } )?
|
|
|
|
(Space)*
|
|
|
|
("4" { lineObject.setTreatAsC(true); } )?
|
|
|
|
(~('\r' | '\n'))*
|
|
|
|
("\r\n" | "\r" | "\n")
|
|
|
|
{
|
|
|
|
preprocessorInfoChannel.addLineForTokenNumber(new LineObject(lineObject), new Integer(tokenNumber));
|
|
|
|
countingTokens = oldCountingTokens;
|
|
|
|
}
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
|
|
|
// inherited from grammar StdCLexer
|
2005-09-25 16:11:32 +02:00
|
|
|
CharLiteral :'\'' ( Escape | ~( '\'' ) ) '\''
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
|
|
|
// inherited from grammar StdCLexer
|
2005-09-25 16:11:32 +02:00
|
|
|
protected BadStringLiteral :// Imaginary token.
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
|
|
|
// inherited from grammar StdCLexer
|
2005-09-25 16:11:32 +02:00
|
|
|
protected Digit :'0'..'9'
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
|
|
|
// inherited from grammar StdCLexer
|
2005-09-25 16:11:32 +02:00
|
|
|
protected LongSuffix :'l'
|
|
|
|
| 'L'
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
|
|
|
// inherited from grammar StdCLexer
|
2005-09-25 16:11:32 +02:00
|
|
|
protected UnsignedSuffix :'u'
|
|
|
|
| 'U'
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
|
|
|
// inherited from grammar StdCLexer
|
2005-09-25 16:11:32 +02:00
|
|
|
protected FloatSuffix :'f'
|
|
|
|
| 'F'
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
|
|
|
// inherited from grammar StdCLexer
|
2005-09-25 16:11:32 +02:00
|
|
|
protected Exponent :( 'e' | 'E' ) ( '+' | '-' )? ( Digit )+
|
2005-10-06 19:18:26 +02:00
|
|
|
;
|
|
|
|
|
|
|
|
// inherited from grammar StdCLexer
|
|
|
|
protected DoubleDoubleConst :;
|
|
|
|
|
|
|
|
// inherited from grammar StdCLexer
|
|
|
|
protected FloatDoubleConst :;
|
|
|
|
|
|
|
|
// inherited from grammar StdCLexer
|
|
|
|
protected LongDoubleConst :;
|
|
|
|
|
|
|
|
// inherited from grammar StdCLexer
|
|
|
|
protected IntOctalConst :;
|
|
|
|
|
|
|
|
// inherited from grammar StdCLexer
|
|
|
|
protected LongOctalConst :;
|
|
|
|
|
|
|
|
// inherited from grammar StdCLexer
|
|
|
|
protected UnsignedOctalConst :;
|
|
|
|
|
|
|
|
// inherited from grammar StdCLexer
|
|
|
|
protected IntIntConst :;
|
|
|
|
|
|
|
|
// inherited from grammar StdCLexer
|
|
|
|
protected LongIntConst :;
|
|
|
|
|
|
|
|
// inherited from grammar StdCLexer
|
|
|
|
protected UnsignedIntConst :;
|
|
|
|
|
|
|
|
// inherited from grammar StdCLexer
|
|
|
|
protected IntHexConst :;
|
|
|
|
|
|
|
|
// inherited from grammar StdCLexer
|
|
|
|
protected LongHexConst :;
|
|
|
|
|
|
|
|
// inherited from grammar StdCLexer
|
|
|
|
protected UnsignedHexConst :;
|
|
|
|
|
|
|
|
|