This commit is contained in:
parent
032a6d9109
commit
8cd5ad51ee
1 changed files with 55 additions and 0 deletions
55
compiler/tests/test_parser.py
Normal file
55
compiler/tests/test_parser.py
Normal 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()
|
||||||
Loading…
Add table
Add a link
Reference in a new issue