Skip to content

Commit

Permalink
Cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
eerimoq committed Jul 22, 2018
1 parent 198b2cb commit 41f7aeb
Showing 1 changed file with 22 additions and 19 deletions.
41 changes: 22 additions & 19 deletions textparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ def __repr__(self):


class _String(object):
"""Matches a specific token kind.
"""

def __init__(self, kind):
self.kind = kind
Expand All @@ -59,25 +62,10 @@ def _wrap_strings(items):
return [_wrap_string(item) for item in items]


Token = namedtuple('Token', ['kind', 'value', 'line', 'column'])


class Error(Exception):
pass


def markup_line(string, offset):
begin = string.rfind('\n', 0, offset)
begin += 1

end = string.find('\n', offset)

if end == -1:
end = len(string)

return string[begin:offset] + '>>!<<' + string[offset:end]


class TokenizerError(Error):

def __init__(self, line, column, offset, string):
Expand All @@ -92,10 +80,7 @@ def __init__(self, line, column, offset, string):
self.string = string


def create_token_re(spec):
return '|'.join([
'(?P<{}>{})'.format(name, regex) for name, regex in spec
])
Token = namedtuple('Token', ['kind', 'value', 'line', 'column'])


class Sequence(object):
Expand Down Expand Up @@ -336,3 +321,21 @@ def choice(*members):
return ChoiceDict(*members)
except Error:
return Choice(*members)


def markup_line(string, offset):
begin = string.rfind('\n', 0, offset)
begin += 1

end = string.find('\n', offset)

if end == -1:
end = len(string)

return string[begin:offset] + '>>!<<' + string[offset:end]


def create_token_re(spec):
return '|'.join([
'(?P<{}>{})'.format(name, regex) for name, regex in spec
])

0 comments on commit 41f7aeb

Please sign in to comment.