def p_postfix_expression(self, p): """postfix_expression : left_hand_side_expression | left_hand_side_expression PLUSPLUS | left_hand_side_expression MINUSMINUS""" if len(p) == 2: p[0] = p[1] else: p[0] = ast.UnaryOp(op='postfix' + p[2], expression=p[1])
def p_unary_expression(self, p): """unary_expression : postfix_expression | DELETE unary_expression | VOID unary_expression | TYPEOF unary_expression | PLUSPLUS unary_expression | MINUSMINUS unary_expression | PLUS unary_expression | MINUS unary_expression | NOT unary_expression | LNOT unary_expression""" if len(p) == 2: p[0] = p[1] else: p[0] = ast.UnaryOp(op=p[1], expression=p[2])
def test_parse_prefix_op(self): self.assertEqual( self.parser.parse('++x'), ast.UnaryOp(op='++', expression=ast.Identifier(name='x')))
def test_parse_unary_op(self): self.assertEqual( self.parser.parse('+-1'), ast.UnaryOp(op='+', expression=ast.UnaryOp( op='-', expression=ast.Literal(value=1))))