Skip to content

Commit

Permalink
add implicit ID rule call for cross refs to getAllReachableRules
Browse files Browse the repository at this point in the history
Fixes #1151

Signed-off-by: Christian Dietrich <[email protected]>
  • Loading branch information
cdietrich committed Aug 15, 2023
1 parent 13f482b commit 55f68cc
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 9 deletions.
28 changes: 20 additions & 8 deletions packages/langium/src/utils/grammar-util.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/******************************************************************************
* Copyright 2021-2022 TypeFox GmbH
* Copyright 2021-2023 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.
******************************************************************************/
Expand Down Expand Up @@ -54,26 +54,38 @@ export function getAllReachableRules(grammar: ast.Grammar, allTerminals: boolean
}

const topMostRules = [entryRule as ast.AbstractRule].concat(getHiddenRules(grammar));
const rules = new Set<ast.AbstractRule>();
for (const rule of topMostRules) {
ruleDfs(rule, ruleNames, allTerminals);
ruleDfs(rule, ruleNames, rules, allTerminals);
}

const rules = new Set<ast.AbstractRule>();

for (const rule of grammar.rules) {
if (ruleNames.has(rule.name) || (ast.isTerminalRule(rule) && rule.hidden)) {
if ((ast.isTerminalRule(rule) && rule.hidden)) {
rules.add(rule);
}
}
return rules;
}

function ruleDfs(rule: ast.AbstractRule, visitedSet: Set<string>, allTerminals: boolean): void {
visitedSet.add(rule.name);
function ruleDfs(rule: ast.AbstractRule, visitedRuleNames: Set<string>, visitedRules: Set<ast.AbstractRule> , allTerminals: boolean): void {
visitedRuleNames.add(rule.name);
visitedRules.add(rule)
streamAllContents(rule).forEach(node => {
if (ast.isRuleCall(node) || (allTerminals && ast.isTerminalRuleCall(node))) {
const refRule = node.rule.ref;
if (refRule && !visitedSet.has(refRule.name)) {
ruleDfs(refRule, visitedSet, allTerminals);
if (refRule && !visitedRuleNames.has(refRule.name)) {
ruleDfs(refRule, visitedRuleNames, visitedRules, allTerminals);
}
} else if (ast.isCrossReference(node)) {
const term = getCrossReferenceTerminal(node)
if (term !== undefined) {
if (ast.isRuleCall(term) || (allTerminals && ast.isTerminalRuleCall(term))) {
const refRule = term.rule.ref;
if (refRule && !visitedRuleNames.has(refRule.name)) {
ruleDfs(refRule, visitedRuleNames, visitedRules, allTerminals);
}
}
}
}
});
Expand Down
46 changes: 45 additions & 1 deletion packages/langium/test/utils/grammar-util.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/******************************************************************************
* Copyright 2022 TypeFox GmbH
* Copyright 2022-2023 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.
******************************************************************************/
Expand All @@ -8,6 +8,7 @@ import type { Grammar } from '../../src';
import { describe, expect, test } from 'vitest';
import { createLangiumGrammarServices, EmptyFileSystem, getAllReachableRules } from '../../src';
import { parseHelper } from '../../src/test';
import { Utils } from 'vscode-uri';

const services = createLangiumGrammarServices(EmptyFileSystem);
const parse = parseHelper<Grammar>(services.grammar);
Expand Down Expand Up @@ -36,4 +37,47 @@ describe('Grammar Utils', () => {
expect(reachableRules).toContain('Ws');
});

test('ID implicit called should be returned by getAllReachableRules', async () => {
// [A] is short for [A:ID] thus the ID rule is needed by the parser and getAllReachableRules should return ID
const grammar1 = await parse(`
grammar G1
entry A:
'A' name=ID;
terminal ID: /[_a-zA-Z][\\w_]*/;
`);
const grammar2 = await parse(`
grammar G2
import './${Utils.basename(grammar1.uri)}'
entry B: ref=[A];
`);
await services.shared.workspace.DocumentBuilder.build([grammar2, grammar1]);
// act
const reachableRules = [...getAllReachableRules(grammar2.parseResult.value, true)].map(r => r.name);
// assert
expect(reachableRules).toContain('ID');
});

test('ID not implicit called should not be returned by getAllReachableRules', async () => {
// no implicit ID rule call in cross ref
// [A] is short for [A:ID] thus the ID rule is needed by the parser and getAllReachableRules should return ID
const grammar1 = await parse(`
grammar G1
entry A:
'A' name=ID;
Other: name=STRING;
terminal ID: /[_a-zA-Z][\\w_]*/;
`);
const grammar2 = await parse(`
grammar G2
import './${Utils.basename(grammar1.uri)}'
entry B: ref=[A];
`);
await services.shared.workspace.DocumentBuilder.build([grammar2, grammar1]);
// act
const reachableRules = [...getAllReachableRules(grammar2.parseResult.value, true)].map(r => r.name);

// assert
expect(reachableRules).not.toContain('STRING');
});

});

0 comments on commit 55f68cc

Please sign in to comment.