Skip to content

Commit be470f9

Browse files
committed
1 parent e409507 commit be470f9

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

peglib.h

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2486,18 +2486,23 @@ inline void ReferenceChecker::visit(Reference& ope) {
24862486
}
24872487

24882488
inline void LinkReferences::visit(Reference& ope) {
2489-
if (grammar_.count(ope.name_)) {
2489+
// Check if the reference is a macro parameter
2490+
auto found_param = false;
2491+
for (size_t i = 0; i < params_.size(); i++) {
2492+
const auto& param = params_[i];
2493+
if (param == ope.name_) {
2494+
ope.iarg_ = i;
2495+
found_param = true;
2496+
break;
2497+
}
2498+
}
2499+
2500+
// Check if the reference is a definition rule
2501+
if (!found_param && grammar_.count(ope.name_)) {
24902502
auto& rule = grammar_.at(ope.name_);
24912503
ope.rule_ = &rule;
2492-
} else {
2493-
for (size_t i = 0; i < params_.size(); i++) {
2494-
const auto& param = params_[i];
2495-
if (param == ope.name_) {
2496-
ope.iarg_ = i;
2497-
break;
2498-
}
2499-
}
25002504
}
2505+
25012506
for (auto arg: ope.args_) {
25022507
arg->accept(*this);
25032508
}

test/test.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1453,6 +1453,18 @@ TEST_CASE("Macro token check test", "[macro]")
14531453
REQUIRE(parser["T"].is_token() == true);
14541454
}
14551455

1456+
TEST_CASE("Macro rule-parameter collision", "[macro]")
1457+
{
1458+
parser parser(R"(
1459+
A <- B(C)
1460+
B(D) <- D
1461+
C <- 'c'
1462+
D <- 'd'
1463+
)");
1464+
1465+
REQUIRE(parser.parse("c"));
1466+
}
1467+
14561468
TEST_CASE("Line information test", "[line information]")
14571469
{
14581470
parser parser(R"(

0 commit comments

Comments
 (0)