Example #1
0
 def test_basic_parse(self):
     input_data = """
     var foo = 45 + 5.4;
     var bar = foo - 2;
     """
     expected_output = [
         ("ASSIGNMENT", ),
         ("NAME", "foo"),
         ("SYMBOL", "="),
         ("NUMBER", 45),
         ("SYMBOL", "+"),
         ("NUMBER", 5.4),
         ("SEMICOLON", ),
         ("ASSIGNMENT", ),
         ("NAME", "bar"),
         ("SYMBOL", "="),
         ("NAME", "foo"),
         ("SYMBOL", "-"),
         ("NUMBER", 2),
         ("SEMICOLON", ),
         ("EOF", )
     ]
     p = progdata(input_data)
     tokens = self.lexer.parse(p)
     self.assertEqual(tokens, expected_output)
Example #2
0
    def parse(self, program):
        program_data = progdata(program)
        blocks = []
        p = True
        while p:
            e = self.parse_line(program_data)
            if not e:
                break
            blocks.append(e)

        return blocks
Example #3
0
 def test_more_complex_parse(self):
     input_data = """
     def foo(one, two) {
         var bacon = one + 2;
         return bacon * (2 + two);
     }
     var bar = foo(2.3, 8);
     """
     expected_output = [
         ("FUNCTIONDEF", ),
         ("NAME", "foo"),
         ("OPENPAREN", ),
         ("NAME", "one"),
         ("SYMBOL", ","),
         ("NAME", "two"),
         ("CLOSEPAREN", ),
         ("OPENBRACKET", ),
         ("ASSIGNMENT", ),
         ("NAME", "bacon"),
         ("SYMBOL", "="),
         ("NAME", "one"),
         ("SYMBOL", "+"),
         ("NUMBER", 2),
         ("SEMICOLON", ),
         ("FUNCTIONRETURN", ),
         ("NAME", "bacon"),
         ("SYMBOL", "*"),
         ("OPENPAREN", ),
         ("NUMBER", 2),
         ("SYMBOL", "+"),
         ("NAME", "two"),
         ("CLOSEPAREN", ),
         ("SEMICOLON", ),
         ("CLOSEBRACKET", ),
         ("ASSIGNMENT", ),
         ("NAME", "bar"),
         ("SYMBOL", "="),
         ("NAME", "foo"),
         ("OPENPAREN", ),
         ("NUMBER", 2.3),
         ("SYMBOL", ","),
         ("NUMBER", 8),
         ("CLOSEPAREN", ),
         ("SEMICOLON", ),
         ("EOF", )
     ]
     p = progdata(input_data)
     tokens = self.lexer.parse(p)
     self.assertEqual(tokens, expected_output)
Example #4
0
 def test_bigger_program(self):
     program = """
     var foo = 2;
     var bar = foo + 3;
     def bam(a) {
         return a + 2;
     }
     var bim = bam(bar);
     """
     p = progdata(program)
     tokens = self.lexer.parse(p)
     ast = self.parser.parse(tokens)
     self.assertIsInstance(ast[0], assignment)
     self.assertIsInstance(ast[1], assignment)
     self.assertIsInstance(ast[2], func_def)
     self.assertIsInstance(ast[3], assignment)
Example #5
0
 def test_basic_assignment(self):
     program = "var foo = 2;"
     p = progdata(program)
     tokens = self.lexer.parse(p)
     ast = self.parser.parse(tokens)
     self.assertIsInstance(ast[0], assignment)
Example #6
0
 def test_parse_close_paren(self):
     input_data = progdata(")  ")
     paren = self.lexer.parse_paren(input_data)
     self.assertEqual(paren, ("CLOSEPAREN", ))
     self.assertEqual(input_data.remaining(), "  ")
Example #7
0
 def test_parse_open_paren(self):
     input_data = progdata("(  ")
     paren = self.lexer.parse_paren(input_data)
     self.assertEqual(paren, ("OPENPAREN", ))
     self.assertEqual(input_data.remaining(), "  ")
Example #8
0
 def test_parse_semicolon(self):
     input_data = progdata(""";  """)
     semicolon = self.lexer.parse_semicolon(input_data)
     self.assertEqual(semicolon, ("SEMICOLON", ))
     self.assertEqual(input_data.remaining(), "  ")
Example #9
0
 def test_parse_symbol(self):
     input_data = progdata("++  ")
     symbol = self.lexer.parse_symbol(input_data)
     self.assertEqual(symbol, ("SYMBOL", "++"))
     self.assertEqual(input_data.remaining(), "  ")
Example #10
0
 def test_parse_name(self):
     input_data = progdata("name  ")
     name = self.lexer.parse_name(input_data)
     self.assertEqual(name, ("NAME", "name"))
     self.assertEqual(input_data.remaining(), "  ")
Example #11
0
 def test_parse_int(self):
     input_data = progdata("4467  ")
     number = self.lexer.parse_number(input_data)
     self.assertEqual(number, ("NUMBER", 4467))
     self.assertEqual(input_data.remaining(), "  ")