def generate_Lookahead(self, out, expr, debugname=None): L1 = out.emit(t.Choice()) L2 = out.emit(t.Choice()) self._generateNode(out, expr) L3 = out.emit(t.Commit(), label=L2) out.emit(t.Fail(), label=L3) out.patchNext(L1)
def test_lookahead(self): x = t.Lookahead(t.Exactly("x")) self.assertEqual(writeBytecode(x), [t.Choice(7), t.Choice(4), t.Match('x'), t.Commit(1), t.Fail(), t.Commit(1), t.Fail()])
def test_tripleOr(self): xy = t.Or([t.Exactly("x"), t.Exactly("y"), t.Exactly("z")]) self.assertEqual(writeBytecode(xy), [ t.Choice(3), t.Match('x'), t.Commit(5), t.Choice(3), t.Match('y'), t.Commit(2), t.Match('z') ])
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 test_many1(self): xs = t.Many1(t.Exactly("x")) self.assertEqual(writeBytecode(xs), [t.Match('x'), t.Choice(3), t.Match('x'), t.Commit(-2)])
def test_not(self): x = t.Not(t.Exactly("x")) self.assertEqual(writeBytecode(x), [t.Choice(4), t.Match('x'), t.Commit(1), t.Fail()])
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 generate_Or(self, out, exprs, debugname=None): if len(exprs.args) == 1: self._generateNode(out, exprs.args[0]) return L = None lcs = [] for ex in exprs.args[:-1]: L = out.emit(t.Choice(), label=L) self._generateNode(out, ex) lcs.append(out.emit(t.Commit())) out.patchNext(L) self._generateNode(out, exprs.args[-1]) for LC in lcs: out.patchNext(LC)
def generate_Not(self, out, expr, debugname=None): L1 = out.emit(t.Choice()) self._generateNode(out, expr) L2 = out.emit(t.Commit()) out.emit(t.Fail(), label=L1) out.patchNext(L2)
def generate_Many(self, out, expr, debugname=None): L = out.emit(t.Choice()) self._generateNode(out, expr, debugname) L2 = out.emit(t.Commit()) out.patchNext(L) out.backpatch(L2, L)