Skip to content

Commit ce75abd

Browse files
committed
refactor(c): update to lexer #24
1 parent efe7163 commit ce75abd

File tree

7 files changed

+87
-118
lines changed

7 files changed

+87
-118
lines changed

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

+4-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ DELIMITED_DOC_COMMENT: '/**' ~'/' .*? '*/' -> channel(COMMENTS_CHANNEL);
1010
SINGLE_LINE_COMMENT: '//' InputCharacter* -> channel(COMMENTS_CHANNEL);
1111
DELIMITED_COMMENT: '/*' .*? '*/' -> channel(COMMENTS_CHANNEL);
1212
WHITESPACES: (Whitespace | Newline)+ -> channel(HIDDEN);
13-
SHARP: '#' -> mode(DIRECTIVE_MODE), skip;
13+
SHARP: '#' -> mode(DIRECTIVE_MODE);
1414

1515
//MultiLineMacro: '#' (~[\n]*? '\\' '\r'? '\n')+ ~ [\n]+ -> channel (HIDDEN);
1616
//
@@ -145,8 +145,8 @@ DigitSequence
145145

146146
IncludeText
147147
: '<' SChar* ('.' | '/' | SChar)* '>'
148-
| STRING
149-
| Identifier // for macro
148+
| DIRECTIVE_STRING
149+
| CONDITIONAL_SYMBOL
150150
;
151151

