Skip to content

Commit 28ecf90

Browse files
committed
add checks for same regex on operators
1 parent d70b852 commit 28ecf90

File tree

1 file changed

+42
-1
lines changed

1 file changed

+42
-1
lines changed

src/diagnostic.ts

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
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';
33
import { existsSync, readFileSync, statSync } from 'fs';
44
import { sysparser, syxparser } from './ast.js';
55
import { tokenizeSys, tokenizeSyx } from './lexer.js';
6+
import { CompilerFunctions } from './compiler.js';
67
import { dictionary } from './dictionary/index.js';
78
import { fileURLToPath } from 'url';
89
import { join } from 'path';
@@ -35,6 +36,7 @@ export function createSyntaxScriptDiagnosticReport(filePath: string, fileContent
3536
items.push(...ruleConflictCheck(ast, filePath));
3637
items.push(...sameRuleCheck(ast, filePath));
3738
items.push(...importedExistentCheck(ast, filePath));
39+
items.push(...sameRegexCheck(ast,filePath));
3840
} catch (error) {
3941
if (isCompilerError(error)) {
4042
items.push({
@@ -218,6 +220,45 @@ function importedExistentCheck(ast: ProgramStatement, filePath: string): Diagnos
218220
return items;
219221
}
220222

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+
221262
// Checks if every exported statement it actually exportable
222263
function exportableCheck(statements: Statement[], filePath: string): Diagnostic[] {
223264

0 commit comments

Comments
 (0)