Fix numerous small bugs in parse_regex.py

This commit is contained in:
Juhani Krekelä 2019-06-02 21:36:12 +03:00
parent 7ade1b5977
commit 1409400cc8
1 changed files with 14 additions and 6 deletions

View File

@ -20,10 +20,17 @@ class ParseError(Exception):
name = 'ParseError'
else:
name = __name__ + '.ParseError'
return '%s(%s, %i)' % (name, self.text, self.position)
if self.position is not None:
return '%s(%s, %i)' % (name, self.text, self.position)
else:
return '%s(%s, None)' % (name, self.text)
def __str__(self):
return '%i: %s' % (self.position, self.text)
if self.position is not None:
return '%i: %s' % (self.position, self.text)
else:
return self.text
def tokenize(text):
tokens = [Token(token_types.start, 0, None)]
@ -193,7 +200,7 @@ def parse(text):
# Figure out what's gone wrong
parens_stack = []
for i in working_stack:
if startp(i) or endp(i) or regexp(i):
if startp(i) or endp(i) or regexp(i) or barp(i):
continue
elif lparenp(i):
@ -209,16 +216,17 @@ def parse(text):
raise ParseError('Missing the thing to repeat before a *', i.position)
elif type(i) == Token:
raise ParseError('(Should be impossible!) Unprocessed token of type %s' % i.type.name)
raise ParseError('(Should be impossible!) Unprocessed token of type %s' % i.type.name, i.position)
else:
raise ParseError('(Should be impossible!) Unprocessed junk in parsing stack: %s' % (i,))
raise ParseError('(Should be impossible!) Unprocessed junk in parsing stack: %s' % (i,), i.position)
if len(parens_stack) > 0:
raise ParseError('Unpaired left parenthesis', parens_stack[0].position)
# Wasn't anything we could figure out
raise ParseError('(Should be impossible!) Error figuring out the type of parse error')
print(working_stack)#debg
raise ParseError('(Should be impossible!) Error figuring out the type of parse error', None)
def main():
try: