diff --git a/src/parse.py b/src/parse.py index 0baebe6..6e8b39d 100644 --- a/src/parse.py +++ b/src/parse.py @@ -312,6 +312,17 @@ return parsed +# Formats a ParseContext +def format_context(context): + task = context.task + if context.token: + line = context.token.location.line + offset = context.token.location.offset + return Message("ParseContextAt", [task, line, offset]) + else: + return Message("ParseContext", [task]) + + # Formats a ParseErrorException def format_exception(exception): id = "ParseError" diff --git a/tests/parse/test_error.py b/tests/parse/test_error.py index 4fdb388..f552354 100644 --- a/tests/parse/test_error.py +++ b/tests/parse/test_error.py @@ -33,6 +33,7 @@ ParseError, ParseErrorException, ParseTask, + format_context, format_exception, ) from tests.templates import template_test_structure @@ -121,6 +122,31 @@ pass +# Tests formatting a ParseContext +# We expect the following behaviour: +# - A Message is returned +# - The message ID begins with ParseContext +# - The first parameter is task +# - If the token field is set, the ID is appended with +# "At" and the second and third parameters are the token's +# location's line and offset +# Two combinations are possible: +# - Message("ParseContext", [task]) +# - Message("ParseContextAt", [task, line, offset]) +@given(draw_parse_context()) +def test_parse_error_format_context(context): + task = context.task + has_location = context.token is not None + if has_location: + line = context.token.location.line + offset = context.token.location.offset + expected = Message("ParseContextAt", [task, line, offset]) + else: + expected = Message("ParseContext", [task]) + value = format_context(context) + assert expected == value + + # Tests formatting a ParseErrorException # We expect the following behaviour: # - A Message is returned