def testSimpleExpression(self): tokens = iter([Identifier('x'), '+', Identifier('y'), EOF()]) parser = Parser(tokens) parser._advance() expression = parser._parse_expression() self.assertIsInstance(expression, Expression) self.assertIsInstance(expression.lhs, Variable) self.assertEquals(expression.lhs.identifier.identifier, 'x') self.assertIsInstance(expression.rhs, Variable) self.assertEquals(expression.rhs.identifier.identifier, 'y') self.assertEquals('+', expression.operator)
def testComplexExpression(self): tokens = iter([Identifier('a'), '+', Identifier('b'), '+', '(', Identifier('c'), '+', Identifier('d'), ')', '*', Identifier('e'), '*', Identifier('f'), '+', Identifier('g'), EOF()]) parser = Parser(tokens) parser._advance() expression = parser._parse_expression() self.assertIsInstance(expression, Expression) self.assertEquals(expression.lhs.lhs.lhs.identifier.identifier, 'a') self.assertEquals(expression.lhs.lhs.operator, '+') self.assertEquals(expression.lhs.lhs.rhs.identifier.identifier, 'b') self.assertEquals(expression.lhs.operator, '+') self.assertEquals(expression.operator, '+') self.assertEquals(expression.lhs.rhs.lhs.lhs.lhs.identifier.identifier, 'c') self.assertEquals(expression.lhs.rhs.lhs.lhs.operator, '+') self.assertEquals(expression.lhs.rhs.lhs.lhs.rhs.identifier.identifier, 'd') self.assertEquals(expression.lhs.rhs.lhs.operator, '*') self.assertEquals(expression.lhs.rhs.lhs.rhs.identifier.identifier, 'e') self.assertEquals(expression.lhs.rhs.operator, '*') self.assertEquals(expression.lhs.rhs.rhs.identifier.identifier, 'f') self.assertEquals(expression.rhs.identifier.identifier, 'g')