Skip to content

Commit 516fe4c

Browse files
authored
Fix the CST after performing assigned actions (#1674)
1 parent b2b1cbe commit 516fe4c

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

packages/langium/src/parser/langium-parser.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@ export class LangiumParser extends AbstractLangiumParser {
342342
let last = this.current;
343343
if (action.feature && action.operator) {
344344
last = this.construct();
345+
this.nodeBuilder.removeNode(last.$cstNode);
345346
const node = this.nodeBuilder.buildCompositeNode(action);
346347
node.content.push(last.$cstNode);
347348
const newItem = { $type };

packages/langium/test/parser/langium-parser-builder.test.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import type { TokenType, TokenVocabulary } from 'chevrotain';
88
import type { AstNode, CstNode, GenericAstNode, Grammar, GrammarAST, LangiumParser, ParseResult, TokenBuilderOptions } from 'langium';
9-
import { EmptyFileSystem, DefaultTokenBuilder, GrammarUtils } from 'langium';
9+
import { EmptyFileSystem, DefaultTokenBuilder, GrammarUtils, CstUtils } from 'langium';
1010
import { describe, expect, test, onTestFailed, beforeAll } from 'vitest';
1111
import { createLangiumGrammarServices, createServicesForGrammar } from 'langium/grammar';
1212
import { expandToString } from 'langium/generate';
@@ -926,6 +926,21 @@ describe('Unassigned subrules', () => {
926926

927927
});
928928

929+
describe('Handling hidden nodes', () => {
930+
test('Should find comment node of element affected by an assigned action', async () => {
931+
const grammar = createLangiumGrammarServices(EmptyFileSystem);
932+
const parser = parseHelper(grammar.grammar);
933+
const doc = await parser("Test returns string: /** comment 1 */ 'A' | /** comment 2 */ 'B' | /** comment 3 */ 'C';");
934+
expect(doc).toBeDefined();
935+
const value = doc.parseResult.value as Grammar;
936+
const ruleDef = value.rules[0].definition as GrammarAST.Alternatives;
937+
const a = ruleDef.elements[0];
938+
const commentNode = CstUtils.findCommentNode(a.$cstNode, ['ML_COMMENT']);
939+
expect(commentNode).toBeDefined();
940+
expect(commentNode!.text).toBe('/** comment 1 */');
941+
});
942+
});
943+
929944
describe('Handling EOF', () => {
930945
test('Use EOF as last part of the entry rule definition', async () => {
931946
const grammar = `

0 commit comments

Comments
 (0)