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_sequence(self): x = t.Exactly("x") y = t.Exactly("y") z = t.And([x, y]) self.assertEqual(writeBytecode(z), [t.Match('x'), t.Match('y')])
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')])
def test_doubleOr(self): xy = t.Or([t.Exactly("x"), t.Exactly("y")]) self.assertEqual(writeBytecode(xy), [t.Choice(3), t.Match('x'), t.Commit(2), t.Match('y')])
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_not(self): x = t.Not(t.Exactly("x")) self.assertEqual(writeBytecode(x), [t.Choice(4), t.Match('x'), t.Commit(1), t.Fail()])
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_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_exactly(self): x = t.Exactly("a") self.assertEqual(writeBytecode(x), [t.Match("a")])
def test_consumedby(self): x = t.ConsumedBy(t.Exactly('x')) self.assertEqual( writeBytecode(x), [t.StartSlice(), t.Match('x'), t.EndSlice()])
def test_rule(self): x = t.Rule("foo", t.Exactly("x")) k, v = writeBytecodeRule(x) self.assertEqual(k, "foo") self.assertEqual(v, [t.Match('x')])
def test_listpattern(self): x = t.List(t.Exactly("x")) self.assertEqual( writeBytecode(x), [t.Descend(), t.Match('x'), t.Ascend()])
def test_bind(self): x = t.Exactly("x") b = t.Bind("var", x) self.assertEqual(writeBytecode(b), [t.Match('x'), t.Bind('var')])
def generate_Exactly(self, out, literal, debugname=None): out.emit(t.Match(literal.data))