def generate_Repeat(self, out, min, max, expr, debugname=None): out.emit(t.Python(str(min.data))) out.emit(t.Push()) out.emit(t.Python(str(max.data))) out.emit(t.Push()) L = out.emit(t.RepeatChoice()) self._generateNode(out, expr, debugname) L2 = out.emit(t.Commit()) out.patchNext(L) out.backpatch(L2, L)
def test_superApply(self): one = t.Action("1") x = t.Action("x") a = t.Apply("super", "main", [one, x]) self.assertEqual(writeBytecode(a), [t.Python('1'), t.Push(), t.Python('x'), t.Push(), t.SuperCall('main')])
def test_foreignApply(self): one = t.Action("1") x = t.Action("x") a = t.ForeignApply("thegrammar", "foo", "main", [one, x]) self.assertEqual(writeBytecode(a), [t.Python('1'), t.Push(), t.Python('x'), t.Push(), t.ForeignCall('thegrammar', 'foo')])
def test_repeat(self): x = t.Repeat(3, 4, t.Exactly('x')) self.assertEqual(writeBytecode(x), [t.Python("3"), t.Push(), t.Python("4"), t.Push(), t.RepeatChoice(3), t.Match('x'), t.Commit(-2)])
def test_apply(self): one = t.Action("1") x = t.Action("x") a = t.Apply("foo", "main", [one, x]) self.assertEqual(writeBytecode(a), [t.Python('1'), t.Push(), t.Python('x'), t.Push(), t.Call('foo')])
def test_optional(self): x = t.Optional(t.Exactly("x")) self.assertEqual(writeBytecode(x), [t.Choice(3), t.Match('x'), t.Commit(2), t.Python("None")])
def generate_Optional(self, out, expr, debugname=None): """ Try to parse an expr and continue if it fails. """ L = out.emit(t.Choice()) self._generateNode(out, expr, debugname) L2 = out.emit(t.Commit()) out.emit(t.Python("None"), label=L) out.patchNext(L2)
def _generateNode(self, out, node, debugname=None): name = node.tag.name args = node.args if node.data is not None: out.emit(t.Literal(node.data)) return if name == 'null': out.emit(t.Python("None")) return getattr(self, "generate_" + name)(out, *args, debugname=debugname)
def test_pred(self): x = t.Predicate(t.Action("doStuff()")) self.assertEqual( writeBytecode(x), [t.Python('doStuff()'), t.Predicate()])
def generate_Python(self, out, expr, debugname=None): out.emit(t.Python(expr.data))