-
For example, langium-lox has binary expressions. In bytescript (bytescript/bytescript@bddbe1b) we're doing something similar: // EXPRESSION
Expression infers Expression: // why "infers Expression" here?
AssignmentExpression;
AssignmentExpression infers Expression:
SumExpression ({infer BinaryExpression.leftOperand=current} operator="=" rightOperand=SumExpression)*;
SumExpression infers Expression:
ProductExpression ({infer BinaryExpression.leftOperand=current} operator=("+" | "-") rightOperand=ProductExpression)*;
ProductExpression infers Expression:
PrimaryExpression ({infer BinaryExpression.leftOperand=current} operator=("*" | "/") rightOperand=PrimaryExpression)*;
PrimaryExpression infers Expression:
GroupLiteral | NumberLiteral | InvalidParenthesis | PossibleCallExpression; But as you can see, the function expressions are not included in the FunctionExpression:
OriginalFunctionExpression | GeneratorFunctionExpression | ArrowFunctionExpression;
OriginalFunctionExpression:
"function" name=IDENTIFIER? Parameters (":" ReturnType)? body=Block;
GeneratorFunctionExpression:
"function" "*" name=IDENTIFIER? Parameters (":" ReturnType)? body=Block;
ArrowFunctionExpression:
Parameters (":" ReturnType)? "=>" body=(ArrowReturnExpression | Block); and Langium says If I add Statement:
( VariableDeclaration
| FunctionDeclaration
| ReturnStatement
| Block
| EmptyStatement
| Expression
) ';'?; So, if I've added it to PrimaryExpression infers Expression:
GroupLiteral | NumberLiteral | InvalidParenthesis | PossibleCallExpression
| FunctionExpression; then this starts to happen: ![]() How can we add function expressions that are not allowed as "statements", while allowing function declarations as statements? This is on the |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
If you allow functions at a statement level, then I would recommend to remove them at the top level and add appropriate validations that correctly handle functions at each level. Everything else will probably lead to a lot of headache, at least in my experience. It's usually for the better to be more flexible during the parsing phase and then prevent invalid programs using the validation feature of Langium. This also allows to create more fine-grained and understandable error messages. Parser errors can be quite cryptic, as you've already pointed out. |
Beta Was this translation helpful? Give feedback.
If you allow functions at a statement level, then I would recommend to remove them at the top level and add appropriate validations that correctly handle functions at each level. Everything else will probably lead to a lot of headache, at least in my experience.
It's usually for the better to be more flexible during the parsing phase and then prevent invalid programs using the validation feature of Langium. This also allows to create more fine-grained and understandable error messages. Parser errors can be quite cryptic, as you've already pointed out.