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

Added index of error to LexError #3

Merged
merged 1 commit into from Feb 17, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 8 additions & 6 deletions sly/lex.py
Expand Up @@ -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):
'''
Expand Down Expand Up @@ -79,7 +81,7 @@ def __setitem__(self, key, value):
value.pattern = self[key]
else:
raise AttributeError(f'Name {key} redefined')

super().__setitem__(key, value)

class LexerMeta(type):
Expand Down Expand Up @@ -160,7 +162,7 @@ def _build(cls, definitions):
pattern = value.pattern
cls._token_funcs[tokname] = value

# Form the regular expression component
# Form the regular expression component
part = f'(?P<{tokname}>{pattern})'

# Make sure the individual regex compiles properly
Expand All @@ -185,7 +187,7 @@ def _build(cls, definitions):
# Verify that that ignore and literals specifiers match the input type
if not isinstance(cls.ignore, str):
raise LexerBuildError('ignore specifier must be a string')

if not all(isinstance(lit, str) for lit in cls.literals):
raise LexerBuildError('literals must be specified as strings')

Expand Down Expand Up @@ -252,4 +254,4 @@ def tokenize(self, text, lineno=1, index=0):

# 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)