Move ParseLogicError into comfy.parse.

Don't catch ParseError in comfy.choices.
This commit is contained in:
Barry Downes 2023-08-05 16:18:52 +10:00
parent 5a673ee930
commit dcb4bdfb9b
2 changed files with 11 additions and 16 deletions

View File

@ -3,14 +3,8 @@
import os import os
import random import random
import comfy.parse import comfy.parse as parse
from comfy.parse import ParseError from comfy.parse import ParseError, ParseLogicError
class ParseLogicError(ParseError):
# something that shouldn't be possible occurred in the code
# not the user's fault
pass
def get_random_seed(): def get_random_seed():
return int.from_bytes(os.urandom(8)) return int.from_bytes(os.urandom(8))
@ -133,12 +127,7 @@ def translate(text, seed=None, strict=True, reescape=frozenset()):
# init our local random number generator # init our local random number generator
rng = random.Random(seed) rng = random.Random(seed)
try: input = parse.Cursor(text)
input = comfy.parse.Cursor(text) out = parse_text_with_choices_outer(input)
out = parse_text_with_choices_outer(input) return out
return out
except (ParseError) as e:
# alternative: re-throw the error
stdout.write(f'Error parsing prompt: {e}');
return text

View File

@ -10,6 +10,12 @@ class ParseError(Exception):
def __str__(self): def __str__(self):
return f'{self.message} {self.input.loc()}' return f'{self.message} {self.input.loc()}'
class ParseLogicError(ParseError):
# like a ParseError, in that it has an associated cursor position which will help in understanding the error
# but unlike a ParseError, because it wasn't the user's fault
# something that shouldn't be possible occurred in the code
pass
class Cursor: class Cursor:
def __init__(self, text, skip_space=False, consume=True, space=r'\s+'): def __init__(self, text, skip_space=False, consume=True, space=r'\s+'):