|
| 1 | +import { CodeTokens } from './CodeTokens'; |
| 2 | +import { PythonIdentifiers } from './identifiers/PythonIdentifiers'; |
| 3 | +import { CommonLangIdentifiers } from './identifiers/CommonLangIdentifiers'; |
| 4 | + |
| 5 | +export class CodeTokenizer { |
| 6 | + private pyIdentifiers = new PythonIdentifiers(); |
| 7 | + |
| 8 | + public tokenizeCode(code: string, language: string): CodeTokens[] { |
| 9 | + switch (language) { |
| 10 | + case 'python': |
| 11 | + return this.tokenizePython(code, this.pyIdentifiers); |
| 12 | + default: |
| 13 | + return []; |
| 14 | + } |
| 15 | + } |
| 16 | + |
| 17 | + private tokenizePython( |
| 18 | + code: string, |
| 19 | + languageIdentifier: CommonLangIdentifiers, |
| 20 | + ): CodeTokens[] { |
| 21 | + const lines = code.split('\n'); |
| 22 | + let tokenizedCode: CodeTokens[] = []; |
| 23 | + |
| 24 | + lines.forEach((line) => { |
| 25 | + const trimedLine = line.trim(); |
| 26 | + |
| 27 | + // Function detection |
| 28 | + if ( |
| 29 | + trimedLine.startsWith(languageIdentifier.functionIdentifier()) && |
| 30 | + trimedLine.endsWith(languageIdentifier.endLoopAndCondIdentifier()) |
| 31 | + ) { |
| 32 | + tokenizedCode.push(CodeTokens.FUNC); |
| 33 | + const numberOfParams = |
| 34 | + CodeTokenizer.numberOfFunctionParameters(trimedLine); |
| 35 | + tokenizedCode = CodeTokenizer.pushTokensInTab( |
| 36 | + tokenizedCode, |
| 37 | + CodeTokens.FUNCPARAM, |
| 38 | + numberOfParams, |
| 39 | + ); |
| 40 | + |
| 41 | + // For line |
| 42 | + } else if ( |
| 43 | + trimedLine.startsWith(languageIdentifier.forIdentifier()) && |
| 44 | + trimedLine.endsWith(languageIdentifier.endLoopAndCondIdentifier()) |
| 45 | + ) { |
| 46 | + tokenizedCode.push(CodeTokens.FOR); |
| 47 | + // While line |
| 48 | + } else if ( |
| 49 | + trimedLine.startsWith(languageIdentifier.whileIdentifier()) && |
| 50 | + trimedLine.endsWith(languageIdentifier.endLoopAndCondIdentifier()) |
| 51 | + ) { |
| 52 | + tokenizedCode.push(CodeTokens.WHILE); |
| 53 | + // If line |
| 54 | + } else if ( |
| 55 | + trimedLine.startsWith(languageIdentifier.ifIdentifier()) && |
| 56 | + trimedLine.endsWith(languageIdentifier.endLoopAndCondIdentifier()) |
| 57 | + ) { |
| 58 | + tokenizedCode.push(CodeTokens.IF); |
| 59 | + const andOccurrences = CodeTokenizer.numberTokenPresentInLine( |
| 60 | + languageIdentifier.andIdentifier(), |
| 61 | + trimedLine, |
| 62 | + ); |
| 63 | + tokenizedCode = CodeTokenizer.pushTokensInTab( |
| 64 | + tokenizedCode, |
| 65 | + CodeTokens.AND, |
| 66 | + andOccurrences, |
| 67 | + ); |
| 68 | + |
| 69 | + const orOccurrences = CodeTokenizer.numberTokenPresentInLine( |
| 70 | + languageIdentifier.orIdentifier(), |
| 71 | + trimedLine, |
| 72 | + ); |
| 73 | + tokenizedCode = CodeTokenizer.pushTokensInTab( |
| 74 | + tokenizedCode, |
| 75 | + CodeTokens.OR, |
| 76 | + orOccurrences, |
| 77 | + ); |
| 78 | + |
| 79 | + // Case line |
| 80 | + } else if ( |
| 81 | + trimedLine.startsWith(languageIdentifier.caseIdentifier()) && |
| 82 | + trimedLine.endsWith(languageIdentifier.endLoopAndCondIdentifier()) |
| 83 | + ) { |
| 84 | + tokenizedCode.push(CodeTokens.CASE); |
| 85 | + // Other lines |
| 86 | + } else { |
| 87 | + tokenizedCode.push(CodeTokens.LINE); |
| 88 | + } |
| 89 | + }); |
| 90 | + |
| 91 | + console.log(tokenizedCode); |
| 92 | + |
| 93 | + return tokenizedCode; |
| 94 | + } |
| 95 | + |
| 96 | + private static numberOfFunctionParameters(line: string): number { |
| 97 | + const attributes = line.substring(line.indexOf('('), line.indexOf(')')); |
| 98 | + return this.numberTokenPresentInLine(',', attributes) + 1; |
| 99 | + } |
| 100 | + |
| 101 | + private static numberTokenPresentInLine(token: string, line: string): number { |
| 102 | + return line.split(token).length - 1; |
| 103 | + } |
| 104 | + |
| 105 | + private static pushTokensInTab( |
| 106 | + tab: CodeTokens[], |
| 107 | + token: CodeTokens, |
| 108 | + occurrences: number, |
| 109 | + ): CodeTokens[] { |
| 110 | + for (let i = 0; i < occurrences; i += 1) { |
| 111 | + tab.push(token); |
| 112 | + } |
| 113 | + return tab; |
| 114 | + } |
| 115 | +} |
0 commit comments