Skip to content

Commit 8eefc07

Browse files
committed
feat: implement cpp tokenization
1 parent 4a774f1 commit 8eefc07

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed

src/code-quality/tokenizer/CodeTokenizer.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,23 @@ import { CodeTokens } from './CodeTokens';
22
import { PythonIdentifiers } from './identifiers/PythonIdentifiers';
33
import { CommonLangIdentifiers } from './identifiers/CommonLangIdentifiers';
44
import { GolangIdentifiers } from './identifiers/GolangIdentifiers';
5+
import { CppIdentifiers } from './identifiers/CppIdentifiers';
56

67
export class CodeTokenizer {
78
private pyIdentifiers = new PythonIdentifiers();
89

910
private golangIdentifiers = new GolangIdentifiers();
1011

12+
private cppIdentifiers = new CppIdentifiers();
13+
1114
public tokenize(code: string, language: string): CodeTokens[] {
1215
switch (language) {
1316
case 'python':
1417
return this.tokenizeCode(code, this.pyIdentifiers);
1518
case 'go':
1619
return this.tokenizeCode(code, this.golangIdentifiers);
1720
case 'cpp':
18-
return [];
21+
return this.tokenizeCode(code, this.cppIdentifiers);
1922
default:
2023
return [];
2124
}
@@ -32,10 +35,7 @@ export class CodeTokenizer {
3235
const trimmedLine = line.trim();
3336

3437
if (trimmedLine !== '' && trimmedLine !== '}')
35-
if (
36-
trimmedLine.startsWith(languageIdentifier.functionIdentifier()) &&
37-
trimmedLine.endsWith(languageIdentifier.endLoopAndCondIdentifier())
38-
) {
38+
if (languageIdentifier.isFunction(trimmedLine)) {
3939
// Function detection
4040
tokenizedCode.push(CodeTokens.FUNC);
4141
const numberOfParams =

src/code-quality/tokenizer/identifiers/CommonLangIdentifiers.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,11 @@ export class CommonLangIdentifiers {
3030
endLoopAndCondIdentifier(): string {
3131
return '{';
3232
}
33+
34+
isFunction(line: string): boolean {
35+
return (
36+
line.startsWith(this.functionIdentifier()) &&
37+
line.endsWith(this.endLoopAndCondIdentifier())
38+
);
39+
}
3340
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { CommonLangIdentifiers } from './CommonLangIdentifiers';
2+
3+
export class CppIdentifiers extends CommonLangIdentifiers {
4+
isFunction(line: string): boolean {
5+
return (
6+
(line.startsWith('void') ||
7+
line.startsWith('char') ||
8+
line.startsWith('int') ||
9+
line.startsWith('float') ||
10+
line.startsWith('double')) &&
11+
line.endsWith('{')
12+
);
13+
}
14+
}

0 commit comments

Comments
 (0)