|
| 1 | +import { Token, TokenType } from './tokenizer.js'; |
| 2 | +import { WarningLevel, MalformationIssue } from './types.js'; |
| 3 | + |
| 4 | +/** |
| 5 | + * Malformation checker for tokens |
| 6 | + */ |
| 7 | +export class MalformationChecker { |
| 8 | + private issues: MalformationIssue[] = []; |
| 9 | + |
| 10 | + /** |
| 11 | + * Check tokens for structural malformations |
| 12 | + * @param tokens - Array of tokens to check |
| 13 | + * @returns Array of malformation issues |
| 14 | + */ |
| 15 | + checkMalformations(tokens: Token[]): MalformationIssue[] { |
| 16 | + this.issues = []; |
| 17 | + |
| 18 | + // Skip whitespace tokens upfront for faster processing |
| 19 | + const nonWhitespaceTokens = tokens.filter(token => token.type !== TokenType.WHITESPACE); |
| 20 | + |
| 21 | + this.checkBraceBalanceFast(nonWhitespaceTokens); |
| 22 | + this.checkEmptyParametersFast(nonWhitespaceTokens); |
| 23 | + this.checkUnexpectedTokensFast(nonWhitespaceTokens); |
| 24 | + this.checkStructuralIssuesFast(nonWhitespaceTokens); |
| 25 | + |
| 26 | + return this.issues; |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * Fast brace balance checking |
| 31 | + */ |
| 32 | + private checkBraceBalanceFast(nonWhitespaceTokens: Token[]): void { |
| 33 | + let braceDepth = 0; |
| 34 | + |
| 35 | + for (const token of nonWhitespaceTokens) { |
| 36 | + if (token.type === TokenType.OPEN_BRACE) { |
| 37 | + braceDepth++; |
| 38 | + } else if (token.type === TokenType.CLOSE_BRACE) { |
| 39 | + braceDepth--; |
| 40 | + if (braceDepth < 0) { |
| 41 | + this.addIssue('Extra closing brace (unmatched optional block closure)', WarningLevel.LEVEL_1); |
| 42 | + braceDepth = 0; // Reset to prevent cascade errors |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + if (braceDepth > 0) { |
| 48 | + this.addIssue(`Unclosed optional block (missing ${braceDepth} closing brace${braceDepth > 1 ? 's' : ''})`, WarningLevel.LEVEL_1); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Fast empty parameter checking |
| 54 | + */ |
| 55 | + private checkEmptyParametersFast(nonWhitespaceTokens: Token[]): void { |
| 56 | + for (let i = 0; i < nonWhitespaceTokens.length; i++) { |
| 57 | + const token = nonWhitespaceTokens[i]; |
| 58 | + const nextToken = nonWhitespaceTokens[i + 1]; |
| 59 | + const prevToken = nonWhitespaceTokens[i - 1]; |
| 60 | + |
| 61 | + if (token.type === TokenType.SEMICOLON) { |
| 62 | + // Check for double semicolon |
| 63 | + if (nextToken && nextToken.type === TokenType.SEMICOLON) { |
| 64 | + this.addIssue('Empty parameter found (double semicolon)', WarningLevel.LEVEL_1); |
| 65 | + } |
| 66 | + |
| 67 | + // Check for semicolon at start |
| 68 | + if (!prevToken) { |
| 69 | + this.addIssue('Empty parameter found (semicolon at start)', WarningLevel.LEVEL_1); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Fast unexpected token checking |
| 77 | + */ |
| 78 | + private checkUnexpectedTokensFast(nonWhitespaceTokens: Token[]): void { |
| 79 | + for (let i = 0; i < nonWhitespaceTokens.length; i++) { |
| 80 | + const token = nonWhitespaceTokens[i]; |
| 81 | + const nextToken = nonWhitespaceTokens[i + 1]; |
| 82 | + const prevToken = nonWhitespaceTokens[i - 1]; |
| 83 | + |
| 84 | + if (token.type === TokenType.COLON) { |
| 85 | + // Check for colon without parameter name |
| 86 | + if (!prevToken || (prevToken.type !== TokenType.PARAMETER_NAME && prevToken.type !== TokenType.SPREAD)) { |
| 87 | + this.addIssue('Unexpected colon (missing parameter name)', WarningLevel.LEVEL_1); |
| 88 | + } |
| 89 | + |
| 90 | + // Check for double colon |
| 91 | + if (nextToken && nextToken.type === TokenType.COLON) { |
| 92 | + this.addIssue('Double colon found in parameter definition', WarningLevel.LEVEL_1); |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Fast structural issue checking |
| 100 | + */ |
| 101 | + private checkStructuralIssuesFast(nonWhitespaceTokens: Token[]): void { |
| 102 | + // Check for malformed parameter names (containing asterisks) |
| 103 | + for (const token of nonWhitespaceTokens) { |
| 104 | + if (token.type === TokenType.PARAMETER_NAME && token.value.includes('*')) { |
| 105 | + this.addIssue(`Parameter name '${token.value}' contains asterisks (likely malformed markup)`, WarningLevel.LEVEL_1); |
| 106 | + } |
| 107 | + |
| 108 | + // Check for invalid characters in type definitions |
| 109 | + if (token.type === TokenType.TYPE && token.value.includes('/')) { |
| 110 | + this.addIssue(`Type definition '${token.value}' contains invalid forward slash characters`, WarningLevel.LEVEL_1); |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Add a malformation issue |
| 117 | + * @param message - Error message |
| 118 | + * @param level - Warning level |
| 119 | + */ |
| 120 | + private addIssue(message: string, level: WarningLevel): void { |
| 121 | + this.issues.push({ |
| 122 | + message, |
| 123 | + level |
| 124 | + }); |
| 125 | + } |
| 126 | +} |
0 commit comments