-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Execute langium-cli using a command to generate types definition
- Loading branch information
Showing
10 changed files
with
321 additions
and
29 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/****************************************************************************** | ||
* 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 { CompositeGeneratorNode, Grammar, LangiumServices, NL, processGeneratorNode } from 'langium'; | ||
import { collectAst, distinctAndSorted, PropertyType } from 'langium/lib/grammar/type-system'; | ||
import { LangiumGrammarGrammar } from 'langium/lib/grammar/generated/grammar'; | ||
import { collectKeywords } from './util'; | ||
|
||
export function generateTypesFile(services: LangiumServices, grammars: Grammar[]): string { | ||
const astTypes = collectAst(services.shared.workspace.LangiumDocuments, grammars); | ||
const fileNode = new CompositeGeneratorNode(); | ||
const reservedWords = new Set(collectKeywords(LangiumGrammarGrammar())); | ||
astTypes.unions.filter((union) => !astTypes.interfaces.find((iface) => | ||
iface.name === union.name | ||
)).forEach((union) => { | ||
fileNode.append(`type ${escapeReservedWords(union.name, reservedWords)} = ${propertyTypesToString(union.union)};`); | ||
fileNode.append(NL).append(NL); | ||
}); | ||
astTypes.interfaces.forEach((iFace) => { | ||
fileNode.append(`interface ${escapeReservedWords(iFace.name, reservedWords)}${iFace.interfaceSuperTypes.length > 0 ? (' extends ' + Array.from(iFace.interfaceSuperTypes).join(', ')) : ''} {`); | ||
fileNode.append(NL); | ||
fileNode.indent((body) => { | ||
iFace.properties.forEach(property => | ||
body.append(`${escapeReservedWords(property.name, reservedWords)}${property.optional ? '?' : ''}: ${propertyTypesToString(property.typeAlternatives)}`).append(NL) | ||
); | ||
}); | ||
fileNode.append('}').append(NL).append(NL); | ||
} | ||
); | ||
return processGeneratorNode(fileNode); | ||
} | ||
|
||
function propertyTypesToString(alternatives: PropertyType[]): string { | ||
return distinctAndSorted(alternatives.map(typePropertyToString)).join(' | '); | ||
} | ||
|
||
function typePropertyToString(propertyType: PropertyType): string { | ||
let res = distinctAndSorted(propertyType.types).join(' | '); | ||
res = propertyType.reference ? `@${res}` : res; | ||
res = propertyType.array ? `${res}[]` : res; | ||
return res; | ||
} | ||
|
||
function escapeReservedWords(name: string, reserved: Set<string>): string { | ||
return reserved.has(name) ? `^${name}` : name; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
packages/langium-cli/test/generator/types-generator.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/****************************************************************************** | ||
* 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 { createLangiumGrammarServices, EmptyFileSystem, Grammar } from 'langium'; | ||
import { parseHelper } from 'langium/test'; | ||
import { generateTypesFile } from '../../src/generator/types-generator'; | ||
|
||
const { grammar } = createLangiumGrammarServices(EmptyFileSystem); | ||
|
||
describe('Types generator', () => { | ||
|
||
test('should generate types file', async () => { | ||
const result = (await parseHelper<Grammar>(grammar)(TEST_GRAMMAR)).parseResult; | ||
const typesFileContent = generateTypesFile(grammar, [result.value]); | ||
expect(typesFileContent).toMatch(EXPECTED_TYPES); | ||
}); | ||
|
||
}); | ||
|
||
const EXPECTED_TYPES = | ||
`type AbstractDefinition = DeclaredParameter | Definition; | ||
type Expression = BinaryExpression | FunctionCall | NumberLiteral; | ||
type Statement = Definition | Evaluation; | ||
interface BinaryExpression { | ||
left: Expression | ||
operator: '*' | '+' | '-' | '/' | ||
right: Expression | ||
} | ||
interface DeclaredParameter { | ||
name: string | ||
} | ||
interface Definition { | ||
name: string | ||
args?: DeclaredParameter[] | ||
expr: Expression | ||
} | ||
interface Evaluation { | ||
expression: Expression | ||
} | ||
interface FunctionCall { | ||
func: @AbstractDefinition | ||
args?: Expression[] | ||
} | ||
interface Module { | ||
name: string | ||
statements?: Statement[] | ||
} | ||
interface NumberLiteral { | ||
value: number | ||
} | ||
`; | ||
|
||
const TEST_GRAMMAR = | ||
` | ||
grammar Arithmetics | ||
entry Module: | ||
'module' name=ID | ||
(statements+=Statement)*; | ||
Statement: | ||
Definition | Evaluation; | ||
Definition: | ||
'def' name=ID ('(' args+=DeclaredParameter (',' args+=DeclaredParameter)* ')')? | ||
':' expr=Expression ';'; | ||
DeclaredParameter: | ||
name=ID; | ||
type AbstractDefinition = Definition | DeclaredParameter; | ||
Evaluation: | ||
expression=Expression ';'; | ||
Expression: | ||
Addition; | ||
Addition infers Expression: | ||
Multiplication ({infer BinaryExpression.left=current} operator=('+' | '-') right=Multiplication)*; | ||
Multiplication infers Expression: | ||
PrimaryExpression ({infer BinaryExpression.left=current} operator=('*' | '/') right=PrimaryExpression)*; | ||
PrimaryExpression infers Expression: | ||
'(' Expression ')' | | ||
{infer NumberLiteral} value=NUMBER | | ||
{infer FunctionCall} func=[AbstractDefinition] ('(' args+=Expression (',' args+=Expression)* ')')?; | ||
hidden terminal WS: /\\s+/; | ||
terminal ID: /[_a-zA-Z][\\w_]*/; | ||
terminal NUMBER returns number: /[0-9]+(\\.[0-9]*)?/; | ||
hidden terminal ML_COMMENT: /\\/\\*[\\s\\S]*?\\*\\//; | ||
hidden terminal SL_COMMENT: /\\/\\/[^\\n\\r]*/; | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.