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 eclipse-langium#1151

Signed-off-by: Christian Dietrich <christian.dietrich.opensource@gmail.com>
  • Loading branch information
cdietrich committed Aug 15, 2023
1 parent 13f482b commit 02b3bb9
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
6 changes: 5 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 @@ -75,6 +75,10 @@ function ruleDfs(rule: ast.AbstractRule, visitedSet: Set<string>, allTerminals:
if (refRule && !visitedSet.has(refRule.name)) {
ruleDfs(refRule, visitedSet, allTerminals);
}
} else if (ast.isCrossReference(node)) {
if (node.terminal === undefined) {
visitedSet.add("ID")
}
}
});
}
Expand Down
42 changes: 41 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 Down Expand Up @@ -36,4 +36,44 @@ 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 input = `
grammar HelloWorld
entry Model: A|B;
A: name=STRING;
B: ref=[A];
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).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 02b3bb9

Please sign in to comment.