-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
be78c58
commit b77f887
Showing
14 changed files
with
199 additions
and
218 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,21 @@ | ||
class JsonParsingException(Exception): | ||
pass | ||
def __init__(self, message: str, char_position: int): | ||
super().__init__( | ||
f"Failed to parse json string at position {char_position}: {message}" | ||
) | ||
|
||
|
||
def unexpected_symbol_error( | ||
char: str, char_position: int | ||
) -> JsonParsingException: | ||
return JsonParsingException( | ||
f"Failed to parse json string: unexpected symbol {char} at position {char_position}" | ||
) | ||
return JsonParsingException(f"Unexpected symbol {char}.", char_position) | ||
|
||
|
||
def unexpected_end_of_stream_error(char_position: int) -> JsonParsingException: | ||
return JsonParsingException( | ||
f"Failed to parse json string: unexpected end of stream at position {char_position}" | ||
) | ||
return JsonParsingException("Unexpected end of stream.", char_position) | ||
|
||
|
||
def invalid_sequence_error( | ||
sequence: str, char_position: int | ||
) -> JsonParsingException: | ||
return JsonParsingException(f"Invalid sequence {sequence}.", char_position) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,35 @@ | ||
import json | ||
|
||
from typing_extensions import override | ||
|
||
from aidial_assistant.json_stream.json_node import PrimitiveNode | ||
from aidial_assistant.json_stream.exceptions import invalid_sequence_error | ||
from aidial_assistant.json_stream.json_node import AtomicNode | ||
|
||
TRUE_STRING = "true" | ||
FALSE_STRING = "false" | ||
|
||
|
||
class JsonBoolean(PrimitiveNode[bool]): | ||
class JsonBoolean(AtomicNode[bool]): | ||
def __init__(self, raw_data: str, char_position: int): | ||
super().__init__(char_position) | ||
self._raw_data = raw_data | ||
self._value: bool = json.loads(raw_data) | ||
super().__init__(raw_data, char_position) | ||
self._value: bool = JsonBoolean._parse_boolean(raw_data, char_position) | ||
|
||
@override | ||
def type(self) -> str: | ||
return "boolean" | ||
|
||
@override | ||
def raw_data(self) -> str: | ||
return self._raw_data | ||
|
||
@override | ||
def value(self) -> bool: | ||
return self._value | ||
|
||
@staticmethod | ||
def is_bool(char: str) -> bool: | ||
def starts_with(char: str) -> bool: | ||
return char == "t" or char == "f" | ||
|
||
@staticmethod | ||
def _parse_boolean(string: str, char_position: int) -> bool: | ||
if string == TRUE_STRING: | ||
return True | ||
|
||
if string == FALSE_STRING: | ||
return False | ||
|
||
raise invalid_sequence_error(string, char_position) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.