def test_super(self): """ Rules can call the implementation in a superclass. """ from pymeta3.grammar import OMeta grammar1 = "expr = letter" TestGrammar1 = OMeta.makeGrammar(grammar1, {}) grammar2 = "expr = super | digit" TestGrammar2 = TestGrammar1.makeGrammar(grammar2, {}) self.assertEqual(TestGrammar2("x").apply("expr")[0], "x") self.assertEqual(TestGrammar2("3").apply("expr")[0], "3")
def test_makeGrammar(self): #imported here to prevent OMetaGrammar from being constructed before #tests are run from pymeta3.grammar import OMeta results = [] grammar = dedent(""" digit = :x ?('0' <= x <= '9') -> int(x) num = (num:n digit:d !(results.append(True)) -> n * 10 + d | digit) """) TestGrammar = OMeta.makeGrammar(grammar, {'results': results}) g = TestGrammar("314159") self.assertEqual(g.apply("num")[0], 314159) self.assertNotEqual(len(results), 0)
def test_subclassing(self): """ A subclass of an OMeta subclass should be able to call rules on its parent. """ from pymeta3.grammar import OMeta grammar1 = dedent(""" dig = :x ?('0' <= x <= '9') -> int(x) """) TestGrammar1 = OMeta.makeGrammar(grammar1, {}) grammar2 = dedent(""" num = (num:n dig:d -> n * 10 + d | dig) """) TestGrammar2 = TestGrammar1.makeGrammar(grammar2, {}) g = TestGrammar2("314159") self.assertEqual(g.apply("num")[0], 314159)