def test_closed_interval_start(self): self.check_parser('1 in [2..3]', AST.In(AST.Number(1), AST.PositiveUnaryTests([AST.Interval(AST.ClosedIntervalStart(), AST.Endpoint(AST.Number(2)), AST.Endpoint(AST.Number(3)), AST.ClosedIntervalEnd())])))
def test_simple_positive_unary_test(self): self.check_parser('1 in <2', AST.In(AST.Number(1), AST.PositiveUnaryTests([AST.LtEp(AST.Endpoint(AST.Number(2)))]))) self.check_parser('1 in <=2', AST.In(AST.Number(1), AST.PositiveUnaryTests([AST.LteEp(AST.Endpoint(AST.Number(2)))]))) self.check_parser('1 in >2', AST.In(AST.Number(1), AST.PositiveUnaryTests([AST.GtEp(AST.Endpoint(AST.Number(2)))]))) self.check_parser('1 in >=2', AST.In(AST.Number(1), AST.PositiveUnaryTests([AST.GteEp(AST.Endpoint(AST.Number(2)))])))
def test_arithmetic_expression(self): self.check_parser('%s**true' % DATE_STR, AST.Exp(DATE_AST, AST.Boolean(True))) self.check_parser('%s **true' % NEGATION_STR, AST.Exp(NEGATION_AST, AST.Boolean(True))) self.check_parser('%s /true **%s' % (NEGATION_STR, NAME_STR), AST.Div(NEGATION_AST, AST.Exp(AST.Boolean(True), NAME_AST))) self.check_parser('%s *true **%s' % (NEGATION_STR, NAME_STR), AST.Mul(NEGATION_AST, AST.Exp(AST.Boolean(True), NAME_AST))) self.check_parser('%s +1.2 /true **%s' % (NEGATION_STR, NAME_STR), AST.Sum(NEGATION_AST, AST.Div(AST.Number(1.2), AST.Exp(AST.Boolean(True), NAME_AST)))) self.check_parser(ARITHMETIC_EXPRESSION_STR, ARITHMETIC_EXPRESSION_AST)
def test_numeric_literal(self): self.check_parser('1', AST.Number(1)) self.check_parser('1.42', AST.Number(1.42)) self.check_parser('1 . 42', AST.Number(1.42)) self.check_parser('.42', AST.Number(.42)) self.check_parser('. 42', AST.Number(.42)) self.check_parser('42', AST.Number(42))
def test_open_interval_end(self): self.check_parser('1 in ]2..3)', AST.In(AST.Number(1), AST.PositiveUnaryTests([AST.Interval(AST.OpenIntervalStart(), AST.Endpoint(AST.Number(2)), AST.Endpoint(AST.Number(3)), AST.OpenIntervalEnd())]))) self.check_parser('1 in ]2..3[', AST.In(AST.Number(1), AST.PositiveUnaryTests([AST.Interval(AST.OpenIntervalStart(), AST.Endpoint(AST.Number(2)), AST.Endpoint(AST.Number(3)), AST.OpenIntervalEnd())])))
def test_arithmetic_negation(self): self.check_parser(NEGATION_STR, NEGATION_AST) self.check_parser('--2', AST.Negation(AST.Negation(AST.Number(2)))) self.check_parser('- -2', AST.Negation(AST.Negation(AST.Number(2)))) self.check_parser('- -2', AST.Negation(AST.Negation(AST.Number(2))))
IF_AST = AST.If(QUANTIFIED_AST, DISJUNCTION_AST, CONJUNCTION_AST) FOR_STR = 'for ala in %s return %s' % (IF_STR, QUANTIFIED_STR) FOR_AST = AST.For([(AST.Name('ala'), IF_AST)], QUANTIFIED_AST) PATH_STR = '%s.ala' % DATE_STR PATH_AST = AST.Path(DATE_AST, AST.Name('ala')) INVOCATION_STR = '%s(%s, %s)' % (PATH_STR, FOR_STR, IF_STR) INVOCATION_AST = AST.Invocation(PATH_AST, [FOR_AST, IF_AST]) NEGATION_STR = '-?A_zÊÝĄͼൺ⁶Ⰲ〠邏2𐎅' NEGATION_AST = AST.Negation(AST.Name('?A_zÊÝĄͼൺ⁶Ⰲ〠邏2𐎅')) ARITHMETIC_EXPRESSION_STR = '1.2-%s /true **%s' % (NEGATION_STR, NAME_STR) ARITHMETIC_EXPRESSION_AST = AST.Dif(AST.Number(1.2), AST.Div(NEGATION_AST, AST.Exp(AST.Boolean(True), NAME_AST))) NOT_STR = 'not (null)' NOT_AST = AST.Not(AST.PositiveUnaryTests([AST.Null()])) POSITIVE_UNARY_TESTS_STR = '%s in (null, null)' % ARITHMETIC_EXPRESSION_STR POSITIVE_UNARY_TESTS_AST = AST.In(ARITHMETIC_EXPRESSION_AST, AST.PositiveUnaryTests([AST.Null()] * 2)) POSITIVE_UNARY_TEST_STR = '%s in (null, %s)' % (POSITIVE_UNARY_TESTS_STR, DATE_STR) POSITIVE_UNARY_TEST_AST = AST.In(POSITIVE_UNARY_TESTS_AST, AST.PositiveUnaryTests([AST.Null(), AST.Endpoint(DATE_AST)])) SIMPLE_POSITIVE_UNARY_TESTS_STR = 'ala, "ala"' SIMPLE_POSITIVE_UNARY_TESTS_AST = AST.PositiveUnaryTests([AST.Endpoint([AST.Name('ala')]), AST.Endpoint(AST.StringLiteral('ala'))])
def p_numeric_literal(self, p): """numeric_literal : NUMERIC_LITERAL""" p[0] = AST.Number(p[1])