Errors

Every error in this package inherits from the following class:

class parse_ebnf.EBNFError

Base class of all parse_ebnf errors. Does not define anything.

Errors only occur during parsing, all of them inherit from:

class parse_ebnf.parsing.ParsingError(message, parser: parse_ebnf.parsing.ParserState)

Base class of all parsing related errors.

Each instance of this class has a variable named parser that represents the parsing state at the time the error occurred. With that you can get both the line and column of the error along with the partial parse tree.

Parsing errors reference

class parse_ebnf.parsing.EOFError(parser: ParserState, reason: str | None = None)

An end of file was reached in an unexpected place.

class parse_ebnf.parsing.UnexpectedCharacterError(parser: ParserState, expected: str | None = None)

An unexpected character occurred during parsing.

The unexpected character can be found in parser.c.

class parse_ebnf.parsing.NoLiteralError(parser: ParserState, literals: list | str, read: str)

Could not match a literal.

The expected literal or literals can be found in the instance variable literals that may be a string or list of strings. The instance variable read contains the characters that were read whilst matching the given literal.

class parse_ebnf.parsing.UndelimitedTermError(term: parse_ebnf.nodes.Term, parser: parse_ebnf.parsing.ParserState)

The previous Term was not delimited with a comma.

The previous term can be found in the instance variable term.

class parse_ebnf.parsing.MultipleTermRepetitions(term: parse_ebnf.nodes.Term, parser: parse_ebnf.parsing.ParserState)

Multiple Repetition nodes were parsed for a single Term

The term in question can be found in the instance variable term.

class parse_ebnf.parsing.MultipleTermExceptions(term: parse_ebnf.nodes.Term, parser: parse_ebnf.parsing.ParserState)

Multiple Exception nodes were parsed for a single Term

The term in question can be found in the instance variable term.

class parse_ebnf.parsing.MultipleTermPrimariesError(term: parse_ebnf.nodes.Term, parser: parse_ebnf.parsing.ParserState)

Multiple Primary nodes were parsed for a single Term

The term in question can be found in the instance variable term.

class parse_ebnf.parsing.UnexpectedLiteralError(lit: parse_ebnf.nodes.Literal, parser: parse_ebnf.parsing.ParserState)

Did not expect to parser a literal.

The literal in question can be found in the instance variable literal.

Handling parsing errors

Parsing errors can be handled gracefully, upon which a partial parse tree will be generated. Do do so, wrap your parsing function in a try block and except ParsingError:

try
     #Your parsing function here
except parse_ebnf.parsing.ParsingError as e:
     pt = e.parser.pt
     #Make sure to mark the fact that the tree is partial! You cannot easily
     #tell if a tree is partial just by looking at it.
     partial = True