yhirose / cpp-peglib

A single file C++ header-only PEG (Parsing Expression Grammars) library
MIT License
900 stars 112 forks source link

Macro parameters are hidden by rule identifiers #71

Closed ktul closed 4 years ago

ktul commented 4 years ago

Currently, whenever a macro defines a parameter whose name is already used as the identifier of a rule, the macro will use the rule instead of the parameter. This can lead to weird behaviors when extending the grammar.

Below is an example. Grammar 2 extends Grammar 1 by the last definition. Now, while Grammar 1 will only accept the code "c", Grammar 2 will only accept "d".

I would have excepted for Grammar 2 to also accept only "c" as I assumed parameter and rule names were scoped and hence D was the parameter and not the rule. Using the playground I found out that this is not the case.

Regarding the unexpected behavior when extending a grammar, is this the intended implementation?

Grammar 1:

A    <- B(C)
B(D) <- D
C    <- 'c'

Grammar 2:

A    <- B(C)
B(D) <- D
C    <- 'c'
D    <- 'd'
yhirose commented 4 years ago

@ktul, thanks for the bug report. Yes, the result for Grammar 2 should be the same as Grammar 1. I 'll look into it when I am not busy. Thank a lot!

yhirose commented 4 years ago

@ktul, I fixed it. Thanks for your contribution!