Skip to content

Support infix operator rules #1836

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 2 additions & 12 deletions examples/arithmetics/src/language-server/arithmetics.langium
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,9 @@ Evaluation:
expression=Expression ';';

Expression:
Addition;
BinaryExpression;

Addition infers Expression:
Multiplication ({infer BinaryExpression.left=current} operator=('+' | '-') right=Multiplication)*;

Multiplication infers Expression:
Exponentiation ({infer BinaryExpression.left=current} operator=('*' | '/') right=Exponentiation)*;

Exponentiation infers Expression:
Modulo ({infer BinaryExpression.left=current} operator='^' right=Modulo)*;

Modulo infers Expression:
PrimaryExpression ({infer BinaryExpression.left=current} operator='%' right=PrimaryExpression)*;
infix BinaryExpression on PrimaryExpression: '%' > '^' > '*' | '/' > '+' | '-';
Copy link
Contributor

Choose a reason for hiding this comment

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

Normally the precedence of > is greater than the one of |. It reads a bit unnatural.
Adding parentheses could be an option to enumerate all alternatives.
Or maybe a equal sign instead of the |.
WDYT?

Copy link
Contributor

Choose a reason for hiding this comment

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

And why not using the comma operator instead of '>' ?

'%' , '^' , '*' | '/' , '+' | '-';

Copy link
Contributor

Choose a reason for hiding this comment

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

‚>‘ implies an order between the operators, that makes totally sense to me. Having a comma, you do not know what comes first: low or high precedence. But you can assume some convention, true.

Copy link
Contributor

Choose a reason for hiding this comment

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

You can mitigate this by writing each group in its own line:

infix BinaryExpression on PrimaryExpression:
    '%'
    > '^'
    > '*' | '/'
    > '+' | '-';

This is how I'd format such expressions for readability.


