def test_parse_power_expr_(): # TODO: More complex examples result0 = Parser.from_code("5^6").parse_power_expr() result1 = Parser.from_code("5²").parse_power_expr() result1 = Parser.from_code("5").parse_power_expr() result2 = Parser.from_code("√5²").parse_power_expr() result3 = Parser.from_code("√5^5").parse_power_expr()
def test_parse_unary_expr_(): # TODO: More complex examples result0 = Parser.from_code("5^-6").parse_unary_expr() result1 = Parser.from_code("-5²").parse_unary_expr() result2 = Parser.from_code("√~5²").parse_unary_expr() result3 = Parser.from_code("-√5^5").parse_unary_expr() result3 = Parser.from_code("-~+5_00").parse_unary_expr()
def test_parse_mul_expr_(): # TODO: More complex examples result0 = Parser.from_code("-5.0f").parse_mul_expr() result1 = Parser.from_code("0xff*3//4").parse_mul_expr() result2 = Parser.from_code("√~5f²").parse_mul_expr() result3 = Parser.from_code("5^5*3").parse_mul_expr() result3 = Parser.from_code("-5/-4*+3").parse_mul_expr()
def test_parse_newline_parses_newlines_successfully(): result0 = Parser.from_code("\n").parse_newline() result1 = Parser.from_code("\r\n").parse_newline() result2 = Parser.from_code("\r").parse_newline() assert result0 == Newline(0) assert result1 == Newline(0) assert result2 == Newline(0)
def test_parse_string_parses_string_literals_successfully(): result0 = Parser.from_code("'hello\t there'").parse_string() result1 = Parser.from_code('" This is a new world"').parse_string() result2 = Parser.from_code("'''\n This is a new world\n'''").parse_string() result3 = Parser.from_code('"""\n This is a new world\n"""').parse_string() assert result0 == String(0) assert result1 == String(0) assert result2 == String(0) assert result3 == String(0)
def test_parse_imag_float_parses_imaginary_float_literals_successfully(): result0 = Parser.from_code(".05im").parse_imag_float() result1 = Parser.from_code("0.0_55im").parse_imag_float() result2 = Parser.from_code("1_00.00_50im").parse_imag_float() result3 = Parser.from_code("1.e-5_00im").parse_imag_float() result4 = Parser.from_code("1_00.1_00e-1_00im").parse_imag_float() assert result0 == ImagFloat(0) assert result1 == ImagFloat(0) assert result2 == ImagFloat(0) assert result3 == ImagFloat(0) assert result4 == ImagFloat(0)
def test_parse_integer_parses_integer_literals_successfully(): result0 = Parser.from_code("5_000").parse_integer() result1 = Parser.from_code("0001").parse_integer() result2 = Parser.from_code("0b11_00").parse_integer() result3 = Parser.from_code("0o217").parse_integer() result4 = Parser.from_code("0xffEE_210").parse_integer() assert result0 == Integer(0) assert result1 == Integer(0) assert result2 == Integer(0) assert result3 == Integer(0) assert result4 == Integer(0)
def setUp(self): with open('tests/input/file', 'w') as file: file.write("program{int x;};") self.s: Scanner = Scanner('tests/input/file') tokens: list = self.s.scan_file() self.p: Parser = Parser(tokens)
def test_parser_backtracks_on_fail_successfully(): parser0 = Parser.from_code("1hello") result0 = parser0.parse_identifier() # TODO: Try more complex parser functions assert parser0.cursor == -1 assert result0 is None
def test_parser_memoizes_called_parser_functions_successfully(): # Memoize if parser successful parser0 = Parser.from_code("identifier") result0 = parser0.parse_identifier() # TODO: Memoize if parser fails # TODO: Use memoized result assert result0 == Identifier(0) assert parser0.cache == {-1: {'parse_identifier': (Identifier(0), 0)}}
class Compiler: def __init__(self): self.lexer = Lexer() self.parser = Parser() self.typechecker = Typechecker() @with_logger.log_result('LEXER') def lex(self, source): return self.lexer.lex(source) @with_logger.log_result('PARSER') def parse(self, source): return self.parser.parse(self.lex(source)) @with_logger.log_result('TYPECHECKER') def typecheck(self, source): return self.typechecker.typecheck(self.parse(source))
def test_parse_float_parses_float_literals_successfully(): result0 = Parser.from_code(".05").parse_float() result1 = Parser.from_code("0.0_55").parse_float() result2 = Parser.from_code("1_00.00_50").parse_float() result3 = Parser.from_code("1.e-5_00").parse_float() result4 = Parser.from_code("1.").parse_float() result5 = Parser.from_code("1_00.1_00e-1_00").parse_float() assert result0 == Float(0) assert result1 == Float(0) assert result2 == Float(0) assert result3 == Float(0) assert result4 == Float(0) assert result5 == Float(0)
def test_parse_imag_integer_parses_imaginary_integer_literals_successfully(): result0 = Parser.from_code("5_000im").parse_imag_integer() result1 = Parser.from_code("0001im").parse_imag_integer() assert result0 == ImagInteger(0) assert result1 == ImagInteger(0)
def test_parse_identifier_parses_identifiers_successfully(): result = Parser.from_code("_HEoDagu123").parse_identifier() assert result == Identifier(0)
import unittest from rply import Token from compiler.parser.parser import Parser import compiler.parser.ast as ast parser = Parser() class TestExpr(unittest.TestCase): def test_add(self): '''Can parse a simple addition: `1 + 1;`''' given = iter([ Token('INTEGER', '1'), Token('PLUS', '+'), Token('INTEGER', '1'), Token('SEMI', ';') ]) expected = ast.Block([ ast.Statement(ast.BinOp('+', ast.Integer(1), ast.Integer(1))) ]) result = parser.parse(given) assert expected == result def test_op_order(self): '''Enforces the right op. precedence: `1 + 5 * 20;`''' given = iter([
from compiler.scanner.scanner import Scanner from compiler.parser.parser import Parser DEBUG_TEST_FILE = "compiler/samples/sample_2" args_len: int = len(sys.argv) if args_len >= 2: s = Scanner(sys.argv[1]) else: s = Scanner(DEBUG_TEST_FILE) token_sequence: list = s.scan_file() print("----------\nScanner\n----------") if args_len >= 3: s.log(sys.argv[2]) else: s.log() print("----------\nParser\n----------") p: Parser = Parser(token_sequence) result: bool = p.parse() if args_len >= 3: p.log_results(sys.argv[2]) print('----------\n'+str(result)) else: p.log_results() print('----------\nResult: ' + str(result))
def __init__(self): self.lexer_instance = Lexer() self.parser_instance = Parser()
def __init__(self): self.lexer = Lexer() self.parser = Parser() self.typechecker = Typechecker()