Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate language meta data #170

Merged
merged 3 commits into from
Jul 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/******************************************************************************
* This file was generated by langium-cli 0.1.0.
* DO NOT EDIT MANUALLY!
******************************************************************************/

export class ArithmeticsLanguageMetaData {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case I'd rather use a plain object than a class.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, that makes sense. Should the object this be generated into a meta-data.ts file, or should it be build in the generated injection module? like this:

LanguageMetaData: () => ({
    languageId: 'id',
    extensions: ['.id']
})

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exporting a const for it could be useful so you can access it directly in your language implementation. But it could be in the generated/module.ts instead of a new file to keep the number of generated files low.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, yeah I agree with that.

languageId = 'arithmetics';
extensions = ['.calc'];
msujew marked this conversation as resolved.
Show resolved Hide resolved
}
4 changes: 3 additions & 1 deletion examples/arithmetics/src/language-server/generated/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
import { LangiumGeneratedServices, LangiumServices, Module } from 'langium';
import { ArithmeticsAstReflection } from './ast';
import { ArithmeticsGrammarAccess } from './grammar-access';
import { ArithmeticsLanguageMetaData } from './meta-data';
import { Parser } from './parser';

export const ArithmeticsGeneratedModule: Module<LangiumServices, LangiumGeneratedServices> = {
parser: {
LangiumParser: (injector) => new Parser(injector)
},
GrammarAccess: () => new ArithmeticsGrammarAccess(),
AstReflection: () => new ArithmeticsAstReflection()
AstReflection: () => new ArithmeticsAstReflection(),
LanguageMetaData: () => new ArithmeticsLanguageMetaData()
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/******************************************************************************
* This file was generated by langium-cli 0.1.0.
* DO NOT EDIT MANUALLY!
******************************************************************************/

export class DomainModelLanguageMetaData {
languageId = 'domain-model';
extensions = ['.dmodel'];
}
4 changes: 3 additions & 1 deletion examples/domainmodel/src/language-server/generated/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
import { LangiumGeneratedServices, LangiumServices, Module } from 'langium';
import { DomainModelAstReflection } from './ast';
import { DomainModelGrammarAccess } from './grammar-access';
import { DomainModelLanguageMetaData } from './meta-data';
import { Parser } from './parser';

export const DomainModelGeneratedModule: Module<LangiumServices, LangiumGeneratedServices> = {
parser: {
LangiumParser: (injector) => new Parser(injector)
},
GrammarAccess: () => new DomainModelGrammarAccess(),
AstReflection: () => new DomainModelAstReflection()
AstReflection: () => new DomainModelAstReflection(),
LanguageMetaData: () => new DomainModelLanguageMetaData()
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/******************************************************************************
* This file was generated by langium-cli 0.1.0.
* DO NOT EDIT MANUALLY!
******************************************************************************/

export class StatemachineLanguageMetaData {
languageId = 'statemachine';
extensions = ['.statemachine'];
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
import { LangiumGeneratedServices, LangiumServices, Module } from 'langium';
import { StatemachineAstReflection } from './ast';
import { StatemachineGrammarAccess } from './grammar-access';
import { StatemachineLanguageMetaData } from './meta-data';
import { Parser } from './parser';

export const StatemachineGeneratedModule: Module<LangiumServices, LangiumGeneratedServices> = {
parser: {
LangiumParser: (injector) => new Parser(injector)
},
GrammarAccess: () => new StatemachineGrammarAccess(),
AstReflection: () => new StatemachineAstReflection()
AstReflection: () => new StatemachineAstReflection(),
LanguageMetaData: () => new StatemachineLanguageMetaData()
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"build": "tsc -b tsconfig.build.json",
"watch": "tsc -b tsconfig.build.json -w",
"lint": "npm run lint --workspaces",
"langium:generate": "npm run langium:generate --workspace=langium --workspace=domainmodel --workspace=arithmetics --workspace=statemachine",
"prepare": "npm run clean && npm run build"
},
"devDependencies": {
Expand Down
4 changes: 4 additions & 0 deletions packages/langium-cli/src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { generateModule } from './generator/module-generator';
import { generateTextMate } from './generator/textmate-generator';
import path from 'path';
import { serializeGrammar } from './generator/grammar-serializer';
import { generateMetaData } from './generator/meta-data-generator';

export type GenerateOptions = {
file?: string;
Expand Down Expand Up @@ -58,6 +59,9 @@ export function generate(opts: GenerateOptions): void {
const genAst = generateAst(grammar, pack.langium);
fs.writeFileSync(`${output}/ast.ts`, genAst);

const metaData = generateMetaData(grammar, pack.langium);
fs.writeFileSync(`${output}/meta-data.ts`, metaData);

console.log('Generating dependency injection module...');
const genModule = generateModule(grammar, pack.langium);
fs.writeFileSync(`${output}/module.ts`, genModule);
Expand Down
27 changes: 27 additions & 0 deletions packages/langium-cli/src/generator/meta-data-generator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/******************************************************************************
* 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 { CompositeGeneratorNode, Grammar, NL, processGeneratorNode } from 'langium';
import { LangiumConfig } from '../package';
import { generatedHeader } from './util';

export function generateMetaData(grammar: Grammar, config: LangiumConfig): string {
const node = new CompositeGeneratorNode();
node.append(generatedHeader, 'export class ', grammar.name, 'LanguageMetaData {', NL);
node.indent(classBody => {
classBody.append(`languageId = '${config.languageId}';`, NL);
classBody.append(`extensions = [${config.extensions && config.extensions.map(e => appendQuotesAndDot(e)).join(', ')}];`, NL);
});
node.append('}', NL);
return processGeneratorNode(node);
}

function appendQuotesAndDot(input: string): string {
if (!input.startsWith('.')) {
input = '.' + input;
}
return `'${input}'`;
}
4 changes: 3 additions & 1 deletion packages/langium-cli/src/generator/module-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export function generateModule(grammar: langium.Grammar, config: LangiumConfig):
node.append(
'import { ', grammar.name, "AstReflection } from './ast';", NL,
'import { ', grammar.name, "GrammarAccess } from './grammar-access';", NL,
'import { ', grammar.name, "LanguageMetaData } from './meta-data';", NL,
"import { Parser } from './parser';", NL, NL
);

Expand All @@ -33,7 +34,8 @@ export function generateModule(grammar: langium.Grammar, config: LangiumConfig):
moduleNode.append(
'},', NL,
'GrammarAccess: () => new ', grammar.name, 'GrammarAccess(),', NL,
'AstReflection: () => new ', grammar.name, 'AstReflection()', NL
'AstReflection: () => new ', grammar.name, 'AstReflection(),', NL,
'LanguageMetaData: () => new ', grammar.name, 'LanguageMetaData()', NL
);
});
node.append('};', NL);
Expand Down
1 change: 1 addition & 0 deletions packages/langium/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"publish:latest": "npm publish --tag latest"
},
"langium": {
"languageId": "langium",
"grammar": "src/grammar/langium-grammar.langium",
"extensions": [
"langium"
Expand Down
9 changes: 9 additions & 0 deletions packages/langium/src/grammar/generated/meta-data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/******************************************************************************
* This file was generated by langium-cli 0.1.0.
* DO NOT EDIT MANUALLY!
******************************************************************************/

export class LangiumGrammarLanguageMetaData {
languageId = 'langium';
extensions = ['.langium'];
}
4 changes: 3 additions & 1 deletion packages/langium/src/grammar/generated/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import { Module } from '../../dependency-injection';
import { LangiumGeneratedServices, LangiumServices } from '../../services';
import { LangiumGrammarAstReflection } from './ast';
import { LangiumGrammarGrammarAccess } from './grammar-access';
import { LangiumGrammarLanguageMetaData } from './meta-data';
import { Parser } from './parser';

export const LangiumGrammarGeneratedModule: Module<LangiumServices, LangiumGeneratedServices> = {
parser: {
LangiumParser: (injector) => new Parser(injector)
},
GrammarAccess: () => new LangiumGrammarGrammarAccess(),
AstReflection: () => new LangiumGrammarAstReflection()
AstReflection: () => new LangiumGrammarAstReflection(),
LanguageMetaData: () => new LangiumGrammarLanguageMetaData()
};
10 changes: 10 additions & 0 deletions packages/langium/src/grammar/language-meta-data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/******************************************************************************
* 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.
******************************************************************************/

export interface LanguageMetaData {
languageId: string;
extensions: string[];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shall we name this property fileExtensions for clarity? "extensions" is quite generic.

}
1 change: 1 addition & 0 deletions packages/langium/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export * from './grammar/generated/parser';
export * from './grammar/grammar-access';
export * from './grammar/grammar-util';
export * from './grammar/langium-grammar-module';
export * from './grammar/language-meta-data';
export * from './lsp/completion/completion-provider';
export * from './lsp/completion/follow-element-computation';
export * from './lsp/completion/rule-interpreter';
Expand Down
2 changes: 2 additions & 0 deletions packages/langium/src/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ import { DocumentHighlighter } from './lsp/document-highlighter';
import { References } from './references/references';
import { ValidationRegistry } from './validation/validation-registry';
import { DocumentValidator } from './validation/document-validator';
import { LanguageMetaData } from './grammar/language-meta-data';

export type LangiumGeneratedServices = {
parser: {
LangiumParser: LangiumParser
}
AstReflection: AstReflection
GrammarAccess: GrammarAccess
LanguageMetaData: LanguageMetaData
}

export type LangiumLspServices = {
Expand Down