Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add implicit ID rule call for cross refs to getAllReachableRules #1152

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

cdietrich
Copy link
Contributor

add implicit ID rule call for cross refs to getAllReachableRules
Fixes #1151

@cdietrich cdietrich marked this pull request as draft August 15, 2023 15:25
Copy link
Member

@msujew msujew left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this is correct. Langium never uses magic names for convention reasons (only exception is the name property). Instead Langium uses the terminal associated with the name property for cross references. See also this method.


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(`
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this proper setup to have 2 grammars reference each other?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that works fine 👍

@cdietrich cdietrich force-pushed the cd_issue1151 branch 3 times, most recently from d837ea9 to 55f68cc Compare August 15, 2023 16:19
for (const rule of grammar.rules) {
if (ruleNames.has(rule.name) || (ast.isTerminalRule(rule) && rule.hidden)) {
if ((ast.isTerminalRule(rule) && rule.hidden)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do i miss something now?

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 {
Copy link
Contributor Author

@cdietrich cdietrich Aug 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a more elegant way? do we need to make breadth first search / two loops
to catch overriden rules?

or just collect all rule names in a first run and the actual rules in a second?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Langium prevents adopters from overriding imported rules.

ruleDfs(refRule, visitedRuleNames, visitedRules, allTerminals);
}
} else if (ast.isCrossReference(node)) {
const term = getCrossReferenceTerminal(node)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is the override semantic here: rule used at the name place is overriden/also defined in current grmmar

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Langium prevents adopters from overriding imported rules.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, need to try that out

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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that we don't allow rule overrides, I don't think we need this set?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

packages/langium/test/utils/grammar-util.test.ts Outdated Show resolved Hide resolved
packages/langium/test/utils/grammar-util.test.ts Outdated Show resolved Hide resolved
@cdietrich
Copy link
Contributor Author

cdietrich commented Aug 16, 2023

is the build fail related to my changes?
do we need to reserve sorting?

@msujew
Copy link
Member

msujew commented Aug 16, 2023

@cdietrich Yes, the errors are related to your changes. The order of terminals is very important for the Chevrotain lexer, which is why it needs to be preserved.

@cdietrich
Copy link
Contributor Author

what do i need to execute to replicate the github action build locally?
(so that i can try locally without pushing)
where is the rules from imported rules sorted in? at the end?

@msujew
Copy link
Member

msujew commented Aug 16, 2023

You just need to run npm test to run the test that the Actions suite is running.

where is the rules from imported rules sorted in? at the end?

Yes, local rules have priority over imported rules. They should be at the end.

@cdietrich cdietrich force-pushed the cd_issue1151 branch 2 times, most recently from bbd5d81 to 71ced3b Compare August 16, 2023 14:11
// act
const reachableRules = [...getAllReachableRules(grammar2.parseResult.value, true)].map(r => r.name);
// assert
expect(reachableRules).toEqual(['B', 'STRING', 'ID', 'Called', 'INT' ])
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this sufficient as testcase or do we need deeply nested calls too?

Fixes eclipse-langium#1151

Signed-off-by: Christian Dietrich <christian.dietrich.opensource@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

getAllReachableRules should return implicit ID rule
2 participants