From 214a1504f3e64430b3a532664384458d819befae Mon Sep 17 00:00:00 2001 From: Phodal Huang Date: Tue, 30 Jan 2024 17:26:46 +0800 Subject: [PATCH] feat(c): modify C grammar to support one-line macro declarations #24 - 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. --- chapi-ast-c/src/main/antlr/C.g4 | 22 +++++-------------- .../chapi/ast/cast/CFullIdentListenerTest.kt | 3 +++ 2 files changed, 8 insertions(+), 17 deletions(-) diff --git a/chapi-ast-c/src/main/antlr/C.g4 b/chapi-ast-c/src/main/antlr/C.g4 index 881c5c19..4e2a65ae 100644 --- a/chapi-ast-c/src/main/antlr/C.g4 +++ b/chapi-ast-c/src/main/antlr/C.g4 @@ -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 @@ -194,7 +194,7 @@ constantExpression ; declaration - : declarationSpecifier+ initDeclaratorList? ';' + : Static? declarationSpecifier+ initDeclaratorList? ';' | staticAssertDeclaration ; @@ -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 diff --git a/chapi-ast-c/src/test/kotlin/chapi/ast/cast/CFullIdentListenerTest.kt b/chapi-ast-c/src/test/kotlin/chapi/ast/cast/CFullIdentListenerTest.kt index 979fb9e9..b8e359c0 100644 --- a/chapi-ast-c/src/test/kotlin/chapi/ast/cast/CFullIdentListenerTest.kt +++ b/chapi-ast-c/src/test/kotlin/chapi/ast/cast/CFullIdentListenerTest.kt @@ -23,6 +23,9 @@ internal class CFullIdentListenerTest { val code = """ #include +static RedisModuleType *MemAllocType; + +#define MAX_DB 16 """ val codeFile = CAnalyser().analysis(code, "helloworld.c")