diff --git a/src/parse.py b/src/parse.py index 64d21b6..423839f 100644 --- a/src/parse.py +++ b/src/parse.py @@ -2,6 +2,34 @@ # Copyright 2022 Jookia from src import tokenize +import enum + + +# The type of syntax +class SyntaxType(enum.Enum): + TOKEN = enum.auto() # pragma: no mutate + + +# Represents a syntax node +class Syntax: + def __init__(self, value, location, type): + self.value = value + self.location = location + self.type = type + + def __repr__(self): + return "Syntax(value %s, location %s, type %s)" % ( # pragma: no mutate + repr(self.value), + repr(self.location), + str(self.type), + ) + + def __eq__(self, other): + return ( + self.value == other.value + and self.location == other.location + and self.type == other.type + ) # Removes whitespace tokens diff --git a/tests/test_parse.py b/tests/test_parse.py index eb9e366..5b36f6b 100644 --- a/tests/test_parse.py +++ b/tests/test_parse.py @@ -2,16 +2,55 @@ # Copyright 2022 Jookia from hypothesis import given -from hypothesis.strategies import ( - composite, - lists, -) +from hypothesis.strategies import composite, lists, text, sampled_from from src import tokenize from src import parse from tests import test_tokenize +# Draws a random syntax location +@composite +def draw_syntax_location(draw): + return draw(test_tokenize.draw_token_location()) + + +# Draws a random syntax type +@composite +def draw_syntax_type(draw): + return draw(sampled_from(list(parse.SyntaxType))) + + +# Draws a token syntax value +@composite +def draw_syntax_token(draw): + value = draw(test_tokenize.draw_token_random()) + location = draw(draw_syntax_location()) + type = parse.SyntaxType.TOKEN + return parse.Syntax(value, location, type) + + +# Test syntax getters +@given(text(), draw_syntax_location(), draw_syntax_type()) +def test_parse_syntax_getters(value, location, type): + # Use text as a somewhat random value + test = parse.Syntax(value, location, type) + assert test.value == value + assert test.location == location + assert test.type == type + + +# Test syntax equals +@given(draw_syntax_token(), draw_syntax_token()) +def test_parse_syntax_equality(syntax1, syntax2): + equals = ( + syntax1.value == syntax2.value + and syntax1.location == syntax2.location + and syntax1.type == syntax2.type + ) + assert (syntax1 == syntax2) == equals + + # Draws classified tokens and a list without whitespace in them @composite def draw_tokens_whitespace(draw):