Skip to content

Commit

Permalink
Merge pull request guidance-ai#864 from hudson-ai/more_parser_excepti…
Browse files Browse the repository at this point in the history
…on_info

Improve JSON and Pydantic negative tests with more `ParserException` info
  • Loading branch information
hudson-ai authored May 31, 2024
2 parents cb263a8 + 0273662 commit c009b1a
Show file tree
Hide file tree
Showing 4 changed files with 338 additions and 106 deletions.
8 changes: 6 additions & 2 deletions guidance/_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class ParserException(Exception):
def __init__(self, *args, **kwargs):
self.current_byte = kwargs.pop("current_byte", None)
self.allowed_bytes = kwargs.pop("allowed_bytes", None)
self.consumed_bytes = kwargs.pop("consumed_bytes", None)
super().__init__(*args, **kwargs)


Expand Down Expand Up @@ -357,6 +358,8 @@ def consume_byte(self, byte, log_prob=0.0):
raise ParserException(
"Attempted to consume a byte that the grammar does not accept!",
current_byte=byte,
allowed_bytes=self.valid_next_bytes(),
consumed_bytes=self.bytes,
)
if found_invalid: # only update if we changed the set
self.state_sets[self.state_set_pos + 1] = OrderedSet(new_next_state_set)
Expand Down Expand Up @@ -654,8 +657,9 @@ def _compute_children(self, state_set_pos, item, reversed_state_sets, values_pos
if self._compute_children(
state_set_pos, item, reversed_state_sets, values_pos + 1
):
item.children[values_pos] = (
EarleyItem(value, tuple(), 0, state_set_pos, 0, state_set_pos) # this child has zero length since it was nullable
# this child has zero length since it was nullable
item.children[values_pos] = EarleyItem(
value, tuple(), 0, state_set_pos, 0, state_set_pos
)
return True

Expand Down
Loading

0 comments on commit c009b1a

Please sign in to comment.