Skip to content

Commit

Permalink
Support multi-target references
Browse files Browse the repository at this point in the history
  • Loading branch information
msujew committed May 23, 2024
1 parent 6971c83 commit 942f802
Show file tree
Hide file tree
Showing 41 changed files with 712 additions and 282 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,8 @@ export const ArithmeticsGrammar = (): Grammar => loadedArithmeticsGrammar ?? (lo
"type": {
"$ref": "#/types@0"
},
"deprecatedSyntax": false
"deprecatedSyntax": false,
"isMulti": false
}
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,24 @@ export class DomainModelRenameProvider extends DefaultRenameProvider {
const offset = document.textDocument.offsetAt(params.position);
const leafNode = CstUtils.findDeclarationNodeAtOffset(rootNode, offset, this.grammarConfig.nameRegexp);
if (!leafNode) return undefined;
const targetNode = this.references.findDeclaration(leafNode);
if (!targetNode) return undefined;
if (isNamed(targetNode)) targetNode.name = params.newName;
const location = this.getNodeLocation(targetNode);
if (location) {
const change = TextEdit.replace(location.range, params.newName);
const uri = location.uri;
if (uri) {
if (changes[uri]) {
changes[uri].push(change);
} else {
changes[uri] = [change];
const targetNodes = this.references.findDeclarations(leafNode);
if (!targetNodes.length) return undefined;
for (const node of targetNodes) {
if (isNamed(node)) node.name = params.newName;
const location = this.getNodeLocation(node);
if (location) {
const change = TextEdit.replace(location.range, params.newName);
const uri = location.uri;
if (uri) {
if (changes[uri]) {
changes[uri].push(change);
} else {
changes[uri] = [change];
}
}
}
}

const targetNode = targetNodes[0];
for (const node of AstUtils.streamAst(targetNode)) {
const qn = this.buildQualifiedName(node);
if (qn) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ DataType:
'datatype' name=ID;

Entity:
'entity' name=ID ('extends' superType=[Entity:QualifiedName])? '{'
'entity' name=ID ('extends' superType=[*Entity:QualifiedName])? '{'
(features+=Feature)*
'}';

Expand Down
4 changes: 2 additions & 2 deletions examples/domainmodel/src/language-server/generated/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
******************************************************************************/

/* eslint-disable */
import type { AstNode, Reference, ReferenceInfo, TypeMetaData } from 'langium';
import type { AstNode, Reference, MultiReference, ReferenceInfo, TypeMetaData } from 'langium';
import { AbstractAstReflection } from 'langium';

export const DomainModelTerminals = {
Expand Down Expand Up @@ -64,7 +64,7 @@ export interface Entity extends AstNode {
readonly $type: 'Entity';
features: Array<Feature>;
name: string;
superType?: Reference<Entity>;
superType?: MultiReference<Entity>;
}

export const Entity = 'Entity';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ export const DomainModelGrammar = (): Grammar => loadedDomainModelGrammar ?? (lo
"operator": "=",
"terminal": {
"$type": "CrossReference",
"isMulti": true,
"type": {
"$ref": "#/rules@5"
},
Expand Down Expand Up @@ -305,7 +306,8 @@ export const DomainModelGrammar = (): Grammar => loadedDomainModelGrammar ?? (lo
},
"arguments": []
},
"deprecatedSyntax": false
"deprecatedSyntax": false,
"isMulti": false
}
}
]
Expand Down
24 changes: 16 additions & 8 deletions examples/requirements/src/language-server/generated/grammar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,8 @@ export const RequirementsGrammar = (): Grammar => loadedRequirementsGrammar ?? (
"type": {
"$ref": "#/rules@1"
},
"deprecatedSyntax": false
"deprecatedSyntax": false,
"isMulti": false
}
},
{
Expand All @@ -187,7 +188,8 @@ export const RequirementsGrammar = (): Grammar => loadedRequirementsGrammar ?? (
"type": {
"$ref": "#/rules@1"
},
"deprecatedSyntax": false
"deprecatedSyntax": false,
"isMulti": false
}
}
],
Expand Down Expand Up @@ -428,7 +430,8 @@ export const TestsGrammar = (): Grammar => loadedTestsGrammar ?? (loadedTestsGra
},
"arguments": []
},
"deprecatedSyntax": false
"deprecatedSyntax": false,
"isMulti": false
}
},
{
Expand All @@ -454,7 +457,8 @@ export const TestsGrammar = (): Grammar => loadedTestsGrammar ?? (loadedTestsGra
},
"arguments": []
},
"deprecatedSyntax": false
"deprecatedSyntax": false,
"isMulti": false
}
}
],
Expand All @@ -480,7 +484,8 @@ export const TestsGrammar = (): Grammar => loadedTestsGrammar ?? (loadedTestsGra
"type": {
"$ref": "#/rules@3"
},
"deprecatedSyntax": false
"deprecatedSyntax": false,
"isMulti": false
}
},
{
Expand All @@ -499,7 +504,8 @@ export const TestsGrammar = (): Grammar => loadedTestsGrammar ?? (loadedTestsGra
"type": {
"$ref": "#/rules@3"
},
"deprecatedSyntax": false
"deprecatedSyntax": false,
"isMulti": false
}
}
],
Expand Down Expand Up @@ -672,7 +678,8 @@ export const TestsGrammar = (): Grammar => loadedTestsGrammar ?? (loadedTestsGra
"type": {
"$ref": "#/rules@3"
},
"deprecatedSyntax": false
"deprecatedSyntax": false,
"isMulti": false
}
},
{
Expand All @@ -691,7 +698,8 @@ export const TestsGrammar = (): Grammar => loadedTestsGrammar ?? (loadedTestsGra
"type": {
"$ref": "#/rules@3"
},
"deprecatedSyntax": false
"deprecatedSyntax": false,
"isMulti": false
}
}
],
Expand Down
12 changes: 8 additions & 4 deletions examples/statemachine/src/language-server/generated/grammar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ export const StatemachineGrammar = (): Grammar => loadedStatemachineGrammar ?? (
"type": {
"$ref": "#/rules@3"
},
"deprecatedSyntax": false
"deprecatedSyntax": false,
"isMulti": false
}
},
{
Expand Down Expand Up @@ -204,7 +205,8 @@ export const StatemachineGrammar = (): Grammar => loadedStatemachineGrammar ?? (
"type": {
"$ref": "#/rules@2"
},
"deprecatedSyntax": false
"deprecatedSyntax": false,
"isMulti": false
},
"cardinality": "+"
},
Expand Down Expand Up @@ -256,7 +258,8 @@ export const StatemachineGrammar = (): Grammar => loadedStatemachineGrammar ?? (
"type": {
"$ref": "#/rules@1"
},
"deprecatedSyntax": false
"deprecatedSyntax": false,
"isMulti": false
}
},
{
Expand All @@ -272,7 +275,8 @@ export const StatemachineGrammar = (): Grammar => loadedStatemachineGrammar ?? (
"type": {
"$ref": "#/rules@3"
},
"deprecatedSyntax": false
"deprecatedSyntax": false,
"isMulti": false
}
}
]
Expand Down
14 changes: 10 additions & 4 deletions packages/langium-cli/src/generator/ast-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* 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, LangiumCoreServices } from 'langium';
import { type Generated, expandToNode, joinToNode, toString } from 'langium/generate';
import type { AstTypes, Property, PropertyDefaultValue } from 'langium/grammar';
Expand All @@ -14,14 +15,14 @@ import { collectTerminalRegexps } from './langium-util.js';

