diff --git a/src/ast_types.py b/src/ast_types.py index 01054f9..8783255 100644 --- a/src/ast_types.py +++ b/src/ast_types.py @@ -9,6 +9,9 @@ def __repr__(self): return "Reference('%s')" % (self.value) + def __eq__(self, other): + return self.value == other.value + class Bool: def __init__(self, value): diff --git a/src/parse.py b/src/parse.py index bb3d177..d4f7388 100644 --- a/src/parse.py +++ b/src/parse.py @@ -2,7 +2,7 @@ # Copyright 2022 Jookia import enum -from src.ast_types import Bool, Text +from src.ast_types import Bool, Reference, Text from src.token import TokenStream @@ -13,6 +13,7 @@ CLEAR_NOTES = enum.auto() # pragma: no mutate PARSE_TEXT = enum.auto() # pragma: no mutate PARSE_BOOL = enum.auto() # pragma: no mutate + PARSE_REFERENCE = enum.auto() # pragma: no mutate # Context used for parse error exception @@ -158,6 +159,12 @@ else: raise ParseErrorException(ParseError.NOT_BOOL, t, None, context) + # Parses a reference node + def parse_reference(self, stream, parent_context): + context = ParseContext(ParseTask.PARSE_REFERENCE, stream.peek(), parent_context) + t = read_token(stream, None, context) + return Reference(t.value) + # Parses tokens def parse(tokens, context): diff --git a/tests/parse/test_reference.py b/tests/parse/test_reference.py new file mode 100644 index 0000000..4153049 --- /dev/null +++ b/tests/parse/test_reference.py @@ -0,0 +1,35 @@ +# SPDX-License-Identifier: LGPL-2.1-only +# Copyright 2022 Jookia + +from hypothesis.strategies import composite + +from src.ast_types import Reference +from src.parse import ParseContext, ParseError, ParseErrorException, ParseTask, Parser +from tests.parse.templates import template_parse_valid, template_parse_invalid +from tests.test_token import draw_token_random + + +# Draws tokens to make a reference +@composite +def draw_token_reference_valid(draw): + token = draw(draw_token_random()) + return ([token], Reference(token.value)) + + +# Tests parse_reference works correctly +# We expect the following behaviour: +# - The resulting reference has the token's value +# template_parse_valid provides general parsing properties +@template_parse_valid(Parser().parse_reference, draw_token_reference_valid()) +def test_parse_reference_valid(): + pass + + +# Tests parsing of empty references +# We expect the following behaviour: +# - Error if there isn't a token +@template_parse_invalid(Parser().parse_reference) +def test_parse_reference_invalid_empty(draw, parent_context): + context = ParseContext(ParseTask.PARSE_REFERENCE, None, parent_context) + error = ParseErrorException(ParseError.NO_TOKEN, None, None, context) + return ([], error)