diff --git a/src/parse.py b/src/parse.py index 842a48e..cdadea9 100644 --- a/src/parse.py +++ b/src/parse.py @@ -49,18 +49,20 @@ # Exception thrown when a parse error is encountered class ParseErrorException(BaseException): - def __init__(self, error, syntax, expected): + def __init__(self, error, syntax, expected, context): self.error = error self.syntax = syntax self.expected = expected + self.context = context def __str__(self): return ( - "ParseErrorException(error %s, syntax %s, expected %s)" # pragma: no mutate + "ParseErrorException(error %s, syntax %s, expected %s, context %s)" # pragma: no mutate % ( # pragma: no mutate self.error, self.syntax, self.expected, + self.context, ) ) @@ -69,6 +71,7 @@ self.error == other.error and self.syntax == other.syntax and self.expected == other.expected + and self.context == other.context ) @@ -76,11 +79,11 @@ def read_token(stream, value): s = stream.pop() if s is None: - raise ParseErrorException(ParseError.NO_TOKEN, None, None) + raise ParseErrorException(ParseError.NO_TOKEN, None, None, None) elif s.type != SyntaxType.TOKEN: - raise ParseErrorException(ParseError.NOT_TOKEN, s, None) + raise ParseErrorException(ParseError.NOT_TOKEN, s, None, None) elif value is not None and s.value != value: - raise ParseErrorException(ParseError.WRONG_TOKEN, s, value) + raise ParseErrorException(ParseError.WRONG_TOKEN, s, value, None) return s @@ -93,7 +96,7 @@ s = read_token(stream, None) # Don't allow StartNote in notes if s.value in ["StartNote"]: - raise ParseErrorException(ParseError.FOUND_STARTNOTE, s, None) + raise ParseErrorException(ParseError.FOUND_STARTNOTE, s, None, None) # EndNote found, end things elif s.value == "EndNote": break @@ -109,7 +112,7 @@ self.skip_note(stream) # EndNote found outside note elif token.value == "EndNote": - raise ParseErrorException(ParseError.FOUND_ENDNOTE, token, None) + raise ParseErrorException(ParseError.FOUND_ENDNOTE, token, None, None) # Add the token if it's not note related else: tokens.append(stream.pop()) @@ -129,7 +132,7 @@ s = read_token(stream, None) # Don't allow StartText in text if s.value in ["StartText"]: - raise ParseErrorException(ParseError.FOUND_STARTTEXT, s, None) + raise ParseErrorException(ParseError.FOUND_STARTTEXT, s, None, None) # EndText found, end things elif s.value == "EndText": break @@ -147,7 +150,7 @@ elif s.value == "False": return Syntax(False, s.location, SyntaxType.BOOL) else: - raise ParseErrorException(ParseError.NOT_BOOL, s, None) + raise ParseErrorException(ParseError.NOT_BOOL, s, None, None) # Parses tokens diff --git a/tests/parse/test_bool.py b/tests/parse/test_bool.py index e1376e8..bb3bc98 100644 --- a/tests/parse/test_bool.py +++ b/tests/parse/test_bool.py @@ -50,12 +50,12 @@ not (token.type == SyntaxType.TOKEN and token.value in ["True", "False"]) ) if token.type == SyntaxType.TOKEN: - error = ParseErrorException(ParseError.NOT_BOOL, token, None) + error = ParseErrorException(ParseError.NOT_BOOL, token, None, None) else: - error = ParseErrorException(ParseError.NOT_TOKEN, token, None) + error = ParseErrorException(ParseError.NOT_TOKEN, token, None, None) return ([token], error) else: - error = ParseErrorException(ParseError.NO_TOKEN, None, None) + error = ParseErrorException(ParseError.NO_TOKEN, None, None, None) return ([], error) diff --git a/tests/parse/test_clear_notes.py b/tests/parse/test_clear_notes.py index 8d13367..9df41f9 100644 --- a/tests/parse/test_clear_notes.py +++ b/tests/parse/test_clear_notes.py @@ -24,7 +24,7 @@ # This redefines skip_note to always throw an error def clear_notes_skip_note_error(stream): s = stream.peek() - raise ParseErrorException(ParseError.TEST_ERROR, s, None) + raise ParseErrorException(ParseError.TEST_ERROR, s, None, None) # Draws a random token suitable for note clearing testing @@ -69,7 +69,7 @@ new_tokens = tokens + [start] for token in new_tokens: if token.value == "StartNote": - error = ParseErrorException(ParseError.TEST_ERROR, token, None) + error = ParseErrorException(ParseError.TEST_ERROR, token, None, None) return (new_tokens, error) raise AssertionError("Unable to find StartNote?") @@ -100,7 +100,7 @@ new_tokens = tokens + [start] for token in new_tokens: if token.value == "EndNote": - error = ParseErrorException(ParseError.FOUND_ENDNOTE, token, None) + error = ParseErrorException(ParseError.FOUND_ENDNOTE, token, None, None) return (new_tokens, error) raise AssertionError("Unable to find EndNote?") diff --git a/tests/parse/test_note.py b/tests/parse/test_note.py index 37222f4..7acd9d2 100644 --- a/tests/parse/test_note.py +++ b/tests/parse/test_note.py @@ -76,12 +76,14 @@ assume(not (token.type == SyntaxType.TOKEN and token.value == "StartNote")) new_tokens = [token] + tokens[1:0] if token.type == SyntaxType.TOKEN: - error = ParseErrorException(ParseError.WRONG_TOKEN, token, "StartNote") + error = ParseErrorException( + ParseError.WRONG_TOKEN, token, "StartNote", None + ) else: - error = ParseErrorException(ParseError.NOT_TOKEN, token, None) + error = ParseErrorException(ParseError.NOT_TOKEN, token, None, None) return (new_tokens, error) else: - error = ParseErrorException(ParseError.NO_TOKEN, None, None) + error = ParseErrorException(ParseError.NO_TOKEN, None, None, None) return ([], error) @@ -93,7 +95,7 @@ tokens = draw(draw_syntax_note_valid()) start = draw(draw_token_by_value("StartNote")) new_tokens = insert_random(draw, tokens, start) - error = ParseErrorException(ParseError.FOUND_STARTNOTE, start, None) + error = ParseErrorException(ParseError.FOUND_STARTNOTE, start, None, None) return (new_tokens, error) @@ -103,7 +105,7 @@ @composite def draw_syntax_note_invalid_noendnote(draw): tokens = draw(draw_syntax_note_valid()) - error = ParseErrorException(ParseError.NO_TOKEN, None, None) + error = ParseErrorException(ParseError.NO_TOKEN, None, None, None) return (tokens[0:-1], error) diff --git a/tests/parse/test_parse.py b/tests/parse/test_parse.py index 6ef82b1..3c951d2 100644 --- a/tests/parse/test_parse.py +++ b/tests/parse/test_parse.py @@ -68,16 +68,18 @@ error = draw(draw_parse_error()) syntax = draw(draw_syntax_random()) expected = draw(text()) - return ParseErrorException(error, syntax, expected) + context = draw(draw_parse_context()) + return ParseErrorException(error, syntax, expected, context) # Test parse error exception getters -@given(draw_parse_error(), draw_syntax_random(), text()) -def test_parse_error_getters(error, syntax, expected): - test = ParseErrorException(error, syntax, expected) +@given(draw_parse_error(), draw_syntax_random(), text(), draw_parse_context()) +def test_parse_error_getters(error, syntax, expected, context): + test = ParseErrorException(error, syntax, expected, context) assert test.error == error assert test.syntax == syntax assert test.expected == expected + assert test.context == context # Test parse error exception equals @@ -87,6 +89,7 @@ except1.error == except2.error and except1.syntax == except2.syntax and except1.expected == except2.expected + and except1.context == except2.context ) assert (except1 == except2) == equals diff --git a/tests/parse/test_text.py b/tests/parse/test_text.py index 8823765..f433698 100644 --- a/tests/parse/test_text.py +++ b/tests/parse/test_text.py @@ -85,12 +85,14 @@ assume(not (token.type == SyntaxType.TOKEN and token.value == "StartText")) new_tokens = [token] + tokens[1:0] if token.type == SyntaxType.TOKEN: - error = ParseErrorException(ParseError.WRONG_TOKEN, token, "StartText") + error = ParseErrorException( + ParseError.WRONG_TOKEN, token, "StartText", None + ) else: - error = ParseErrorException(ParseError.NOT_TOKEN, token, None) + error = ParseErrorException(ParseError.NOT_TOKEN, token, None, None) return (new_tokens, error) else: - error = ParseErrorException(ParseError.NO_TOKEN, None, None) + error = ParseErrorException(ParseError.NO_TOKEN, None, None, None) return ([], error) @@ -103,7 +105,7 @@ token = draw(draw_syntax_random()) assume(token.type != SyntaxType.TOKEN) new_tokens = insert_random(draw, tokens, token) - error = ParseErrorException(ParseError.NOT_TOKEN, token, None) + error = ParseErrorException(ParseError.NOT_TOKEN, token, None, None) return (new_tokens, error) @@ -115,7 +117,7 @@ (tokens, _) = draw(draw_syntax_text_valid()) start = draw(draw_token_by_value("StartText")) new_tokens = insert_random(draw, tokens, start) - error = ParseErrorException(ParseError.FOUND_STARTTEXT, start, None) + error = ParseErrorException(ParseError.FOUND_STARTTEXT, start, None, None) return (new_tokens, error) @@ -125,7 +127,7 @@ @composite def draw_syntax_text_invalid_noendtext(draw): (tokens, _) = draw(draw_syntax_text_valid()) - error = ParseErrorException(ParseError.NO_TOKEN, None, None) + error = ParseErrorException(ParseError.NO_TOKEN, None, None, None) return (tokens[0:-1], error)