完成世界书、骰子、apiconfig页面处理

This commit is contained in:
2026-04-30 01:35:10 +08:00
parent a3e3711b2b
commit ba9b925c32
4602 changed files with 785225 additions and 23 deletions

View File

@@ -0,0 +1,7 @@
/******************************************************************************
* This file was generated by langium-cli 4.2.0.
* DO NOT EDIT MANUALLY!
******************************************************************************/
import type { Grammar } from '../../languages/generated/ast.js';
export declare const LangiumGrammarGrammar: () => Grammar;
//# sourceMappingURL=grammar.d.ts.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1 @@
{"version":3,"file":"module.js","sourceRoot":"","sources":["../../../src/grammar/generated/module.ts"],"names":[],"mappings":"AAAA;;;gFAGgF;AAGhF,OAAO,EAAE,2BAA2B,EAAE,MAAM,kCAAkC,CAAC;AAI/E,OAAO,EAAE,qBAAqB,EAAE,MAAM,cAAc,CAAC;AAErD,MAAM,CAAC,MAAM,8BAA8B,GAAG;IAC1C,UAAU,EAAE,SAAS;IACrB,cAAc,EAAE,CAAC,UAAU,CAAC;IAC5B,eAAe,EAAE,KAAK;IACtB,IAAI,EAAE,YAAY;CACe,CAAC;AAEtC,MAAM,CAAC,MAAM,0BAA0B,GAAkB;IACrD,YAAY,EAAE,CAAC;CAClB,CAAC;AAEF,MAAM,CAAC,MAAM,mCAAmC,GAA0E;IACtH,aAAa,EAAE,GAAG,EAAE,CAAC,IAAI,2BAA2B,EAAE;CACzD,CAAC;AAEF,MAAM,CAAC,MAAM,6BAA6B,GAA8D;IACpG,OAAO,EAAE,GAAG,EAAE,CAAC,qBAAqB,EAAE;IACtC,gBAAgB,EAAE,GAAG,EAAE,CAAC,8BAA8B;IACtD,MAAM,EAAE;QACJ,YAAY,EAAE,GAAG,EAAE,CAAC,0BAA0B;KACjD;CACJ,CAAC"}

View File

@@ -0,0 +1,198 @@
/******************************************************************************
* Copyright 2021-2022 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 { URI } from '../utils/uri-utils.js';
import * as ast from '../languages/generated/ast.js';
import { getDocument } from '../utils/ast-utils.js';
import { UriUtils } from '../utils/uri-utils.js';
import { createLangiumGrammarServices } from './langium-grammar-module.js';
import { inject } from '../dependency-injection.js';
import { createDefaultModule, createDefaultSharedModule } from '../lsp/default-lsp-module.js';
import { EmptyFileSystem } from '../workspace/file-system-provider.js';
import { interpretAstReflection } from './ast-reflection-interpreter.js';
import { getTypeName, isDataType } from '../utils/grammar-utils.js';
export function hasDataTypeReturn(rule) {
const returnType = rule.returnType?.ref;
return rule.dataType !== undefined || (ast.isType(returnType) && isDataType(returnType));
}
export function isStringGrammarType(type) {
return isStringTypeInternal(type, new Set());
}
function isStringTypeInternal(type, visited) {
if (visited.has(type)) {
return true;
}
else {
visited.add(type);
}
if (ast.isParserRule(type)) {
if (type.dataType) {
return type.dataType === 'string';
}
if (type.returnType?.ref) {
return isStringTypeInternal(type.returnType.ref, visited);
}
}
else if (ast.isType(type)) {
return isStringTypeInternal(type.type, visited);
}
else if (ast.isArrayType(type)) {
return false;
}
else if (ast.isReferenceType(type)) {
return false;
}
else if (ast.isUnionType(type)) {
return type.types.every(e => isStringTypeInternal(e, visited));
}
else if (ast.isSimpleType(type)) {
if (type.primitiveType === 'string') {
return true;
}
else if (type.stringType) {
return true;
}
else if (type.typeRef?.ref) {
return isStringTypeInternal(type.typeRef.ref, visited);
}
}
return false;
}
export function getTypeNameWithoutError(type) {
if (!type) {
return undefined;
}
try {
return getTypeName(type);
}
catch {
return undefined;
}
}
export function resolveImportUri(imp) {
if (imp.path === undefined || imp.path.length === 0) {
return undefined;
}
const dirUri = UriUtils.dirname(getDocument(imp).uri);
let grammarPath = imp.path;
if (!grammarPath.endsWith('.langium')) {
grammarPath += '.langium';
}
return UriUtils.resolvePath(dirUri, grammarPath);
}
export function resolveImport(documents, imp) {
const resolvedUri = resolveImportUri(imp);
if (!resolvedUri) {
return undefined;
}
const resolvedDocument = documents.getDocument(resolvedUri);
if (!resolvedDocument) {
return undefined;
}
const node = resolvedDocument.parseResult.value;
if (ast.isGrammar(node)) {
return node;
}
return undefined;
}
export function resolveTransitiveImports(documents, grammarOrImport) {
if (ast.isGrammarImport(grammarOrImport)) {
const resolvedGrammar = resolveImport(documents, grammarOrImport);
if (resolvedGrammar) {
const transitiveGrammars = resolveTransitiveImportsInternal(documents, resolvedGrammar);
transitiveGrammars.push(resolvedGrammar);
return transitiveGrammars;
}
return [];
}
else {
return resolveTransitiveImportsInternal(documents, grammarOrImport);
}
}
/**
* Resolves all transitively imported grammars of the given grammar.
* In case of grammars importing each other in circular way, each grammar is remembered only once.
* The initial grammar will never be part of the result.
* @param documents the service to get all available Langium documents
* @param grammar the grammar to transitively resolve its imported grammars
* @param initialGrammar Even if the initial grammar transitively imports itself in circular way again, the initial grammar will not be part of the result!
* @param visited since grammars might import each other in circular way, this set remembers the already visited gramar URIs to prevent loops
* @param grammars the result set of already imported and resolved grammars
* @returns the collected `grammars` in a new array
*/
function resolveTransitiveImportsInternal(documents, grammar, initialGrammar = grammar, visited = new Set(), grammars = new Set()) {
const doc = getDocument(grammar);
if (initialGrammar !== grammar) {
grammars.add(grammar);
}
if (!visited.has(doc.uri)) {
visited.add(doc.uri);
for (const imp of grammar.imports) {
const importedGrammar = resolveImport(documents, imp);
if (importedGrammar) {
resolveTransitiveImportsInternal(documents, importedGrammar, initialGrammar, visited, grammars);
}
}
}
return Array.from(grammars);
}
export function extractAssignments(element) {
if (ast.isAssignment(element)) {
return [element];
}
else if (ast.isAlternatives(element) || ast.isGroup(element) || ast.isUnorderedGroup(element)) {
return element.elements.flatMap(e => extractAssignments(e));
}
else if (ast.isRuleCall(element) && element.rule.ref) {
if (ast.isInfixRule(element.rule.ref)) {
return [];
}
return extractAssignments(element.rule.ref.definition);
}
return [];
}
const primitiveTypes = ['string', 'number', 'boolean', 'Date', 'bigint'];
export function isPrimitiveGrammarType(type) {
return primitiveTypes.includes(type);
}
/**
* Create an instance of the language services for the given grammar. This function is very
* useful when the grammar is defined on-the-fly, for example in tests of the Langium framework.
*/
export async function createServicesForGrammar(config) {
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;
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: ['.txt'],
languageId: grammarNode.name ?? 'UNKNOWN',
mode: 'development'
};
const generatedSharedModule = {
AstReflection: () => interpretAstReflection(grammarNode),
};
const generatedModule = {
Grammar: () => grammarNode,
LanguageMetaData: () => languageMetaData,
parser: {
ParserConfig: () => parserConfig
}
};
const shared = inject(createDefaultSharedModule(EmptyFileSystem), generatedSharedModule, config.sharedModule);
const services = inject(createDefaultModule({ shared }), generatedModule, config.module);
shared.ServiceRegistry.register(services);
return services;
}
//# sourceMappingURL=internal-grammar-util.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,34 @@
/******************************************************************************
* 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 { Module } from '../dependency-injection.js';
import type { DeepPartial } from '../services.js';
import type { LangiumServices, LangiumSharedServices, PartialLangiumServices, PartialLangiumSharedServices } from '../lsp/lsp-services.js';
import { type DefaultSharedModuleContext } from '../lsp/default-lsp-module.js';
import { LangiumGrammarValidator } from './validation/validator.js';
import { LangiumGrammarValidationResourcesCollector } from './validation/validation-resources-collector.js';
import { LangiumGrammarTypesValidator } from './validation/types-validator.js';
export type LangiumGrammarAddedServices = {
validation: {
LangiumGrammarValidator: LangiumGrammarValidator;
ValidationResourcesCollector: LangiumGrammarValidationResourcesCollector;
LangiumGrammarTypesValidator: LangiumGrammarTypesValidator;
};
};
export type LangiumGrammarServices = LangiumServices & LangiumGrammarAddedServices;
export declare const LangiumGrammarModule: Module<LangiumGrammarServices, PartialLangiumServices & LangiumGrammarAddedServices>;
/**
* Creates Langium grammar services, enriched with LSP functionality
*
* @param context Shared module context, used to create additional shared modules
* @param sharedModule Existing shared module to inject together with new shared services
* @param module Additional/modified service implementations for the language services
* @returns Shared services enriched with LSP services + Grammar services, per usual
*/
export declare function createLangiumGrammarServices(context: DefaultSharedModuleContext, sharedModule?: Module<LangiumSharedServices, PartialLangiumSharedServices>, module?: Module<LangiumGrammarServices, DeepPartial<LangiumServices & LangiumGrammarAddedServices>>): {
shared: LangiumSharedServices;
grammar: LangiumGrammarServices;
};
//# sourceMappingURL=langium-grammar-module.d.ts.map

View File

@@ -0,0 +1,77 @@
/******************************************************************************
* 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 { LangiumGrammarTypeHierarchyProvider } from './lsp/grammar-type-hierarchy.js';
import { createDefaultModule, createDefaultSharedModule } from '../lsp/default-lsp-module.js';
import { inject } from '../dependency-injection.js';
import { LangiumGrammarGeneratedModule, LangiumGrammarGeneratedSharedModule } from './generated/module.js';
import { LangiumGrammarScopeComputation, LangiumGrammarScopeProvider } from './references/grammar-scope.js';
import { LangiumGrammarValidator, registerValidationChecks } from './validation/validator.js';
import { LangiumGrammarCodeActionProvider } from './lsp/grammar-code-actions.js';
import { LangiumGrammarCompletionProvider } from './lsp/grammar-completion-provider.js';
import { LangiumGrammarFoldingRangeProvider } from './lsp/grammar-folding-ranges.js';
import { LangiumGrammarFormatter } from './lsp/grammar-formatter.js';
import { LangiumGrammarSemanticTokenProvider } from './lsp/grammar-semantic-tokens.js';
import { LangiumGrammarNameProvider } from './references/grammar-naming.js';
import { LangiumGrammarReferences } from './references/grammar-references.js';
import { LangiumGrammarDefinitionProvider } from './lsp/grammar-definition.js';
import { LangiumGrammarCallHierarchyProvider } from './lsp/grammar-call-hierarchy.js';
import { LangiumGrammarValidationResourcesCollector } from './validation/validation-resources-collector.js';
import { LangiumGrammarTypesValidator, registerTypeValidationChecks } from './validation/types-validator.js';
import { DocumentState } from '../workspace/documents.js';
export const LangiumGrammarModule = {
validation: {
LangiumGrammarValidator: (services) => new LangiumGrammarValidator(services),
ValidationResourcesCollector: (services) => new LangiumGrammarValidationResourcesCollector(services),
LangiumGrammarTypesValidator: () => new LangiumGrammarTypesValidator(),
},
lsp: {
FoldingRangeProvider: (services) => new LangiumGrammarFoldingRangeProvider(services),
CodeActionProvider: (services) => new LangiumGrammarCodeActionProvider(services),
SemanticTokenProvider: (services) => new LangiumGrammarSemanticTokenProvider(services),
Formatter: () => new LangiumGrammarFormatter(),
DefinitionProvider: (services) => new LangiumGrammarDefinitionProvider(services),
CallHierarchyProvider: (services) => new LangiumGrammarCallHierarchyProvider(services),
TypeHierarchyProvider: (services) => new LangiumGrammarTypeHierarchyProvider(services),
CompletionProvider: (services) => new LangiumGrammarCompletionProvider(services)
},
references: {
ScopeComputation: (services) => new LangiumGrammarScopeComputation(services),
ScopeProvider: (services) => new LangiumGrammarScopeProvider(services),
References: (services) => new LangiumGrammarReferences(services),
NameProvider: () => new LangiumGrammarNameProvider()
}
};
/**
* Creates Langium grammar services, enriched with LSP functionality
*
* @param context Shared module context, used to create additional shared modules
* @param sharedModule Existing shared module to inject together with new shared services
* @param module Additional/modified service implementations for the language services
* @returns Shared services enriched with LSP services + Grammar services, per usual
*/
export function createLangiumGrammarServices(context, sharedModule, module) {
const shared = inject(createDefaultSharedModule(context), LangiumGrammarGeneratedSharedModule, sharedModule);
const grammar = inject(createDefaultModule({ shared }), LangiumGrammarGeneratedModule, LangiumGrammarModule, module);
addTypeCollectionPhase(shared, grammar);
shared.ServiceRegistry.register(grammar);
registerValidationChecks(grammar);
registerTypeValidationChecks(grammar);
if (!context.connection) {
// We don't run inside a language server
// Therefore, initialize the configuration provider instantly
shared.workspace.ConfigurationProvider.initialized({});
}
return { shared, grammar };
}
function addTypeCollectionPhase(sharedServices, grammarServices) {
const documentBuilder = sharedServices.workspace.DocumentBuilder;
documentBuilder.onDocumentPhase(DocumentState.IndexedReferences, async (document) => {
const typeCollector = grammarServices.validation.ValidationResourcesCollector;
const grammar = document.parseResult.value;
document.validationResources = typeCollector.collectValidationResources(grammar);
});
}
//# sourceMappingURL=langium-grammar-module.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"grammar-call-hierarchy.d.ts","sourceRoot":"","sources":["../../../src/grammar/lsp/grammar-call-hierarchy.ts"],"names":[],"mappings":"AAAA;;;;gFAIgF;AAEhF,OAAO,KAAK,EAAE,yBAAyB,EAAE,yBAAyB,EAAS,MAAM,uBAAuB,CAAC;AACzG,OAAO,KAAK,EAAE,OAAO,EAAW,MAAM,sBAAsB,CAAC;AAC7D,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAC;AACpD,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,qCAAqC,CAAC;AAEhF,OAAO,EAAE,6BAA6B,EAAE,MAAM,sCAAsC,CAAC;AAKrF,qBAAa,mCAAoC,SAAQ,6BAA6B;IAElF,SAAS,CAAC,gBAAgB,CAAC,IAAI,EAAE,OAAO,EAAE,UAAU,EAAE,MAAM,CAAC,oBAAoB,CAAC,GAAG,yBAAyB,EAAE,GAAG,SAAS;IAiD5H,SAAS,CAAC,gBAAgB,CAAC,IAAI,EAAE,OAAO,GAAG,yBAAyB,EAAE,GAAG,SAAS;CAmErF"}

View File

@@ -0,0 +1 @@
{"version":3,"file":"grammar-call-hierarchy.js","sourceRoot":"","sources":["../../../src/grammar/lsp/grammar-call-hierarchy.ts"],"names":[],"mappings":"AAAA;;;;gFAIgF;AAMhF,OAAO,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAC;AACnD,OAAO,EAAE,6BAA6B,EAAE,MAAM,sCAAsC,CAAC;AACrF,OAAO,EAAE,kBAAkB,EAAE,WAAW,EAAE,iBAAiB,EAAE,MAA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,GAAG,CAAC,QAAQ,EAAE,CAAC;YACjE,OAAO,CAAC;oBACJ,EAAE,EAAE;wBACA,IAAI,EAAE,UAAU,CAAC,MAAM;wBACvB,IAAI,EAAE,WAAW,CAAC,IAAI;wBACtB,KAAK,EAAE,UAAU,CAAC,KAAK;wBACvB,cAAc,EAAE,WAAW,CAAC,KAAK;wBACjC,GAAG,EAAE,SAAS;qBACjB;oBACD,UAAU,EAAE,CAAC,OAAO,CAAC,KAAK,CAAC;iBAC9B,CAAC,CAAC;QACP,CAAC;aAAM,CAAC;YACJ,OAAO,SAAS,CAAC;QACrB,CAAC;IACL,CAAC;CACJ"}

View File

@@ -0,0 +1,42 @@
/******************************************************************************
* 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 { CodeActionParams } from 'vscode-languageserver-protocol';
import type { CodeAction, Command } from 'vscode-languageserver-types';
import * as ast from '../../languages/generated/ast.js';
import type { CodeActionProvider } from '../../lsp/code-action.js';
import type { LangiumServices } from '../../lsp/lsp-services.js';
import type { AstReflection } from '../../syntax-tree.js';
import type { MaybePromise } from '../../utils/promise-utils.js';
import type { LangiumDocument } from '../../workspace/documents.js';
import type { IndexManager } from '../../workspace/index-manager.js';
export declare class LangiumGrammarCodeActionProvider implements CodeActionProvider {
protected readonly reflection: AstReflection;
protected readonly indexManager: IndexManager;
constructor(services: LangiumServices);
getCodeActions(document: LangiumDocument<ast.Grammar>, params: CodeActionParams): MaybePromise<Array<Command | CodeAction>>;
private createCodeActions;
/**
* Adds missing returns for parser rule
*/
private fixMissingReturns;
private fixInvalidReturnsInfers;
private fixMissingInfer;
private fixMissingCrossRefTerminal;
private fixSuperfluousInfer;
private isRuleReplaceable;
private replaceRule;
private isDefinitionReplaceable;
private replaceDefinition;
private replaceParserRuleByTypeDeclaration;
private fixUnnecessaryFileExtension;
private makeUpperCase;
private addEntryKeyword;
private fixRegexTokens;
private fixCrossRefSyntax;
private addNewRule;
private lookInGlobalScope;
}
//# sourceMappingURL=grammar-code-actions.d.ts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"grammar-code-actions.d.ts","sourceRoot":"","sources":["../../../src/grammar/lsp/grammar-code-actions.ts"],"names":[],"mappings":"AAAA;;;;gFAIgF;AAIhF,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,gCAAgC,CAAC;AACvE,OAAO,KAAK,EAAE,UAAU,EAAE,OAAO,EAAY,MAAM,6BAA6B,CAAC;AACjF,OAAO,KAAK,GAAG,MAAM,kCAAkC,CAAC;AACxD,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,0BAA0B,CAAC;AACnE,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,2BAA2B,CAAC;AACjE,OAAO,KAAK,EAAE,aAAa,EAA4B,MAAM,sBAAsB,CAAC;AAGpF,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,8BAA8B,CAAC;AAOjE,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,8BAA8B,CAAC;AACpE,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,kCAAkC,CAAC;AAGrE,qBAAa,gCAAiC,YAAW,kBAAkB;IAEvE,SAAS,CAAC,QAAQ,CAAC,UAAU,EAAE,aAAa,CAAC;IAC7C,SAAS,CAAC,QAAQ,CAAC,YAAY,EAAE,YAAY,CAAC;gBAElC,QAAQ,EAAE,eAAe;IAKrC,cAAc,CAAC,QAAQ,EAAE,eAAe,CAAC,GAAG,CAAC,OAAO,CAAC,EAAE,MAAM,EAAE,gBAAgB,GAAG,YAAY,CAAC,KAAK,CAAC,OAAO,GAAG,UAAU,CAAC,CAAC;IAS3H,OAAO,CAAC,iBAAiB;IAmDzB;;OAEG;IACH,OAAO,CAAC,iBAAiB;IAoBzB,OAAO,CAAC,uBAAuB;IAqB/B,OAAO,CAAC,eAAe;IAuBvB,OAAO,CAAC,0BAA0B;IAwBlC,OAAO,CAAC,mBAAmB;IAoB3B,OAAO,CAAC,iBAAiB;IAOzB,OAAO,CAAC,WAAW;IAInB,OAAO,CAAC,uBAAuB;IAS/B,OAAO,CAAC,iBAAiB;IAUzB,OAAO,CAAC,kCAAkC;IA8B1C,OAAO,CAAC,2BAA2B;IAwBnC,OAAO,CAAC,aAAa;IAwBrB,OAAO,CAAC,eAAe;IAiBvB,OAAO,CAAC,cAAc;IA4BtB,OAAO,CAAC,iBAAiB;IAiBzB,OAAO,CAAC,UAAU;IA6BlB,OAAO,CAAC,iBAAiB;CA0E5B"}

