Skip to content

Commit

Permalink
Fix the CST after performing assigned actions (#1674)
Browse files Browse the repository at this point in the history
  • Loading branch information
msujew authored Oct 25, 2024
1 parent b2b1cbe commit 516fe4c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/langium/src/parser/langium-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ export class LangiumParser extends AbstractLangiumParser {
let last = this.current;
if (action.feature && action.operator) {
last = this.construct();
this.nodeBuilder.removeNode(last.$cstNode);
const node = this.nodeBuilder.buildCompositeNode(action);
node.content.push(last.$cstNode);
const newItem = { $type };
Expand Down
17 changes: 16 additions & 1 deletion packages/langium/test/parser/langium-parser-builder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

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

});

describe('Handling hidden nodes', () => {
test('Should find comment node of element affected by an assigned action', async () => {
const grammar = createLangiumGrammarServices(EmptyFileSystem);
const parser = parseHelper(grammar.grammar);
const doc = await parser("Test returns string: /** comment 1 */ 'A' | /** comment 2 */ 'B' | /** comment 3 */ 'C';");
expect(doc).toBeDefined();
const value = doc.parseResult.value as Grammar;
const ruleDef = value.rules[0].definition as GrammarAST.Alternatives;
const a = ruleDef.elements[0];
const commentNode = CstUtils.findCommentNode(a.$cstNode, ['ML_COMMENT']);
expect(commentNode).toBeDefined();
expect(commentNode!.text).toBe('/** comment 1 */');
});
});

describe('Handling EOF', () => {
test('Use EOF as last part of the entry rule definition', async () => {
const grammar = `
Expand Down

0 comments on commit 516fe4c

Please sign in to comment.