Skip to content

Commit

Permalink
feat(chapi-ast-c): add support for macro statements #24
Browse files Browse the repository at this point in the history
Add support for macro statements in the chapi-ast-c module. This includes adding the `macroStatement` rule to the `C.g4` grammar file and modifying the `SlowMacro2.h` file to define some preprocessor constants.
  • Loading branch information
phodal committed Jan 31, 2024
1 parent 8744a9d commit 879e24f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
29 changes: 18 additions & 11 deletions chapi-ast-c/src/main/antlr/C.g4
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ assignmentExpression
: conditionalExpression
| unaryExpression assignmentOperator assignmentExpression
| DigitSequence // for
| macroStatement
;

assignmentOperator
Expand Down Expand Up @@ -429,6 +430,8 @@ typedefName
initializer
: assignmentExpression
| '{' initializerList ','? '}'
// | '#if' expressionStatement initializerList '#' 'endif'
| macroStatement ','? initializerList ','? macroStatement
;

initializerList
Expand Down Expand Up @@ -458,7 +461,8 @@ statement
| macroStatement
;

normalStatement : labeledStatement
normalStatement
: labeledStatement
| compoundStatement
| expressionStatement
| selectionStatement
Expand All @@ -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
Expand Down Expand Up @@ -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
;

Expand Down Expand Up @@ -992,10 +1003,6 @@ fragment IdentifierNondigit
//| // other implementation-defined characters...
;

fragment UpperCase
: [A-Z_]
;

fragment Nondigit
: [a-zA-Z_]
;
Expand Down
14 changes: 14 additions & 0 deletions chapi-ast-c/src/test/resources/realworld/SlowMacro2.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 879e24f

Please sign in to comment.