cariboujs 2.0.4 → 2.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -2
- package/ada/ada2012/AdaLexer.js +486 -316
- package/ada/ada2012/AdaLexerBase.js +25 -0
- package/ada/ada2012/AdaParser.js +4547 -3823
- package/ada/ada2012/AdaParserBase.js +682 -0
- package/ada/ada2012/AdaParserListener.js +36 -0
- package/ada/ada2012/AdaParserVisitor.js +24 -0
- package/ada/ada2012/Symbol.js +20 -0
- package/ada/ada2012/SymbolTable.js +144 -0
- package/ada/ada2012/TypeClassification.js +15 -0
- package/ada/ada2022/AdaLexer.js +699 -0
- package/ada/ada2022/AdaLexerBase.js +25 -0
- package/ada/ada2022/AdaParser.js +37818 -0
- package/ada/ada2022/AdaParserBase.js +686 -0
- package/ada/ada2022/AdaParserListener.js +3024 -0
- package/ada/ada2022/AdaParserVisitor.js +2020 -0
- package/ada/ada2022/Symbol.js +20 -0
- package/ada/ada2022/SymbolTable.js +144 -0
- package/ada/ada2022/TypeClassification.js +15 -0
- package/c/CLexer.js +919 -0
- package/c/CLexerBase.js +96 -0
- package/c/CParser.js +14878 -0
- package/c/CParserBase.js +591 -0
- package/c/CParserListener.js +1062 -0
- package/c/CParserVisitor.js +712 -0
- package/c/ErrorListener.js +50 -0
- package/c/Symbol.js +28 -0
- package/c/SymbolTable.js +215 -0
- package/c/TypeClassification.js +21 -0
- package/cpp/CPP14Parser.js +215 -215
- package/cpp/CPP14ParserListener.js +56 -56
- package/cpp/CPP14ParserVisitor.js +28 -28
- package/csharp/v7/CSharpLexer.js +1339 -0
- package/csharp/v7/CSharpLexerBase.js +332 -0
- package/csharp/v7/CSharpParser.js +29239 -0
- package/csharp/v7/CSharpParserBase.js +48 -0
- package/csharp/v7/CSharpParserListener.js +2304 -0
- package/csharp/v7/CSharpParserVisitor.js +1540 -0
- package/csharp/v8-spec/CSharpLexer.js +1549 -0
- package/csharp/v8-spec/CSharpLexerBase.js +296 -0
- package/csharp/v8-spec/CSharpParser.js +45044 -0
- package/csharp/v8-spec/CSharpParserBase.js +569 -0
- package/csharp/v8-spec/CSharpParserListener.js +3582 -0
- package/csharp/v8-spec/CSharpParserVisitor.js +2392 -0
- package/geekcode/geekcodeLexer.js +232 -0
- package/geekcode/geekcodeListener.js +81 -0
- package/geekcode/geekcodeParser.js +1516 -0
- package/geekcode/geekcodeVisitor.js +58 -0
- package/kotlin/kotlin/KotlinLexer.js +1257 -1247
- package/kotlin/kotlin/KotlinParser.js +3991 -3425
- package/kotlin/kotlin/KotlinParserListener.js +36 -0
- package/kotlin/kotlin/KotlinParserVisitor.js +24 -0
- package/package.json +1 -1
- package/plantUML/PlantUMLLexer.js +215 -316
- package/plantUML/PlantUMLParser.js +1045 -2393
- package/plantUML/PlantUMLParserListener.js +171 -0
- package/plantUML/PlantUMLParserVisitor.js +118 -0
- package/sql/plsql/PlSqlParser.js +27973 -43771
- package/sql/plsql/PlSqlParserBase.js +30 -0
- package/sql/plsql/PlSqlParserListener.js +0 -9
- package/sql/plsql/PlSqlParserVisitor.js +0 -6
- package/sql/snowflake/SnowflakeLexer.js +5675 -5497
- package/sql/snowflake/SnowflakeParser.js +27230 -22653
- package/sql/snowflake/SnowflakeParserListener.js +278 -8
- package/sql/snowflake/SnowflakeParserVisitor.js +184 -4
- package/sql/sqlite/SQLiteParser.js +1115 -1115
- package/sql/sqlite/SQLiteParserListener.js +9 -9
- package/sql/sqlite/SQLiteParserVisitor.js +6 -6
- package/sysml-v2/SysMLv2Lexer.js +1047 -0
- package/sysml-v2/SysMLv2Parser.js +51339 -0
- package/sysml-v2/SysMLv2ParserListener.js +4464 -0
- package/sysml-v2/SysMLv2ParserVisitor.js +2980 -0
- package/plantUML/PlantUMLListener.js +0 -279
- package/plantUML/PlantUMLVisitor.js +0 -190
package/c/CParserBase.js
ADDED
|
@@ -0,0 +1,591 @@
|
|
|
1
|
+
import antlr4 from 'antlr4';
|
|
2
|
+
import { SymbolTable } from "./SymbolTable.js";
|
|
3
|
+
import { Symbol } from "./Symbol.js";
|
|
4
|
+
import { TypeClassification } from "./TypeClassification.js";
|
|
5
|
+
import CLexer from "./CLexer.js";
|
|
6
|
+
import CParser from "./CParser.js";
|
|
7
|
+
|
|
8
|
+
function isDeclarationContext(x) {
|
|
9
|
+
return x?.ruleIndex === CParser.RULE_declaration;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function isFunctionDefinitionContext(x) {
|
|
13
|
+
return x?.ruleIndex === CParser.RULE_functionDefinition;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const ALL_SEMANTIC_FUNCTIONS = [
|
|
17
|
+
"IsAlignmentSpecifier", "IsAtomicTypeSpecifier", "IsAttributeDeclaration",
|
|
18
|
+
"IsAttributeSpecifier", "IsAttributeSpecifierSequence", "IsDeclaration",
|
|
19
|
+
"IsDeclarationSpecifier", "IsTypeSpecifierQualifier", "IsEnumSpecifier",
|
|
20
|
+
"IsFunctionSpecifier", "IsStatement", "IsStaticAssertDeclaration",
|
|
21
|
+
"IsStorageClassSpecifier", "IsStructOrUnionSpecifier", "IsTypedefName",
|
|
22
|
+
"IsTypeofSpecifier", "IsTypeQualifier", "IsTypeSpecifier", "IsCast",
|
|
23
|
+
"IsNullStructDeclarationListExtension",
|
|
24
|
+
"IsGnuAttributeBeforeDeclarator",
|
|
25
|
+
"IsSomethingOfTypeName", "IsSpecifierQualifierList", "IsTypeName",
|
|
26
|
+
"IsInitDeclaratorList"
|
|
27
|
+
];
|
|
28
|
+
|
|
29
|
+
function parseNoSemantics(args) {
|
|
30
|
+
const result = new Set();
|
|
31
|
+
for (const a of args) {
|
|
32
|
+
const lower = a.toLowerCase();
|
|
33
|
+
if (lower.startsWith("--no-semantics")) {
|
|
34
|
+
const eqIndex = a.indexOf('=');
|
|
35
|
+
if (eqIndex === -1) {
|
|
36
|
+
for (const func of ALL_SEMANTIC_FUNCTIONS) {
|
|
37
|
+
result.add(func);
|
|
38
|
+
}
|
|
39
|
+
} else {
|
|
40
|
+
const value = a.substring(eqIndex + 1);
|
|
41
|
+
const funcs = value.split(',');
|
|
42
|
+
for (const func of funcs) {
|
|
43
|
+
result.add(func.trim());
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return result;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export default class CParserBase extends antlr4.Parser {
|
|
52
|
+
constructor(input) {
|
|
53
|
+
super(input);
|
|
54
|
+
const args = process.argv;
|
|
55
|
+
this.noSemantics = parseNoSemantics(args);
|
|
56
|
+
this._debug = args.some(a => a.toLowerCase().includes("--debug"));
|
|
57
|
+
this.outputSymbolTable = args.some(a => a.toLowerCase().includes("--output-symbol-table"));
|
|
58
|
+
this.outputAppliedOccurrences = args.some(a => a.toLowerCase().includes("--output-applied-occurrences"));
|
|
59
|
+
this._st = new SymbolTable();
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
IsAlignmentSpecifier(k = 1) {
|
|
63
|
+
if (this.noSemantics.has("IsAlignmentSpecifier")) return true;
|
|
64
|
+
const lt1 = this._input.LT(k);
|
|
65
|
+
const text = lt1.text;
|
|
66
|
+
if (this._debug) process.stdout.write("IsAlignmentSpecifier " + lt1);
|
|
67
|
+
const resolved = this._resolveWithOutput(lt1);
|
|
68
|
+
let result = false;
|
|
69
|
+
if (resolved === null) {
|
|
70
|
+
result = false;
|
|
71
|
+
} else if (resolved.classification.has(TypeClassification.AlignmentSpecifier_)) {
|
|
72
|
+
result = true;
|
|
73
|
+
} else {
|
|
74
|
+
result = false;
|
|
75
|
+
}
|
|
76
|
+
if (this._debug) console.log(" " + result);
|
|
77
|
+
return result;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
IsAtomicTypeSpecifier(k = 1) {
|
|
81
|
+
if (this.noSemantics.has("IsAtomicTypeSpecifier")) return true;
|
|
82
|
+
const lt1 = this._input.LT(k);
|
|
83
|
+
const text = lt1.text;
|
|
84
|
+
if (this._debug) process.stdout.write("IsAtomicTypeSpecifier " + lt1);
|
|
85
|
+
const resolved = this._resolveWithOutput(lt1);
|
|
86
|
+
let result = false;
|
|
87
|
+
if (resolved === null) {
|
|
88
|
+
result = false;
|
|
89
|
+
} else if (resolved.classification.has(TypeClassification.AtomicTypeSpecifier_)) {
|
|
90
|
+
result = true;
|
|
91
|
+
} else {
|
|
92
|
+
result = false;
|
|
93
|
+
}
|
|
94
|
+
if (this._debug) console.log(" " + result);
|
|
95
|
+
return result;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
IsAttributeDeclaration() {
|
|
99
|
+
if (this.noSemantics.has("IsAttributeDeclaration")) return true;
|
|
100
|
+
return this.IsAttributeSpecifierSequence();
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
IsAttributeSpecifier() {
|
|
104
|
+
if (this.noSemantics.has("IsAttributeSpecifier")) return true;
|
|
105
|
+
const lt1 = this._input.LT(1);
|
|
106
|
+
if (this._debug) process.stdout.write("IsAttributeSpecifier " + lt1);
|
|
107
|
+
const result = lt1.type === CLexer.LeftBracket;
|
|
108
|
+
if (this._debug) console.log(" " + result);
|
|
109
|
+
return result;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
IsAttributeSpecifierSequence() {
|
|
113
|
+
if (this.noSemantics.has("IsAttributeSpecifierSequence")) return true;
|
|
114
|
+
return this.IsAttributeSpecifier();
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
IsDeclaration() {
|
|
118
|
+
if (this.noSemantics.has("IsDeclaration")) return true;
|
|
119
|
+
if (this._debug) console.log("IsDeclaration");
|
|
120
|
+
const result = this.IsDeclarationSpecifiers()
|
|
121
|
+
|| this.IsAttributeSpecifierSequence()
|
|
122
|
+
|| this.IsStaticAssertDeclaration()
|
|
123
|
+
|| this.IsAttributeDeclaration();
|
|
124
|
+
if (this._debug) console.log("IsDeclaration " + result);
|
|
125
|
+
return result;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
IsDeclarationSpecifier() {
|
|
129
|
+
if (this.noSemantics.has("IsDeclarationSpecifier")) return true;
|
|
130
|
+
const lt1 = this._input.LT(1);
|
|
131
|
+
const text = lt1.text;
|
|
132
|
+
if (this._debug) console.log("IsDeclarationSpecifier " + lt1);
|
|
133
|
+
const result = this.IsStorageClassSpecifier()
|
|
134
|
+
|| this.IsTypeSpecifier()
|
|
135
|
+
|| this.IsTypeQualifier()
|
|
136
|
+
|| (this.IsFunctionSpecifier() && !this.IsGnuAttributeBeforeDeclarator())
|
|
137
|
+
|| this.IsAlignmentSpecifier();
|
|
138
|
+
if (this._debug) console.log("IsDeclarationSpecifier " + result + " for " + lt1);
|
|
139
|
+
return result;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
IsTypeSpecifierQualifier(k = 1) {
|
|
143
|
+
if (this.noSemantics.has("IsTypeSpecifierQualifier")) return true;
|
|
144
|
+
if (this._debug) console.log("IsDeclarationSpecifier");
|
|
145
|
+
const result = this.IsTypeSpecifier(k)
|
|
146
|
+
|| this.IsTypeQualifier(k)
|
|
147
|
+
|| this.IsAlignmentSpecifier(k);
|
|
148
|
+
if (this._debug) console.log("IsDeclarationSpecifier " + result);
|
|
149
|
+
return result;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
IsDeclarationSpecifiers() {
|
|
153
|
+
if (this.noSemantics.has("IsDeclarationSpecifiers")) return true;
|
|
154
|
+
return this.IsDeclarationSpecifier();
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
IsEnumSpecifier(k = 1) {
|
|
158
|
+
if (this.noSemantics.has("IsEnumSpecifier")) return true;
|
|
159
|
+
const lt1 = this._input.LT(k);
|
|
160
|
+
if (this._debug) process.stdout.write("IsEnumSpecifier " + lt1);
|
|
161
|
+
const result = lt1.type === CLexer.Enum;
|
|
162
|
+
if (this._debug) console.log(" " + result);
|
|
163
|
+
return result;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
IsFunctionSpecifier() {
|
|
167
|
+
if (this.noSemantics.has("IsFunctionSpecifier")) return true;
|
|
168
|
+
const lt1 = this._input.LT(1);
|
|
169
|
+
const text = lt1.text;
|
|
170
|
+
if (this._debug) process.stdout.write("IsFunctionSpecifier " + lt1);
|
|
171
|
+
const resolved = this._resolveWithOutput(lt1);
|
|
172
|
+
let result = false;
|
|
173
|
+
if (resolved === null) {
|
|
174
|
+
result = false;
|
|
175
|
+
} else if (resolved.classification.has(TypeClassification.FunctionSpecifier_)) {
|
|
176
|
+
result = true;
|
|
177
|
+
} else {
|
|
178
|
+
result = false;
|
|
179
|
+
}
|
|
180
|
+
if (this._debug) console.log("IsFunctionSpecifier " + result);
|
|
181
|
+
return result;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
IsGnuAttributeBeforeDeclarator(k = 1) {
|
|
185
|
+
if (this.noSemantics.has("IsGnuAttributeBeforeDeclarator")) return true;
|
|
186
|
+
const ts = this._input;
|
|
187
|
+
let i = k;
|
|
188
|
+
if (ts.LT(i).type !== CLexer.Attribute) return false;
|
|
189
|
+
i++;
|
|
190
|
+
let depth = 0;
|
|
191
|
+
while (true) {
|
|
192
|
+
const t = ts.LT(i++);
|
|
193
|
+
if (t.type < 0) return false; // EOF
|
|
194
|
+
if (t.type === CLexer.LeftParen) depth++;
|
|
195
|
+
else if (t.type === CLexer.RightParen) { depth--; if (depth === 0) break; }
|
|
196
|
+
}
|
|
197
|
+
const next = ts.LT(i).type;
|
|
198
|
+
return next === CLexer.Identifier || next === CLexer.Star || next === CLexer.LeftParen;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
IsStatement() {
|
|
202
|
+
if (this.noSemantics.has("IsStatement")) return true;
|
|
203
|
+
const t1 = this._input.LT(1);
|
|
204
|
+
const t2 = this._input.LT(2);
|
|
205
|
+
if (this._debug) console.log("IsStatement1 " + t1);
|
|
206
|
+
if (this._debug) console.log("IsStatement2 " + t2);
|
|
207
|
+
if (t1.type === CLexer.Identifier && t2.type === CLexer.Colon) {
|
|
208
|
+
if (this._debug) process.stdout.write("IsStatement3 true");
|
|
209
|
+
return true;
|
|
210
|
+
}
|
|
211
|
+
const result = !this.IsDeclaration();
|
|
212
|
+
if (this._debug) process.stdout.write("IsStatement " + result);
|
|
213
|
+
return result;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
IsStaticAssertDeclaration() {
|
|
217
|
+
if (this.noSemantics.has("IsStaticAssertDeclaration")) return true;
|
|
218
|
+
const token = this._input.LT(1);
|
|
219
|
+
if (this._debug) process.stdout.write("IsStaticAssertDeclaration " + token);
|
|
220
|
+
const result = token.type === CLexer.Static_assert;
|
|
221
|
+
if (this._debug) console.log(" " + result);
|
|
222
|
+
return result;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
IsStorageClassSpecifier() {
|
|
226
|
+
if (this.noSemantics.has("IsStorageClassSpecifier")) return true;
|
|
227
|
+
const lt1 = this._input.LT(1);
|
|
228
|
+
const text = lt1.text;
|
|
229
|
+
if (this._debug) process.stdout.write("IsStorageClassSpecifier " + lt1);
|
|
230
|
+
const resolved = this._resolveWithOutput(lt1);
|
|
231
|
+
let result = false;
|
|
232
|
+
if (resolved === null) {
|
|
233
|
+
result = false;
|
|
234
|
+
} else if (resolved.classification.has(TypeClassification.StorageClassSpecifier_)) {
|
|
235
|
+
result = true;
|
|
236
|
+
} else {
|
|
237
|
+
result = false;
|
|
238
|
+
}
|
|
239
|
+
if (this._debug) console.log(" " + result);
|
|
240
|
+
return result;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
IsStructOrUnionSpecifier(k = 1) {
|
|
244
|
+
if (this.noSemantics.has("IsStructOrUnionSpecifier")) return true;
|
|
245
|
+
const token = this._input.LT(k);
|
|
246
|
+
if (this._debug) process.stdout.write("IsStructOrUnionSpecifier " + token);
|
|
247
|
+
const result = token.type === CLexer.Struct ||
|
|
248
|
+
token.type === CLexer.Union;
|
|
249
|
+
if (this._debug) console.log(" " + result);
|
|
250
|
+
return result;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
IsTypedefName(k = 1) {
|
|
254
|
+
if (this.noSemantics.has("IsTypedefName")) return true;
|
|
255
|
+
const lt1 = this._input.LT(k);
|
|
256
|
+
const text = lt1.text;
|
|
257
|
+
if (this._debug) process.stdout.write("IsTypedefName " + lt1);
|
|
258
|
+
const resolved = this._resolveWithOutput(lt1);
|
|
259
|
+
let result = false;
|
|
260
|
+
if (resolved === null) {
|
|
261
|
+
result = false;
|
|
262
|
+
} else if (resolved.classification.has(TypeClassification.Variable_)) {
|
|
263
|
+
result = false;
|
|
264
|
+
} else if (resolved.classification.has(TypeClassification.Function_)) {
|
|
265
|
+
result = false;
|
|
266
|
+
} else {
|
|
267
|
+
result = true;
|
|
268
|
+
}
|
|
269
|
+
if (this._debug) console.log(" " + result);
|
|
270
|
+
return result;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
IsTypeofSpecifier(k = 1) {
|
|
274
|
+
if (this.noSemantics.has("IsTypeofSpecifier")) return true;
|
|
275
|
+
const token = this._input.LT(k);
|
|
276
|
+
if (this._debug) process.stdout.write("IsTypeofSpecifier " + token);
|
|
277
|
+
const result = token.type === CLexer.Typeof ||
|
|
278
|
+
token.type === CLexer.Typeof_unqual;
|
|
279
|
+
if (this._debug) console.log(" " + result);
|
|
280
|
+
return result;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
IsTypeQualifier(k = 1) {
|
|
284
|
+
if (this.noSemantics.has("IsTypeQualifier")) return true;
|
|
285
|
+
const lt1 = this._input.LT(k);
|
|
286
|
+
const text = lt1.text;
|
|
287
|
+
if (this._debug) process.stdout.write("IsTypeQualifier " + lt1);
|
|
288
|
+
const resolved = this._resolveWithOutput(lt1);
|
|
289
|
+
let result = false;
|
|
290
|
+
if (resolved === null) {
|
|
291
|
+
result = false;
|
|
292
|
+
} else if (resolved.classification.has(TypeClassification.TypeQualifier_)) {
|
|
293
|
+
result = true;
|
|
294
|
+
} else {
|
|
295
|
+
result = false;
|
|
296
|
+
}
|
|
297
|
+
if (this._debug) console.log(" " + result);
|
|
298
|
+
return result;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
IsTypeSpecifier(k = 1) {
|
|
302
|
+
if (this.noSemantics.has("IsTypeSpecifier")) return true;
|
|
303
|
+
const lt1 = this._input.LT(k);
|
|
304
|
+
const text = lt1.text;
|
|
305
|
+
if (this._debug) process.stdout.write("IsTypeSpecifier " + lt1);
|
|
306
|
+
const resolved = this._resolveWithOutput(lt1);
|
|
307
|
+
let result = false;
|
|
308
|
+
if (resolved === null) {
|
|
309
|
+
result = false;
|
|
310
|
+
} else if (resolved.classification.has(TypeClassification.TypeSpecifier_)) {
|
|
311
|
+
result = true;
|
|
312
|
+
} else {
|
|
313
|
+
result = false;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
if (result) {
|
|
317
|
+
if (this._debug) console.log(" " + result);
|
|
318
|
+
return result;
|
|
319
|
+
}
|
|
320
|
+
result = this.IsAtomicTypeSpecifier(k) || this.IsStructOrUnionSpecifier(k) || this.IsEnumSpecifier(k)
|
|
321
|
+
|| this.IsTypedefName(k) || this.IsTypeofSpecifier(k);
|
|
322
|
+
if (this._debug) console.log(" " + result);
|
|
323
|
+
return result;
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
_myGetDeclarationId(y) {
|
|
327
|
+
const token = this._myGetDeclarationToken(y);
|
|
328
|
+
return token !== null ? token.text : null;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
_myGetDeclarationToken(y) {
|
|
332
|
+
if (y === null || y === undefined) return null;
|
|
333
|
+
|
|
334
|
+
const directDeclarator = y.directDeclarator();
|
|
335
|
+
if (directDeclarator !== null && directDeclarator !== undefined) {
|
|
336
|
+
const more = directDeclarator.declarator();
|
|
337
|
+
const token = this._myGetDeclarationToken(more);
|
|
338
|
+
if (token !== null) return token;
|
|
339
|
+
if (directDeclarator.Identifier() !== null && directDeclarator.Identifier() !== undefined) {
|
|
340
|
+
return directDeclarator.Identifier().symbol;
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
return null;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
EnterDeclaration() {
|
|
348
|
+
if (this._debug) console.log("EnterDeclaration");
|
|
349
|
+
let context = this._ctx;
|
|
350
|
+
for (; context !== null; context = context.parentCtx) {
|
|
351
|
+
if (context instanceof CParser.DeclarationContext) {
|
|
352
|
+
if (!isDeclarationContext(context)) {
|
|
353
|
+
continue;
|
|
354
|
+
}
|
|
355
|
+
const declaration_context = context;
|
|
356
|
+
const declaration_specifiers = declaration_context.declarationSpecifiers();
|
|
357
|
+
const declaration_specifier = declaration_specifiers?.declarationSpecifier() ?? null;
|
|
358
|
+
|
|
359
|
+
let is_typedef = false;
|
|
360
|
+
if (declaration_specifier !== null) {
|
|
361
|
+
for (const ds of declaration_specifier) {
|
|
362
|
+
if (ds.storageClassSpecifier()?.Typedef() !== null && ds.storageClassSpecifier()?.Typedef() !== undefined) {
|
|
363
|
+
is_typedef = true;
|
|
364
|
+
break;
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
for (const ds of declaration_specifier) {
|
|
368
|
+
if (ds.storageClassSpecifier()?.Typedef() !== null && ds.storageClassSpecifier()?.Typedef() !== undefined) {
|
|
369
|
+
is_typedef = true;
|
|
370
|
+
break;
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
for (const ds of declaration_specifier) {
|
|
374
|
+
if (ds.storageClassSpecifier()?.Typedef() !== null && ds.storageClassSpecifier()?.Typedef() !== undefined) {
|
|
375
|
+
is_typedef = true;
|
|
376
|
+
break;
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
const init_declaration_list = declaration_context.initDeclaratorList();
|
|
382
|
+
const init_declarators = init_declaration_list?.initDeclarator() ?? null;
|
|
383
|
+
|
|
384
|
+
if (init_declarators !== null) {
|
|
385
|
+
for (const id of init_declarators) {
|
|
386
|
+
const y = id?.declarator() ?? null;
|
|
387
|
+
const idToken = this._myGetDeclarationToken(y);
|
|
388
|
+
if (idToken !== null && idToken !== undefined) {
|
|
389
|
+
const text = idToken.text;
|
|
390
|
+
const loc = this._getSourceLocation(idToken);
|
|
391
|
+
if (is_typedef) {
|
|
392
|
+
const symbol = new Symbol();
|
|
393
|
+
symbol.name = text;
|
|
394
|
+
symbol.classification = new Set([TypeClassification.TypeSpecifier_]);
|
|
395
|
+
symbol.definedFile = loc.file;
|
|
396
|
+
symbol.definedLine = loc.line;
|
|
397
|
+
symbol.definedColumn = loc.column;
|
|
398
|
+
this._st.define(symbol);
|
|
399
|
+
if (this._debug) console.log("New symbol Declaration2 Declarator " + symbol);
|
|
400
|
+
} else {
|
|
401
|
+
const symbol = new Symbol();
|
|
402
|
+
symbol.name = text;
|
|
403
|
+
symbol.classification = new Set([TypeClassification.Variable_]);
|
|
404
|
+
symbol.definedFile = loc.file;
|
|
405
|
+
symbol.definedLine = loc.line;
|
|
406
|
+
symbol.definedColumn = loc.column;
|
|
407
|
+
this._st.define(symbol);
|
|
408
|
+
if (this._debug) console.log("New symbol Declaration3 Declarator " + symbol);
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
if (context instanceof CParser.FunctionDefinitionContext) {
|
|
415
|
+
if (!isFunctionDefinitionContext(context)) {
|
|
416
|
+
continue;
|
|
417
|
+
}
|
|
418
|
+
const fd = context;
|
|
419
|
+
const de = fd.declarator();
|
|
420
|
+
if (de === null || de === undefined) continue;
|
|
421
|
+
const dd = de?.directDeclarator();
|
|
422
|
+
if (dd === null || dd === undefined) continue;
|
|
423
|
+
if (dd !== null && dd.Identifier() !== null && dd.Identifier() !== undefined) {
|
|
424
|
+
const idToken = dd.Identifier().symbol;
|
|
425
|
+
const text = idToken.text;
|
|
426
|
+
const loc = this._getSourceLocation(idToken);
|
|
427
|
+
const symbol = new Symbol();
|
|
428
|
+
symbol.name = text;
|
|
429
|
+
symbol.classification = new Set([TypeClassification.Function_]);
|
|
430
|
+
symbol.definedFile = loc.file;
|
|
431
|
+
symbol.definedLine = loc.line;
|
|
432
|
+
symbol.definedColumn = loc.column;
|
|
433
|
+
this._st.define(symbol);
|
|
434
|
+
if (this._debug) console.log("New symbol Declarationf Declarator " + symbol);
|
|
435
|
+
return;
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
IsNullStructDeclarationListExtension() {
|
|
442
|
+
if (this.noSemantics.has("IsNullStructDeclarationListExtension")) return true;
|
|
443
|
+
return true;
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
EnterScope() {
|
|
447
|
+
if (this._debug) console.log("EnterScope");
|
|
448
|
+
this._st.pushBlockScope();
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
ExitScope() {
|
|
452
|
+
if (this._debug) console.log("ExitScope");
|
|
453
|
+
this._st.popBlockScope();
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
LookupSymbol() {
|
|
457
|
+
const token = this._input.LT(-1);
|
|
458
|
+
if (token === null) return;
|
|
459
|
+
const text = token.text;
|
|
460
|
+
const resolved = this._st.resolve(text);
|
|
461
|
+
if (this.outputAppliedOccurrences && resolved !== null) {
|
|
462
|
+
const appliedLoc = this._getSourceLocation(token);
|
|
463
|
+
process.stderr.write(`Applied occurrence: ${text} at ${appliedLoc.file}:${appliedLoc.line}:${appliedLoc.column} -> Defined at ${resolved.definedFile}:${resolved.definedLine}:${resolved.definedColumn}\n`);
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
OutputSymbolTable() {
|
|
468
|
+
if (this.outputSymbolTable) {
|
|
469
|
+
process.stderr.write(this._st.toString() + "\n");
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
_resolveWithOutput(token) {
|
|
474
|
+
if (token === null || token === undefined) return null;
|
|
475
|
+
const text = token.text;
|
|
476
|
+
const resolved = this._st.resolve(text);
|
|
477
|
+
if (this.outputAppliedOccurrences && resolved !== null) {
|
|
478
|
+
const appliedLoc = this._getSourceLocation(token);
|
|
479
|
+
process.stderr.write(`Applied occurrence: ${text} at ${appliedLoc.file}:${appliedLoc.line}:${appliedLoc.column} -> Defined at ${resolved.definedFile}:${resolved.definedLine}:${resolved.definedColumn}\n`);
|
|
480
|
+
}
|
|
481
|
+
return resolved;
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
_getSourceLocation(token) {
|
|
485
|
+
if (token === null || token === undefined) {
|
|
486
|
+
return { file: "", line: 0, column: 0 };
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
let fileName = "<unknown>";
|
|
490
|
+
const line = token.line;
|
|
491
|
+
const column = token.column;
|
|
492
|
+
let lineAdjusted = line;
|
|
493
|
+
|
|
494
|
+
const ts = this._input;
|
|
495
|
+
const ind = token.tokenIndex;
|
|
496
|
+
|
|
497
|
+
for (let j = ind; j >= 0; j--) {
|
|
498
|
+
const t = ts.get(j);
|
|
499
|
+
if (t === null) break;
|
|
500
|
+
if (t.type === CLexer.LineDirective) {
|
|
501
|
+
const txt = t.text;
|
|
502
|
+
const parts = txt.split(/\s+/);
|
|
503
|
+
if (parts.length >= 3) {
|
|
504
|
+
const dirLine = parseInt(parts[1], 10);
|
|
505
|
+
if (!isNaN(dirLine)) {
|
|
506
|
+
const lineDirective = t.line;
|
|
507
|
+
const lineDiff = line - lineDirective;
|
|
508
|
+
lineAdjusted = lineDiff + dirLine - 1;
|
|
509
|
+
fileName = parts[2].trim();
|
|
510
|
+
if (fileName.startsWith("\"") && fileName.endsWith("\"")) {
|
|
511
|
+
fileName = fileName.substring(1, fileName.length - 1);
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
break;
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
return { file: fileName, line: lineAdjusted, column: column };
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
IsInitDeclaratorList() {
|
|
523
|
+
// Cannot be initDeclaratorList if the first thing is a type.
|
|
524
|
+
// Types need to go to preceding declarationSpecifiers.
|
|
525
|
+
if (this.noSemantics.has("IsInitDeclaratorList")) return true;
|
|
526
|
+
const lt1 = this._input.LT(1);
|
|
527
|
+
const text = lt1.text;
|
|
528
|
+
if (this._debug) process.stdout.write("IsInitDeclaratorList " + lt1);
|
|
529
|
+
const resolved = this._resolveWithOutput(lt1);
|
|
530
|
+
let result = false;
|
|
531
|
+
if (resolved === null) {
|
|
532
|
+
result = true;
|
|
533
|
+
} else if (resolved.classification.has(TypeClassification.TypeQualifier_) || resolved.classification.has(TypeClassification.TypeSpecifier_)) {
|
|
534
|
+
result = false;
|
|
535
|
+
} else {
|
|
536
|
+
result = true;
|
|
537
|
+
}
|
|
538
|
+
if (this._debug) console.log(" " + result);
|
|
539
|
+
return result;
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
IsSomethingOfTypeName() {
|
|
543
|
+
if (this.noSemantics.has("IsSomethingOfTypeName")) return true;
|
|
544
|
+
const ts = this._input;
|
|
545
|
+
if (!(ts.LT(1).type === CLexer.Sizeof ||
|
|
546
|
+
ts.LT(1).type === CLexer.Countof ||
|
|
547
|
+
ts.LT(1).type === CLexer.Alignof ||
|
|
548
|
+
ts.LT(1).type === CLexer.Maxof ||
|
|
549
|
+
ts.LT(1).type === CLexer.Minof)) return false;
|
|
550
|
+
if (ts.LT(2).type !== CLexer.LeftParen) return false;
|
|
551
|
+
if (this.IsTypeName(3)) return true;
|
|
552
|
+
return false;
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
IsTypeName(k = 1) {
|
|
556
|
+
if (this.noSemantics.has("IsTypeName")) return true;
|
|
557
|
+
return this.IsSpecifierQualifierList(k);
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
IsSpecifierQualifierList(k = 1) {
|
|
561
|
+
if (this.noSemantics.has("IsSpecifierQualifierList")) return true;
|
|
562
|
+
if (this.IsGnuAttributeBeforeDeclarator(k)) return true;
|
|
563
|
+
if (this.IsTypeSpecifierQualifier(k)) return true;
|
|
564
|
+
return false;
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
IsCast() {
|
|
568
|
+
let result = false;
|
|
569
|
+
if (this.noSemantics.has("IsCast")) return true;
|
|
570
|
+
const t1 = this._input.LT(1);
|
|
571
|
+
const t2 = this._input.LT(2);
|
|
572
|
+
if (this._debug) console.log("IsCast1 " + t1);
|
|
573
|
+
if (this._debug) console.log("IsCast2 " + t2);
|
|
574
|
+
if (t1.type !== CLexer.LeftParen) {
|
|
575
|
+
if (this._debug) process.stdout.write("IsCast " + result);
|
|
576
|
+
} else if (t2.type !== CLexer.Identifier) {
|
|
577
|
+
result = true;
|
|
578
|
+
} else {
|
|
579
|
+
const resolved = this._resolveWithOutput(t2);
|
|
580
|
+
if (resolved === null) {
|
|
581
|
+
result = false;
|
|
582
|
+
} else if (resolved.classification.has(TypeClassification.TypeSpecifier_)) {
|
|
583
|
+
result = true;
|
|
584
|
+
} else {
|
|
585
|
+
result = false;
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
if (this._debug) process.stdout.write("IsStatement " + result);
|
|
589
|
+
return result;
|
|
590
|
+
}
|
|
591
|
+
}
|