Merge pull request #3 from pydanny/master

Added index of error to LexError
This commit is contained in:
David Beazley 2017-02-17 11:51:42 -06:00 committed by GitHub
commit c0694aa922

View File

@ -41,11 +41,13 @@ class LexError(Exception):
'''
Exception raised if an invalid character is encountered and no default
error handler function is defined. The .text attribute of the exception
contains all remaining untokenized text.
contains all remaining untokenized text. The .error_index is the index
location of the error.
'''
def __init__(self, message, text):
def __init__(self, message, text, error_index):
self.args = (message,)
self.text = text
self.error_index = error_index
class PatternError(Exception):
'''
@ -252,4 +254,4 @@ class Lexer(metaclass=LexerMeta):
# Default implementations of the error handler. May be changed in subclasses
def error(self, value):
raise LexError(f'Illegal character {value[0]!r} at index {self.index}', value)
raise LexError(f'Illegal character {value[0]!r} at index {self.index}', value, self.index)