View File

@@ -0,0 +1 @@
{"version":3,"file":"grammar-completion-provider.d.ts","sourceRoot":"","sources":["../../../src/grammar/lsp/grammar-completion-provider.ts"],"names":[],"mappings":"AAAA;;;;gFAIgF;AAIhF,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,oDAAoD,CAAC;AACtF,OAAO,EAAE,yBAAyB,EAAE,KAAK,kBAAkB,EAAE,KAAK,iBAAiB,EAAE,MAAM,6CAA6C,CAAC;AACzI,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,8BAA8B,CAAC;AAGjE,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,kCAAkC,CAAC;AAGxE,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,2BAA2B,CAAC;AAEjE,qBAAa,gCAAiC,SAAQ,yBAAyB;IAE3E,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAyB;gBAEvC,QAAQ,EAAE,eAAe;cAKlB,aAAa,CAAC,OAAO,EAAE,iBAAiB,EAAE,IAAI,EAAE,WAAW,CAAC,eAAe,CAAC,EAAE,QAAQ,EAAE,kBAAkB,GAAG,YAAY,CAAC,IAAI,CAAC;IASlJ,OAAO,CAAC,kBAAkB;IAmC1B,OAAO,CAAC,WAAW;CAmBtB"}

View File

@@ -0,0 +1 @@
{"version":3,"file":"grammar-definition.js","sourceRoot":"","sources":["../../../src/grammar/lsp/grammar-definition.ts"],"names":[],"mappings":"AAAA;;;;gFAIgF;AAQhF,OAAO,EAAE,YAAY,EAAE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AAY,CAAC,MAAM,CACf,eAAe,CAAC,SAAS,CAAC,GAAG,CAAC,QAAQ,EAAE,EACxC,YAAY,EACZ,cAAc,EACd,aAAa,CAAC,KAAK,CACtB;iBACJ,CAAC;YACN,CAAC;YACD,OAAO,SAAS,CAAC;QACrB,CAAC;QACD,OAAO,KAAK,CAAC,oBAAoB,CAAC,aAAa,EAAE,OAAO,CAAC,CAAC;IAC9D,CAAC;IAES,gBAAgB,CAAC,eAAwB;QAC/C,4CAA4C;QAC5C,IAAI,eAAe,CAAC,UAAU,EAAE,CAAC;YAC7B,OAAO,eAAe,CAAC;QAC3B,CAAC;QACD,OAAO,cAAc,CAAC,eAAe,CAAC,CAAC,IAAI,EAAE,CAAC;IAClD,CAAC;CACJ"}

View File

