Skip to content

Commit 1f5d124

Browse files
authored
Generate grammar comments (#1335)
1 parent 8a53552 commit 1f5d124

File tree

4 files changed

+59
-6
lines changed

4 files changed

+59
-6
lines changed

packages/langium-cli/src/generator/grammar-serializer.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export function serializeGrammar(services: LangiumServices, grammars: Grammar[],
3636
};
3737
const serializedGrammar = services.serializer.JsonSerializer.serialize(grammar, {
3838
space: production ? undefined : 2,
39+
comments: true,
3940
uriConverter
4041
});
4142
// The json serializer returns strings with \n line delimiter by default
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/******************************************************************************
2+
* Copyright 2021 TypeFox GmbH
3+
* This program and the accompanying materials are made available under the
4+
* terms of the MIT License, which is available in the project root.
5+
******************************************************************************/
6+
7+
import type { LangiumConfig } from '../../src/package.js';
8+
import { EmptyFileSystem, URI, type Grammar } from 'langium';
9+
import { describe, expect, test } from 'vitest';
10+
import { RelativePath } from '../../src/package.js';
11+
import { serializeGrammar } from '../../src/generator/grammar-serializer.js';
12+
import { createLangiumGrammarServices } from 'langium/grammar';
13+
import { expandToString } from 'langium/generate';
14+
15+
const grammarServices = createLangiumGrammarServices(EmptyFileSystem);
16+
17+
describe('Grammar serializer', () => {
18+
test('should include comments of AST elements', async () => {
19+
// arrange
20+
const config: LangiumConfig = {
21+
projectName: 'Magic',
22+
languages: [],
23+
[RelativePath]: '/path/to/magic',
24+
};
25+
26+
const grammarText = expandToString`
27+
grammar Test
28+
29+
/**
30+
* This is the entry rule
31+
*/
32+
entry Model: /** This is the name assignment */ name='test';
33+
`;
34+
35+
const document = grammarServices.shared.workspace.LangiumDocumentFactory.fromString<Grammar>(grammarText, URI.file('test.langium'));
36+
grammarServices.shared.workspace.LangiumDocuments.addDocument(document);
37+
await grammarServices.shared.workspace.DocumentBuilder.build([document]);
38+
const grammar = document.parseResult.value;
39+
40+
// act
41+
const moduleString = serializeGrammar(grammarServices.grammar, [grammar], config);
42+
43+
// assert
44+
expect(moduleString).toMatch('"$comment": "/** This is the name assignment */"');
45+
expect(moduleString).toMatch('"$comment": "/**\\\\n * This is the entry rule\\\\n */"');
46+
});
47+
48+
});

packages/langium/src/grammar/langium-grammar.langium

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
/******************************************************************************
2-
* Copyright 2021 TypeFox GmbH
3-
* This program and the accompanying materials are made available under the
4-
* terms of the MIT License, which is available in the project root.
5-
******************************************************************************/
1+
// ******************************************************************************
2+
// Copyright 2021 TypeFox GmbH
3+
// This program and the accompanying materials are made available under the
4+
// terms of the MIT License, which is available in the project root.
5+
// *****************************************************************************
6+
67
grammar LangiumGrammar
78

89
entry Grammar:

packages/langium/src/serializer/json-serializer.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,10 @@ export class DefaultJsonSerializer implements JsonSerializer {
187187
}
188188
if (comments) {
189189
astNode ??= { ...value };
190-
(astNode as AstNodeWithComment).$comment = this.commentProvider.getComment(value);
190+
const comment = this.commentProvider.getComment(value);
191+
if (comment) {
192+
(astNode as AstNodeWithComment).$comment = comment.replace(/\r/g, '');
193+
}
191194
}
192195
return astNode ?? value;
193196
} else {

0 commit comments

Comments
 (0)