/
grammar-util.ts
82 lines (77 loc) · 4.24 KB
/
grammar-util.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
/******************************************************************************
* Copyright 2023 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 { Module } from '../dependency-injection.js';
import type { LangiumGrammarServices } from '../grammar/langium-grammar-module.js';
import type { LanguageMetaData } from '../languages/language-meta-data.js';
import type { IParserConfig } from '../parser/parser-config.js';
import type { LangiumGeneratedServices, LangiumGeneratedSharedServices, LangiumCoreServices, LangiumSharedCoreServices, PartialLangiumCoreServices, PartialLangiumSharedCoreServices } from '../services.js';
import { createDefaultModule, createDefaultSharedModule } from '../default-module.js';
import { inject } from '../dependency-injection.js';
import { interpretAstReflection } from '../grammar/ast-reflection-interpreter.js';
import type * as ast from '../languages/generated/ast.js';
import { createLangiumGrammarServices } from '../grammar/langium-grammar-module.js';
import { getDocument } from '../utils/ast-util.js';
import { EmptyFileSystem } from '../workspace/file-system-provider.js';
import { URI } from '../utils/uri-util.js';
import { createLSPModule, createSharedLSPModule } from './langium-lsp-module.js';
import type { LangiumServices, PartialLangiumLSPServices, PartialLangiumSharedLSPServices } from './lsp-services.js';
/**
* Create an instance of the language services for the given grammar (with LSP support). This function is very
* useful when the grammar is defined on-the-fly, for example in tests of the Langium framework.
*/
export async function createServicesForGrammarWithLSP(config: {
grammar: string | ast.Grammar,
grammarServices?: LangiumGrammarServices,
parserConfig?: IParserConfig,
languageMetaData?: LanguageMetaData,
module?: Module<LangiumServices, PartialLangiumCoreServices & PartialLangiumLSPServices>
sharedModule?: Module<LangiumSharedCoreServices, PartialLangiumSharedCoreServices & PartialLangiumSharedLSPServices>
}): Promise<LangiumServices> {
// TODO @montymxb: For the reviewer. This is a duplicate of 'createServicesForGrammar'
// It would be nice to generalize this function to support both LSP and non-LSP services, but the solution here, so far, is a dupe.
// Feel free to suggest a better solution (we should remove this pre-merge)
const grammarServices = config.grammarServices ?? createLangiumGrammarServices(EmptyFileSystem).grammar;
const uri = URI.parse('memory:///grammar.langium');
const factory = grammarServices.shared.workspace.LangiumDocumentFactory;
const grammarDocument = typeof config.grammar === 'string'
? factory.fromString(config.grammar, uri)
: getDocument(config.grammar);
const grammarNode = grammarDocument.parseResult.value as ast.Grammar;
const documentBuilder = grammarServices.shared.workspace.DocumentBuilder;
await documentBuilder.build([grammarDocument], { validation: false });
const parserConfig = config.parserConfig ?? {
skipValidations: false
};
const languageMetaData = config.languageMetaData ?? {
caseInsensitive: false,
fileExtensions: [`.${grammarNode.name?.toLowerCase() ?? 'unknown'}`],
languageId: grammarNode.name ?? 'UNKNOWN'
};
const generatedSharedModule: Module<LangiumSharedCoreServices, LangiumGeneratedSharedServices> = {
AstReflection: () => interpretAstReflection(grammarNode),
};
const generatedModule: Module<LangiumCoreServices, LangiumGeneratedServices> = {
Grammar: () => grammarNode,
LanguageMetaData: () => languageMetaData,
parser: {
ParserConfig: () => parserConfig
}
};
const shared = inject(
createDefaultSharedModule(EmptyFileSystem),
createSharedLSPModule(EmptyFileSystem),
generatedSharedModule,
config.sharedModule
);
const services = inject(
createDefaultModule({ shared }),
createLSPModule({ shared }),
generatedModule,
config.module
);
shared.ServiceRegistry.register(services);
return services;
}