@@ -0,0 +1,11 @@
/******************************************************************************
* Copyright 2022 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 { AstNode } from '../../syntax-tree.js';
import { AbstractFormatter } from '../../lsp/formatter.js';
export declare class LangiumGrammarFormatter extends AbstractFormatter {
protected format(node: AstNode): void;
}
//# sourceMappingURL=grammar-formatter.d.ts.map

View File

@@ -0,0 +1,12 @@
/******************************************************************************
* Copyright 2022 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 { AstNode } from '../../syntax-tree.js';
import type { SemanticTokenAcceptor } from '../../lsp/semantic-token-provider.js';
import { AbstractSemanticTokenProvider } from '../../lsp/semantic-token-provider.js';
export declare class LangiumGrammarSemanticTokenProvider extends AbstractSemanticTokenProvider {
protected highlightElement(node: AstNode, acceptor: SemanticTokenAcceptor): void;
}
//# sourceMappingURL=grammar-semantic-tokens.d.ts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"grammar-semantic-tokens.js","sourceRoot":"","sources":["../../../src/grammar/lsp/grammar-semantic-tokens.ts"],"names":[],"mappings":"AAAA;;;;gFAIgF;AAIhF,OAAO,EAAE,kBAAkB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E,CAAC;YACrB,OAAO,IAAI,CAAC,OAAO,CAAC;QACxB,CAAC;aAAM,CAAC;YACJ,OAAO,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;QAC/B,CAAC;IACL,CAAC;IAEQ,WAAW,CAAC,IAAa;QAC9B,IAAI,YAAY,CAAC,IAAI,CAAC,EAAE,CAAC;YACrB,OAAO,mBAAmB,CAAC,IAAI,CAAC,QAAQ,EAAE,SAAS,CAAC,CAAC;QACzD,CAAC;aAAM,CAAC;YACJ,OAAO,KAAK,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC;QACnC,CAAC;IACL,CAAC;CAEJ"}

View File

@@ -0,0 +1,180 @@
/******************************************************************************
* 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 { DefaultReferences } from '../../references/references.js';
import { getContainerOfType, getDocument } from '../../utils/ast-utils.js';
import { toDocumentSegment } from '../../utils/cst-utils.js';
import { findAssignment, findNodeForKeyword, findNodeForProperty, getActionAtElement } from '../../utils/grammar-utils.js';
import { stream } from '../../utils/stream.js';
import { UriUtils } from '../../utils/uri-utils.js';
import { isAbstractParserRule, isAction, isAssignment, isInfixRule, isInterface, isParserRule, isType, isTypeAttribute } from '../../languages/generated/ast.js';
import { extractAssignments } from '../internal-grammar-util.js';
import { collectChildrenTypes, collectSuperTypes } from '../type-system/types-util.js';
import { assertUnreachable } from '../../utils/errors.js';
export class LangiumGrammarReferences extends DefaultReferences {
findDeclarations(sourceCstNode) {
const nodeElem = sourceCstNode.astNode;
const assignment = findAssignment(sourceCstNode);
if (assignment && assignment.feature === 'feature') {
// Only search for a special declaration if the cst node is the feature property of the action/assignment
if (isAssignment(nodeElem)) {
const decl = this.findAssignmentDeclaration(nodeElem);
return decl ? [decl] : [];
}
else if (isAction(nodeElem)) {
const decl = this.findActionDeclaration(nodeElem);
return decl ? [decl] : [];
}
}
return super.findDeclarations(sourceCstNode);
}
findReferences(targetNode, options) {
if (isTypeAttribute(targetNode)) {
return this.findReferencesToTypeAttribute(targetNode, options.includeDeclaration ?? false);
}
else {
return super.findReferences(targetNode, options);
}
}
findReferencesToTypeAttribute(targetNode, includeDeclaration) {
const refs = [];
const interfaceNode = getContainerOfType(targetNode, isInterface);
if (interfaceNode) {
if (includeDeclaration) {
refs.push(...this.getSelfReferences(targetNode));
}
const interfaces = collectChildrenTypes(interfaceNode, this, this.documents, this.nodeLocator);
const targetRules = [];
interfaces.forEach(interf => {
const rules = this.findRulesWithReturnType(interf);
targetRules.push(...rules);
});
targetRules.forEach(rule => {
const references = this.createReferencesToAttribute(rule, targetNode);
refs.push(...references);
});
}
return stream(refs);
}
createReferencesToAttribute(ruleOrAction, attribute) {
const refs = [];
if (isParserRule(ruleOrAction)) {
const assignment = extractAssignments(ruleOrAction.definition).find(a => a.feature === attribute.name);
if (assignment?.$cstNode) {
const leaf = this.nameProvider.getNameNode(assignment);
if (leaf) {
const assignmentUri = getDocument(assignment).uri;
const attributeUri = getDocument(attribute).uri;
refs.push({
sourceUri: assignmentUri,
sourcePath: this.nodeLocator.getAstNodePath(assignment),
targetUri: attributeUri,
targetPath: this.nodeLocator.getAstNodePath(attribute),
segment: toDocumentSegment(leaf),
local: UriUtils.equals(assignmentUri, attributeUri)
});
}
}
}
else if (isInfixRule(ruleOrAction)) {
let leaf;
if (attribute.name === 'left' || attribute.name === 'right') {
// Use the 'on' keyword as segment
leaf = findNodeForKeyword(ruleOrAction.$cstNode, 'on');
}
else if (attribute.name === 'operator') {
// Use the rule definition in 'operators' as segment
leaf = findNodeForProperty(ruleOrAction.$cstNode, 'operators');
}
if (leaf) {
const ruleUri = getDocument(ruleOrAction).uri;
const attributeUri = getDocument(attribute).uri;
refs.push({
sourceUri: ruleUri,
sourcePath: this.nodeLocator.getAstNodePath(ruleOrAction),
targetUri: attributeUri,
targetPath: this.nodeLocator.getAstNodePath(attribute),
segment: toDocumentSegment(leaf),
local: UriUtils.equals(ruleUri, attributeUri)
});
}
}
else if (isAction(ruleOrAction)) {
// If the action references the attribute directly
if (ruleOrAction.feature === attribute.name) {
const leaf = findNodeForProperty(ruleOrAction.$cstNode, 'feature');
if (leaf) {
const actionUri = getDocument(ruleOrAction).uri;
const attributeUri = getDocument(attribute).uri;
refs.push({
sourceUri: actionUri,
sourcePath: this.nodeLocator.getAstNodePath(ruleOrAction),
targetUri: attributeUri,
targetPath: this.nodeLocator.getAstNodePath(attribute),
segment: toDocumentSegment(leaf),
local: UriUtils.equals(actionUri, attributeUri)
});
}
}
// Find all references within the parser rule that contains this action
const parserRule = getContainerOfType(ruleOrAction, isParserRule);
refs.push(...this.createReferencesToAttribute(parserRule, attribute));
}
else {
assertUnreachable(ruleOrAction);
}
return refs;
}
findAssignmentDeclaration(assignment) {
const parserRule = getContainerOfType(assignment, isParserRule);
const action = getActionAtElement(assignment);
if (action) {
const actionDeclaration = this.findActionDeclaration(action, assignment.feature);
if (actionDeclaration) {
return actionDeclaration;
}
}
if (parserRule?.returnType?.ref) {
if (isInterface(parserRule.returnType.ref) || isType(parserRule.returnType.ref)) {
const interfaces = collectSuperTypes(parserRule.returnType.ref);
for (const interf of interfaces) {
const typeAttribute = interf.attributes.find(att => att.name === assignment.feature);
if (typeAttribute) {
return typeAttribute;
}
}
}
}
return assignment;
}
findActionDeclaration(action, featureName) {
if (action.type?.ref) {
const feature = featureName ?? action.feature;
const interfaces = collectSuperTypes(action.type.ref);
for (const interf of interfaces) {
const typeAttribute = interf.attributes.find(att => att.name === feature);
if (typeAttribute) {
return typeAttribute;
}
}
}
return undefined;
}
findRulesWithReturnType(interf) {
const rules = [];
const refs = this.index.findAllReferences(interf, this.nodeLocator.getAstNodePath(interf));
for (const ref of refs) {
const doc = this.documents.getDocument(ref.sourceUri);
if (doc) {
const astNode = this.nodeLocator.getAstNode(doc.parseResult.value, ref.sourcePath);
if (isAbstractParserRule(astNode) || isAction(astNode)) {
rules.push(astNode);
}
}
}
return rules;
}
}
//# sourceMappingURL=grammar-references.js.map

View File

@@ -0,0 +1,164 @@
/******************************************************************************
* 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 { EMPTY_SCOPE, MultiMapScope } from '../../references/scope.js';
import { DefaultScopeComputation } from '../../references/scope-computation.js';
import { DefaultScopeProvider } from '../../references/scope-provider.js';
import { findRootNode, getContainerOfType, getDocument, streamAllContents } from '../../utils/ast-utils.js';
import { toDocumentSegment } from '../../utils/cst-utils.js';
import { AbstractType, InferredType, Interface, NamedArgument, Type, isAbstractParserRule, isAction, isGrammar, isReturnType, isRuleCall } from '../../languages/generated/ast.js';
import { resolveImportUri } from '../internal-grammar-util.js';
export class LangiumGrammarScopeProvider extends DefaultScopeProvider {
constructor(services) {
super(services);
this.langiumDocuments = services.shared.workspace.LangiumDocuments;
}
getScope(context) {
if (context.container.$type === NamedArgument.$type && context.property === 'parameter') {
return this.getNamedArgumentScope(context);
}
const referenceType = this.reflection.getReferenceType(context);
if (referenceType === AbstractType.$type) {
return this.getTypeScope(referenceType, context);
}
else {
return super.getScope(context);
}
}
getNamedArgumentScope(context) {
const ruleCall = context.container.$container;
if (!isRuleCall(ruleCall)) {
return EMPTY_SCOPE;
}
const rule = ruleCall.rule.ref;
if (!isAbstractParserRule(rule)) {
return EMPTY_SCOPE;
}
return this.createScopeForNodes(rule.parameters);
}
getTypeScope(referenceType, context) {
const localSymbols = getDocument(context.container).localSymbols;
const rootNode = findRootNode(context.container);
if (localSymbols && rootNode && localSymbols.has(rootNode)) {
const globalScope = this.getGlobalScope(referenceType, context);
const localScope = localSymbols.getStream(rootNode).filter(des => des.type === Interface.$type || des.type === Type.$type || des.type === InferredType.$type);
return this.createScope(localScope, globalScope);
}
else {
return this.getGlobalScope(referenceType, context);
}
}
getGlobalScope(referenceType, context) {
const grammar = getContainerOfType(context.container, isGrammar);
if (!grammar) {
return EMPTY_SCOPE;
}
const importedUris = new Set();
this.gatherImports(grammar, importedUris);
let importedElements = this.indexManager.allElements(referenceType, importedUris);
if (referenceType === AbstractType.$type) {
importedElements = importedElements.filter(des => des.type === Interface.$type || des.type === Type.$type || des.type === InferredType.$type);
}
return new MultiMapScope(importedElements);
}
gatherImports(grammar, importedUris) {
for (const imp0rt of grammar.imports) {
const uri = resolveImportUri(imp0rt);
if (uri && !importedUris.has(uri.toString())) {
importedUris.add(uri.toString());
const importedDocument = this.langiumDocuments.getDocument(uri);
if (importedDocument) {
const rootNode = importedDocument.parseResult.value;
if (isGrammar(rootNode)) {
this.gatherImports(rootNode, importedUris);
}
}
}
}
}
}
export class LangiumGrammarScopeComputation extends DefaultScopeComputation {
constructor(services) {
super(services);
this.astNodeLocator = services.workspace.AstNodeLocator;
}
addExportedSymbol(node, exports, document) {
// this function is called in order to export nodes to the GLOBAL scope
/* Among others, TYPES need to be exported.
* There are three ways to define types:
* - explicit "type" declarations
* - explicit "interface" declarations
* - "inferred types", which can be distinguished into ...
* - inferred types with explicitly declared names, i.e. parser rules with "infers", actions with "infer"
* Note, that multiple explicitly inferred types might have the same name! Cross-references to such types are resolved to the first declaration.
* - implicitly inferred types, i.e. parser rules without "infers" and without "returns",
* which implicitly declare a type with the same name as the parser rule
* Note, that implicitly inferred types are unique, since names of parser rules must be unique.
*/
// export the top-level elements: parser rules, terminal rules, types, interfaces
super.addExportedSymbol(node, exports, document);
// additionally, export inferred types:
if (isAbstractParserRule(node)) {
if (!node.returnType && !node.dataType) {
// Export implicitly and explicitly inferred type from parser rule
const typeNode = node.inferredType ?? node;
exports.push(this.createInferredTypeDescription(typeNode, typeNode.name, document));
}
streamAllContents(node).forEach(childNode => {
if (isAction(childNode) && childNode.inferredType) {
// Export explicitly inferred type from action
exports.push(this.createInferredTypeDescription(childNode.inferredType, childNode.inferredType.name, document));
}
});
}
}
addLocalSymbol(node, document, symbols) {
// for the precompution of the local scope
if (isReturnType(node)) {
return;
}
this.processTypeNode(node, document, symbols);
this.processActionNode(node, document, symbols);
super.addLocalSymbol(node, document, symbols);
}
/**
* Add synthetic type into the scope in case of explicitly or implicitly inferred type:<br>
* cases: `ParserRule: ...;` or `ParserRule infers Type: ...;`
*/
processTypeNode(node, document, symbols) {
const container = node.$container;
if (container && isAbstractParserRule(node) && !node.returnType && !node.dataType) {
const typeNode = node.inferredType ?? node;
symbols.add(container, this.createInferredTypeDescription(typeNode, typeNode.name, document));
}
}
/**
* Add synthetic type into the scope in case of explicitly inferred type:
*
* case: `{infer Action}`
*/
processActionNode(node, document, symbols) {
const container = findRootNode(node);
if (container && isAction(node) && node.inferredType) {
symbols.add(container, this.createInferredTypeDescription(node.inferredType, node.inferredType.name, document));
}
}
createInferredTypeDescription(node, name, document = getDocument(node)) {
let nameNodeSegment;
const nameSegmentGetter = () => nameNodeSegment ?? (nameNodeSegment = toDocumentSegment(this.nameProvider.getNameNode(node) ?? node.$cstNode));
return {
node,
name,
get nameSegment() {
return nameSegmentGetter();
},
selectionSegment: toDocumentSegment(node.$cstNode),
type: InferredType.$type,
documentUri: document.uri,
path: this.astNodeLocator.getAstNodePath(node)
};
}
}
//# sourceMappingURL=grammar-scope.js.map

