diff --git a/tests/property/test_generated_compiler_cases.py b/tests/property/test_generated_compiler_cases.py index c9c77fa..0d51471 100644 --- a/tests/property/test_generated_compiler_cases.py +++ b/tests/property/test_generated_compiler_cases.py @@ -1,4 +1,6 @@ + import itertools +import string import pytest @@ -7,7 +9,27 @@ from teapot.lexer import Lexer from teapot.parser import Parser +RESERVED_WORDS = ( + set(tokens.KEYWORDS) | set(tokens.TYPE_KEYWORDS) | set(tokens.BOOLEAN_LITERALS) +) + + +def _generated_simple_identifiers(): + starts = string.ascii_letters + "_" + continues = string.ascii_letters + string.digits + "_" + for start in starts: + if start not in RESERVED_WORDS: + yield start + for cont in continues: + name = start + cont + if name not in RESERVED_WORDS: + yield name + + +SIMPLE_IDENTIFIERS = tuple(_generated_simple_identifiers()) + WHITESPACE = ("", " ", "\t", "\n", " \t", "\n\n", " \n", "\t\n", "\r\n") + LEXICAL_ATOMS = tuple( (value, token_type) for value, token_type in itertools.chain( @@ -17,14 +39,21 @@ (("true", tokens.TokenType.BOOLEAN), ("false", tokens.TokenType.BOOLEAN)), ) ) + LEXICAL_CASES = tuple( (value, token_type, prefix, suffix) for value, token_type in LEXICAL_ATOMS for prefix, suffix in itertools.product(WHITESPACE, repeat=2) ) -SIMPLE_INTEGER_LITERALS = ("0", "1", "7", "9", "10", "42", "99", "00", "007", "123") -SIMPLE_FLOAT_LITERALS = ("0.0", "0.5", "1.0", "2.75", "10.01", "00.0", "01.50") +SIMPLE_INTEGER_LITERALS = ( + "0", "1", "7", "9", "10", "42", "99", "00", "007", "123" +) + +SIMPLE_FLOAT_LITERALS = ( + "0.0", "0.5", "1.0", "2.75", "10.01", "00.0", "01.50" +) + NUMERIC_LITERAL_CASES = tuple( (literal, tokens.TokenType.INTEGER, prefix, suffix) for literal in SIMPLE_INTEGER_LITERALS @@ -92,9 +121,34 @@ def test_parser_builds_variable_ast_for_type_literal_and_identifier_matrix( assert declaration.value is not None +@pytest.mark.parametrize("name", SIMPLE_IDENTIFIERS) +def test_lexer_classifies_generated_simple_identifiers(name): + for prefix, suffix in (("", ""), (" ", " "), ("\n", "\t")): + tokenised = Lexer(prefix + name + suffix).tokenise() + + assert tokenised[0].type == tokens.TokenType.IDENTIFIER + assert tokenised[0].value == name + assert tokenised[-1].type == tokens.TokenType.EOF + + +@pytest.mark.parametrize("name", SIMPLE_IDENTIFIERS) +def test_parser_accepts_generated_simple_identifier_names(name): + program = Parser( + Lexer(f"$MEM-GC\nval aint {name} = 1.").tokenise() + ).parse() + + declaration = program.statements[0] + assert isinstance(declaration, ast.DeclareVariable) + assert declaration.identifier == name + assert declaration.datatype.name == "aint" + assert declaration.value is not None + + NUMERIC_DECLARATION_CASES = tuple( ("caint", literal, "value") for literal in SIMPLE_INTEGER_LITERALS -) + tuple(("cdml", literal, "value") for literal in SIMPLE_FLOAT_LITERALS) +) + tuple( + ("cdml", literal, "value") for literal in SIMPLE_FLOAT_LITERALS +) @pytest.mark.parametrize("datatype, literal, name", NUMERIC_DECLARATION_CASES) @@ -108,6 +162,7 @@ def test_parser_builds_numeric_literal_declarations(datatype, literal, name): assert declaration.identifier == name assert declaration.datatype.name == datatype assert isinstance(declaration.value, ast.Literal) + if datatype == "caint": assert declaration.value.value == int(literal) else: @@ -128,7 +183,9 @@ def test_parser_builds_numeric_literal_declarations(datatype, literal, name): @pytest.mark.parametrize("atom_text, atom_node, depth", BALANCED_ATOM_CASES) def test_parser_parses_balanced_parenthesised_atoms(atom_text, atom_node, depth): wrapped = "(" * depth + atom_text + ")" * depth - program = Parser(Lexer(f"$MEM-GC\nval si64 value = {wrapped}.").tokenise()).parse() + program = Parser( + Lexer(f"$MEM-GC\nval si64 value = {wrapped}.").tokenise() + ).parse() declaration = program.statements[0] assert isinstance(declaration, ast.DeclareVariable) @@ -181,8 +238,13 @@ def test_parser_parses_balanced_parenthesised_binary_expressions( right_expression = "(" * right_wrap + right_text + ")" * right_wrap inner = f"{left_expression} {operator} {right_expression}" wrapped = "(" * whole_wrap + inner + ")" * whole_wrap - program = Parser(Lexer(f"$MEM-GC\nval si64 value = {wrapped}.").tokenise()).parse() + + program = Parser( + Lexer(f"$MEM-GC\nval si64 value = {wrapped}.").tokenise() + ).parse() declaration = program.statements[0] assert isinstance(declaration, ast.DeclareVariable) - assert declaration.value == ast.BinaryExpression(left_node, operator, right_node) + assert declaration.value == ast.BinaryExpression( + left_node, operator, right_node + )