How to correctly handle imported rules in overriden files #1429
Unanswered
Yokozuna59
asked this question in
Q&A
Replies: 1 comment 3 replies
-
Hey @Yokozuna59, I would recommend a modified approach of no. 2. You could annotate a terminal rule with something like:
And ask the |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi there, thanks for your work!
We have been discussing for a while in both mermaid-js/mermaid#4799 and mermaid-js/mermaid#4910 how we should handle imported rules for languages that have overriden rules.
So to summarize the discussions, we have a language that imports some common rules from a file, and this language has a greedy overriden rule:
The rule in the grammar:
https://github.com/Yokozuna59/mermaid/blob/bf95a305bc7c8257a13b174368f9952f4513876f/packages/parser/src/language/sankey/sankey.langium#L23
The rule overriden regex:
https://github.com/Yokozuna59/mermaid/blob/bf95a305bc7c8257a13b174368f9952f4513876f/packages/parser/src/language/sankey/matcher.ts#L9
The issue we're facing is that imported rules are sorted at the end of the rules list, and this greedy rule will tokenize everything without reaching the imported rules:
How can we handle it effectively?
We have three solutions in mind:
Unimport the common rules and place them in the grammar itself:
https://github.com/mermaid-js/mermaid/blob/c364715fc782a06b013c6651dbc632da9059e812/packages/parser/src/language/sankey/sankey.langium#L3-L23
But this approach requires us to modify more than one place.
Rearrange the rules array using a rule as a pivot, which in this case it's
TitleAndAccessibilities
, see: feat: addpie
langium parser mermaid-js/mermaid#4751 (comment).But this approach requires us to modify the pivot rule once we add another rule before the pivot one:
https://github.com/Yokozuna59/mermaid/blob/bf95a305bc7c8257a13b174368f9952f4513876f/packages/parser/src/language/common/common.langium#L7
Create a custom function that lookahead and make sure that all rules inside the common grammar aren't matched:
https://github.com/Yokozuna59/mermaid/blob/add-sankey-langium-parser/packages/parser/src/language/sankey/matcher.ts#L42-L53
But this approach requires us to update this rule once a new rule added to the common grammar.
What would you guys recommend? Is there another, better approach?
Beta Was this translation helpful? Give feedback.
All reactions