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 <christian.dietrich.opensource@gmail.com>
  • Loading branch information
cdietrich committed Aug 15, 2023
1 parent 13f482b commit 8068189
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 15 deletions.
18 changes: 17 additions & 1 deletion 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 @@ -69,12 +69,28 @@ export function getAllReachableRules(grammar: ast.Grammar, allTerminals: boolean

function ruleDfs(rule: ast.AbstractRule, visitedSet: Set<string>, allTerminals: boolean): void {
visitedSet.add(rule.name);
console.log("xxxx", visitedSet)

Check failure on line 72 in packages/langium/src/utils/grammar-util.ts

View workflow job for this annotation

GitHub Actions / Langium Lint

Strings must use singlequote

Check failure on line 72 in packages/langium/src/utils/grammar-util.ts

View workflow job for this annotation

GitHub Actions / Langium Lint

Missing semicolon
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);
}
} else if (ast.isCrossReference(node)) {
// TODO why does this not work
const term = getCrossReferenceTerminal(node)

Check failure on line 81 in packages/langium/src/utils/grammar-util.ts

View workflow job for this annotation

GitHub Actions / Langium Lint

Missing semicolon
console.log(term)

Check failure on line 82 in packages/langium/src/utils/grammar-util.ts

View workflow job for this annotation

GitHub Actions / Langium Lint

Missing semicolon
if (term !== undefined) {
console.log(term)

Check failure on line 84 in packages/langium/src/utils/grammar-util.ts

View workflow job for this annotation

GitHub Actions / Langium Lint

Missing semicolon
if (ast.isRuleCall(term) || (allTerminals && ast.isTerminalRuleCall(term))) {
const refRule = term.rule.ref;
console.log(refRule?.name)

Check failure on line 87 in packages/langium/src/utils/grammar-util.ts

View workflow job for this annotation

GitHub Actions / Langium Lint

Missing semicolon
if (refRule && !visitedSet.has(refRule.name)) {
console.log("recurse")

Check failure on line 89 in packages/langium/src/utils/grammar-util.ts

View workflow job for this annotation

GitHub Actions / Langium Lint

Strings must use singlequote

Check failure on line 89 in packages/langium/src/utils/grammar-util.ts

View workflow job for this annotation

GitHub Actions / Langium Lint

Missing semicolon
ruleDfs(refRule, visitedSet, allTerminals);
}
}
}
}
});
}
Expand Down
70 changes: 56 additions & 14 deletions 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,32 +8,74 @@ 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);

describe('Grammar Utils', () => {

test('Terminal fragment rule should be reachable when only used by hidden terminal rule', async () => {
// the actual bug was that the 'Ws' rule marked as unused - so a 'Error: Missing rule reference!' was thrown
// arrange
const input = `
grammar HelloWorld
// test('Terminal fragment rule should be reachable when only used by hidden terminal rule', async () => {
// // the actual bug was that the 'Ws' rule marked as unused - so a 'Error: Missing rule reference!' was thrown
// // arrange
// const input = `
// grammar HelloWorld

entry Model: Hello;
// entry Model: Hello;

Hello: greeting='Hello!';
// Hello: greeting='Hello!';

hidden terminal COMMON__WS: Ws+;
terminal fragment Ws: /[ \t\r\n\f]/;
`;
const output = await parse(input);
// hidden terminal COMMON__WS: Ws+;
// terminal fragment Ws: /[ \t\r\n\f]/;
// `;
// const output = await parse(input);

// // act
// const reachableRules = [...getAllReachableRules(output.parseResult.value, true)].map(r => r.name);

// // assert
// 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
return 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(output.parseResult.value, true)].map(r => r.name);
const reachableRules = [...getAllReachableRules(grammar2.parseResult.value, true)].map(r => r.name);

// assert
expect(reachableRules).toContain('Ws');
expect(reachableRules).toContain('ID');
});

// test('ID not implicit called should not be returned by getAllReachableRules', async () => {
// // no implicit ID rule call in cross ref
// const input = `
// grammar HelloWorld

// entry Model: A|B;
// A: name=STRING;
// B: ref=[A:STRING];
// terminal ID: /[_a-zA-Z][\w_]*/;
// terminal STRING: /"(\\.|[^"\\])*"|'(\\.|[^'\\])*'/;
// `;
// const output = await parse(input);

// // act
// const reachableRules = [...getAllReachableRules(output.parseResult.value, true)].map(r => r.name);

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

});

0 comments on commit 8068189

Please sign in to comment.