def test_grammar(self):
     r1 = t.Rule("foo", t.Exactly("x"))
     r2 = t.Rule("baz", t.Exactly("y"))
     x = t.Grammar("BuilderTest", False, [r1, r2])
     g = writeBytecodeGrammar(x)
     self.assertEqual(sorted(g.keys()), ['baz', 'foo'])
     self.assertEqual(g['foo'], [t.Match('x')])
     self.assertEqual(g['baz'], [t.Match('y')])
示例#2
0
 def test_markAsTree(self):
     """
     Grammars containing list patterns are marked as taking
     tree-shaped input rather than character streams.
     """
     x = t.Rule("foo", t.List(t.Exactly("x")))
     g = t.Grammar("TestGrammar", True, [x])
     self.assert_("\n        tree = True\n" in writePython(g, ""))
示例#3
0
    def test_grammar(self):
        """
        Test generation of an entire grammar.
        """
        r1 = t.Rule("foo", t.Exactly("x"))
        r2 = t.Rule("baz", t.Exactly("y"))
        x = t.Grammar("BuilderTest", False, [r1, r2])
        self.assertEqual(
            writePython(x, ""),
            dd("""
               def createParserClass(GrammarBase, ruleGlobals):
                   if ruleGlobals is None:
                       ruleGlobals = {}
                   class BuilderTest(GrammarBase):
                       def rule_foo(self):
                           _locals = {'self': self}
                           self.locals['foo'] = _locals
                           _G_exactly_1, lastError = self.exactly('x')
                           self.considerError(lastError, 'foo')
                           return (_G_exactly_1, self.currentError)


                       def rule_baz(self):
                           _locals = {'self': self}
                           self.locals['baz'] = _locals
                           _G_exactly_2, lastError = self.exactly('y')
                           self.considerError(lastError, 'baz')
                           return (_G_exactly_2, self.currentError)


                   if BuilderTest.globals is not None:
                       BuilderTest.globals = BuilderTest.globals.copy()
                       BuilderTest.globals.update(ruleGlobals)
                   else:
                       BuilderTest.globals = ruleGlobals
                   return BuilderTest
                            """))