tests: parser: add parser tests
All checks were successful
/ tests (push) Successful in 10s

This commit is contained in:
Antoine Viallon 2024-07-26 00:13:42 +02:00
parent 032a6d9109
commit 8cd5ad51ee
Signed by: aviallon
GPG key ID: 186FC35EDEB25716

View file

@ -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()