diff --git a/compiler/tests/test_parser.py b/compiler/tests/test_parser.py new file mode 100644 index 0000000..62e99db --- /dev/null +++ b/compiler/tests/test_parser.py @@ -0,0 +1,55 @@ +from __future__ import annotations + +import io + +# noinspection PyUnresolvedReferences +from compiler import semantic +from compiler.lexer import Tokens, Lexer, TextIOWithMemory +from compiler.parser import Parser + + +def _lex_string(data: str) -> Lexer: + return Lexer(input_stream=TextIOWithMemory(io.StringIO(data)), + token_filter=lambda token: token.kind not in [Tokens.Blank, Tokens.Newline, Tokens.Comment]) + + +def test_parser_empty(): + lexer = _lex_string("") + my_parser = Parser(tokens=lexer) + node = my_parser.parse() + assert node._pretty(indent=" ").strip() == """ + Block(pure=True) { + BEGIN(None) + EOF(None) + } // Block""".strip() + + +def test_parser_arithmetic(): + lexer = _lex_string(""" + 1+5-41*2/1; + """) + my_parser = Parser(tokens=lexer) + node = my_parser.parse() + assert node._pretty(indent=" ").strip() == """ + Block(pure=True) { + Statement(pure=True) { + Expression(pure=True) { + Sub(pure=True) { + Sum(pure=True) { + Integer(1) + Integer(5) + } // Sum + TrueDivision(pure=True) { + Product(pure=True) { + Integer(41) + Integer(2) + } // Product + Integer(1) + } // TrueDivision + } // Sub + } // Expression + PseudoNode(';') + } // Statement + BEGIN(None) + EOF(None) + } // Block""".strip()