Skip to content

Commit

Permalink
feat(c): modify C grammar to support one-line macro declarations #24
Browse files Browse the repository at this point in the history
- Modify the C grammar in `C.g4` to support one-line macro declarations.
- The `compilationUnit` rule now allows for multiple `oneLineMacroDeclaration` or `externalDeclaration` before the end of file.
- The `oneLineMacroDeclaration` rule has been added to handle `#include` and `#define` declarations.
- The `declaration` rule now allows for an optional `Static` keyword before the declaration specifiers.
  • Loading branch information
phodal committed Jan 30, 2024
1 parent 7f2ded2 commit 214a150
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 17 deletions.
22 changes: 5 additions & 17 deletions chapi-ast-c/src/main/antlr/C.g4
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@
grammar C;

compilationUnit
: (includeDeclaration+)? (externalDeclaration+)? EOF
: (oneLineMacroDeclaration | externalDeclaration)* EOF
;

includeDeclaration
: '#' include (StringLiteral | ('<' includeIdentifier '>' ))
| '#' Identifier expression*
oneLineMacroDeclaration
: '#' include (StringLiteral | ('<' includeIdentifier '>' )) #includeDeclaration
| '#' Identifier expression* #defineDeclaration
;

MultiLineMacro
Expand Down Expand Up @@ -194,7 +194,7 @@ constantExpression
;

declaration
: declarationSpecifier+ initDeclaratorList? ';'
: Static? declarationSpecifier+ initDeclaratorList? ';'
| staticAssertDeclaration
;

Expand Down Expand Up @@ -324,18 +324,6 @@ declarator
;

directDeclarator
// : Identifier
// | '(' declarator ')'
// | directDeclarator '[' typeQualifierList? assignmentExpression? ']'
// | directDeclarator '[' 'static' typeQualifierList? assignmentExpression ']'
// | directDeclarator '[' typeQualifierList 'static' assignmentExpression ']'
// | directDeclarator '[' typeQualifierList? '*' ']'
// | directDeclarator '(' parameterTypeList ')'
// | directDeclarator '(' identifierList? ')'
// | Identifier ':' DigitSequence // bit field
// | vcSpecificModifer Identifier // Visual C Extension
// | '(' vcSpecificModifer declarator ')' // Visual C Extension
// ;
: Identifier #identifierDirectDeclarator
| '(' declarator ')' #declaratorDirectDeclarator
| directDeclarator '[' typeQualifierList? assignmentExpression? ']' #assignmentExpressionDirectDeclarator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ internal class CFullIdentListenerTest {
val code = """
#include <stdio.h>
static RedisModuleType *MemAllocType;
#define MAX_DB 16
"""
val codeFile = CAnalyser().analysis(code, "helloworld.c")

Expand Down

0 comments on commit 214a150

Please sign in to comment.