|
1 | 1 | import { CodeAction, CodeActionKind, Diagnostic, DiagnosticSeverity, DocumentDiagnosticReportKind, FullDocumentDiagnosticReport, Range } from 'lsp-types'; |
2 | | -import { GlobalStatement, ImportStatement, NodeType, ProgramStatement, RuleStatement, Statement, TokenType, isCompilerError, statementIsA } from './types.js'; |
| 2 | +import { GlobalStatement, ImportStatement, NodeType, OperatorStatement, ProgramStatement, RuleStatement, Statement, TokenType, isCompilerError, statementIsA } from './types.js'; |
3 | 3 | import { existsSync, readFileSync, statSync } from 'fs'; |
4 | 4 | import { sysparser, syxparser } from './ast.js'; |
5 | 5 | import { tokenizeSys, tokenizeSyx } from './lexer.js'; |
| 6 | +import { CompilerFunctions } from './compiler.js'; |
6 | 7 | import { dictionary } from './dictionary/index.js'; |
7 | 8 | import { fileURLToPath } from 'url'; |
8 | 9 | import { join } from 'path'; |
@@ -35,6 +36,7 @@ export function createSyntaxScriptDiagnosticReport(filePath: string, fileContent |
35 | 36 | items.push(...ruleConflictCheck(ast, filePath)); |
36 | 37 | items.push(...sameRuleCheck(ast, filePath)); |
37 | 38 | items.push(...importedExistentCheck(ast, filePath)); |
| 39 | + items.push(...sameRegexCheck(ast,filePath)); |
38 | 40 | } catch (error) { |
39 | 41 | if (isCompilerError(error)) { |
40 | 42 | items.push({ |
@@ -218,6 +220,45 @@ function importedExistentCheck(ast: ProgramStatement, filePath: string): Diagnos |
218 | 220 | return items; |
219 | 221 | } |
220 | 222 |
|
| 223 | +// Checks if there are multiple operators with the same regex |
| 224 | +function sameRegexCheck(ast:ProgramStatement, filePath:string): Diagnostic[] { |
| 225 | + const items:Diagnostic[] = []; |
| 226 | + |
| 227 | + const encounteredRegexes:RegExp[] = []; |
| 228 | + |
| 229 | + ast.body.filter(r=>statementIsA(r,NodeType.Operator)).map(r => r as OperatorStatement).forEach(stmt=>{ |
| 230 | + |
| 231 | + const regex = new RegExp(CompilerFunctions.generateRegexMatcher(stmt)); |
| 232 | + |
| 233 | + if(encounteredRegexes.some(r=>r.source===regex.source)) items.push({ |
| 234 | + message:'Regex of this operator is same with another operator.', |
| 235 | + range:subRange(syxparser.combineTwo(stmt.regex[0].range,stmt.regex[stmt.regex.length-1].range)), |
| 236 | + severity:DiagnosticSeverity.Error, |
| 237 | + source:'syntax-script', |
| 238 | + data:[ |
| 239 | + { |
| 240 | + title:'Remove this operator', |
| 241 | + kind:CodeActionKind.QuickFix, |
| 242 | + edit:{ |
| 243 | + changes:{ |
| 244 | + [filePath]:[ |
| 245 | + { |
| 246 | + newText:'', |
| 247 | + range:subRange(stmt.range) |
| 248 | + } |
| 249 | + ] |
| 250 | + } |
| 251 | + } |
| 252 | + } |
| 253 | + ] as CodeAction[] |
| 254 | + }); |
| 255 | + else encounteredRegexes.push(regex); |
| 256 | + |
| 257 | + }); |
| 258 | + |
| 259 | + return items; |
| 260 | +} |
| 261 | + |
221 | 262 | // Checks if every exported statement it actually exportable |
222 | 263 | function exportableCheck(statements: Statement[], filePath: string): Diagnostic[] { |
223 | 264 |
|
|
0 commit comments