export function generateAst(services: LangiumCoreServices, grammars: Grammar[], config: LangiumConfig): string {
const astTypes = collectAst(grammars, services.shared.workspace.LangiumDocuments);
const crossRef = grammars.some(grammar => hasCrossReferences(grammar));
const crossRef = getCrossReferenceTypes(grammars);
const importFrom = config.langiumInternal ? `../../syntax-tree${config.importExtension}` : 'langium';
/* eslint-disable @typescript-eslint/indent */
const fileNode = expandToNode`
${generatedHeader}
/* eslint-disable */
import type { AstNode${crossRef ? ', Reference' : ''}, ReferenceInfo, TypeMetaData } from '${importFrom}';
import type { AstNode${crossRef.single ? ', Reference' : ''}${crossRef.multi ? ', MultiReference' : ''}, ReferenceInfo, TypeMetaData } from '${importFrom}';
import { AbstractAstReflection } from '${importFrom}';
${generateTerminalConstants(grammars, config)}
Expand All @@ -37,8 +38,13 @@ export function generateAst(services: LangiumCoreServices, grammars: Grammar[],
/* eslint-enable @typescript-eslint/indent */
}

function hasCrossReferences(grammar: Grammar): boolean {
return Boolean(AstUtils.streamAllContents(grammar).find(GrammarAST.isCrossReference));
function getCrossReferenceTypes(grammars: Grammar[]): { single: boolean, multi: boolean } {
const allCrossReferences = grammars.flatMap(grammar => AstUtils.streamAllContents(grammar).filter(GrammarAST.isCrossReference).toArray());
const multiCrossReferences = allCrossReferences.filter(e => e.isMulti);
return {
single: multiCrossReferences.length < allCrossReferences.length,
multi: multiCrossReferences.length > 0
};
}

function generateAstReflection(config: LangiumConfig, astTypes: AstTypes): Generated {
Expand Down
6 changes: 3 additions & 3 deletions packages/langium-cli/src/generator/grammar-serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* terms of the MIT License, which is available in the project root.
******************************************************************************/

import type { Grammar, LangiumCoreServices, Reference } from 'langium';
import type { Grammar, LangiumCoreServices } from 'langium';
import { expandToNode, joinToNode, normalizeEOL, toString } from 'langium/generate';
import type { URI } from 'vscode-uri';
import type { LangiumConfig } from '../package-types.js';
Expand All @@ -30,9 +30,9 @@ export function serializeGrammar(services: LangiumCoreServices, grammars: Gramma
grammar => {
const production = config.mode === 'production';
const delimiter = production ? "'" : '`';
const uriConverter = (uri: URI, ref: Reference) => {
const uriConverter = (uri: URI) => {
// We expect the grammar to be self-contained after the transformations we've done before
throw new Error(`Unexpected reference to symbol '${ref.$refText}' in document: ${uri.toString()}`);
throw new Error(`Unexpected reference to element in document: ${uri.toString()}`);
};
const serializedGrammar = services.serializer.JsonSerializer.serialize(grammar, {
space: production ? undefined : 2,
Expand Down
Loading

0 comments on commit 942f802

Please sign in to comment.