Error handling: Module error

Basically, parse functions of grammar return a boolean. If the parsing was done, it’s true. If something was wrong, an exception error.Diagnostic is raised.:

#!/usr/bin/env python3
from pyrser import grammar
from pyrser import error

class Z(grammar.Grammar):
    entry = "main"
    grammar = """
        main = ['z'+]
    """

z = Z()
try:
    res = z.parse("xxx")
    if res:
        print("cool")
except error.Diagnostic as e:
    print(e)

Sometimes you don’t want exception, you could do it with the extra parameter raise_diagnostic, and so the returned node by the parse function is False. But you could get what’s wrong with the diagnostic attribute.

#!/usr/bin/env python3
from pyrser import grammar

class Z(grammar.Grammar):
    entry = "main"
    grammar = """
        main = ['z'+]
    """

z = Z(raise_diagnostic=False)
res = z.parse("xxx")
if res:
    print("cool")
else:
    print(res.diagnostic.get_content())

Here, we can’t parse xxx. We got the following message:

===============================================================================
error : Parse error in 'main'
from /tmp/tmpg7j29i at line:1 col:1 :
xxx
^
-------------------------------------------------------------------------------

Sometime you do wrong in the DSL part so you fucked up your python interpretor. Just catch the Diagnostic exception:

#!/usr/bin/env python3
from pyrser import error
try:
    from pyrser import grammar

    class Z(grammar.Grammar):
        entry = "main"
        grammar = """
            main = ['z'+] -> error here
        """

    z = Z()
    res = z.parse("zzzz")
except error.Diagnostic as e:
    print(e)

And you got:

===============================================================================
error : Parse error in 'Base.eof' in EBNF bnf
from /tmp/tmpvrf4_n at line:2 col:27 :
            main = ['z'+] -> error here
                          ^
-------------------------------------------------------------------------------
error : Parse error in 'Base.eof' in EBNF bnf

-------------------------------------------------------------------------------

Python API: error module

class pyrser.error.LocationInfo(filepath: str, line: int, col: int, size: int=1)[source]

Bases: object

__init__(self, filepath: str, line: int, col: int, size: int= 1)[source]
static from_stream(stream, is_error= False)[source]
static from_maxstream(stream, is_error= False)[source]
static from_here(pos= 1)[source]
get_content(self) → str[source]
class pyrser.error.Notification(severity: pyrser.meta.Enum, msg: str, location: pyrser.error.LocationInfo=None, details: str=None)[source]

Bases: object

Just One notification

__init__(self, severity: Enum, msg: str, location: LocationInfo= None, details: str= None)[source]
get_content(self, with_locinfos= False, with_details= False) → str[source]
exception pyrser.error.Diagnostic[source]

Bases: Exception

The diagnostic object is use to handle easily all errors/warnings/infos in a compiler that you could encounter. Error while parsing, Error while type checking etc... You could use different severity for your notification.

__init__(self)[source]
__bool__(self)[source]
__str__(self) → str[source]
notify(self, severity: Enum, msg: str, location: LocationInfo= None, details: str= None) → int[source]
add(self, n: Notification) → int[source]
get_content(self, with_locinfos= True, with_details= False) → str[source]
get_infos(self)[source]
have_errors