Пример #1
0
    def test_basic_b(self):
        class TestParser(parsing.Glr):
            def __init__(self, spec):
                parsing.Glr.__init__(self, spec)

        from parsing.tests.specs import b
        spec = parsing.Spec(b, skinny=False)

        parser = TestParser(spec)
        parser.token(b.id(parser))
        parser.token(b.star(parser))
        parser.token(b.id(parser))
        parser.token(b.plus(parser))
        parser.token(b.id(parser))
        parser.eoi()
        self.assertEqual(len(parser.start), 1)
        self.assertEqual(parser.start[0].val, '[[ID * ID] + ID]')

        parser = TestParser(spec)
        parser.token(b.id(parser))
        parser.token(b.plus(parser))
        parser.token(b.id(parser))
        parser.token(b.star(parser))
        parser.token(b.id(parser))
        parser.eoi()
        self.assertEqual(len(parser.start), 1)
        self.assertEqual(parser.start[0].val, '[ID + [ID * ID]]')

        parser = TestParser(spec)
        parser.token(b.id(parser))
        parser.token(b.star(parser))
        parser.token(b.lparen(parser))
        parser.token(b.id(parser))
        parser.token(b.plus(parser))
        parser.token(b.id(parser))
        parser.token(b.rparen(parser))
        parser.eoi()
        self.assertEqual(len(parser.start), 1)
        self.assertEqual(parser.start[0].val, '[ID * ([ID + ID])]')
Пример #2
0
    def test_basic_pickle(self):
        class TestGlrParser(parsing.Glr):
            def __init__(self, spec):
                parsing.Glr.__init__(self, spec)

        from parsing.tests.specs import b

        spec = parsing.Spec(b, skinny=False)
        import six.moves.cPickle
        specPickle = six.moves.cPickle.dumps(spec)
        spec2 = six.moves.cPickle.loads(specPickle)

        parser = TestGlrParser(spec2)
        parser.token(b.id(parser))
        parser.token(b.star(parser))
        parser.token(b.id(parser))
        parser.token(b.plus(parser))
        parser.token(b.id(parser))
        parser.eoi()
        self.assertEqual(len(parser.start), 1)
        self.assertEqual(parser.start[0].val, '[[ID * ID] + ID]')