Skip to content

Commit 89e2082

Browse files
committed
fix(chapi-ast-c): modify grammar rules for single line macro declarations #24
This commit modifies the grammar rules for single line macro declarations in the chapi-ast-c project. The changes include adding support for various types of macro declarations and expressions. The modifications aim to improve the parsing of macros in the code.
1 parent e3f4e4d commit 89e2082

File tree

2 files changed

+58
-50
lines changed

2 files changed

+58
-50
lines changed

README.md

+20-3
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,28 @@ Algol Family [https://wiki.c2.com/?AlgolFamily](https://wiki.c2.com/?AlgolFamily
9898

9999
Since Macro can be everywhere, If your code includes lots of Macro, it will be slow.
100100

101+
Test in Lua 5.3.5: with 17402 C line, 3298 C Header line.
102+
101103
| | without Macro | simple Macro (with postfixCall) | full Macro (maybe) |
102104
|-------|---------------|---------------------------------|--------------------|
103-
| time1 | 100425ms | 185662ms | |
104-
| time2 | 96674ms | 201791ms | |
105-
| time3 | 95434ms | 184508ms | |
105+
| time1 | 100425ms | 185662ms | 158849ms |
106+
| time2 | 96674ms | 201791ms | 173202ms |
107+
| time3 | 95434ms | 184508ms | 149230ms |
108+
109+
Known issues: line break in Macro will cause parse error.
110+
111+
For example:
112+
113+
```c
114+
#if LUA_VERSION_NUM < 503
115+
lua_pushnumber(L,
116+
#else
117+
lua_pushinteger(L,
118+
#endif
119+
(int64_t)c->p[8]);
120+
mp_cur_consume(c,9);
121+
break;
122+
```
106123
107124
### Kotlin
108125

chapi-ast-c/src/main/antlr/C.g4

+38-47
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ grammar C;
3535

3636
compilationUnit
3737
// statement for macro support
38-
: (externalDeclaration)* EOF
39-
// | (externalDeclaration | statement | macroPostixCall)* EOF
38+
// : (externalDeclaration)* EOF
39+
: (externalDeclaration | statement | macroPostixCall)* EOF
4040
;
4141

4242
MultiLineMacro
@@ -489,42 +489,42 @@ logicals : logicalOrExpression (',' logicalOrExpression)* ;
489489
Directive: '#' ~ [\n]* -> channel (HIDDEN);
490490

491491
macroStatement
492-
// : '#' singleLineMacroDeclaration*
493-
: '#' include (StringLiteral | Identifier | ('<' includeIdentifier '>' )) #includeDeclaration
494-
;
495-
496-
//singleLineMacroDeclaration
497-
// : include (StringLiteral | Identifier | ('<' includeIdentifier '>' )) #includeDeclaration
498-
// | ('ifdef' | 'ifndef' | 'if') Identifier statement* ('#' 'else' statement*)? '#' 'endif'
499-
// #ifdefDeclaration
500-
// | 'define' Identifier defineMacro #defineDeclaration
501-
//// | macroKeywords (assignmentExpression)*
502-
//// ('#' macroKeywords)? identifierList? #macroDefineDeclaration
503-
// | '#'? Identifier #macroCastDeclaration
504-
// | macroKeywords macroFunctionExpression? #macroStatementDeclaration
505-
// ;
506-
//
507-
//defineMacro
508-
// : expressionStatement? #macroAssignDeclaration
509-
// | postixCall Identifier postixCall #macroAliasDeclaration
510-
// | structOrUnionSpecifier #macroStructureDeclaration
511-
// ;
512-
//
513-
//
514-
//macroFunctionExpression
515-
// : Identifier
516-
// | Identifier '(' ( parameterTypeList| (macroType (',' macroType)*) ) ')'
517-
// | assignmentExpression
518-
// ;
519-
//
520-
//macroType
521-
// : typeQualifier? (typeKeywords | Identifier | '==' | '!=' | comparator) (Identifier | typeKeywords)* pointer?
522-
// | expressionStatement
523-
// ;
524-
//
525-
//macroKeywords
526-
// : 'if' | 'undef' | 'else' | 'pragma' | 'endif' | 'ifdef' | 'ifndef' | 'elif' | 'define' | 'ifndef' | 'error'
527-
// ;
492+
: '#' singleLineMacroDeclaration*
493+
// : '#' include (StringLiteral | Identifier | ('<' includeIdentifier '>' )) #includeDeclaration
494+
;
495+
496+
singleLineMacroDeclaration
497+
: include (StringLiteral | Identifier | ('<' includeIdentifier '>' )) #includeDeclaration
498+
| ('ifdef' | 'ifndef' | 'if') Identifier statement* ('#' 'else' statement*)? '#' 'endif'
499+
#ifdefDeclaration
500+
| 'define' Identifier defineMacro #defineDeclaration
501+
// | macroKeywords (assignmentExpression)*
502+
// ('#' macroKeywords)? identifierList? #macroDefineDeclaration
503+
| '#'? Identifier #macroCastDeclaration
504+
| macroKeywords macroFunctionExpression? #macroStatementDeclaration
505+
;
506+
507+
defineMacro
508+
: expressionStatement? #macroAssignDeclaration
509+
| postixCall Identifier postixCall #macroAliasDeclaration
510+
| structOrUnionSpecifier #macroStructureDeclaration
511+
;
512+
513+
514+
macroFunctionExpression
515+
: Identifier
516+
| Identifier '(' ( parameterTypeList| (macroType (',' macroType)*) ) ')'
517+
| assignmentExpression
518+
;
519+
520+
macroType
521+
: typeQualifier? (typeKeywords | Identifier | '==' | '!=' | comparator) (Identifier | typeKeywords)* pointer?
522+
| expressionStatement
523+
;
524+
525+
macroKeywords
526+
: 'if' | 'undef' | 'else' | 'pragma' | 'endif' | 'ifdef' | 'ifndef' | 'elif' | 'define' | 'ifndef' | 'error'
527+
;
528528

529529
labeledStatement
530530
: Identifier ':' statement?
@@ -1023,15 +1023,6 @@ fragment Nondigit
10231023
: [a-zA-Z_]
10241024
;
10251025

1026-
1027-
MacroId
1028-
: UpperedId (UpperedId)*
1029-
;
1030-
1031-
fragment UpperedId
1032-
: [A-Z_]
1033-
;
1034-
10351026
fragment Digit
10361027
: [0-9]
10371028
;

0 commit comments

Comments
 (0)