Example #1
0
    def test_char_range(self):
        ctx = ahp.ParseCtx()
        ctx.text = 'foobar'

        prod = ahp.CharRange('a', 'z')
        self.assertTrue(prod.evaluate(ctx))
        self.assertPeek(ctx, 'o', (0, 1))

        prod = ahp.CharRange('A', 'Z')
        self.assertFalse(prod.evaluate(ctx))
        self.assertPeek(ctx, 'o', (0, 1))
Example #2
0
    def test_decl_ref(self):
        ctx = ahp.ParseCtx()
        ctx.text = '$12345 $67890'

        ctx.add_decl(ahp.Decl('digits', ahp.OneOrMore(ahp.CharRange('0',
                                                                    '9'))))
        ctx.add_decl(ahp.Decl('money', ahp.Literal('$'),
                              ahp.DeclRef('digits')))
        prod = ahp.DeclRef('money')

        result = prod.evaluate(ctx)
        self.assertTrue(result)
        self.assertPeek(ctx, ' ', (0, 6))
        self.assertAst(result, [
            ahp.AstNode('money', '$12345',
                        (0, 0), ahp.AstNode('digits', '12345', (0, 1)))
        ])

        ctx.next()
        result = prod.evaluate(ctx)
        self.assertTrue(result)
        self.assertPeek(ctx, '', (0, 13))
        self.assertAst(result, [
            ahp.AstNode('money', '$67890',
                        (0, 7), ahp.AstNode('digits', '67890', (0, 8)))
        ])
Example #3
0
    def test_one_or_more(self):
        ctx = ahp.ParseCtx()
        ctx.text = '12345.67890'

        prod = ahp.OneOrMore(ahp.CharRange('0', '9'))
        self.assertTrue(prod.evaluate(ctx))
        self.assertPeek(ctx, '.', (0, 5))
        self.assertFalse(prod.evaluate(ctx))
        self.assertPeek(ctx, '.', (0, 5))
Example #4
0
    def test_zero_or_more(self):
        ctx = ahp.ParseCtx()
        ctx.text = '$12345.67890'

        prod = ahp.ZeroOrMore(ahp.CharRange('0', '9'))
        self.assertTrue(prod.evaluate(ctx))
        self.assertPeek(ctx, '$', (0, 0))
        ctx.next()
        self.assertTrue(prod.evaluate(ctx))
        self.assertPeek(ctx, '.', (0, 6))
Example #5
0
    def test_unreported_decl(self):
        ctx = ahp.ParseCtx()
        ctx.text = '$12345 $67890'

        ctx.add_decl(ahp.Decl('digits', ahp.OneOrMore(ahp.CharRange('0',
                                                                    '9'))))
        ctx.add_decl(
            ahp.UnreportedDecl('money', ahp.Literal('$'),
                               ahp.DeclRef('digits')))
        prod = ctx.get_decl('money')

        result = prod.evaluate(ctx)
        self.assertTrue(result)
        self.assertPeek(ctx, ' ', (0, 6))
        self.assertAst(result, [])

        ctx.next()
        result = prod.evaluate(ctx)
        self.assertTrue(result)
        self.assertPeek(ctx, '', (0, 13))
        self.assertAst(result, [])