Skip to content

Commit faa656e

Browse files
committed
Improve performance even further
1 parent 940fa56 commit faa656e

File tree

3 files changed

+19
-19
lines changed

3 files changed

+19
-19
lines changed

packages/langium/src/parser/cst-node-builder.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,30 +54,34 @@ export class CstNodeBuilder {
5454
}
5555
}
5656

57-
addHiddenNode(token: IToken): void {
58-
const leafNode = new LeafCstNodeImpl(token.startOffset, token.image.length, tokenToRange(token), token.tokenType, true);
59-
leafNode.root = this.rootNode;
57+
addHiddenNodes(tokens: IToken[]): void {
58+
const nodes: LeafCstNode[] = [];
59+
for (const token of tokens) {
60+
const leafNode = new LeafCstNodeImpl(token.startOffset, token.image.length, tokenToRange(token), token.tokenType, true);
61+
leafNode.root = this.rootNode;
62+
nodes.push(leafNode);
63+
}
6064
let current: CompositeCstNode = this.current;
6165
let added = false;
62-
// If we are within a composite node, we add the hidden node to the content
66+
// If we are within a composite node, we add the hidden nodes to the content
6367
if (current.content.length > 0) {
64-
current.content.push(leafNode);
68+
current.content.push(...nodes);
6569
return;
6670
}
6771
while (current.container) {
6872
const index = current.container.content.indexOf(current);
6973
if (index > 0) {
70-
// Add the hidden node before the current node
71-
current.container.content.splice(index, 0, leafNode);
74+
// Add the hidden nodes before the current node
75+
current.container.content.splice(index, 0, ...nodes);
7276
added = true;
7377
break;
7478
}
7579
current = current.container;
7680
}
77-
// If we arrive at the root node, we add the hidden node at the beginning
78-
// This is the case if the hidden node is the first node in the tree
81+
// If we arrive at the root node, we add the hidden nodes at the beginning
82+
// This is the case if the hidden nodes are the first nodes in the tree
7983
if (!added) {
80-
this.rootNode.content.unshift(leafNode);
84+
this.rootNode.content.unshift(...nodes);
8185
}
8286
}
8387

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ export class LangiumParser extends AbstractLangiumParser {
240240
throw new Error(options.rule ? `No rule found with name '${options.rule}'` : 'No main rule available.');
241241
}
242242
const result = ruleMethod.call(this.wrapper, {});
243-
this.appendHiddenTokens(lexerResult.hidden);
243+
this.nodeBuilder.addHiddenNodes(lexerResult.hidden);
244244
this.unorderedGroups.clear();
245245
this.lexerResult = undefined;
246246
return {
@@ -275,12 +275,6 @@ export class LangiumParser extends AbstractLangiumParser {
275275
};
276276
}
277277

278-
private appendHiddenTokens(tokens: IToken[]): void {
279-
for (const token of tokens) {
280-
this.nodeBuilder.addHiddenNode(token);
281-
}
282-
}
283-
284278
private getHiddenTokens(token: IToken): IToken[] {
285279
const hiddenTokens = this.lexerResult!.hidden;
286280
if (!hiddenTokens.length) {
@@ -300,7 +294,7 @@ export class LangiumParser extends AbstractLangiumParser {
300294
const token = this.wrapper.wrapConsume(idx, tokenType);
301295
if (!this.isRecording() && this.isValidToken(token)) {
302296
const hiddenTokens = this.getHiddenTokens(token);
303-
this.appendHiddenTokens(hiddenTokens);
297+
this.nodeBuilder.addHiddenNodes(hiddenTokens);
304298
const leafNode = this.nodeBuilder.buildLeafNode(token, feature);
305299
const { assignment, isCrossRef } = this.getAssignment(feature);
306300
const current = this.current;

packages/langium/test/utils/cst-utils.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,13 @@ describe('findCommentNode', () => {
8585
test('Finds correct comment at the start of the file', async () => {
8686
const text = expandToString`
8787
/** A */
88+
/** B */
89+
/** C */
8890
grammar test
8991
`;
9092
const grammar = await parser(text);
9193
const comment = CstUtils.findCommentNode(grammar.parseResult.value.$cstNode, ['ML_COMMENT']);
92-
expect(comment?.text).toBe('/** A */');
94+
expect(comment?.text).toBe('/** C */');
9395
});
9496
});
9597

0 commit comments

Comments
 (0)