1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
|
from unittest import TestCase
from terml.nodes import termMaker as t
from ometa.vm_builder import writeBytecode, writeBytecodeRule, writeBytecodeGrammar
class TestVMBuilder(TestCase):
def test_exactly(self):
x = t.Exactly("a")
self.assertEqual(writeBytecode(x),
[t.Match("a")])
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_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_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_many(self):
xs = t.Many(t.Exactly("x"))
self.assertEqual(writeBytecode(xs),
[t.Choice(3),
t.Match("x"),
t.Commit(-2)])
# self.assertEqual(writeBytecode(xs),
# [t.Choice(3),
# t.Match("x"),
# t.PartialCommit(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)])
# self.assertEqual(writeBytecode(xs),
# [t.Match('x'),
# t.Choice(4),
# t.Match('x'),
# t.PartialCommit(1)])
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_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_singleOr(self):
x1 = t.Or([t.Exactly("x")])
x = t.Exactly("x")
self.assertEqual(writeBytecode(x1),
writeBytecode(x))
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()])
# self.assertEqual(writeBytecode(x),
# [t.Choice(3),
# t.Match('x'),
# t.FailTwice()])
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()])
# self.assertEqual(writeBytecode(x),
# [t.Choice(5),
# t.Choice(2),
# t.Match('x'),
# t.Commit(1),
# t.Fail()])
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_bind(self):
x = t.Exactly("x")
b = t.Bind("var", x)
self.assertEqual(writeBytecode(b),
[t.Match('x'),
t.Bind('var')])
def test_bind_apply(self):
x = t.Apply("members", "object", [])
b = t.Bind("m", x)
self.assertEqual(writeBytecode(b),
[t.Call('members'),
t.Bind('m')])
def test_pred(self):
x = t.Predicate(t.Action("doStuff()"))
self.assertEqual(writeBytecode(x),
[t.Python('doStuff()'),
t.Predicate()])
def test_listpattern(self):
x = t.List(t.Exactly("x"))
self.assertEqual(writeBytecode(x),
[t.Descend(),
t.Match('x'),
t.Ascend()])
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_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_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_consumedby(self):
x = t.ConsumedBy(t.Exactly('x'))
self.assertEqual(writeBytecode(x),
[t.StartSlice(),
t.Match('x'),
t.EndSlice()])
|