1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
|
import { Position, TextDocumentContentChangeEvent, TextEditor, TextLine, workspace } from "vscode";
import ICodeParser from "../../Common/ICodeParser";
import { IDocGen } from "../../Common/IDocGen";
import { Config } from "../../Config";
import { CppArgument } from "./CppArgument";
import { CasingType, CommentType, CppDocGen, SpecialCase } from "./CppDocGen";
import { CppParseTree } from "./CppParseTree";
import { CppToken, CppTokenType } from "./CppToken";
/**
*
* Parses C code for methods and signatures
*
* @export
* @class CParser
* @implements {ICodeParser}
*/
export default class CppParser implements ICodeParser {
/**
* Get the casing of a specified text
*
* @private
* @param {string} name Text to check
* @param {number} validateFrom Check if only a substr is the same casing as the whole string.
* Set to 0 to disable check.
* @returns {CasingType} Detected type of casing
*
* @memberOf CppParser
*/
public static checkCasing(name: string, validateFrom: number): CasingType {
let containsUnderscores = name.indexOf("_") !== -1;
if (containsUnderscores) {
containsUnderscores = name.indexOf("_") !== name.length - 1; // last character _ may be Google style
}
// first letter upper case
let methodCasing: CasingType;
let match = name.match("^([_|\\d|]*[A-Z]).+");
if (match !== null) {
match = name.match("^([A-Z|_|\\d]{2,})");
if (match !== null) {
methodCasing = containsUnderscores ? CasingType.SCREAMING_SNAKE : CasingType.UPPER;
} else {
methodCasing = containsUnderscores ? CasingType.uncertain : CasingType.Pascal;
}
} else {
methodCasing = containsUnderscores ? CasingType.snake : CasingType.camel;
}
if (validateFrom > 0 && methodCasing !== CasingType.uncertain) {
// validate after
switch (methodCasing) {
case CasingType.SCREAMING_SNAKE: {
// Take the leading _ after removing the characters into consideration
const testCasing = this.checkCasing(name.substr(validateFrom + 1), 0);
// screaming or upper
if (testCasing !== CasingType.SCREAMING_SNAKE && testCasing !== CasingType.UPPER) {
methodCasing = CasingType.uncertain;
}
break;
}
case CasingType.snake: {
// Take the leading _ after removing the characters into consideration
const textCheck = name.substr(validateFrom + 1);
const testCasing = this.checkCasing(textCheck, 0);
// snake
if (testCasing !== CasingType.snake) {
if (textCheck.match("([a-z\\d]+)") === null) {
methodCasing = CasingType.uncertain;
}
}
break;
}
case CasingType.Pascal:
case CasingType.camel: {
const testCasing = this.checkCasing(name.substr(validateFrom), 0);
// pascal
if (testCasing !== CasingType.Pascal) {
methodCasing = CasingType.uncertain;
}
break;
}
case CasingType.UPPER: {
const testCasing = this.checkCasing(name.substr(validateFrom), 0);
// upper
if (name.substr(validateFrom).match("([A-Z\\d]+)") === null) {
methodCasing = CasingType.uncertain;
}
break;
}
default: {
break; // No op
}
}
}
return methodCasing;
}
protected activeEditor: TextEditor;
protected activeSelection: Position;
protected readonly cfg: Config;
private typeKeywords: string[];
private stripKeywords: string[];
private keywords: string[];
private attributes: string[];
private lexerVocabulary;
private specialCase: SpecialCase;
private commentType: CommentType;
private casingType: CasingType;
private vscodeAutoGeneratedComment: boolean;
constructor(cfg: Config) {
this.cfg = cfg;
this.typeKeywords = [
"constexpr",
"const",
"struct",
"enum",
];
this.stripKeywords = [
"final",
"static",
"inline",
"friend",
"virtual",
"extern",
"explicit",
"class",
"override",
"typename",
];
this.attributes = [
"noexcept",
"throw",
"alignas",
];
// Non type keywords will be stripped from the final return type.
this.keywords = this.typeKeywords.concat(this.stripKeywords);
this.lexerVocabulary = {
ArraySubscript: (x: string): string => (x.match("^\\[[^\\[]*?\\]") || [])[0],
Arrow: (x: string): string => (x.match("^->") || [])[0],
Assignment: (x: string): string => {
if (!x.match("^=")) {
return undefined;
}
const nesters: Map<string, string> = new Map<string, string>([
["<", ">"], ["(", ")"], ["{", "}"], ["[", "]"],
]);
for (let i = 0; i < x.length; i++) {
const v = nesters.get(x[i]);
if (v !== undefined) {
const startEndOffset: number[] = this.GetSubExprStartEnd(x, i, x[i], v);
if (startEndOffset[1] === 0) {
return undefined;
}
i = startEndOffset[1] - 1;
} else if (x[i] === "\"" || x[i] === "'") {
// Check if raw literal. Since those may have unescaped characters
// but require ()
if (x[i - 1] !== "R") {
// Skip to next end of the string or char literal.
let found: boolean = false;
for (let j = i + 1; j < x.length; j++) {
if (x[j] === x[i] && x[j - 1] !== "\\") {
found = true;
i = j;
break;
}
}
if (!found) {
return undefined;
}
} else {
const startEndOffset: number[] = this.GetSubExprStartEnd(x, i, "(", ")");
if (startEndOffset[1] === 0) {
return undefined;
}
i = startEndOffset[1];
}
} else if (x[i] === "," || x[i] === ")") {
return x.slice(0, i);
}
}
return x;
},
Attribute: (x: string): string => {
const attribute: string = (x.match("^\\[\\[[^\\[]*?\\]\\]") || [])[0];
if (attribute !== undefined) {
return attribute;
}
const foundIndex: number = this.attributes
.findIndex((n: string) => x.startsWith(n) === true);
if (foundIndex === -1) {
return undefined;
}
if (x.slice(this.attributes[foundIndex].length).trim().startsWith("(") === false) {
return x.slice(0, this.attributes[foundIndex].length);
}
const startEndOffset: number[] = this.GetSubExprStartEnd(x, 0, "(", ")");
return startEndOffset[1] === 0 ? undefined : x.slice(0, startEndOffset[1]);
},
CloseParenthesis: (x: string): string => (x.match("^\\)") || [])[0],
Comma: (x: string): string => (x.match("^,") || [])[0],
CommentBlock: (x: string): string => {
if (x.startsWith("/*") === false) {
return undefined;
}
let closeOffset: number = x.indexOf("*/");
closeOffset = closeOffset === -1 ? x.length : closeOffset + 2;
return x.slice(0, closeOffset);
},
CommentLine: (x: string): string => {
if (x.startsWith("//") === false) {
return undefined;
}
let closeOffset: number = x.indexOf("\n");
closeOffset = closeOffset === -1 ? x.length : closeOffset + 1;
return x.slice(0, closeOffset);
},
CurlyBlock: (x: string): string => {
if (x.startsWith("{") === false) {
return undefined;
}
const startEndOffset: number[] = this.GetSubExprStartEnd(x, 0, "{", "}");
return startEndOffset[1] === 0 ? undefined : x.slice(0, startEndOffset[1]);
},
Ellipsis: (x: string): string => (x.match("^\\.\\.\\.") || [])[0],
OpenParenthesis: (x: string): string => (x.match("^\\(") || [])[0],
Pointer: (x: string): string => (x.match("^\\*") || [])[0],
Reference: (x: string): string => (x.match("^&") || [])[0],
Symbol: (x: string): string => {
// Handle specifiers
const specifierFound: number = this.attributes
.findIndex((n: string) => x.startsWith(n) === true);
if (specifierFound !== -1) {
return undefined;
}
// Handle decltype special cases.
if (x.startsWith("decltype") === true) {
const startEndOffset: number[] = this.GetSubExprStartEnd(x, 0, "(", ")");
return startEndOffset[1] === 0 ? undefined : x.slice(0, startEndOffset[1]);
}
// Special case group up the fundamental types with the modifiers.
// tslint:disable-next-line:max-line-length
let reMatch: string = (x.match("^(unsigned|signed|short|long|int|char|double|float)(\\s*(unsigned|signed|short|long|int|char|double|float)\\s)+(?!a-z|A-Z|:|_|\\d)") || [])[0];
if (reMatch !== undefined) {
return reMatch.trim();
}
// Regex to handle a part of all symbols and includes all symbol special cases.
// This is run in a loop because template parts of a symbol can't be parsed using regex.
// Also check if it doesn't start with a number since those are always literals
// tslint:disable-next-line:max-line-length
const symbolRegex: string = "^([a-z|A-Z|:|_|~|\\d]*operator\\s*(\"\"_[a-z|A-Z|_|\\d]+|>>=|<<=|->\\*|\\+=|-=|\\*=|\\/=|%=|\\^=|&=|\\|=|<<|>>|==|!=|<=|->|>=|&&|\\|\\||\\+\\+|--|\\+|-|\\*|\\/|%|\\^|&|\||~|!|=|<|>|,|\\[\\s*\\]|\\(\\s*\\)|(new|delete)\\s*(\\[\\s*\\]){0,1}){0,1}|[a-z|A-Z|:|_|~|\\d]+)";
reMatch = (x.match(symbolRegex) || [])[0];
if (reMatch === undefined || x.match(/^\d/)) {
return undefined;
}
let symbol: string = reMatch;
while (true) {
if (x.slice(symbol.length).trim().startsWith("<") === true) {
const offsets: number[] = this.GetSubExprStartEnd(x, symbol.length, "<", ">");
if (offsets[1] === 0) {
return undefined;
}
symbol = x.slice(0, offsets[1]);
}
reMatch = (x.slice(symbol.length).match(symbolRegex) || [])[0];
if (reMatch === undefined) {
break;
}
symbol += reMatch;
}
return symbol.replace(/\s+$/, "");
},
};
this.specialCase = SpecialCase.none;
this.commentType = CommentType.method;
this.vscodeAutoGeneratedComment = false;
}
/**
* @inheritdoc
*/
public Parse(activeEdit: TextEditor): IDocGen {
this.activeEditor = activeEdit;
this.activeSelection = this.activeEditor.selection.active;
let line: string = "";
try {
line = this.getLogicalLine();
} catch (err) {
// console.dir(err);
}
const templateArgs: string[] = [];
let args: [CppArgument, CppArgument[]] = [new CppArgument(), []];
if (activeEdit.selection.active.line === 0 && line.length === 0) { // head of file
this.commentType = CommentType.file;
} else { // method
// template parsing is simpler by using heuristics rather then CppTokenizing first.
while (line.startsWith("template")) {
const template: string = this.GetTemplate(line);
templateArgs.push.apply(templateArgs, this.GetArgsFromTemplate(template));
line = line.slice(template.length, line.length + 1).trim();
}
try {
args = this.GetReturnAndArgs(line);
} catch (err) {
// console.dir(err);
}
}
if (args[0].name !== null) {
const methodName = args[0].name;
if (methodName.toLowerCase().startsWith("get")) {
this.casingType = CppParser.checkCasing(methodName, 3);
if (this.casingType !== CasingType.uncertain) {
this.specialCase = SpecialCase.getter;
}
} else if (methodName.toLowerCase().startsWith("set")) {
this.casingType = CppParser.checkCasing(methodName, 3);
if (this.casingType !== CasingType.uncertain) {
this.specialCase = SpecialCase.setter;
}
} else if (methodName.toLowerCase().startsWith("create")) {
this.casingType = CppParser.checkCasing(methodName, 6);
if (this.casingType !== CasingType.uncertain) {
this.specialCase = SpecialCase.factoryMethod;
}
}
}
return new CppDocGen(
this.activeEditor,
this.activeSelection,
this.cfg,
templateArgs,
args[0],
args[1],
this.specialCase,
this.commentType,
this.casingType,
this.vscodeAutoGeneratedComment,
);
}
/***************************************************************************
Implementation
***************************************************************************/
private getLogicalLine(): string {
let logicalLine: string = "";
let nextLine: Position = new Position(this.activeSelection.line + 1, this.activeSelection.character);
let nextLineTxt: string = this.activeEditor.document.lineAt(nextLine.line).text.trim();
// VSCode may enter a * on itself, we don"t want that in our method
if (nextLineTxt === "*") {
nextLineTxt = "";
}
let currentNest: number = 0;
logicalLine = nextLineTxt;
// Get method end line
let linesToGet: number = this.cfg.Generic.linesToGet;
while (linesToGet-- > 0) { // Check for end of expression.
nextLine = new Position(nextLine.line + 1, nextLine.character);
nextLineTxt = this.activeEditor.document.lineAt(nextLine.line).text.trim();
// Check if method has finished if curly brace is opened while
// nesting is occuring.
for (let i: number = 0; i < nextLineTxt.length; i++) {
if (nextLineTxt[i] === "(") {
currentNest++;
} else if (nextLineTxt[i] === ")") {
currentNest--;
} else if (nextLineTxt[i] === "{" && currentNest === 0) {
logicalLine += "\n" + nextLineTxt.slice(0, i);
break;
} else if ((nextLineTxt[i] === ";"
|| (nextLineTxt[i] === ":" && nextLineTxt[i - 1] !== ":" && nextLineTxt[i + 1] !== ":"))
&& currentNest === 0) {
logicalLine += "\n" + nextLineTxt.slice(0, i);
break;
}
}
// Head of file probably
if (nextLineTxt.startsWith("#include")) {
this.commentType = CommentType.file;
return "";
}
if (this.isVsCodeAutoComplete(nextLineTxt)) {
logicalLine += "\n" + nextLineTxt;
logicalLine.replace(/\*\//g, "");
}
return logicalLine.trim();
}
throw new Error("More than " + linesToGet + " lines were read from editor and no end of expression was found.");
}
private Tokenize(expression: string): CppToken[] {
const CppTokens: CppToken[] = [];
expression = expression.replace(/^\s+|\s+$/g, "");
while (expression.length !== 0) {
const matches: CppToken[] = Object.keys(this.lexerVocabulary)
.map((k): CppToken => new CppToken(CppTokenType[k], this.lexerVocabulary[k](expression)))
.filter((t) => t.value !== undefined);
if (matches.length === 0) {
throw new Error("Next CppToken couldn\'t be determined: " + expression);
} else if (matches.length > 1) {
throw new Error("Multiple matches for next CppToken: " + expression);
}
CppTokens.push(matches[0]);
expression = expression.slice(matches[0].value.length, expression.length).replace(/^\s+|\s+$/g, "");
}
return CppTokens;
}
private GetReturnAndArgs(line: string): [CppArgument, CppArgument[]] {
if (this.GetArgumentFromCastOperator(line) !== null) {
const opFunc = new CppArgument();
opFunc.name = this.GetArgumentFromCastOperator(line)[1].trim();
opFunc.type.nodes.push(new CppToken(CppTokenType.Symbol, opFunc.name));
return [opFunc, []];
}
// CppTokenize rest of expression and remove comment CppTokens;
const CppTokens: CppToken[] = this.Tokenize(line)
.filter((t) => t.type !== CppTokenType.CommentBlock)
.filter((t) => t.type !== CppTokenType.CommentLine);
// Create hierarchical tree based on the parenthesis.
const tree: CppParseTree = CppParseTree.CreateTree(CppTokens).Compact();
// return argument.
const func = this.GetArgument(tree);
// check if it is a constructor or descructor since these have no name.
// Also reverse the assignment of type and name.
if (func.name === null) {
if (func.type.nodes.length !== 1) {
throw new Error("Too many symbols found for constructor/descructor.");
} else if (func.type.nodes[0] instanceof CppParseTree) {
throw new Error("One node found with just a CppParseTree. Malformed input.");
}
if (line.includes("~")) {
this.specialCase = SpecialCase.destructor;
} else {
this.specialCase = SpecialCase.constructor;
}
func.name = (func.type.nodes[0] as CppToken).value;
func.type.nodes = [];
}
// Get arguments list as a CppParseTree and create arguments from them.
const params = this.GetArgumentList(tree)
.map((a) => this.GetArgument(a));
return [func, params];
}
private RemoveUnusedTokens(tree: CppParseTree): CppParseTree {
tree = tree.Copy();
// First slice of everything after assignment since that will not be used.
const assignmentIndex = tree.nodes
.findIndex((n) => n instanceof CppToken && n.type === CppTokenType.Assignment);
if (assignmentIndex !== -1) {
tree.nodes = tree.nodes.slice(0, assignmentIndex);
}
// Specifiers aren't needed so remove them.
tree.nodes = tree.nodes
.filter((n) => n instanceof CppParseTree || (n instanceof CppToken && n.type !== CppTokenType.Attribute));
return tree;
}
private GetArgumentList(tree: CppParseTree): CppParseTree[] {
const args: CppParseTree[] = [];
tree = this.RemoveUnusedTokens(tree);
let cursor: CppParseTree = tree;
while (this.IsFuncPtr(cursor.nodes) === true) {
cursor = cursor.nodes.find((n) => n instanceof CppParseTree) as CppParseTree;
}
const argTree: CppParseTree = cursor.nodes.find((n) => n instanceof CppParseTree) as CppParseTree;
if (argTree === undefined) {
throw new Error("Function arguments not found.");
}
// Split the argument tree on commas
let arg: CppParseTree = new CppParseTree();
for (const node of argTree.nodes) {
if (node instanceof CppToken && node.type === CppTokenType.Comma) {
args.push(arg);
arg = new CppParseTree();
} else {
arg.nodes.push(node);
}
}
if (arg.nodes.length > 0) {
args.push(arg);
}
return args;
}
private IsFuncPtr(nodes: Array<CppToken | CppParseTree>) {
return nodes.filter((n) => n instanceof CppParseTree).length === 2;
}
private StripNonTypeNodes(tree: CppParseTree) {
tree.nodes = tree.nodes
// All strippable keywords.
.filter((n) => {
return !(n instanceof CppToken
&& n.type === CppTokenType.Symbol
&& this.stripKeywords.find((k) => k === n.value) !== undefined);
});
}
private GetArgumentFromCastOperator(line: string) {
const copy = line;
return copy.match("[explicit|\\s]*\\s*operator\\s*([a-zA-Z].*)\\(\\).*");
}
private GetArgumentFromTrailingReturn(tree: CppParseTree, startTrailingReturn: number): CppArgument {
const argument: CppArgument = new CppArgument();
// Find index of auto prior to the first CppParseTree.
// If auto is not found something is going wrong since trailing return
// requires auto.
let autoIndex: number = -1;
for (let i: number = 0; i < tree.nodes.length; i++) {
const node = tree.nodes[i];
if (node instanceof CppParseTree) {
break;
}
if (node.type === CppTokenType.Symbol && node.value === "auto") {
autoIndex = i;
break;
}
}
if (autoIndex === -1) {
throw new Error("Function declaration has trailing return but type is not auto.");
}
// Get symbol between auto and CppParseTree which is the argument name. It also may not be a keyword.
for (let i: number = autoIndex + 1; i < tree.nodes.length; i++) {
const node = tree.nodes[i];
if (node instanceof CppParseTree) {
break;
}
if (node.type === CppTokenType.Symbol && this.keywords.find((k) => k === node.value) === undefined) {
argument.name = node.value;
break;
}
}
argument.type.nodes = tree.nodes.slice(startTrailingReturn + 1, tree.nodes.length);
this.StripNonTypeNodes(argument.type);
return argument;
}
private GetArgumentFromFuncPtr(tree: CppParseTree): CppArgument {
const argument: CppArgument = new CppArgument();
argument.type = tree;
let cursor: CppParseTree = tree;
while (this.IsFuncPtr(cursor.nodes) === true) {
cursor = cursor.nodes.find((n) => n instanceof CppParseTree) as CppParseTree;
}
// Remove CppParseTree. This can be if it is a function declaration.
const argumentsIndex = cursor.nodes.findIndex((n) => n instanceof CppParseTree);
if (argumentsIndex !== -1) {
cursor.nodes.splice(argumentsIndex, 1);
}
// Find first symbol that is the argument name.
// Remove it from the tree and set the name to the argument name
for (let i: number = 0; i < cursor.nodes.length; i++) {
const node = cursor.nodes[i];
if (node instanceof CppParseTree) {
continue;
}
if (node.type === CppTokenType.Symbol && this.keywords.find((k) => k === node.value) === undefined) {
argument.name = node.value;
cursor.nodes.splice(i, 1);
}
}
this.StripNonTypeNodes(argument.type);
return argument;
}
private GetDefaultArgument(tree: CppParseTree): CppArgument {
const argument: CppArgument = new CppArgument();
for (const node of tree.nodes) {
if (node instanceof CppParseTree) {
break;
}
const symbolCount = argument.type.nodes
.filter((n) => n instanceof CppToken)
.map((n) => n as CppToken)
.filter((n) => n.type === CppTokenType.Symbol)
.filter((n) => this.keywords.find((k) => k === n.value) === undefined)
.length;
if (node.type === CppTokenType.Symbol
&& this.keywords.find((k) => k === node.value) === undefined
) {
if (symbolCount === 1 && argument.name === null) {
argument.name = node.value;
continue;
} else if (symbolCount > 1) {
throw new Error("Too many non keyword symbols.");
}
}
argument.type.nodes.push(node);
}
this.StripNonTypeNodes(argument.type);
return argument;
}
private GetArgument(tree: CppParseTree): CppArgument {
// Copy tree structure leave original untouched.
const copy = this.RemoveUnusedTokens(tree);
// Special case with only ellipsis. C style variadic arguments
if (copy.nodes.length === 1) {
const node = copy.nodes[0];
if (node instanceof CppToken && node.type === CppTokenType.Ellipsis) {
const argument: CppArgument = new CppArgument();
argument.name = node.value;
return argument;
}
}
// Check if it is has a trailing return.
const startTrailingReturn: number = copy.nodes
.findIndex((t) => t instanceof CppToken ? t.type === CppTokenType.Arrow : false);
// Special case trailing return.
if (startTrailingReturn !== -1) {
return this.GetArgumentFromTrailingReturn(copy, startTrailingReturn);
}
// Handle function pointers
if (this.IsFuncPtr(copy.nodes) === true) {
return this.GetArgumentFromFuncPtr(copy);
}
// Handle member pointers
for (let token: number = 0; token < copy.nodes.length - 1; token++) {
const firstToken: CppToken = copy.nodes[token] as CppToken;
const secondToken: CppToken = copy.nodes[token + 1] as CppToken;
if (firstToken.type === CppTokenType.Symbol && secondToken.type === CppTokenType.Pointer &&
firstToken.value.endsWith("::")) {
firstToken.type = CppTokenType.MemberPointer;
}
}
return this.GetDefaultArgument(copy);
}
private GetSubExprStartEnd(expression: string, startSearch: number, openExpr: string, closeExpr: string): number[] {
let openExprOffset: number = -1;
let nestedCount: number = 0;
for (let i: number = startSearch; i < expression.length; i++) {
if (expression[i] === openExpr && openExprOffset === -1) {
openExprOffset = i;
}
if (expression[i] === openExpr) {
nestedCount++;
} else if (expression[i] === closeExpr && nestedCount > 0) {
nestedCount--;
}
if (expression[i] === closeExpr && nestedCount === 0 && openExprOffset !== -1) {
return [openExprOffset, i + 1];
}
}
return [0, 0];
}
private GetTemplate(expression: string): string {
if (expression.startsWith("template") === false) {
return "";
}
let startTemplateOffset: number = -1;
for (let i: number = "template".length; i < expression.length; i++) {
if (expression[i] === "<") {
startTemplateOffset = i;
break;
} else if (expression[i] !== " ") {
return "";
}
}
if (startTemplateOffset === -1) {
return "";
}
const [start, end] = this.GetSubExprStartEnd(expression, startTemplateOffset, "<", ">");
return expression.slice(0, end);
}
private GetArgsFromTemplate(template: string): string[] {
const args: string[] = [];
if (template === "") {
return args;
}
// Remove <> and add a comma to the end to remove edge case.
template = template.slice(template.indexOf("<") + 1, template.lastIndexOf(">")).replace(/^\s+|\s+$/g, "") + ",";
const nestedCounts: { [key: string]: number; } = {
"(": 0,
"<": 0,
"{": 0,
};
let lastSeparator: number = 0;
for (let i: number = 0; i < template.length; i++) {
const notInSubExpr: boolean = nestedCounts["<"] === 0
&& nestedCounts["("] === 0
&& nestedCounts["{"] === 0;
if (notInSubExpr === true && template[i] === ",") {
args.push(template.slice(lastSeparator + 1, i).replace(/^\s+|\s+$/g, ""));
} else if (notInSubExpr === true && (template[i] === " " || template[i] === ".")) {
lastSeparator = i;
}
if (template[i] === "(") {
nestedCounts["("]++;
} else if (template[i] === ")" && nestedCounts["("] > 0) {
nestedCounts["("]--;
} else if (template[i] === "<") {
nestedCounts["<"]++;
} else if (template[i] === ">" && nestedCounts["<"] > 0) {
nestedCounts["<"]--;
} else if (template[i] === "{") {
nestedCounts["{"]++;
} else if (template[i] === "}" && nestedCounts["{"] > 0) {
nestedCounts["{"]--;
}
}
return args;
}
private isVsCodeAutoComplete(line: string): boolean {
switch (line) {
case "*/":
this.vscodeAutoGeneratedComment = true;
return true;
default:
return false;
}
}
}
|