|
1 | 1 | import { CodeAction, CodeActionKind, Diagnostic, DiagnosticSeverity, DocumentDiagnosticReportKind, FullDocumentDiagnosticReport, Range } from 'lsp-types'; |
2 | | -import { GlobalStatement, ImportStatement, NodeType, OperatorStatement, ProgramStatement, RuleStatement, Statement, TokenType, isCompilerError, statementIsA } from './types.js'; |
| 2 | +import { FunctionStatement, GlobalStatement, ImportStatement, KeywordStatement, 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'; |
@@ -37,6 +37,7 @@ export function createSyntaxScriptDiagnosticReport(filePath: string, fileContent |
37 | 37 | items.push(...sameRuleCheck(ast, filePath)); |
38 | 38 | items.push(...importedExistentCheck(ast, filePath)); |
39 | 39 | items.push(...sameRegexCheck(ast,filePath)); |
| 40 | + items.push(...sameNameCheck(ast.body,filePath)); |
40 | 41 | } catch (error) { |
41 | 42 | if (isCompilerError(error)) { |
42 | 43 | items.push({ |
@@ -294,6 +295,41 @@ function exportableCheck(statements: Statement[], filePath: string): Diagnostic[ |
294 | 295 | return items; |
295 | 296 | } |
296 | 297 |
|
| 298 | +// Check if everything has a unique name |
| 299 | +function sameNameCheck(statements: Statement[], filePath: string): Diagnostic[] { |
| 300 | + const items:Diagnostic[] = []; |
| 301 | + |
| 302 | + function c(s:Statement[]){ |
| 303 | + const encounteredNames = []; |
| 304 | + |
| 305 | + s |
| 306 | + .filter(r=>statementIsA(r,NodeType.Function)||statementIsA(r,NodeType.Global)||statementIsA(r,NodeType.Keyword)) |
| 307 | + .map(r=>{ |
| 308 | + if(statementIsA(r,NodeType.Function))return r as FunctionStatement; |
| 309 | + if(statementIsA(r,NodeType.Global))return r as GlobalStatement; |
| 310 | + if(statementIsA(r,NodeType.Keyword))return r as KeywordStatement; |
| 311 | + }).forEach(stmt=>{ |
| 312 | + |
| 313 | + const n = stmt[statementIsA(stmt,NodeType.Keyword)?'word':'name']; |
| 314 | + |
| 315 | + if(encounteredNames.includes(n)) items.push({ |
| 316 | + message:`Name '${n}' is already seen before.`, |
| 317 | + range:subRange(stmt.range), |
| 318 | + source:'syntax-script', |
| 319 | + severity:DiagnosticSeverity.Error |
| 320 | + }); |
| 321 | + else encounteredNames.push(n); |
| 322 | + |
| 323 | + if(statementIsA(stmt,NodeType.Global)) c(stmt.body); |
| 324 | + }); |
| 325 | + |
| 326 | + } |
| 327 | + |
| 328 | + c(statements); |
| 329 | + |
| 330 | + return items; |
| 331 | +} |
| 332 | + |
297 | 333 | /** |
298 | 334 | * Modifies the given range to be zero-based. |
299 | 335 | * @param {Range} r Any range. |
|
0 commit comments