/
document-validator.ts
258 lines (233 loc) · 10.9 KB
/
document-validator.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/******************************************************************************
* Copyright 2021 TypeFox GmbH
* This program and the accompanying materials are made available under the
* terms of the MIT License, which is available in the project root.
******************************************************************************/
import type { MismatchedTokenException } from 'chevrotain';
import type { Diagnostic } from 'vscode-languageserver';
import type { LanguageMetaData } from '../languages/language-meta-data.js';
import type { ParseResult } from '../parser/langium-parser.js';
import type { LangiumCoreServices } from '../services.js';
import type { AstNode, CstNode } from '../syntax-tree.js';
import type { LangiumDocument } from '../workspace/documents.js';
import type { DiagnosticData, DiagnosticInfo, ValidationAcceptor, ValidationCategory, ValidationRegistry } from './validation-registry.js';
import { DiagnosticSeverity, Position, Range } from 'vscode-languageserver';
import { CancellationToken } from '../utils/cancellation.js';
import { findNodeForKeyword, findNodeForProperty } from '../utils/grammar-utils.js';
import { streamAst } from '../utils/ast-utils.js';
import { tokenToRange } from '../utils/cst-utils.js';
import { interruptAndCheck, isOperationCancelled } from '../utils/promise-utils.js';
import { diagnosticData } from './validation-registry.js';
export interface ValidationOptions {
/**
* If this is set, only the checks associated with these categories are executed; otherwise
* all checks are executed. The default category if not specified to the registry is `'fast'`.
*/
categories?: ValidationCategory[];
/** If true, no further diagnostics are reported if there are lexing errors. */
stopAfterLexingErrors?: boolean
/** If true, no further diagnostics are reported if there are parsing errors. */
stopAfterParsingErrors?: boolean
/** If true, no further diagnostics are reported if there are linking errors. */
stopAfterLinkingErrors?: boolean
}
/**
* Language-specific service for validating `LangiumDocument`s.
*/
export interface DocumentValidator {
/**
* Validates the whole specified document.
*
* @param document specified document to validate
* @param options options to control the validation process
* @param cancelToken allows to cancel the current operation
* @throws `OperationCanceled` if a user action occurs during execution
*/
validateDocument(document: LangiumDocument, options?: ValidationOptions, cancelToken?: CancellationToken): Promise<Diagnostic[]>;
}
export class DefaultDocumentValidator implements DocumentValidator {
protected readonly validationRegistry: ValidationRegistry;
protected readonly metadata: LanguageMetaData;
constructor(services: LangiumCoreServices) {
this.validationRegistry = services.validation.ValidationRegistry;
this.metadata = services.LanguageMetaData;
}
async validateDocument(document: LangiumDocument, options: ValidationOptions = {}, cancelToken = CancellationToken.None): Promise<Diagnostic[]> {
const parseResult = document.parseResult;
const diagnostics: Diagnostic[] = [];
await interruptAndCheck(cancelToken);
if (!options.categories || options.categories.includes('built-in')) {
this.processLexingErrors(parseResult, diagnostics, options);
if (options.stopAfterLexingErrors && diagnostics.some(d => d.data?.code === DocumentValidator.LexingError)) {
return diagnostics;
}
this.processParsingErrors(parseResult, diagnostics, options);
if (options.stopAfterParsingErrors && diagnostics.some(d => d.data?.code === DocumentValidator.ParsingError)) {
return diagnostics;
}
this.processLinkingErrors(document, diagnostics, options);
if (options.stopAfterLinkingErrors && diagnostics.some(d => d.data?.code === DocumentValidator.LinkingError)) {
return diagnostics;
}
}
// Process custom validations
try {
diagnostics.push(...await this.validateAst(parseResult.value, options, cancelToken));
} catch (err) {
if (isOperationCancelled(err)) {
throw err;
}
console.error('An error occurred during validation:', err);
}
await interruptAndCheck(cancelToken);
return diagnostics;
}
protected processLexingErrors(parseResult: ParseResult, diagnostics: Diagnostic[], _options: ValidationOptions): void {
for (const lexerError of parseResult.lexerErrors) {
const diagnostic: Diagnostic = {
severity: DiagnosticSeverity.Error,
range: {
start: {
line: lexerError.line! - 1,
character: lexerError.column! - 1
},
end: {
line: lexerError.line! - 1,
character: lexerError.column! + lexerError.length - 1
}
},
message: lexerError.message,
data: diagnosticData(DocumentValidator.LexingError),
source: this.getSource()
};
diagnostics.push(diagnostic);
}
}
protected processParsingErrors(parseResult: ParseResult, diagnostics: Diagnostic[], _options: ValidationOptions): void {
for (const parserError of parseResult.parserErrors) {
let range: Range | undefined = undefined;
// We can run into the chevrotain error recovery here
// The token contained in the parser error might be automatically inserted
// In this case every position value will be `NaN`
if (isNaN(parserError.token.startOffset)) {
// Some special parser error types contain a `previousToken`
// We can simply append our diagnostic to that token
if ('previousToken' in parserError) {
const token = (parserError as MismatchedTokenException).previousToken;
if (!isNaN(token.startOffset)) {
const position = Position.create(token.endLine! - 1, token.endColumn!);
range = Range.create(position, position);
} else {
// No valid prev token. Might be empty document or containing only hidden tokens.
// Point to document start
range = Range.create(0, 0, 0, 0);
}
}
} else {
range = tokenToRange(parserError.token);
}
if (range) {
const diagnostic: Diagnostic = {
severity: DiagnosticSeverity.Error,
range,
message: parserError.message,
data: diagnosticData(DocumentValidator.ParsingError),
source: this.getSource()
};
diagnostics.push(diagnostic);
}
}
}
protected processLinkingErrors(document: LangiumDocument, diagnostics: Diagnostic[], _options: ValidationOptions): void {
for (const reference of document.references) {
const linkingError = reference.error;
if (linkingError) {
const info: DiagnosticInfo<AstNode, string> = {
node: linkingError.container,
property: linkingError.property,
index: linkingError.index,
data: {
code: DocumentValidator.LinkingError,
containerType: linkingError.container.$type,
property: linkingError.property,
refText: linkingError.reference.$refText
} satisfies LinkingErrorData
};
diagnostics.push(this.toDiagnostic('error', linkingError.message, info));
}
}
}
protected async validateAst(rootNode: AstNode, options: ValidationOptions, cancelToken = CancellationToken.None): Promise<Diagnostic[]> {
const validationItems: Diagnostic[] = [];
const acceptor: ValidationAcceptor = <N extends AstNode>(severity: 'error' | 'warning' | 'info' | 'hint', message: string, info: DiagnosticInfo<N>) => {
validationItems.push(this.toDiagnostic(severity, message, info));
};
await Promise.all(streamAst(rootNode).map(async node => {
await interruptAndCheck(cancelToken);
const checks = this.validationRegistry.getChecks(node.$type, options.categories);
for (const check of checks) {
await check(node, acceptor, cancelToken);
}
}));
return validationItems;
}
protected toDiagnostic<N extends AstNode>(severity: 'error' | 'warning' | 'info' | 'hint', message: string, info: DiagnosticInfo<N, string>): Diagnostic {
return {
message,
range: getDiagnosticRange(info),
severity: toDiagnosticSeverity(severity),
code: info.code,
codeDescription: info.codeDescription,
tags: info.tags,
relatedInformation: info.relatedInformation,
data: info.data,
source: this.getSource()
};
}
protected getSource(): string | undefined {
return this.metadata.languageId;
}
}
export function getDiagnosticRange<N extends AstNode>(info: DiagnosticInfo<N, string>): Range {
if (Range.is(info.range)) {
return info.range;
}
let cstNode: CstNode | undefined;
if (typeof info.property === 'string') {
cstNode = findNodeForProperty(info.node.$cstNode, info.property, info.index);
} else if (typeof info.keyword === 'string') {
cstNode = findNodeForKeyword(info.node.$cstNode, info.keyword, info.index);
}
cstNode ??= info.node.$cstNode;
if (!cstNode) {
return {
start: { line: 0, character: 0 },
end: { line: 0, character: 0 }
};
}
return cstNode.range;
}
export function toDiagnosticSeverity(severity: 'error' | 'warning' | 'info' | 'hint'): DiagnosticSeverity {
switch (severity) {
case 'error':
return DiagnosticSeverity.Error;
case 'warning':
return DiagnosticSeverity.Warning;
case 'info':
return DiagnosticSeverity.Information;
case 'hint':
return DiagnosticSeverity.Hint;
default:
throw new Error('Invalid diagnostic severity: ' + severity);
}
}
export namespace DocumentValidator {
export const LexingError = 'lexing-error';
export const ParsingError = 'parsing-error';
export const LinkingError = 'linking-error';
}
export interface LinkingErrorData extends DiagnosticData {
containerType: string
property: string
refText: string
}