diagnostics.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import * as vscode from 'vscode';
  2. const MODULE_HEADER = /\b(DEFINITION\s+)?MODULE\s+([A-Za-z_][A-Za-z0-9_]*)/;
  3. const END_MODULE = /\bEND\s+([A-Za-z_][A-Za-z0-9_]*)\s*(\.)?/g;
  4. interface ScanResult {
  5. diagnostics: vscode.Diagnostic[];
  6. code: string;
  7. }
  8. function positionAt(document: vscode.TextDocument, offset: number): vscode.Position {
  9. return document.positionAt(Math.max(0, Math.min(offset, document.getText().length)));
  10. }
  11. function rangeAt(document: vscode.TextDocument, start: number, end: number): vscode.Range {
  12. return new vscode.Range(positionAt(document, start), positionAt(document, end));
  13. }
  14. function diagnostic(
  15. document: vscode.TextDocument,
  16. start: number,
  17. end: number,
  18. message: string,
  19. severity = vscode.DiagnosticSeverity.Error
  20. ): vscode.Diagnostic {
  21. const item = new vscode.Diagnostic(rangeAt(document, start, end), message, severity);
  22. item.source = 'Oberon';
  23. return item;
  24. }
  25. /**
  26. * Lightweight diagnostics used alongside the Coco/R dialect validators.
  27. *
  28. * Only constructs recognisable without pretending to be a complete
  29. * Oberon parser: unterminated comments/strings, the module header, and
  30. * the final `END name.` mismatch. Note Oberon is case-sensitive, so the
  31. * header match is exact. Real syntax diagnostics come from the
  32. * configured dialect validator.
  33. */
  34. function scan(document: vscode.TextDocument): ScanResult {
  35. const text = document.getText();
  36. const diagnostics: vscode.Diagnostic[] = [];
  37. let code = '';
  38. let i = 0;
  39. while (i < text.length) {
  40. const ch = text[i];
  41. const next = text[i + 1];
  42. if (ch === '(' && next === '*') {
  43. const start = i;
  44. i += 2;
  45. let depth = 1;
  46. while (i < text.length && depth > 0) {
  47. if (text[i] === '(' && text[i + 1] === '*') { depth++; i += 2; continue; }
  48. if (text[i] === '*' && text[i + 1] === ')') { depth--; i += 2; continue; }
  49. i++;
  50. }
  51. if (depth > 0) {
  52. diagnostics.push(diagnostic(document, start, Math.min(start + 2, text.length), 'Unterminated Oberon comment.'));
  53. }
  54. code += ' ';
  55. continue;
  56. }
  57. if (ch === '"' || ch === "'") {
  58. const start = i;
  59. i++;
  60. let closed = false;
  61. while (i < text.length && text[i] !== '\n') {
  62. if (text[i] === ch) { i++; closed = true; break; }
  63. i++;
  64. }
  65. if (!closed) {
  66. diagnostics.push(diagnostic(document, start, Math.min(start + 1, text.length), 'Unterminated string literal.'));
  67. }
  68. code += ' ';
  69. continue;
  70. }
  71. code += ch;
  72. i++;
  73. }
  74. const header = MODULE_HEADER.exec(code);
  75. if (!header) {
  76. diagnostics.push(diagnostic(document, 0, Math.min(code.length, 1), 'Expected a MODULE header.'));
  77. return { diagnostics, code };
  78. }
  79. const moduleName = header[2];
  80. const headerEnd = header.index + header[0].length;
  81. const semicolon = code.indexOf(';', headerEnd);
  82. if (semicolon < 0) {
  83. diagnostics.push(diagnostic(document, headerEnd, Math.min(headerEnd + moduleName.length, code.length), 'Expected ";" after the module header.'));
  84. }
  85. END_MODULE.lastIndex = 0;
  86. let match: RegExpExecArray | null = null;
  87. let lastEnd: RegExpExecArray | null = null;
  88. while ((match = END_MODULE.exec(code)) !== null) {
  89. lastEnd = match;
  90. }
  91. if (!lastEnd) {
  92. diagnostics.push(diagnostic(document, Math.max(0, code.trimEnd().length - 1), code.length, `Expected "END ${moduleName}." at the end of the module.`));
  93. } else {
  94. const endName = lastEnd[1];
  95. const nameStart = lastEnd.index + lastEnd[0].indexOf(endName);
  96. // Case-sensitive: END Name must match MODULE Name exactly.
  97. if (endName !== moduleName) {
  98. diagnostics.push(diagnostic(document, nameStart, nameStart + endName.length, `Module name "${moduleName}" expected after END, found "${endName}".`));
  99. }
  100. if (!lastEnd[2]) {
  101. const dot = lastEnd.index + lastEnd[0].length;
  102. diagnostics.push(diagnostic(document, dot, Math.min(dot + 1, code.length), `Expected "." after END ${endName}.`));
  103. }
  104. }
  105. return { diagnostics, code };
  106. }
  107. export function createDiagnostics(context: vscode.ExtensionContext): void {
  108. const collection = vscode.languages.createDiagnosticCollection('oberon');
  109. context.subscriptions.push(collection);
  110. const update = (document: vscode.TextDocument): void => {
  111. if (document.languageId !== 'oberon') {
  112. return;
  113. }
  114. collection.set(document.uri, scan(document).diagnostics);
  115. };
  116. for (const document of vscode.workspace.textDocuments) {
  117. update(document);
  118. }
  119. context.subscriptions.push(
  120. vscode.workspace.onDidOpenTextDocument(update),
  121. vscode.workspace.onDidChangeTextDocument(event => update(event.document)),
  122. vscode.workspace.onDidCloseTextDocument(document => collection.delete(document.uri))
  123. );
  124. }