Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug with zero or more rule #81

Open
heckad opened this issue Feb 18, 2023 · 5 comments · May be fixed by #82
Open

Bug with zero or more rule #81

heckad opened this issue Feb 18, 2023 · 5 comments · May be fixed by #82

Comments

@heckad
Copy link

heckad commented Feb 18, 2023

Gram file

start: stmts

stmts: stmt*

stmt: block

block: "{" stmts "}"

Input

{ {} }

Result

  File "<unknown>", line 1
    { {} }
       ^
SyntaxError: input.txt

The problem in the generated parse rule for the block stmt
Code

    @memoize
    def block(self) -> Optional[Any]:
        # block: "{" stmts "}"
        mark = self._mark()
        if (
            (literal := self.expect("{"))
            and
            (stmts := self.stmts())
            and
            (literal_1 := self.expect("}"))
        ):
            return [literal, stmts, literal_1];
        self._reset(mark)
        return None;

(stmts := self.stmts()) shoud be (stmts := self.stmts()) is not None because an empty array is interpreted as false.

@0dminnimda
Copy link
Contributor

0dminnimda commented Feb 18, 2023

stmt* have a value of None if no stmt was found.
I haven't checked i, but it think that stmts returns None and block doesn't handle it

An easy, but possible problematic way to solve this is recursively checking expressions until we find single Repeat
A possibly more compilated way to solve this is to properly handle this is stmts

I will look into that

@0dminnimda
Copy link
Contributor

Unfortunately here we need a more general indicator of a failure
The problem with using None is that if user wants to return it:

start: stmts

stmts: r=stmt* { None if not r else r }

stmt: block

block: "{" stmts "}"

@0dminnimda 0dminnimda linked a pull request Feb 19, 2023 that will close this issue
@heckad
Copy link
Author

heckad commented Feb 19, 2023

I haven't checked i, but it think that stmts returns None and block doesn't handle it

stmts returns an empty array not None. And that is interpreted as False. None is a good marker.

@0dminnimda
Copy link
Contributor

stmts returns an empty array not None

Yes, initially i was wrong about it

@heckad
Copy link
Author

heckad commented Feb 25, 2023

My attempt to solve the problem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants