Skip to content

Commit

Permalink
Make Langium language server more robust (#1634)
Browse files Browse the repository at this point in the history
  • Loading branch information
msujew authored Aug 23, 2024
1 parent bfca81f commit 381ab43
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ import { isParserRule } from '../../../languages/generated/ast.js';
import { resolveImport } from '../../internal-grammar-util.js';
import { isDataTypeRule } from '../../../utils/grammar-utils.js';

export type AstResources = {
parserRules: ParserRule[],
datatypeRules: ParserRule[],
interfaces: Interface[],
types: Type[],
export interface AstResources {
parserRules: ParserRule[]
datatypeRules: ParserRule[]
interfaces: Interface[]
types: Type[]
}

export type TypeResources = {
inferred: PlainAstTypes,
declared: PlainAstTypes,
astResources: AstResources,
export interface TypeResources {
inferred: PlainAstTypes
declared: PlainAstTypes
astResources: AstResources
}

export interface ValidationAstTypes {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,9 +320,16 @@ function buildDataRuleType(element: AbstractElement, cancel: () => PlainProperty
const ref = element.rule?.ref;
if (ref) {
if (isTerminalRule(ref)) {
let regex: string | undefined;
try {
regex = terminalRegex(ref).toString();
} catch {
// If the regex cannot be built, we assume it's just a string
regex = undefined;
}
return {
primitive: ref.type?.name ?? 'string',
regex: terminalRegex(ref).toString()
regex
};
} else {
return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,16 @@ export class LangiumGrammarValidationResourcesCollector {
}

collectValidationResources(grammar: Grammar): ValidationResources {
const typeResources = collectValidationAst(grammar, this.documents);
return {
typeToValidationInfo: this.collectValidationInfo(typeResources),
typeToSuperProperties: this.collectSuperProperties(typeResources),
};
try {
const typeResources = collectValidationAst(grammar, this.documents);
return {
typeToValidationInfo: this.collectValidationInfo(typeResources),
typeToSuperProperties: this.collectSuperProperties(typeResources),
};
} catch (err) {
console.error('Error collecting validation resources', err);
return { typeToValidationInfo: new Map(), typeToSuperProperties: new Map() };
}
}

private collectValidationInfo({ astResources, inferred, declared }: ValidationAstTypes) {
Expand Down
16 changes: 16 additions & 0 deletions packages/langium/test/grammar/grammar-validator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1039,3 +1039,19 @@ describe('Prohibit empty parser rules', async () => {
detectEmptyRules(validationResult, 'EmptyItem');
});
});

describe('Check validation is not crashing', async () => {

test('Calling missing terminal rule', async () => {
const specialGrammar = `
grammar Foo
entry Foo:
TERMINAL;
terminal TERMINAL returns string:
MISSING_TERMINAL;
`;
const validationResult = await validate(specialGrammar);
expect(validationResult).toBeDefined();
});
});

0 comments on commit 381ab43

Please sign in to comment.