fix(parser): resolve expression ambiguity in switch/match cases

Signed-off-by: erick-alcachofa <erick@artichoke.dev>

Implement precedence capping in `parseExpression` for switch cases to
prevent the parser from misinterpreting the case arrow (`->`) as a
pointer member access operator.

Additionally, increased the binding power of `ModuleAccess` (::) to
ensure namespaced identifiers are correctly resolved within case
patterns before hitting the precedence limit.

- Use `PointerMemberAccess.right` as the precedence floor for cases.
- Update `ModuleAccess` binding power to {23, 24}.
This commit is contained in:
erick-alcachofa 2025-12-28 00:44:02 -06:00
parent 3180ca4662
commit f024334da5
Signed by: me
GPG Key ID: 6FA5F8643444BAFA
2 changed files with 6 additions and 2 deletions

View File

@ -40,7 +40,7 @@ namespace arti::lang::pratt {
BindingPower infixBindingPower(ast::InfixOperator op) {
switch (op) {
// Member Access (Highest)
case ast::InfixOperator::ModuleAccess:
case ast::InfixOperator::ModuleAccess: return { 23, 24 };
case ast::InfixOperator::MemberAccess:
case ast::InfixOperator::PointerMemberAccess: return { 21, 22 };

View File

@ -21,6 +21,7 @@
//============================================================================//
#include <artichoke/Parser/Parser.hpp>
#include <artichoke/Parser/Pratt.hpp>
#include <artichoke/Util/OverloadSet.hpp>
@ -884,6 +885,9 @@ namespace arti::lang {
return Unexpected<>{ std::move(lSquirly).error() };
}
uint16_t limit =
pratt::infixBindingPower(ast::InfixOperator::PointerMemberAccess).right;
bool keepParsing = true;
while (keepParsing) {
@ -912,7 +916,7 @@ namespace arti::lang {
else {
auto curCase = ast::MakeNode<ast::SwitchCaseNode>();
if (auto expr = parseExpression(); ! expr) {
if (auto expr = parseExpression(limit); ! expr) {
return Unexpected<>{ std::move(expr).error() };
}
else {