View File

@@ -0,0 +1,30 @@
/******************************************************************************
* Copyright 2022 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 { ParserRule, Interface, Type, Grammar, InfixRule } from '../../../languages/generated/ast.js';
import type { URI } from '../../../utils/uri-utils.js';
import type { LangiumCoreServices } from '../../../index.js';
import type { PlainAstTypes } from './plain-types.js';
import type { AstTypes } from './types.js';
export interface AstResources {
parserRules: ParserRule[];
infixRules: InfixRule[];
datatypeRules: ParserRule[];
interfaces: Interface[];
types: Type[];
}
export interface TypeResources {
inferred: PlainAstTypes;
declared: PlainAstTypes;
astResources: AstResources;
}
export interface ValidationAstTypes {
inferred: AstTypes;
declared: AstTypes;
astResources: AstResources;
}
export declare function collectTypeResources(grammars: Grammar | Grammar[], services?: LangiumCoreServices): TypeResources;
export declare function collectAllAstResources(grammars: Grammar | Grammar[], visited?: Set<URI>, astResources?: AstResources, services?: LangiumCoreServices): AstResources;
//# sourceMappingURL=all-types.d.ts.map

View File

@@ -0,0 +1,124 @@
/******************************************************************************
* Copyright 2022 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 { isArrayLiteral, isBooleanLiteral } from '../../../languages/generated/ast.js';
import { isArrayType, isReferenceType, isUnionType, isSimpleType } from '../../../languages/generated/ast.js';
import { getTypeNameWithoutError, isPrimitiveGrammarType } from '../../internal-grammar-util.js';
import { getTypeName } from '../../../utils/grammar-utils.js';
export function collectDeclaredTypes(interfaces, unions, services) {
const commentProvider = services?.documentation.CommentProvider;
const declaredTypes = { unions: [], interfaces: [] };
// add interfaces
for (const type of interfaces) {
const properties = [];
for (const attribute of type.attributes) {
const property = {
name: attribute.name,
optional: attribute.isOptional,
astNodes: new Set([attribute]),
type: typeDefinitionToPropertyType(attribute.type),
comment: commentProvider?.getComment(attribute)
};
if (attribute.defaultValue) {
property.defaultValue = toPropertyDefaultValue(attribute.defaultValue);
}
properties.push(property);
}
const superTypes = new Set();
for (const superType of type.superTypes) {
if (superType.ref) {
superTypes.add(getTypeName(superType.ref));
}
}
const interfaceType = {
name: type.name,
declared: true,
abstract: false,
properties: properties,
superTypes: superTypes,
subTypes: new Set(),
comment: commentProvider?.getComment(type),
};
declaredTypes.interfaces.push(interfaceType);
}
// add types
for (const union of unions) {
const unionType = {
name: union.name,
declared: true,
type: typeDefinitionToPropertyType(union.type),
superTypes: new Set(),
subTypes: new Set(),
comment: commentProvider?.getComment(union),
};
declaredTypes.unions.push(unionType);
}
return declaredTypes;
}
function toPropertyDefaultValue(literal) {
if (isBooleanLiteral(literal)) {
return literal.true;
}
else if (isArrayLiteral(literal)) {
return literal.elements.map(toPropertyDefaultValue);
}
else {
return literal.value;
}
}
export function typeDefinitionToPropertyType(type) {
if (isArrayType(type)) {
return {
elementType: typeDefinitionToPropertyType(type.elementType)
};
}
else if (isReferenceType(type)) {
return {
referenceType: typeDefinitionToPropertyType(type.referenceType),
isMulti: type.isMulti,
isSingle: !type.isMulti
};
}
else if (isUnionType(type)) {
return {
types: type.types.map(typeDefinitionToPropertyType)
};
}
else if (isSimpleType(type)) {
let value;
if (type.primitiveType) {
value = type.primitiveType;
return {
primitive: value
};
}
else if (type.stringType) {
value = type.stringType;
return {
string: value
};
}
else if (type.typeRef) {
const ref = type.typeRef.ref;
const value = getTypeNameWithoutError(ref);
if (value) {
if (isPrimitiveGrammarType(value)) {
return {
primitive: value
};
}
else {
return {
value
};
}
}
}
}
return {
primitive: 'unknown'
};
}
//# sourceMappingURL=declared-types.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1 @@
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../../src/grammar/type-system/type-collector/types.ts"],"names":[],"mappings":"AAAA;;;;gFAIgF;AAGhF,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,yBAAyB,CAAC;AACvD,OAAO,KAAK,EAAE,MAAM,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,qCAAqC,CAAC;AAG7F,MAAM,WAAW,QAAQ;IACrB,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,OAAO,CAAC;IAClB,IAAI,EAAE,YAAY,CAAC;IACnB,YAAY,CAAC,EAAE,oBAAoB,CAAC;IACpC,QAAQ,EAAE,GAAG,CAAC,UAAU,GAAG,MAAM,GAAG,aAAa,CAAC,CAAC;IACnD,OAAO,CAAC,EAAE,MAAM,CAAC;CACpB;AAED,MAAM,MAAM,oBAAoB,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,oBAAoB,EAAE,CAAC;AAEtF,MAAM,MAAM,YAAY,GAClB,aAAa,GACb,SAAS,GACT,aAAa,GACb,SAAS,GACT,aAAa,GACb,UAAU,CAAC;AAEjB,MAAM,WAAW,aAAa;IAC1B,aAAa,EAAE,YAAY,CAAA;IAC3B,OAAO,EAAE,OAAO,CAAC;IACjB,QAAQ,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,CAAC,MAAM,EAAE,OAAO,GAAG,SAAS,CAAC;gBAEzB,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,GAAG,SAAS;CAM3D;AAED,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,YAAY,EAAE,EAAE,EAAE,YAAY,GAAG,OAAO,CAE9E;AAuGD,wBAAgB,oBAAoB,CAAC,IAAI,CAAC,EAAE,YAAY,EAAE,IAAI,GAAE,SAAS,GAAG,cAA0B,GAAG,MAAM,CAsB9G;AAiCD,wBAAgB,uBAAuB,CAAC,YAAY,EAAE,YAAY,GAAG,OAAO,CAa3E"}

View File

@@ -0,0 +1,34 @@
/******************************************************************************
* Copyright 2022 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 { References } from '../../references/references.js';
import type { AstNodeLocator } from '../../workspace/ast-node-locator.js';
import type { LangiumDocuments } from '../../workspace/documents.js';
import type { Interface, Type, AbstractType } from '../../languages/generated/ast.js';
import type { PlainInterface, PlainProperty } from './type-collector/plain-types.js';
import type { AstTypes, InterfaceType, PropertyType, TypeOption } from './type-collector/types.js';
import { MultiMap } from '../../utils/collections.js';
/**
* Collects all properties of all interface types. Includes super type properties.
* @param interfaces A topologically sorted array of interfaces.
*/
export declare function collectAllPlainProperties(interfaces: PlainInterface[]): MultiMap<string, PlainProperty>;
export declare function distinctAndSorted<T>(list: T[], compareFn?: (a: T, b: T) => number): T[];
export declare function collectChildrenTypes(interfaceNode: Interface, references: References, langiumDocuments: LangiumDocuments, nodeLocator: AstNodeLocator): Set<Interface | Type>;
export declare function collectTypeHierarchy(types: TypeOption[]): {
superTypes: MultiMap<string, string>;
subTypes: MultiMap<string, string>;
};
export declare function collectSuperTypes(ruleNode: AbstractType): Set<Interface>;
export declare function mergeInterfaces(inferred: AstTypes, declared: AstTypes): InterfaceType[];
export declare function mergeTypesAndInterfaces(astTypes: AstTypes): TypeOption[];
export declare function hasArrayType(type: PropertyType): boolean;
export declare function hasBooleanType(type: PropertyType): boolean;
export declare function findReferenceTypes(type: PropertyType): string[];
export declare function findAstTypes(type: PropertyType): string[];
export declare function isAstType(type: PropertyType): boolean;
export declare function isAstTypeInternal(type: PropertyType, visited: Map<PropertyType, boolean>): boolean;
export declare function escapeQuotes(str: string, type?: '"' | "'"): string;
//# sourceMappingURL=types-util.d.ts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"types-validator.d.ts","sourceRoot":"","sources":["../../../src/grammar/validation/types-validator.ts"],"names":[],"mappings":"AAAA;;;;gFAIgF;AAGhF,OAAO,KAAK,EAAkB,kBAAkB,EAAoB,MAAM,yCAAyC,CAAC;AACpH,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,8BAA8B,CAAC;AAG3E,OAAO,KAAK,GAAG,MAAM,kCAAkC,CAAC;AAQxD,wBAAgB,4BAA4B,CAAC,QAAQ,EAAE,sBAAsB,GAAG,IAAI,CAuBnF;AAED,qBAAa,4BAA4B;IAErC,0BAA0B,CAAC,gBAAgB,EAAE,GAAG,CAAC,SAAS,EAAE,MAAM,EAAE,kBAAkB,GAAG,IAAI;IAiB7F,eAAe,CAAC,IAAI,EAAE,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,kBAAkB,GAAG,IAAI;IAMjE,oBAAoB,CAAC,IAAI,EAAE,GAAG,CAAC,SAAS,EAAE,MAAM,EAAE,kBAAkB,GAAG,IAAI;IAM3E,6BAA6B,CAAC,OAAO,EAAE,GAAG,CAAC,OAAO,EAAE,MAAM,EAAE,kBAAkB,GAAG,IAAI;IAarF,wCAAwC,CAAC,OAAO,EAAE,GAAG,CAAC,OAAO,EAAE,MAAM,EAAE,kBAAkB,GAAG,IAAI;IAchG,yBAAyB,CAAC,MAAM,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,kBAAkB,GAAG,IAAI;IAM/E,gCAAgC,CAAC,SAAS,EAAE,GAAG,CAAC,SAAS,EAAE,MAAM,EAAE,kBAAkB,GAAG,IAAI;IAgE5F,OAAO,CAAC,2BAA2B;IAOnC,OAAO,CAAC,4BAA4B;CAQvC"}

View File

@@ -0,0 +1,17 @@
/******************************************************************************
* Copyright 2022 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 { Grammar } from '../../languages/generated/ast.js';
import type { ValidationResources } from '../workspace/documents.js';
import type { LangiumGrammarServices } from '../langium-grammar-module.js';
export declare class LangiumGrammarValidationResourcesCollector {
private readonly services;
constructor(services: LangiumGrammarServices);
collectValidationResources(grammar: Grammar): ValidationResources;
private collectValidationInfo;
private collectSuperProperties;
private addSuperProperties;
}
//# sourceMappingURL=validation-resources-collector.d.ts.map

View File

@@ -0,0 +1,93 @@
/******************************************************************************
* Copyright 2022 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 { MultiMap } from '../../utils/collections.js';
import { stream } from '../../utils/stream.js';
import { isAction, isAlternatives, isGroup, isUnorderedGroup } from '../../languages/generated/ast.js';
import { mergeInterfaces, mergeTypesAndInterfaces } from '../type-system/types-util.js';
import { collectValidationAst } from '../type-system/ast-collector.js';
import { getActionType, getRuleTypeName } from '../../utils/grammar-utils.js';
export class LangiumGrammarValidationResourcesCollector {
constructor(services) {
this.services = services;
}
collectValidationResources(grammar) {
try {
const typeResources = collectValidationAst(grammar, this.services);
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() };
}
}
collectValidationInfo({ astResources, inferred, declared }) {
const res = new Map();
const typeNameToRulesActions = collectNameToRulesActions(astResources);
for (const type of mergeTypesAndInterfaces(inferred)) {
res.set(type.name, { inferred: type, inferredNodes: typeNameToRulesActions.get(type.name) });
}
const typeNametoInterfacesUnions = stream(astResources.interfaces)
.concat(astResources.types)
.reduce((acc, type) => acc.set(type.name, type), new Map());
for (const type of mergeTypesAndInterfaces(declared)) {
const node = typeNametoInterfacesUnions.get(type.name);
if (node) {
const inferred = res.get(type.name);
res.set(type.name, { ...inferred ?? {}, declared: type, declaredNode: node });
}
}
return res;
}
collectSuperProperties({ inferred, declared }) {
const typeToSuperProperties = new Map();
const interfaces = mergeInterfaces(inferred, declared);
const interfaceMap = new Map(interfaces.map(e => [e.name, e]));
for (const type of mergeInterfaces(inferred, declared)) {
typeToSuperProperties.set(type.name, this.addSuperProperties(type, interfaceMap, new Set()));
}
return typeToSuperProperties;
}
addSuperProperties(interfaceType, map, visited) {
if (visited.has(interfaceType.name)) {
return [];
}
visited.add(interfaceType.name);
const properties = [...interfaceType.properties];
for (const superType of interfaceType.superTypes) {
const value = map.get(superType.name);
if (value) {
properties.push(...this.addSuperProperties(value, map, visited));
}
}
return properties;
}
}
function collectNameToRulesActions({ parserRules, datatypeRules }) {
const acc = new MultiMap();
// collect rules
stream(parserRules)
.concat(datatypeRules)
.forEach(rule => acc.add(getRuleTypeName(rule), rule));
// collect actions
function collectActions(element) {
if (isAction(element)) {
const name = getActionType(element);
if (name) {
acc.add(name, element);
}
}
if (isAlternatives(element) || isGroup(element) || isUnorderedGroup(element)) {
element.elements.forEach(e => collectActions(e));
}
}
parserRules
.forEach(rule => collectActions(rule.definition));
return acc;
}
//# sourceMappingURL=validation-resources-collector.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"validation-resources-collector.js","sourceRoot":"","sources":["../../../src/grammar/validation/validation-resources-collector.ts"],"names":[],"mappings":"AAAA;;;;gFAIgF;AAQhF,OAAO,EAAE,QAAQ,EAAE,MAAM,4BAA4B,CAAC;AACtD,OAAO,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAC;AAC/C,OAAO,EAAE,QAAQ,EAAE,cAAc,EAAE,OAAO,EAAE,gBAAgB,EAAE,MAAM,kCAAkC,CAAC;AACvG,OAAO,EAAE,eAAe,EAAE,uBAAuB,EAAE,MAAM,8BAA8B,CAAC;AAC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AO,CAAC,IAAI,OAAO,CAAC,OAAO,CAAC,IAAI,gBAAgB,CAAC,OAAO,CAAC,EAAE,CAAC;YAC7E,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,CAAC;QACrD,CAAC;IACL,CAAC;IAED,WAAW;SACN,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC,cAAc,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC,CAAC;IAEtD,OAAO,GAAG,CAAC;AACf,CAAC"}

View File

@@ -0,0 +1,33 @@
/******************************************************************************
* Copyright 2022 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 { LangiumDocument } from '../../workspace/documents.js';
import type { Action, Grammar, Interface, ParserRule, Type } from '../../languages/generated/ast.js';
import type { Property, TypeOption } from '../type-system/type-collector/types.js';
/**
* A Langium document holds the parse result (AST and CST) and any additional state that is derived
* from the AST, e.g. the result of scope precomputation.
*/
export interface LangiumGrammarDocument extends LangiumDocument<Grammar> {
validationResources?: ValidationResources;
}
export type ValidationResources = {
typeToValidationInfo: TypeToValidationInfo;
typeToSuperProperties: Map<string, Property[]>;
};
export type TypeToValidationInfo = Map<string, InferredInfo | DeclaredInfo | InferredInfo & DeclaredInfo>;
export type InferredInfo = {
inferred: TypeOption;
inferredNodes: ReadonlyArray<ParserRule | Action>;
};
export type DeclaredInfo = {
declared: TypeOption;
declaredNode: Type | Interface;
};
export declare function isDeclared(type: InferredInfo | DeclaredInfo | InferredInfo & DeclaredInfo): type is DeclaredInfo;
export declare function isInferred(type: InferredInfo | DeclaredInfo | InferredInfo & DeclaredInfo): type is InferredInfo;
export declare function isInferredAndDeclared(type: InferredInfo | DeclaredInfo | InferredInfo & DeclaredInfo): type is InferredInfo & DeclaredInfo;
export declare function getTypeOption(info: InferredInfo | DeclaredInfo): TypeOption;
//# sourceMappingURL=documents.d.ts.map