152152
STRING
@@ -222,6 +222,7 @@ DIRECTIVE_OP_EQ: '==' -> channel(DIREC
222222
DIRECTIVE_OP_NE: '!=' -> channel(DIRECTIVE), type(OP_NE);
223223
DIRECTIVE_OP_AND: '&&' -> channel(DIRECTIVE), type(OP_AND);
224224
DIRECTIVE_OP_OR: '||' -> channel(DIRECTIVE), type(OP_OR);
225+
DIRECTIVE_DOUBLE_QUOTE: '"' -> channel(DIRECTIVE);
225226
DIRECTIVE_STRING: '"' ~('"' | [\r\n\u0085\u2028\u2029])* '"' -> channel(DIRECTIVE), type(STRING);
226227
CONDITIONAL_SYMBOL: Identifier -> channel(DIRECTIVE);
227228
DIRECTIVE_SINGLE_LINE_COMMENT: '//' ~[\r\n\u0085\u2028\u2029]* -> channel(COMMENTS_CHANNEL), type(SINGLE_LINE_COMMENT);

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ parser grammar CPreprocessorParser;
77
options { tokenVocab=CLexer; superClass=chapi.ast.antlr.CPreprocessorParserBase; }
88

99
preprocessor_directive returns [Boolean value]
10-
: DEFINE CONDITIONAL_SYMBOL (Identifier | DIGITS) directive_new_line_or_sharp { this.OnPreprocessorDirectiveDefine(); } #preprocessorDeclaration
10+
: DEFINE CONDITIONAL_SYMBOL (Identifier | DIGITS | preprocessor_directive)? directive_new_line_or_sharp { this.OnPreprocessorDirectiveDefine(); } #preprocessorDeclaration
1111
| INCLUDE IncludeText directive_new_line_or_sharp { this.OnPreprocessorDirectiveInclude(); } #preprocessorIncludeDeclaration
1212
| UNDEF CONDITIONAL_SYMBOL directive_new_line_or_sharp { this.OnPreprocessorDirectiveUndef(); } #preprocessorDeclaration
13-
| IFDEF CONDITIONAL_SYMBOL directive_new_line_or_sharp { this.OnPreprocessorDirectiveIfdef(); } #preprocessorConditional
14-
| IFNDEF CONDITIONAL_SYMBOL directive_new_line_or_sharp { this.OnPreprocessorDirectiveIfndef(); } #preprocessorConditional
13+
// | IFDEF CONDITIONAL_SYMBOL directive_new_line_or_sharp { this.OnPreprocessorDirectiveIfdef(); } #preprocessorConditional
14+
// | IFNDEF CONDITIONAL_SYMBOL directive_new_line_or_sharp { this.OnPreprocessorDirectiveIfndef(); } #preprocessorConditional
1515
| If expr=preprocessor_expression directive_new_line_or_sharp { this.OnPreprocessorDirectiveIf(); } #preprocessorConditional
1616
| ELIF expr=preprocessor_expression directive_new_line_or_sharp { this.OnPreprocessorDirectiveElif(); } #preprocessorConditional
1717
| Else directive_new_line_or_sharp { this.OnPreprocessorDirectiveElse(); } #preprocessorConditional

chapi-ast-c/src/main/java/chapi/ast/antlr/CPreprocessorParserBase.java

+72-95
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
import java.util.Stack;
99

1010
abstract class CPreprocessorParserBase extends Parser {
11-
protected CPreprocessorParserBase(TokenStream input)
12-
{
11+
protected CPreprocessorParserBase(TokenStream input) {
1312
super(input);
1413
conditions.push(true);
1514
ConditionalSymbols.add("DEBUG");
@@ -19,206 +18,184 @@ protected CPreprocessorParserBase(TokenStream input)
1918
public HashSet<String> ConditionalSymbols = new HashSet<String>();
2019
public HashSet<String> IncludeSymbols = new HashSet<String>();
2120

22-
protected Boolean AllConditions()
23-
{
24-
for(Boolean condition: conditions)
25-
{
21+
protected Boolean AllConditions() {
22+
for (Boolean condition : conditions) {
2623
if (!condition)
2724
return false;
2825
}
2926
return true;
3027
}
3128

32-
protected void OnPreprocessorDirectiveInclude()
33-
{
29+
protected void OnPreprocessorDirectiveInclude() {
3430
ParserRuleContext c = this._ctx;
3531
CPreprocessorParser.PreprocessorIncludeDeclarationContext d = (CPreprocessorParser.PreprocessorIncludeDeclarationContext) c;
36-
IncludeSymbols.add(d.getText());
32+
IncludeSymbols.add(d.IncludeText().getText());
3733
d.value = AllConditions();
3834
}
3935

40-
protected void OnPreprocessorDirectiveDefine()
41-
{
36+
protected void OnPreprocessorDirectiveDefine() {
4237
ParserRuleContext c = this._ctx;
43-
CPreprocessorParser.PreprocessorDeclarationContext d = (CPreprocessorParser.PreprocessorDeclarationContext)c;
38+
CPreprocessorParser.PreprocessorDeclarationContext d = (CPreprocessorParser.PreprocessorDeclarationContext) c;
4439
ConditionalSymbols.add(d.CONDITIONAL_SYMBOL().getText());
4540
d.value = AllConditions();
4641
}
4742

48-
protected void OnPreprocessorDirectiveUndef()
49-
{
43+
protected void OnPreprocessorDirectiveUndef() {
5044
ParserRuleContext c = this._ctx;
51-
CPreprocessorParser.PreprocessorDeclarationContext d = (CPreprocessorParser.PreprocessorDeclarationContext)c;
45+
CPreprocessorParser.PreprocessorDeclarationContext d = (CPreprocessorParser.PreprocessorDeclarationContext) c;
5246
ConditionalSymbols.remove(d.CONDITIONAL_SYMBOL().getText());
5347
d.value = AllConditions();
5448
}
5549

56-
protected void OnPreprocessorDirectiveIf()
57-
{
50+
protected void OnPreprocessorDirectiveIf() {
5851
ParserRuleContext c = this._ctx;
59-
CPreprocessorParser.PreprocessorConditionalContext d = (CPreprocessorParser.PreprocessorConditionalContext)c;
60-
d.value = d.expr.value.equals("true") && AllConditions();
61-
conditions.push(d.expr.value.equals("true"));
52+
CPreprocessorParser.PreprocessorConditionalContext d = (CPreprocessorParser.PreprocessorConditionalContext) c;
53+
if (d.expr.value != null) {
54+
d.value = d.expr.value.equals("true") && AllConditions();
55+
conditions.push(d.expr.value.equals("true"));
56+
}
6257
}
6358

64-
protected void OnPreprocessorDirectiveIfdef()
65-
{
59+
protected void OnPreprocessorDirectiveIfdef() {
6660
ParserRuleContext c = this._ctx;
67-
CPreprocessorParser.PreprocessorDeclarationContext d = (CPreprocessorParser.PreprocessorDeclarationContext)c;
61+
CPreprocessorParser.PreprocessorDeclarationContext d = (CPreprocessorParser.PreprocessorDeclarationContext) c;
6862
ConditionalSymbols.add(d.CONDITIONAL_SYMBOL().getText());
6963
d.value = AllConditions();
7064
}
7165

72-
protected void OnPreprocessorDirectiveIfndef()
73-
{
66+
protected void OnPreprocessorDirectiveIfndef() {
7467
ParserRuleContext c = this._ctx;
75-
CPreprocessorParser.PreprocessorDeclarationContext d = (CPreprocessorParser.PreprocessorDeclarationContext)c;
68+
CPreprocessorParser.PreprocessorDeclarationContext d = (CPreprocessorParser.PreprocessorDeclarationContext) c;
7669
ConditionalSymbols.add(d.CONDITIONAL_SYMBOL().getText());
7770
d.value = AllConditions();
7871
}
7972

80-
protected void OnPreprocessorDirectiveElif()
81-
{
73+
protected void OnPreprocessorDirectiveElif() {
8274
ParserRuleContext c = this._ctx;
83-
CPreprocessorParser.PreprocessorConditionalContext d = (CPreprocessorParser.PreprocessorConditionalContext)c;
84-
if (!conditions.peek())
85-
{
75+
CPreprocessorParser.PreprocessorConditionalContext d = (CPreprocessorParser.PreprocessorConditionalContext) c;
76+
if (!conditions.peek()) {
8677
conditions.pop();
8778
d.value = d.expr.value.equals("true") && AllConditions();
8879
conditions.push(d.expr.value.equals("true"));
89-
}
90-
else
91-
{
80+
} else {
9281
d.value = false;
9382
}
9483
}
9584

96-
protected void OnPreprocessorDirectiveElse()
97-
{
85+
protected void OnPreprocessorDirectiveElse() {
9886
ParserRuleContext c = this._ctx;
99-
CPreprocessorParser.PreprocessorConditionalContext d = (CPreprocessorParser.PreprocessorConditionalContext)c;
100-
if (!conditions.peek())
101-
{
102-
conditions.pop();
103-
d.value = true && AllConditions();
104-
conditions.push(true);
105-
}
106-
else
107-
{
87+
CPreprocessorParser.PreprocessorConditionalContext d = (CPreprocessorParser.PreprocessorConditionalContext) c;
88+
if (!conditions.isEmpty()) {
89+
if (!conditions.peek()) {
90+
conditions.pop();
91+
d.value = true && AllConditions();
92+
conditions.push(true);
93+
} else {
94+
d.value = false;
95+
}
96+
} else {
10897
d.value = false;
10998
}
11099
}
111100

112-
protected void OnPreprocessorDirectiveEndif()
113-
{
101+
protected void OnPreprocessorDirectiveEndif() {
114102
ParserRuleContext c = this._ctx;
115-
CPreprocessorParser.PreprocessorConditionalContext d = (CPreprocessorParser.PreprocessorConditionalContext)c;
116-
conditions.pop();
117-
d.value = conditions.peek();
103+
CPreprocessorParser.PreprocessorConditionalContext d = (CPreprocessorParser.PreprocessorConditionalContext) c;
104+
if (!conditions.isEmpty()) {
105+
conditions.pop();
106+
if (!conditions.isEmpty()) {
107+
d.value = conditions.peek();
108+
}
109+
}
118110
}
119111

120-
protected void OnPreprocessorDirectiveError()
121-
{
112+
protected void OnPreprocessorDirectiveError() {
122113
ParserRuleContext c = this._ctx;
123-
CPreprocessorParser.PreprocessorDiagnosticContext d = (CPreprocessorParser.PreprocessorDiagnosticContext)c;
114+
CPreprocessorParser.PreprocessorDiagnosticContext d = (CPreprocessorParser.PreprocessorDiagnosticContext) c;
124115
d.value = AllConditions();
125116
}
126117

127-
protected void OnPreprocessorDirectiveWarning()
128-
{
118+
protected void OnPreprocessorDirectiveWarning() {
129119
ParserRuleContext c = this._ctx;
130-
CPreprocessorParser.PreprocessorDiagnosticContext d = (CPreprocessorParser.PreprocessorDiagnosticContext)c;
120+
CPreprocessorParser.PreprocessorDiagnosticContext d = (CPreprocessorParser.PreprocessorDiagnosticContext) c;
131121
d.value = AllConditions();
132122
}
133123

134-
protected void OnPreprocessorDirectiveRegion()
135-
{
124+
protected void OnPreprocessorDirectiveRegion() {
136125
ParserRuleContext c = this._ctx;
137-
CPreprocessorParser.PreprocessorRegionContext d = (CPreprocessorParser.PreprocessorRegionContext)c;
126+
CPreprocessorParser.PreprocessorRegionContext d = (CPreprocessorParser.PreprocessorRegionContext) c;
138127
d.value = AllConditions();
139128
}
140129

141-
protected void OnPreprocessorDirectiveEndregion()
142-
{
130+
protected void OnPreprocessorDirectiveEndregion() {
143131
ParserRuleContext c = this._ctx;
144-
CPreprocessorParser.PreprocessorRegionContext d = (CPreprocessorParser.PreprocessorRegionContext)c;
132+
CPreprocessorParser.PreprocessorRegionContext d = (CPreprocessorParser.PreprocessorRegionContext) c;
145133
d.value = AllConditions();
146134
}
147135

148-
protected void OnPreprocessorDirectivePragma()
149-
{
136+
protected void OnPreprocessorDirectivePragma() {
150137
ParserRuleContext c = this._ctx;
151-
CPreprocessorParser.PreprocessorPragmaContext d = (CPreprocessorParser.PreprocessorPragmaContext)c;
138+
CPreprocessorParser.PreprocessorPragmaContext d = (CPreprocessorParser.PreprocessorPragmaContext) c;
152139
d.value = AllConditions();
153140
}
154141

155-
protected void OnPreprocessorDirectiveNullable()
156-
{
142+
protected void OnPreprocessorDirectiveNullable() {
157143
ParserRuleContext c = this._ctx;
158-
CPreprocessorParser.PreprocessorNullableContext d = (CPreprocessorParser.PreprocessorNullableContext)c;
144+
CPreprocessorParser.PreprocessorNullableContext d = (CPreprocessorParser.PreprocessorNullableContext) c;
159145
d.value = AllConditions();
160146
}
161147

162-
protected void OnPreprocessorExpressionTrue()
163-
{
148+
protected void OnPreprocessorExpressionTrue() {
164149
ParserRuleContext c = this._ctx;
165-
CPreprocessorParser.Preprocessor_expressionContext d = (CPreprocessorParser.Preprocessor_expressionContext)c;
150+
CPreprocessorParser.Preprocessor_expressionContext d = (CPreprocessorParser.Preprocessor_expressionContext) c;
166151
d.value = "true";
167152
}
168153

169-
protected void OnPreprocessorExpressionFalse()
170-
{
154+
protected void OnPreprocessorExpressionFalse() {
171155
ParserRuleContext c = this._ctx;
172-
CPreprocessorParser.Preprocessor_expressionContext d = (CPreprocessorParser.Preprocessor_expressionContext)c;
156+
CPreprocessorParser.Preprocessor_expressionContext d = (CPreprocessorParser.Preprocessor_expressionContext) c;
173157
d.value = "false";
174158
}
175159

176-
protected void OnPreprocessorExpressionConditionalSymbol()
177-
{
160+
protected void OnPreprocessorExpressionConditionalSymbol() {
178161
ParserRuleContext c = this._ctx;
179-
CPreprocessorParser.Preprocessor_expressionContext d = (CPreprocessorParser.Preprocessor_expressionContext)c;
162+
CPreprocessorParser.Preprocessor_expressionContext d = (CPreprocessorParser.Preprocessor_expressionContext) c;
180163
d.value = ConditionalSymbols.contains(d.CONDITIONAL_SYMBOL().getText()) ? "true" : "false";
181164
}
182165

183-
protected void OnPreprocessorExpressionConditionalOpenParens()
184-
{
166+
protected void OnPreprocessorExpressionConditionalOpenParens() {
185167
ParserRuleContext c = this._ctx;
186-
CPreprocessorParser.Preprocessor_expressionContext d = (CPreprocessorParser.Preprocessor_expressionContext)c;
168+
CPreprocessorParser.Preprocessor_expressionContext d = (CPreprocessorParser.Preprocessor_expressionContext) c;
187169
d.value = d.expr.value;
188170
}
189171

190-
protected void OnPreprocessorExpressionConditionalBang()
191-
{
172+
protected void OnPreprocessorExpressionConditionalBang() {
192173
ParserRuleContext c = this._ctx;
193-
CPreprocessorParser.Preprocessor_expressionContext d = (CPreprocessorParser.Preprocessor_expressionContext)c;
174+
CPreprocessorParser.Preprocessor_expressionContext d = (CPreprocessorParser.Preprocessor_expressionContext) c;
194175
d.value = d.expr.value.equals("true") ? "false" : "true";
195176
}
196177

197-
protected void OnPreprocessorExpressionConditionalEq()
198-
{
178+
protected void OnPreprocessorExpressionConditionalEq() {
199179
ParserRuleContext c = this._ctx;
200-
CPreprocessorParser.Preprocessor_expressionContext d = (CPreprocessorParser.Preprocessor_expressionContext)c;
180+
CPreprocessorParser.Preprocessor_expressionContext d = (CPreprocessorParser.Preprocessor_expressionContext) c;
201181
d.value = (d.expr1.value == d.expr2.value ? "true" : "false");
202182
}
203183

204-
protected void OnPreprocessorExpressionConditionalNe()
205-
{
184+
protected void OnPreprocessorExpressionConditionalNe() {
206185
ParserRuleContext c = this._ctx;
207-
CPreprocessorParser.Preprocessor_expressionContext d = (CPreprocessorParser.Preprocessor_expressionContext)c;
186+
CPreprocessorParser.Preprocessor_expressionContext d = (CPreprocessorParser.Preprocessor_expressionContext) c;
208187
d.value = (d.expr1.value != d.expr2.value ? "true" : "false");
209188
}
210189

211-
protected void OnPreprocessorExpressionConditionalAnd()
212-
{
190+
protected void OnPreprocessorExpressionConditionalAnd() {
213191
ParserRuleContext c = this._ctx;
214-
CPreprocessorParser.Preprocessor_expressionContext d = (CPreprocessorParser.Preprocessor_expressionContext)c;
192+
CPreprocessorParser.Preprocessor_expressionContext d = (CPreprocessorParser.Preprocessor_expressionContext) c;
215193
d.value = (d.expr1.value.equals("true") && d.expr2.value.equals("true") ? "true" : "false");
216194
}
217195

218-
protected void OnPreprocessorExpressionConditionalOr()
219-
{
196+
protected void OnPreprocessorExpressionConditionalOr() {
220197
ParserRuleContext c = this._ctx;
221-
CPreprocessorParser.Preprocessor_expressionContext d = (CPreprocessorParser.Preprocessor_expressionContext)c;
198+
CPreprocessorParser.Preprocessor_expressionContext d = (CPreprocessorParser.Preprocessor_expressionContext) c;
222199
d.value = (d.expr1.value.equals("true") || d.expr2.value.equals("true") ? "true" : "false");
223200
}
224201
}

chapi-ast-c/src/main/kotlin/chapi/ast/cast/CAnalyser.kt

+4-6
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ open class CAnalyser : Analyser {
6363
// Parse condition in preprocessor directive (based on CSharpPreprocessorParser.g4 grammar).
6464
try {
6565
val directive = preprocessorParser.preprocessor_directive()
66-
6766
if (directive.value != null) {
6867
// if true than next code is valid and not ignored.
6968
compiledTokens = directive.value
@@ -88,12 +87,11 @@ open class CAnalyser : Analyser {
8887

8988
includesDirective = preprocessorParser.IncludeSymbols.toMutableList()
9089

91-
codeTokens.map {
92-
print(it.text + " ")
93-
}
94-
90+
// codeTokens.map {
91+
// print(it.text + " ")
92+
// }
9593
// At the second stage, tokens are parsed in the usual way.
96-
val codeTokenSource = ListTokenSource(tokens)
94+
val codeTokenSource = ListTokenSource(codeTokens)
9795

9896
val codeTokenStream = CommonTokenStream(codeTokenSource)
9997
val parser = CParser(codeTokenStream)

chapi-ast-c/src/main/kotlin/chapi/ast/cast/CFullIdentListener.kt

+1-4
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,9 @@ open class CFullIdentListener(fileName: String, includes: MutableList<String>) :
1010
private var structMap = mutableMapOf<String, CodeDataStruct>()
1111
private var codeContainer: CodeContainer = CodeContainer(FullName = fileName)
1212

13-
private val importRegex = Regex("""#include\s+(<[^>]+>|\"[^\"]+\")""")
14-
1513
init {
1614
includes.forEach {
17-
val matchResult = importRegex.find(it) ?: return@forEach
18-
val value = matchResult.groupValues[1]
15+
val value = it
1916
.removeSurrounding("\"", "\"")
2017
.removeSurrounding("<", ">")
2118

0 commit comments

Comments
 (0)