dabeaz / sly

Sly Lex Yacc
Other
816 stars 107 forks source link

How to terminate parsing on an error detection #68

Closed Renegade85 closed 3 years ago

Renegade85 commented 3 years ago

Hello,

this is more a question then a bug report. How to properly terminate parsing, if the parser stumbles on an error? I just want to report that one error, but don't want to continue on parsing nor execute the AST. Just throw away everything and terminate gracefully reporting the line where the error has been detected.

Is there a way to clear the parsing stack and iterate over the rest of the tokens in the input or something similar?

My error function follows:

error(self, t):
    while True:
        tok = next(self.tokens, None)
        if not tok:
            break
    self.restart()

    return tok

However, this seems to be not working. Just one more point, which may be important, the error is "artificial" meaning, I got parts of my grammar which are unsupported and if the parser parses such code, the "error()" method is invoked.

@_(some_syntax)
def some_rule(self, t):
    self.error(t)

The error I'm getting from python:

File "C:\Users\nxf25094\Documents\sProjects\git_repos\spsdk\venv38\lib\site-packages\sly\yacc.py", line 2091, in parse self.state = goto[statestack[-1]][pname] IndexError: list index out of range

dabeaz commented 3 years ago

The easiest way to terminate everything would be to raise an exception. For example:

raise SystemExit("Goodbye")

(Of course, you could make your own exception too).

Renegade85 commented 3 years ago

Seems the restart of the parser was not necessary. Just pumping out all the tokens and returning the last one does the trick.

def error(self, t):
        while True:
            tok = next(self.tokens, None)
            if not tok:
                break

        return tok

In case this will cause issues in the future, I'll go the exception way. :-)

Thank you very much for your help.