diff --git a/chapi-ast-c/src/main/antlr/C.g4 b/chapi-ast-c/src/main/antlr/C.g4 index e31a041e..2b99b123 100644 --- a/chapi-ast-c/src/main/antlr/C.g4 +++ b/chapi-ast-c/src/main/antlr/C.g4 @@ -168,6 +168,7 @@ assignmentExpression : conditionalExpression | unaryExpression assignmentOperator assignmentExpression | DigitSequence // for + | macroStatement ; assignmentOperator @@ -429,6 +430,8 @@ typedefName initializer : assignmentExpression | '{' initializerList ','? '}' +// | '#if' expressionStatement initializerList '#' 'endif' + | macroStatement ','? initializerList ','? macroStatement ; initializerList @@ -458,7 +461,8 @@ statement | macroStatement ; -normalStatement : labeledStatement +normalStatement + : labeledStatement | compoundStatement | expressionStatement | selectionStatement @@ -479,15 +483,21 @@ macroStatement singleLineMacroDeclaration : include (StringLiteral | Identifier | ('<' includeIdentifier '>' )) #includeDeclaration - | macroKeywords Identifier structOrUnionSpecifier #macroStructureDeclaration - | 'define' expressionStatement macroFunctionExpression #macroAliasDeclaration - | macroKeywords (expressionStatement)* - ('#' macroKeywords)? identifierList? #macroDefineDeclaration + | ('ifdef' | 'ifndef' | 'if') Identifier statement* ('#' 'else' statement*)? '#' 'endif' + #ifdefDeclaration + | 'define' Identifier expressionStatement #macroAssignDeclaration + | 'define' expressionStatement macroFunctionExpression #macroAliasDeclaration + | 'define' Identifier structOrUnionSpecifier #macroStructureDeclaration +// | macroKeywords (assignmentExpression)* +// ('#' macroKeywords)? identifierList? #macroDefineDeclaration | '#'? Identifier #macroCastDeclaration + | macroKeywords macroFunctionExpression? #macroStatementDeclaration ; macroFunctionExpression - : Identifier '(' ( parameterTypeList| (macroType (',' macroType)*) ) ')' + : Identifier + | Identifier '(' ( parameterTypeList| (macroType (',' macroType)*) ) ')' + | assignmentExpression ; macroType @@ -525,7 +535,8 @@ expressionStatement ; selectionStatement - : 'if' '(' expression ')' statement ('else' statement)? + // the macro may break the grammar, so statement is optional + : 'if' '(' expression ')' statement? ('else' statement)? | 'switch' '(' expression ')' statement ; @@ -992,10 +1003,6 @@ fragment IdentifierNondigit //| // other implementation-defined characters... ; -fragment UpperCase - : [A-Z_] - ; - fragment Nondigit : [a-zA-Z_] ; diff --git a/chapi-ast-c/src/test/resources/realworld/SlowMacro2.h b/chapi-ast-c/src/test/resources/realworld/SlowMacro2.h index 42962da8..25f03023 100644 --- a/chapi-ast-c/src/test/resources/realworld/SlowMacro2.h +++ b/chapi-ast-c/src/test/resources/realworld/SlowMacro2.h @@ -6,3 +6,17 @@ #define hashpointer(t,p) hashmod(t, IntPoint(p)) #define numints cast_int(sizeof(lua_Number)/sizeof(int)) #define dummynode (&dummynode_) + +#ifndef MINSTRTABSIZE +#define MINSTRTABSIZE 32 +#endif + +#ifndef LUA_MINBUFFER +#define LUA_MINBUFFER 32 +#endif + +#ifndef lua_lock +#define lua_lock(L) ((void) 0) +#define lua_unlock(L) ((void) 0) +#endif +