Beispiel #1
0
 def test_parse_block(self):
     self.assertEqual(
         self.parser.parse('{ 1; 3; }'),
         ast.Block(statements=[
             ast.ExpressionStatement(expression=ast.Literal(value=1)),
             ast.ExpressionStatement(expression=ast.Literal(value=3))
         ]))
Beispiel #2
0
 def test_parse_compound_assignment(self):
     self.assertEqual(self.parser.parse('x /= 5 - 2'),
                      ast.Assignment(op='/=',
                                     reference=ast.Identifier(name='x'),
                                     expression=ast.BinaryOp(op='-',
                                                             left_expression=ast.Literal(value=5),
                                                             right_expression=ast.Literal(value=2))))
Beispiel #3
0
 def test_parse_binary_op(self):
     self.assertEqual(self.parser.parse('1 + 2 * 7'),
                      ast.BinaryOp(op='+',
                                   left_expression=ast.Literal(value=1),
                                   right_expression=ast.BinaryOp(op='*',
                                                                 left_expression=ast.Literal(value=2),
                                                                 right_expression=ast.Literal(value=7))))
Beispiel #4
0
 def test_parse_object_literal(self):
     self.assertEqual(self.parser.parse('{7: [9, 10, "ala ma kota"], "ala ma kota": {3: 4}}'),
                      ast.ObjectLiteral(
             items={7: ast.ArrayLiteral(items=[ast.Literal(value=9),
                                               ast.Literal(value=10),
                                               ast.Literal(value='ala ma kota')]),
                    'ala ma kota': ast.ObjectLiteral(items={3: ast.Literal(value=4)})}))
Beispiel #5
0
 def test_parse_variable_statement(self):
     self.assertEqual(self.parser.parse('var x = 7, y = 5;'),
                      ast.VariableDeclarationList(
             declarations=[ast.VariableDeclaration(identifier=ast.Identifier(name='x'),
                                                   initialiser=ast.Literal(value=7)),
                           ast.VariableDeclaration(identifier=ast.Identifier(name='y'),
                                                   initialiser=ast.Literal(value=5))]))
Beispiel #6
0
 def p_property_access_call_expression(self, p):
     """call_expression : call_expression LBRACE expression RBRACE
                        | call_expression PERIOD ID"""
     if len(p) == 5:
         p[0] = ast.PropertyAccess(obj=p[1], key=p[3])
     else:
         p[0] = ast.PropertyAccess(obj=p[1], key=ast.Literal(value=p[3]))
Beispiel #7
0
 def p_property_access_expression(self, p):
     """property_access_expression : member_expression LBRACKET expression RBRACKET
                                   | member_expression PERIOD ID"""
     if len(p) == 5:
         p[0] = ast.PropertyAccess(obj=p[1], key=p[3])
     else:
         p[0] = ast.PropertyAccess(obj=p[1], key=ast.Literal(value=p[3]))
Beispiel #8
0
 def p_literal(self, p):
     """literal : NUMBER
                | REGEXP
                | TRUE
                | FALSE
                | NULL"""
     p[0] = ast.Literal(value=p[1])
Beispiel #9
0
 def test_parse_unary_op(self):
     self.assertEqual(
         self.parser.parse('+-1'),
         ast.UnaryOp(op='+',
                     expression=ast.UnaryOp(
                         op='-', expression=ast.Literal(value=1))))