PrimaryExpression infers Expression:
'(' Expression ')' |
Expand Down
285 changes: 40 additions & 245 deletions examples/arithmetics/src/language-server/generated/grammar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const ArithmeticsGrammar = (): Grammar => loadedArithmeticsGrammar ?? (lo
"terminal": {
"$type": "RuleCall",
"rule": {
"$ref": "#/rules@12"
"$ref": "#/rules@9"
},
"arguments": []
}
Expand Down Expand Up @@ -102,7 +102,7 @@ export const ArithmeticsGrammar = (): Grammar => loadedArithmeticsGrammar ?? (lo
"terminal": {
"$type": "RuleCall",
"rule": {
"$ref": "#/rules@12"
"$ref": "#/rules@9"
},
"arguments": []
}
Expand Down Expand Up @@ -194,7 +194,7 @@ export const ArithmeticsGrammar = (): Grammar => loadedArithmeticsGrammar ?? (lo
"terminal": {
"$type": "RuleCall",
"rule": {
"$ref": "#/rules@12"
"$ref": "#/rules@9"
},
"arguments": []
}
Expand Down Expand Up @@ -255,270 +255,65 @@ export const ArithmeticsGrammar = (): Grammar => loadedArithmeticsGrammar ?? (lo
"wildcard": false
},
{
"$type": "ParserRule",
"name": "Addition",
"inferredType": {
"$type": "InferredType",
"name": "Expression"
"$type": "InfixRule",
"name": "BinaryExpression",
"call": {
"$type": "RuleCall",
"rule": {
"$ref": "#/rules@7"
},
"arguments": []
},
"definition": {
"$type": "Group",
"elements": [
{
"$type": "RuleCall",
"rule": {
"$ref": "#/rules@7"
},
"arguments": []
},
"operators": {
"$type": "InfixRuleOperators",
"precedences": [
{
"$type": "Group",
"elements": [
"$type": "InfixRuleOperatorList",
"operators": [
{
"$type": "Action",
"inferredType": {
"$type": "InferredType",
"name": "BinaryExpression"
},
"feature": "left",
"operator": "="
},
{
"$type": "Assignment",
"feature": "operator",
"operator": "=",
"terminal": {
"$type": "Alternatives",
"elements": [
{
"$type": "Keyword",
"value": "+"
},
{
"$type": "Keyword",
"value": "-"
}
]
}
},
{
"$type": "Assignment",
"feature": "right",
"operator": "=",
"terminal": {
"$type": "RuleCall",
"rule": {
"$ref": "#/rules@7"
},
"arguments": []
}
"$type": "Keyword",
"value": "%"
}
],
"cardinality": "*"
}
]
},
"definesHiddenTokens": false,
"entry": false,
"fragment": false,
"hiddenTokens": [],
"parameters": [],
"wildcard": false
},
{
"$type": "ParserRule",
"name": "Multiplication",
"inferredType": {
"$type": "InferredType",
"name": "Expression"
},
"definition": {
"$type": "Group",
"elements": [
{
"$type": "RuleCall",
"rule": {
"$ref": "#/rules@8"
},
"arguments": []
]
},
{
"$type": "Group",
"elements": [
{
"$type": "Action",
"inferredType": {
"$type": "InferredType",
"name": "BinaryExpression"
},
"feature": "left",
"operator": "="
},
"$type": "InfixRuleOperatorList",
"operators": [
{
"$type": "Assignment",
"feature": "operator",
"operator": "=",
"terminal": {
"$type": "Alternatives",
"elements": [
{
"$type": "Keyword",
"value": "*"
},
{
"$type": "Keyword",
"value": "/"
}
]
}
},
{
"$type": "Assignment",
"feature": "right",
"operator": "=",
"terminal": {
"$type": "RuleCall",
"rule": {
"$ref": "#/rules@8"
},
"arguments": []
}
"$type": "Keyword",
"value": "^"
}
],
"cardinality": "*"
}
]
},
"definesHiddenTokens": false,
"entry": false,
"fragment": false,
"hiddenTokens": [],
"parameters": [],
"wildcard": false
},
{
"$type": "ParserRule",
"name": "Exponentiation",
"inferredType": {
"$type": "InferredType",
"name": "Expression"
},
"definition": {
"$type": "Group",
"elements": [
{
"$type": "RuleCall",
"rule": {
"$ref": "#/rules@9"
},
"arguments": []
]
},
{
"$type": "Group",
"elements": [
{
"$type": "Action",
"inferredType": {
"$type": "InferredType",
"name": "BinaryExpression"
},
"feature": "left",
"operator": "="
},
"$type": "InfixRuleOperatorList",
"operators": [
{
"$type": "Assignment",
"feature": "operator",
"operator": "=",
"terminal": {
"$type": "Keyword",
"value": "^"
}
"$type": "Keyword",
"value": "*"
},
{
"$type": "Assignment",
"feature": "right",
"operator": "=",
"terminal": {
"$type": "RuleCall",
"rule": {
"$ref": "#/rules@9"
},
"arguments": []
}
"$type": "Keyword",
"value": "/"
}
],
"cardinality": "*"
}
]
},
"definesHiddenTokens": false,
"entry": false,
"fragment": false,
"hiddenTokens": [],
"parameters": [],
"wildcard": false
},
{
"$type": "ParserRule",
"name": "Modulo",
"inferredType": {
"$type": "InferredType",
"name": "Expression"
},
"definition": {
"$type": "Group",
"elements": [
{
"$type": "RuleCall",
"rule": {
"$ref": "#/rules@10"
},
"arguments": []
]
},
{
"$type": "Group",
"elements": [
{
"$type": "Action",
"inferredType": {
"$type": "InferredType",
"name": "BinaryExpression"
},
"feature": "left",
"operator": "="
},
"$type": "InfixRuleOperatorList",
"operators": [
{
"$type": "Assignment",
"feature": "operator",
"operator": "=",
"terminal": {
"$type": "Keyword",
"value": "%"
}
"$type": "Keyword",
"value": "+"
},
{
"$type": "Assignment",
"feature": "right",
"operator": "=",
"terminal": {
"$type": "RuleCall",
"rule": {
"$ref": "#/rules@10"
},
"arguments": []
}
"$type": "Keyword",
"value": "-"
}
],
"cardinality": "*"
]
}
]
},
"definesHiddenTokens": false,
"entry": false,
"fragment": false,
"hiddenTokens": [],
"parameters": [],
"wildcard": false
"parameters": []
},
{
"$type": "ParserRule",
Expand Down Expand Up @@ -567,7 +362,7 @@ export const ArithmeticsGrammar = (): Grammar => loadedArithmeticsGrammar ?? (lo
"terminal": {
"$type": "RuleCall",
"rule": {
"$ref": "#/rules@13"
"$ref": "#/rules@10"
},
"arguments": []
}
Expand Down
2 changes: 1 addition & 1 deletion packages/langium-cli/src/generator/langium-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export function collectKeywords(grammar: Grammar): string[] {
const reachableRules = GrammarUtils.getAllReachableRules(grammar, false);

for (const keyword of stream(reachableRules)
.filter(GrammarAST.isParserRule)
.filter(rule => GrammarAST.isParserRule(rule) || GrammarAST.isInfixRule(rule))
.flatMap(rule => AstUtils.streamAllContents(rule).filter(GrammarAST.isKeyword))) {
keywords.add(keyword.value);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/langium-vscode/data/langium.tmLanguage.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
},
{
"name": "keyword.control.langium",
"match": "\\b(current|entry|extends|fragment|grammar|hidden|import|infer|infers|interface|returns|terminal|type|with)\\b"
"match": "\\b(current|entry|extends|fragment|grammar|hidden|import|infer|infers|infix|interface|returns|terminal|type|with|on)\\b"
},
{
"name": "constant.language.langium",
Expand Down
Loading