Open philip-h-dye opened 1 year ago
I suspect it is the same problem of testing the boolean value of the result and not actually checking if the node was matched.
If you look at the generated code you probably will see something like
if self.num()
which will fail if the result is 0
as bool(0) == False
It was not taken care of before, because all ast.X
are classes and always evaluate to True
The other issue caused by this behavior is #81
In the meantime you can use values that don't evaluate to False
, probably the best choice is to wrap the number in your class, i.e.
@dataclass
class NumberNode:
value: int | float
And then create it like NumberNode(...)
and use it like number.value
Also, personally I want to ask you to check if #82 fixes this issue and report about it if possible
@pablogsal if you have time, it would be nice of you to take a look at the PRs
For the moment, I edited the loop accepting num: was:
@memoize
def _loop1_1(self) -> Optional[Any]:
# _loop1_1: num
mark = self._mark()
children = []
while (
(num := self.num()) # <== 0 and None both fail when 0 should succeed
):
children.append(num)
mark = self._mark()
self._reset(mark)
return children;
Fix _loop1_1 by adding is not None :
@memoize
def _loop1_1(self) -> Optional[Any]:
# _loop1_1: num
mark = self._mark()
children = []
while (
(num := self.num()) is not None # Now only None will fail and 0 will succeed
):
children.append(num)
mark = self._mark()
self._reset(mark)
return children;
Making a global "fix" in src/pegen/python_generator.py sadly results in 94 newly failing tests.
Essentially, my action coercing the strings to integers or floats is the fundamental cause if PEGEN requires that actions always result in a value that is semantically True. My apologies if this is documented somewhere. If not, I would suggest adding it.
Thanks
With this extremely simple grammar "1" succeeds but "0" fails. Can anyone shed light on this issue? Thanks
zero-fails.py
Output