예제 #1
0
 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')
     ])
예제 #3
0
 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)
예제 #4
0
 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")])
예제 #5
0
 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)])
예제 #6
0
 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()])
예제 #7
0
 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)
예제 #8
0
 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)
예제 #9
0
 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)])
예제 #10
0
 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)
예제 #11
0
 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)
예제 #12
0
 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)