/** * Simple JavaScript/TypeScript Tokenizer (Lexer) * Zero-dependency tokenizer for accurate code analysis * * Converts source code into tokens to avoid false positives from regex-based scanning */ class Tokenizer { constructor() { this.keywords = new Set([ 'if', 'else', 'for', 'while', 'do', 'switch', 'case', 'break', 'continue', 'return', 'function', 'const', 'let', 'var', 'class', 'import', 'export', 'async', 'await', 'try', 'catch', 'finally', 'throw', 'new', 'typeof', 'instanceof', 'delete', 'void', 'this', 'super', 'static', 'extends', 'implements', 'interface', 'type', 'enum', 'namespace', 'module' ]); this.operators = new Set([ '+', '-', '*', '/', '%', '=', '==', '===', '!=', '!==', '<', '>', '<=', '>=', '&&', '||', '!', '?', ':', '++', '--', '+=', '-=', '*=', '/=', '%=', '&', '|', '^', '~', '<<', '>>', '>>>', '&=', '|=', '^=', '<<=', '>>=', '>>>=', '=>', '...', '?.', '??', '??=' ]); } /** * Tokenize source code into array of tokens * @param {string} code - Source code to tokenize * @param {string} filePath - File path for context * @returns {Array} Array of tokens with type, value, line, column */ tokenize(code, filePath = 'unknown') { const tokens = []; let line = 1; let column = 1; let i = 0; while (i < code.length) { const char = code[i]; // Skip whitespace if (this.isWhitespace(char)) { if (char === '\n') { line++; column = 1; } else { column++; } i++; continue; } // Single-line comment: // ... if (char === '/' && code[i + 1] === '/') { const start = i; const startColumn = column; i += 2; column += 2; let value = ''; while (i < code.length && code[i] !== '\n') { value += code[i]; i++; column++; } tokens.push({ type: 'COMMENT', subtype: 'SINGLE_LINE', value: value.trim(), line, column: startColumn, start, end: i }); continue; } // Multi-line comment: /* ... */ if (char === '/' && code[i + 1] === '*') { const start = i; const startLine = line; const startColumn = column; i += 2; column += 2; let value = ''; while (i < code.length - 1) { if (code[i] === '*' && code[i + 1] === '/') { i += 2; column += 2; break; } if (code[i] === '\n') { line++; column = 1; } else { column++; } value += code[i]; i++; } tokens.push({ type: 'COMMENT', subtype: 'MULTI_LINE', value: value.trim(), line: startLine, column: startColumn, start, end: i }); continue; } // String literals: "..." or '...' or `...` if (char === '"' || char === "'" || char === '`') { const quote = char; const start = i; const startColumn = column; i++; column++; let value = ''; let escaped = false; while (i < code.length) { const c = code[i]; if (escaped) { value += c; escaped = false; } else if (c === '\\') { escaped = true; value += c; } else if (c === quote) { i++; column++; break; } else { if (c === '\n') { line++; column = 1; } else { column++; } value += c; } i++; } tokens.push({ type: 'STRING', quote, value, line, column: startColumn, start, end: i }); continue; } // Numbers: 123, 0x1A, 0b1010, 12.34, 1e10 if (this.isDigit(char)) { const start = i; const startColumn = column; let value = ''; // Hex: 0x... if (char === '0' && (code[i + 1] === 'x' || code[i + 1] === 'X')) { value += code[i] + code[i + 1]; i += 2; column += 2; while (i < code.length && this.isHexDigit(code[i])) { value += code[i]; i++; column++; } } // Binary: 0b... else if (char === '0' && (code[i + 1] === 'b' || code[i + 1] === 'B')) { value += code[i] + code[i + 1]; i += 2; column += 2; while (i < code.length && (code[i] === '0' || code[i] === '1')) { value += code[i]; i++; column++; } } // Decimal else { while (i < code.length && (this.isDigit(code[i]) || code[i] === '.')) { value += code[i]; i++; column++; } // Scientific notation: 1e10, 2.5e-3 if (i < code.length && (code[i] === 'e' || code[i] === 'E')) { value += code[i]; i++; column++; if (code[i] === '+' || code[i] === '-') { value += code[i]; i++; column++; } while (i < code.length && this.isDigit(code[i])) { value += code[i]; i++; column++; } } } tokens.push({ type: 'NUMBER', value, line, column: startColumn, start, end: i }); continue; } // Identifiers and Keywords if (this.isIdentifierStart(char)) { const start = i; const startColumn = column; let value = ''; while (i < code.length && this.isIdentifierPart(code[i])) { value += code[i]; i++; column++; } const type = this.keywords.has(value) ? 'KEYWORD' : 'IDENTIFIER'; tokens.push({ type, value, line, column: startColumn, start, end: i }); continue; } // Operators and Punctuation const start = i; const startColumn = column; // Try 3-char operators first if (i + 2 < code.length) { const threeChar = code.substring(i, i + 3); if (this.operators.has(threeChar)) { tokens.push({ type: 'OPERATOR', value: threeChar, line, column: startColumn, start, end: i + 3 }); i += 3; column += 3; continue; } } // Try 2-char operators if (i + 1 < code.length) { const twoChar = code.substring(i, i + 2); if (this.operators.has(twoChar)) { tokens.push({ type: 'OPERATOR', value: twoChar, line, column: startColumn, start, end: i + 2 }); i += 2; column += 2; continue; } } // Single char operators/punctuation if (this.operators.has(char) || this.isPunctuation(char)) { tokens.push({ type: this.isPunctuation(char) ? 'PUNCTUATION' : 'OPERATOR', value: char, line, column: startColumn, start, end: i + 1 }); i++; column++; continue; } // Unknown character - skip i++; column++; } return tokens; } /** * Filter out comments and strings for accurate code analysis */ filterCodeTokens(tokens) { return tokens.filter(t => t.type !== 'COMMENT' && t.type !== 'STRING'); } /** * Get tokens by type */ getTokensByType(tokens, type) { return tokens.filter(t => t.type === type); } /** * Count keyword occurrences (accurate complexity calculation) */ countKeywords(tokens, keywords) { const codeTokens = this.filterCodeTokens(tokens); let count = 0; for (const token of codeTokens) { if (token.type === 'KEYWORD' && keywords.includes(token.value)) { count++; } } return count; } /** * Find TODO/FIXME/BUG comments with accurate line numbers */ findAnnotations(tokens) { const annotations = []; const commentTokens = tokens.filter(t => t.type === 'COMMENT'); for (const token of commentTokens) { const value = token.value.toUpperCase(); let type = null; if (value.includes('TODO:') || value.startsWith('TODO ')) { type = 'TODO'; } else if (value.includes('FIXME:') || value.startsWith('FIXME ')) { type = 'FIXME'; } else if (value.includes('BUG:') || value.startsWith('BUG ')) { type = 'BUG'; } else if (value.includes('HACK:') || value.startsWith('HACK ')) { type = 'HACK'; } else if (value.includes('XXX:') || value.startsWith('XXX ')) { type = 'XXX'; } if (type) { annotations.push({ type, message: token.value, line: token.line, column: token.column }); } } return annotations; } // Helper methods isWhitespace(char) { return char === ' ' || char === '\t' || char === '\n' || char === '\r'; } isDigit(char) { return char >= '0' && char <= '9'; } isHexDigit(char) { return (char >= '0' && char <= '9') || (char >= 'a' && char <= 'f') || (char >= 'A' && char <= 'F'); } isIdentifierStart(char) { return (char >= 'a' && char <= 'z') || (char >= 'A' && char <= 'Z') || char === '_' || char === '$'; } isIdentifierPart(char) { return this.isIdentifierStart(char) || this.isDigit(char); } isPunctuation(char) { return '(){}[],.;:'.includes(char); } } module.exports = Tokenizer;