You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If return is a keyword of your language, it's customary to lex it as a separate token rather than consider it as an identifier, in particular because it may be ambiguous in some cases (that's why, in most language, you can't use keywords as identifier).
Otherwise, if it can never be ambiguous and you want to keep return as a valid identifier, one possibility is to match on the ident from within the action code. That is, if you want to mirror something like (beware, the following code is NOT possible in current LALRPOP, that's an ideal):
<id: "ident"> <exp: Expression> when id == "return" => ir::Expression::Return(Box::new(exp))
You can approximate it with:
<id: "ident"> <exp: Expression> => {
if id == "return" {
ir::Expression::Return(Box::new(exp))
}
else {
panic!("expected return, got {id}")
}
Of course, in a real use case, you might not want to panic but to rather return an error. This is possible in LALRPOP via fallible actions and custom parse errors. Or even simpler if you have an error node in your AST.
Let's say I use logos for lexer:
And now I want to match for a return expression:
There is no way to do so. And I have to use ident token, since I want to be able to declare a variable named "return".
The text was updated successfully, but these errors were encountered: