Merge branch 'python-bindings' of https://github.com/thequux/hammer into python-bindings
Conflicts: src/bindings/python/SConscript src/bindings/python/hammer_tests.py src/bindings/swig/hammer.i src/hammer.h
This commit is contained in:
commit
2ee82f3ac7
33 changed files with 822 additions and 328 deletions
|
|
@ -18,6 +18,6 @@ libhammer_python = pythonenv.SharedLibrary('hammer', swig, SHLIBPREFIX='_')
|
|||
|
||||
pytestenv = pythonenv.Clone()
|
||||
pytestenv['ENV']['LD_LIBRARY_PATH'] = os.path.dirname(str(libhammer_shared[0]))
|
||||
pytestenv.Command(None, ['hammer_tests.py', libhammer_python], "nosetests -vv $SOURCE")
|
||||
pytestenv.Command(None, ['hammer_tests.py'] + libhammer_python, "nosetests -vv $SOURCE")
|
||||
|
||||
Clean('.', ['hammer.pyc', 'hammer_tests.py', 'hammer_tests.pyc'])
|
||||
|
|
|
|||
|
|
@ -4,462 +4,461 @@ import hammer as h
|
|||
class TestTokenParser(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.parser = h.h_token("95\xa2", 3)
|
||||
cls.parser = h.token("95\xa2")
|
||||
def test_success(self):
|
||||
self.assertEqual(h.h_parse(self.parser, "95\xa2", 3).ast.token_data.bytes, "95\xa2")
|
||||
self.assertEqual(self.parser.parse("95\xa2"), "95\xa2")
|
||||
def test_partial_fails(self):
|
||||
self.assertEqual(h.h_parse(self.parser, "95", 2), None)
|
||||
self.assertEqual(self.parser.parse("95"), None)
|
||||
|
||||
class TestChParser(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.parser_int = h.h_ch(0xa2)
|
||||
cls.parser_chr = h.h_ch("\xa2")
|
||||
cls.parser_int = h.ch(0xa2)
|
||||
cls.parser_chr = h.ch("\xa2")
|
||||
def test_success(self):
|
||||
self.assertEqual(h.h_parse(self.parser_int, "\xa2", 1).ast.token_data.uint, 0xa2)
|
||||
self.assertEqual(h.h_parse(self.parser_chr, "\xa2", 1).ast.token_data.uint, ord("\xa2"))
|
||||
self.assertEqual(self.parser_int.parse("\xa2"), 0xa2)
|
||||
self.assertEqual(self.parser_chr.parse("\xa2"), "\xa2")
|
||||
def test_failure(self):
|
||||
self.assertEqual(h.h_parse(self.parser_int, "\xa3", 1), None)
|
||||
self.assertEqual(h.h_parse(self.parser_chr, "\xa3", 1), None)
|
||||
self.assertEqual(self.parser_int.parse("\xa3"), None)
|
||||
self.assertEqual(self.parser_chr.parse("\xa3"), None)
|
||||
|
||||
class TestChRange(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.parser = h.h_ch_range("a", "c")
|
||||
cls.parser = h.ch_range("a", "c")
|
||||
def test_success(self):
|
||||
self.assertEqual(h.h_parse(self.parser, "b", 1).ast.token_data.uint, ord("b"))
|
||||
self.assertEqual(self.parser.parse("b"), "b")
|
||||
def test_failure(self):
|
||||
self.assertEqual(h.h_parse(self.parser, "d", 1), None)
|
||||
self.assertEqual(self.parser.parse("d"), None)
|
||||
|
||||
class TestInt64(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.parser = h.h_int64()
|
||||
cls.parser = h.int64()
|
||||
def test_success(self):
|
||||
self.assertEqual(h.h_parse(self.parser, "\xff\xff\xff\xfe\x00\x00\x00\x00", 8).ast.token_data.sint, -0x200000000)
|
||||
self.assertEqual(self.parser.parse("\xff\xff\xff\xfe\x00\x00\x00\x00"), -0x200000000)
|
||||
def test_failure(self):
|
||||
self.assertEqual(h.h_parse(self.parser, "\xff\xff\xff\xfe\x00\x00\x00", 7), None)
|
||||
self.assertEqual(self.parser.parse("\xff\xff\xff\xfe\x00\x00\x00"), None)
|
||||
|
||||
class TestInt32(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.parser = h.h_int32()
|
||||
cls.parser = h.int32()
|
||||
def test_success(self):
|
||||
self.assertEqual(h.h_parse(self.parser, "\xff\xfe\x00\x00", 4).ast.token_data.sint, -0x20000)
|
||||
self.assertEqual(h.h_parse(self.parser, "\x00\x02\x00\x00", 4).ast.token_data.sint, 0x20000)
|
||||
self.assertEqual(self.parser.parse("\xff\xfe\x00\x00"), -0x20000)
|
||||
self.assertEqual(self.parser.parse("\x00\x02\x00\x00"), 0x20000)
|
||||
def test_failure(self):
|
||||
self.assertEqual(h.h_parse(self.parser, "\xff\xfe\x00", 3), None)
|
||||
self.assertEqual(h.h_parse(self.parser, "\x00\x02\x00", 3), None)
|
||||
self.assertEqual(self.parser.parse("\xff\xfe\x00"), None)
|
||||
self.assertEqual(self.parser.parse("\x00\x02\x00"), None)
|
||||
|
||||
class TestInt16(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.parser = h.h_int16()
|
||||
cls.parser = h.int16()
|
||||
def test_success(self):
|
||||
self.assertEqual(h.h_parse(self.parser, "\xfe\x00", 2).ast.token_data.sint, -0x200)
|
||||
self.assertEqual(h.h_parse(self.parser, "\x02\x00", 2).ast.token_data.sint, 0x200)
|
||||
self.assertEqual(self.parser.parse("\xfe\x00"), -0x200)
|
||||
self.assertEqual(self.parser.parse("\x02\x00"), 0x200)
|
||||
def test_failure(self):
|
||||
self.assertEqual(h.h_parse(self.parser, "\xfe", 1), None)
|
||||
self.assertEqual(h.h_parse(self.parser, "\x02", 1), None)
|
||||
self.assertEqual(self.parser.parse("\xfe"), None)
|
||||
self.assertEqual(self.parser.parse("\x02"), None)
|
||||
|
||||
class TestInt8(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.parser = h.h_int8()
|
||||
cls.parser = h.int8()
|
||||
def test_success(self):
|
||||
self.assertEqual(h.h_parse(self.parser, "\x88", 1).ast.token_data.sint, -0x78)
|
||||
self.assertEqual(self.parser.parse("\x88"), -0x78)
|
||||
def test_failure(self):
|
||||
self.assertEqual(h.h_parse(self.parser, "", 0), None)
|
||||
self.assertEqual(self.parser.parse(""), None)
|
||||
|
||||
class TestUint64(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.parser = h.h_uint64()
|
||||
cls.parser = h.uint64()
|
||||
def test_success(self):
|
||||
self.assertEqual(h.h_parse(self.parser, "\x00\x00\x00\x02\x00\x00\x00\x00", 8).ast.token_data.uint, 0x200000000)
|
||||
self.assertEqual(self.parser.parse("\x00\x00\x00\x02\x00\x00\x00\x00"), 0x200000000)
|
||||
def test_failure(self):
|
||||
self.assertEqual(h.h_parse(self.parser, "\x00\x00\x00\x02\x00\x00\x00", 7), None)
|
||||
self.assertEqual(self.parser.parse("\x00\x00\x00\x02\x00\x00\x00"), None)
|
||||
|
||||
class TestUint32(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.parser = h.h_uint32()
|
||||
cls.parser = h.uint32()
|
||||
def test_success(self):
|
||||
self.assertEqual(h.h_parse(self.parser, "\x00\x02\x00\x00", 4).ast.token_data.uint, 0x20000)
|
||||
self.assertEqual(self.parser.parse("\x00\x02\x00\x00"), 0x20000)
|
||||
def test_failure(self):
|
||||
self.assertEqual(h.h_parse(self.parser, "\x00\x02\x00", 3), None)
|
||||
self.assertEqual(self.parser.parse("\x00\x02\x00"), None)
|
||||
|
||||
class TestUint16(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.parser = h.h_uint16()
|
||||
cls.parser = h.uint16()
|
||||
def test_success(self):
|
||||
self.assertEqual(h.h_parse(self.parser, "\x02\x00", 2).ast.token_data.uint, 0x200)
|
||||
self.assertEqual(self.parser.parse("\x02\x00"), 0x200)
|
||||
def test_failure(self):
|
||||
self.assertEqual(h.h_parse(self.parser, "\x02", 1), None)
|
||||
self.assertEqual(self.parser.parse("\x02"), None)
|
||||
|
||||
class TestUint8(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.parser = h.h_uint8()
|
||||
cls.parser = h.uint8()
|
||||
def test_success(self):
|
||||
self.assertEqual(h.h_parse(self.parser, "\x78", 1).ast.token_data.uint, 0x78)
|
||||
self.assertEqual(self.parser.parse("\x78"), 0x78)
|
||||
def test_failure(self):
|
||||
self.assertEqual(h.h_parse(self.parser, "", 0), None)
|
||||
self.assertEqual(self.parser.parse(""), None)
|
||||
|
||||
class TestIntRange(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.parser = h.h_int_range(h.h_uint8(), 3, 10)
|
||||
cls.parser = h.int_range(h.uint8(), 3, 10)
|
||||
def test_success(self):
|
||||
self.assertEqual(h.h_parse(self.parser, "\x05", 1).ast.token_data.uint, 5)
|
||||
self.assertEqual(self.parser.parse("\x05"), 5)
|
||||
def test_failure(self):
|
||||
self.assertEqual(h.h_parse(self.parser, "\x0b", 1), None)
|
||||
self.assertEqual(self.parser.parse("\x0b"), None)
|
||||
|
||||
class TestWhitespace(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.parser = h.h_whitespace(h.h_ch("a"))
|
||||
cls.parser = h.whitespace(h.ch("a"))
|
||||
def test_success(self):
|
||||
self.assertEqual(h.h_parse(self.parser, "a", 1).ast.token_data.uint, ord("a"))
|
||||
self.assertEqual(h.h_parse(self.parser, " a", 2).ast.token_data.uint, ord("a"))
|
||||
self.assertEqual(h.h_parse(self.parser, " a", 3).ast.token_data.uint, ord("a"))
|
||||
self.assertEqual(h.h_parse(self.parser, "\ta", 2).ast.token_data.uint, ord("a"))
|
||||
self.assertEqual(self.parser.parse("a"), "a")
|
||||
self.assertEqual(self.parser.parse(" a"), "a")
|
||||
self.assertEqual(self.parser.parse(" a"), "a")
|
||||
self.assertEqual(self.parser.parse("\ta"), "a")
|
||||
def test_failure(self):
|
||||
self.assertEqual(h.h_parse(self.parser, "_a", 2), None)
|
||||
self.assertEqual(self.parser.parse("_a"), None)
|
||||
|
||||
class TestWhitespaceEnd(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.parser = h.h_whitespace(h.h_end_p())
|
||||
cls.parser = h.whitespace(h.end_p())
|
||||
def test_success(self):
|
||||
self.assertEqual(h.h_parse(self.parser, "", 0).ast, None) # empty string
|
||||
self.assertEqual(h.h_parse(self.parser, " ", 2).ast, None) # empty string
|
||||
self.assertEqual(self.parser.parse(""), None) # empty string
|
||||
self.assertEqual(self.parser.parse(" "), None) # empty string
|
||||
def test_failure(self):
|
||||
self.assertEqual(h.h_parse(self.parser, " x", 3), None)
|
||||
self.assertEqual(self.parser.parse(" x"), None)
|
||||
|
||||
class TestLeft(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.parser = h.h_left(h.h_ch("a"), h.h_ch(" "))
|
||||
cls.parser = h.left(h.ch("a"), h.ch(" "))
|
||||
def test_success(self):
|
||||
self.assertEqual(h.h_parse(self.parser, "a ", 2).ast.token_data.uint, ord("a"))
|
||||
self.assertEqual(self.parser.parse("a "), "a")
|
||||
def test_failure(self):
|
||||
self.assertEqual(h.h_parse(self.parser, "a", 1), None)
|
||||
self.assertEqual(h.h_parse(self.parser, " ", 1), None)
|
||||
self.assertEqual(h.h_parse(self.parser, "ab", 2), None)
|
||||
self.assertEqual(self.parser.parse("a"), None)
|
||||
self.assertEqual(self.parser.parse(" "), None)
|
||||
self.assertEqual(self.parser.parse("ab"), None)
|
||||
|
||||
class TestRight(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.parser = h.h_right(h.h_ch(" "), h.h_ch("a"))
|
||||
cls.parser = h.right(h.ch(" "), h.ch("a"))
|
||||
def test_success(self):
|
||||
self.assertEqual(h.h_parse(self.parser, " a", 2).ast.token_data.uint, ord("a"))
|
||||
self.assertEqual(self.parser.parse(" a"), "a")
|
||||
def test_failure(self):
|
||||
self.assertEqual(h.h_parse(self.parser, "a", 1), None)
|
||||
self.assertEqual(h.h_parse(self.parser, " ", 1), None)
|
||||
self.assertEqual(h.h_parse(self.parser, "ba", 2), None)
|
||||
self.assertEqual(self.parser.parse("a"), None)
|
||||
self.assertEqual(self.parser.parse(" "), None)
|
||||
self.assertEqual(self.parser.parse("ba"), None)
|
||||
|
||||
class TestMiddle(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.parser = h.h_middle(h.h_ch(" "), h.h_ch("a"), h.h_ch(" "))
|
||||
cls.parser = h.middle(h.ch(" "), h.ch("a"), h.ch(" "))
|
||||
def test_success(self):
|
||||
self.assertEqual(h.h_parse(self.parser, " a ", 3).ast.token_data.uint, ord("a"))
|
||||
self.assertEqual(self.parser.parse(" a "), "a")
|
||||
def test_failure(self):
|
||||
self.assertEqual(h.h_parse(self.parser, "a", 1), None)
|
||||
self.assertEqual(h.h_parse(self.parser, " ", 1), None)
|
||||
self.assertEqual(h.h_parse(self.parser, " a", 2), None)
|
||||
self.assertEqual(h.h_parse(self.parser, "a ", 2), None)
|
||||
self.assertEqual(h.h_parse(self.parser, " b ", 3), None)
|
||||
self.assertEqual(h.h_parse(self.parser, "ba ", 3), None)
|
||||
self.assertEqual(h.h_parse(self.parser, " ab", 3), None)
|
||||
self.assertEqual(self.parser.parse("a"), None)
|
||||
self.assertEqual(self.parser.parse(" "), None)
|
||||
self.assertEqual(self.parser.parse(" a"), None)
|
||||
self.assertEqual(self.parser.parse("a "), None)
|
||||
self.assertEqual(self.parser.parse(" b "), None)
|
||||
self.assertEqual(self.parser.parse("ba "), None)
|
||||
self.assertEqual(self.parser.parse(" ab"), None)
|
||||
|
||||
# class TestAction(unittest.TestCase):
|
||||
# @classmethod
|
||||
# def setUpClass(cls):
|
||||
# cls.parser = h.h_action(h.h_sequence__a([h.h_choice__a([h.h_ch("a"), h.h_ch("A"), None]), h.h_choice__a([h.h_ch("b"), h.h_ch("B"), None]), None]), lambda x: [y.upper() for y in x])
|
||||
# def test_success(self):
|
||||
# self.assertEqual(h.h_parse(self.parser, "ab", 2).ast.token_data.seq, ["A", "B"])
|
||||
# self.assertEqual(h.h_parse(self.parser, "AB", 2).ast.token_data.seq, ["A", "B"])
|
||||
# def test_failure(self):
|
||||
# self.assertEqual(h.h_parse(self.parser, "XX", 2), None)
|
||||
class TestAction(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.parser = h.action(h.sequence(h.choice(h.ch("a"), h.ch("A")),
|
||||
h.choice(h.ch("b"), h.ch("B"))),
|
||||
lambda x: [y.upper() for y in x])
|
||||
def test_success(self):
|
||||
self.assertEqual(self.parser.parse("ab"), ["A", "B"])
|
||||
self.assertEqual(self.parser.parse("AB"), ["A", "B"])
|
||||
def test_failure(self):
|
||||
self.assertEqual(self.parser.parse("XX"), None)
|
||||
|
||||
class TestIn(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.parser = h.h_in("abc", 3)
|
||||
cls.parser = h.in_("abc")
|
||||
def test_success(self):
|
||||
self.assertEqual(h.h_parse(self.parser, "b", 1).ast.token_data.uint, ord("b"))
|
||||
self.assertEqual(self.parser.parse("b"), "b")
|
||||
def test_failure(self):
|
||||
self.assertEqual(h.h_parse(self.parser, "d", 1), None)
|
||||
self.assertEqual(self.parser.parse("d"), None)
|
||||
|
||||
class TestNotIn(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.parser = h.h_not_in("abc", 3)
|
||||
cls.parser = h.not_in("abc")
|
||||
def test_success(self):
|
||||
self.assertEqual(h.h_parse(self.parser, "d", 1).ast.token_data.uint, ord("d"))
|
||||
self.assertEqual(self.parser.parse("d"), "d")
|
||||
def test_failure(self):
|
||||
self.assertEqual(h.h_parse(self.parser, "a", 1), None)
|
||||
self.assertEqual(self.parser.parse("a"), None)
|
||||
|
||||
class TestEndP(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.parser = h.h_sequence__a([h.h_ch("a"), h.h_end_p(), None])
|
||||
cls.parser = h.sequence(h.ch("a"), h.end_p())
|
||||
def test_success(self):
|
||||
self.assertEqual([x.token_data.uint for x in h.h_parse(self.parser, "a", 1).ast.token_data.seq], [ord(y) for y in ["a"]])
|
||||
self.assertEqual(self.parser.parse("a"), ("a",))
|
||||
def test_failure(self):
|
||||
self.assertEqual(h.h_parse(self.parser, "aa", 2), None)
|
||||
self.assertEqual(self.parser.parse("aa"), None)
|
||||
|
||||
class TestNothingP(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.parser = h.h_nothing_p()
|
||||
cls.parser = h.nothing_p()
|
||||
def test_success(self):
|
||||
pass
|
||||
def test_failure(self):
|
||||
self.assertEqual(h.h_parse(self.parser, "a", 1), None)
|
||||
self.assertEqual(self.parser.parse("a"), None)
|
||||
|
||||
class TestSequence(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.parser = h.h_sequence__a([h.h_ch("a"), h.h_ch("b"), None])
|
||||
cls.parser = h.sequence(h.ch("a"), h.ch("b"))
|
||||
def test_success(self):
|
||||
self.assertEqual([x.token_data.uint for x in h.h_parse(self.parser, "ab", 2).ast.token_data.seq], [ord(y) for y in ["a", "b"]])
|
||||
self.assertEqual(self.parser.parse("ab"), ('a','b'))
|
||||
def test_failure(self):
|
||||
self.assertEqual(h.h_parse(self.parser, "a", 1), None)
|
||||
self.assertEqual(h.h_parse(self.parser, "b", 1), None)
|
||||
self.assertEqual(self.parser.parse("a"), None)
|
||||
self.assertEqual(self.parser.parse("b"), None)
|
||||
|
||||
class TestSequenceWhitespace(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.parser = h.h_sequence__a([h.h_ch("a"), h.h_whitespace(h.h_ch("b")), None])
|
||||
cls.parser = h.sequence(h.ch("a"), h.whitespace(h.ch("b")))
|
||||
def test_success(self):
|
||||
self.assertEqual([x.token_data.uint for x in h.h_parse(self.parser, "ab", 2).ast.token_data.seq], [ord(y) for y in ["a", "b"]])
|
||||
self.assertEqual([x.token_data.uint for x in h.h_parse(self.parser, "a b", 3).ast.token_data.seq], [ord(y) for y in ["a", "b"]])
|
||||
self.assertEqual([x.token_data.uint for x in h.h_parse(self.parser, "a b", 4).ast.token_data.seq], [ord(y) for y in ["a", "b"]])
|
||||
self.assertEqual(self.parser.parse("ab"), ('a','b'))
|
||||
self.assertEqual(self.parser.parse("a b"), ('a','b'))
|
||||
self.assertEqual(self.parser.parse("a b"), ('a','b'))
|
||||
def test_failure(self):
|
||||
self.assertEqual(h.h_parse(self.parser, "a c", 4), None)
|
||||
self.assertEqual(self.parser.parse("a c"), None)
|
||||
|
||||
class TestChoice(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.parser = h.h_choice__a([h.h_ch("a"), h.h_ch("b"), None])
|
||||
cls.parser = h.choice(h.ch("a"), h.ch("b"))
|
||||
def test_success(self):
|
||||
self.assertEqual(h.h_parse(self.parser, "a", 1).ast.token_data.uint, ord("a"))
|
||||
self.assertEqual(h.h_parse(self.parser, "b", 1).ast.token_data.uint, ord("b"))
|
||||
self.assertEqual(self.parser.parse("a"), "a")
|
||||
self.assertEqual(self.parser.parse("b"), "b")
|
||||
def test_failure(self):
|
||||
self.assertEqual(h.h_parse(self.parser, "c", 1), None)
|
||||
self.assertEqual(self.parser.parse("c"), None)
|
||||
|
||||
class TestButNot(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.parser = h.h_butnot(h.h_ch("a"), h.h_token("ab", 2))
|
||||
cls.parser = h.butnot(h.ch("a"), h.token("ab"))
|
||||
def test_success(self):
|
||||
self.assertEqual(h.h_parse(self.parser, "a", 1).ast.token_data.uint, ord("a"))
|
||||
self.assertEqual(h.h_parse(self.parser, "aa", 2).ast.token_data.uint, ord("a"))
|
||||
self.assertEqual(self.parser.parse("a"), "a")
|
||||
self.assertEqual(self.parser.parse("aa"), "a")
|
||||
def test_failure(self):
|
||||
self.assertEqual(h.h_parse(self.parser, "ab", 2), None)
|
||||
self.assertEqual(self.parser.parse("ab"), None)
|
||||
|
||||
class TestButNotRange(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.parser = h.h_butnot(h.h_ch_range("0", "9"), h.h_ch("6"))
|
||||
cls.parser = h.butnot(h.ch_range("0", "9"), h.ch("6"))
|
||||
def test_success(self):
|
||||
self.assertEqual(h.h_parse(self.parser, "4", 1).ast.token_data.uint, ord("4"))
|
||||
self.assertEqual(self.parser.parse("4"), "4")
|
||||
def test_failure(self):
|
||||
self.assertEqual(h.h_parse(self.parser, "6", 1), None)
|
||||
self.assertEqual(self.parser.parse("6"), None)
|
||||
|
||||
class TestDifference(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.parser = h.h_difference(h.h_token("ab", 2), h.h_ch("a"))
|
||||
cls.parser = h.difference(h.token("ab"), h.ch("a"))
|
||||
def test_success(self):
|
||||
self.assertEqual(h.h_parse(self.parser, "ab", 2).ast.token_data.bytes, "ab")
|
||||
self.assertEqual(self.parser.parse("ab"), "ab")
|
||||
def test_failure(self):
|
||||
self.assertEqual(h.h_parse(self.parser, "a", 1), None)
|
||||
self.assertEqual(self.parser.parse("a"), None)
|
||||
|
||||
class TestXor(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.parser = h.h_xor(h.h_ch_range("0", "6"), h.h_ch_range("5", "9"))
|
||||
cls.parser = h.xor(h.ch_range("0", "6"), h.ch_range("5", "9"))
|
||||
def test_success(self):
|
||||
self.assertEqual(h.h_parse(self.parser, "0", 1).ast.token_data.uint, ord("0"))
|
||||
self.assertEqual(h.h_parse(self.parser, "9", 1).ast.token_data.uint, ord("9"))
|
||||
self.assertEqual(self.parser.parse("0"), "0")
|
||||
self.assertEqual(self.parser.parse("9"), "9")
|
||||
def test_failure(self):
|
||||
self.assertEqual(h.h_parse(self.parser, "5", 1), None)
|
||||
self.assertEqual(h.h_parse(self.parser, "a", 1), None)
|
||||
self.assertEqual(self.parser.parse("5"), None)
|
||||
self.assertEqual(self.parser.parse("a"), None)
|
||||
|
||||
class TestMany(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.parser = h.h_many(h.h_choice__a([h.h_ch("a"), h.h_ch("b"), None]))
|
||||
cls.parser = h.many(h.choice(h.ch("a"), h.ch("b")))
|
||||
def test_success(self):
|
||||
self.assertEqual(h.h_parse(self.parser, "", 0).ast.token_data.seq, [])
|
||||
self.assertEqual([x.token_data.uint for x in h.h_parse(self.parser, "a", 1).ast.token_data.seq], [ord(y) for y in ["a"]])
|
||||
self.assertEqual([x.token_data.uint for x in h.h_parse(self.parser, "b", 1).ast.token_data.seq], [ord(y) for y in ["b"]])
|
||||
self.assertEqual([x.token_data.uint for x in h.h_parse(self.parser, "aabbaba", 7).ast.token_data.seq], [ord(y) for y in ["a", "a", "b", "b", "a", "b", "a"]])
|
||||
self.assertEqual(self.parser.parse(""), ())
|
||||
self.assertEqual(self.parser.parse("a"), ('a',))
|
||||
self.assertEqual(self.parser.parse("b"), ('b',))
|
||||
self.assertEqual(self.parser.parse("aabbaba"), ('a','a','b','b','a','b','a'))
|
||||
def test_failure(self):
|
||||
pass
|
||||
|
||||
class TestMany1(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.parser = h.h_many1(h.h_choice__a([h.h_ch("a"), h.h_ch("b"), None]))
|
||||
cls.parser = h.many1(h.choice(h.ch("a"), h.ch("b")))
|
||||
def test_success(self):
|
||||
self.assertEqual([x.token_data.uint for x in h.h_parse(self.parser, "a", 1).ast.token_data.seq], [ord(y) for y in ["a"]])
|
||||
self.assertEqual([x.token_data.uint for x in h.h_parse(self.parser, "b", 1).ast.token_data.seq], [ord(y) for y in ["b"]])
|
||||
self.assertEqual([x.token_data.uint for x in h.h_parse(self.parser, "aabbaba", 7).ast.token_data.seq], [ord(y) for y in ["a", "a", "b", "b", "a", "b", "a"]])
|
||||
self.assertEqual(self.parser.parse("a"), ("a",))
|
||||
self.assertEqual(self.parser.parse("b"), ("b",))
|
||||
self.assertEqual(self.parser.parse("aabbaba"), ("a", "a", "b", "b", "a", "b", "a"))
|
||||
def test_failure(self):
|
||||
self.assertEqual(h.h_parse(self.parser, "", 0), None)
|
||||
self.assertEqual(h.h_parse(self.parser, "daabbabadef", 11), None)
|
||||
self.assertEqual(self.parser.parse(""), None)
|
||||
self.assertEqual(self.parser.parse("daabbabadef"), None)
|
||||
|
||||
class TestRepeatN(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.parser = h.h_repeat_n(h.h_choice__a([h.h_ch("a"), h.h_ch("b"), None]), 2)
|
||||
cls.parser = h.repeat_n(h.choice(h.ch("a"), h.ch("b")), 2)
|
||||
def test_success(self):
|
||||
self.assertEqual([x.token_data.uint for x in h.h_parse(self.parser, "abdef", 5).ast.token_data.seq], [ord(y) for y in ["a", "b"]])
|
||||
self.assertEqual(self.parser.parse("abdef"), ('a', 'b'))
|
||||
def test_failure(self):
|
||||
self.assertEqual(h.h_parse(self.parser, "adef", 4), None)
|
||||
self.assertEqual(h.h_parse(self.parser, "dabdef", 5), None)
|
||||
self.assertEqual(self.parser.parse("adef"), None)
|
||||
self.assertEqual(self.parser.parse("dabdef"), None)
|
||||
|
||||
class TestOptional(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.parser = h.h_sequence__a([h.h_ch("a"), h.h_optional(h.h_choice__a([h.h_ch("b"), h.h_ch("c"), None])), h.h_ch("d"), None])
|
||||
cls.parser = h.sequence(h.ch("a"), h.optional(h.choice(h.ch("b"), h.ch("c"))), h.ch("d"))
|
||||
def test_success(self):
|
||||
self.assertEqual([x.token_data.uint for x in h.h_parse(self.parser, "abd", 3).ast.token_data.seq], [ord(y) for y in ["a", "b", "d"]])
|
||||
self.assertEqual([x.token_data.uint for x in h.h_parse(self.parser, "acd", 3).ast.token_data.seq], [ord(y) for y in ["a", "c", "d"]])
|
||||
### FIXME check this out in repl, what does tree look like
|
||||
#self.assertEqual([x.token_data.uint for x in h.h_parse(self.parser, "ad", 2).ast.token_data.seq], [ord(y)["a", None, "d"]])
|
||||
self.assertEqual(self.parser.parse("abd"), ('a','b','d'))
|
||||
self.assertEqual(self.parser.parse("acd"), ('a','c','d'))
|
||||
self.assertEqual(self.parser.parse("ad"), ('a',h.Placeholder(), 'd'))
|
||||
def test_failure(self):
|
||||
self.assertEqual(h.h_parse(self.parser, "aed", 3), None)
|
||||
self.assertEqual(h.h_parse(self.parser, "ab", 2), None)
|
||||
self.assertEqual(h.h_parse(self.parser, "ac", 2), None)
|
||||
self.assertEqual(self.parser.parse("aed"), None)
|
||||
self.assertEqual(self.parser.parse("ab"), None)
|
||||
self.assertEqual(self.parser.parse("ac"), None)
|
||||
|
||||
class TestIgnore(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.parser = h.h_sequence__a([h.h_ch("a"), h.h_ignore(h.h_ch("b")), h.h_ch("c"), None])
|
||||
cls.parser = h.sequence(h.ch("a"), h.ignore(h.ch("b")), h.ch("c"))
|
||||
def test_success(self):
|
||||
self.assertEqual([x.token_data.uint for x in h.h_parse(self.parser, "abc", 3).ast.token_data.seq], [ord(y) for y in ["a", "c"]])
|
||||
self.assertEqual(self.parser.parse("abc"), ("a","c"))
|
||||
def test_failure(self):
|
||||
self.assertEqual(h.h_parse(self.parser, "ac", 2), None)
|
||||
self.assertEqual(self.parser.parse("ac"), None)
|
||||
|
||||
class TestSepBy(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.parser = h.h_sepBy(h.h_choice__a([h.h_ch("1"), h.h_ch("2"), h.h_ch("3"), None]), h.h_ch(","))
|
||||
cls.parser = h.sepBy(h.choice(h.ch("1"), h.ch("2"), h.ch("3")), h.ch(","))
|
||||
def test_success(self):
|
||||
self.assertEqual([x.token_data.uint for x in h.h_parse(self.parser, "1,2,3", 5).ast.token_data.seq], [ord(y) for y in ["1", "2", "3"]])
|
||||
self.assertEqual([x.token_data.uint for x in h.h_parse(self.parser, "1,3,2", 5).ast.token_data.seq], [ord(y) for y in ["1", "3", "2"]])
|
||||
self.assertEqual([x.token_data.uint for x in h.h_parse(self.parser, "1,3", 3).ast.token_data.seq], [ord(y) for y in ["1", "3"]])
|
||||
self.assertEqual([x.token_data.uint for x in h.h_parse(self.parser, "3", 1).ast.token_data.seq], [ord(y) for y in ["3"]])
|
||||
self.assertEqual(h.h_parse(self.parser, "", 0).ast.token_data.seq, [])
|
||||
self.assertEqual(self.parser.parse("1,2,3"), ('1','2','3'))
|
||||
self.assertEqual(self.parser.parse("1,3,2"), ('1','3','2'))
|
||||
self.assertEqual(self.parser.parse("1,3"), ('1','3'))
|
||||
self.assertEqual(self.parser.parse("3"), ('3',))
|
||||
self.assertEqual(self.parser.parse(""), ())
|
||||
def test_failure(self):
|
||||
pass
|
||||
|
||||
class TestSepBy1(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.parser = h.h_sepBy1(h.h_choice__a([h.h_ch("1"), h.h_ch("2"), h.h_ch("3"), None]), h.h_ch(","))
|
||||
cls.parser = h.sepBy1(h.choice(h.ch("1"), h.ch("2"), h.ch("3")), h.ch(","))
|
||||
def test_success(self):
|
||||
self.assertEqual([x.token_data.uint for x in h.h_parse(self.parser, "1,2,3", 5).ast.token_data.seq], [ord(y) for y in ["1", "2", "3"]])
|
||||
self.assertEqual([x.token_data.uint for x in h.h_parse(self.parser, "1,3,2", 5).ast.token_data.seq], [ord(y) for y in ["1", "3", "2"]])
|
||||
self.assertEqual([x.token_data.uint for x in h.h_parse(self.parser, "1,3", 3).ast.token_data.seq], [ord(y) for y in ["1", "3"]])
|
||||
self.assertEqual([x.token_data.uint for x in h.h_parse(self.parser, "3", 1).ast.token_data.seq], [ord(y) for y in ["3"]])
|
||||
self.assertEqual(self.parser.parse("1,2,3"), ('1','2','3'))
|
||||
self.assertEqual(self.parser.parse("1,3,2"), ('1','3','2'))
|
||||
self.assertEqual(self.parser.parse("1,3"), ('1','3'))
|
||||
self.assertEqual(self.parser.parse("3"), ('3',))
|
||||
def test_failure(self):
|
||||
self.assertEqual(h.h_parse(self.parser, "", 0), None)
|
||||
self.assertEqual(self.parser.parse(""), None)
|
||||
|
||||
### segfaults
|
||||
class TestEpsilonP1(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.parser = h.h_sequence__a([h.h_ch("a"), h.h_epsilon_p(), h.h_ch("b"), None])
|
||||
cls.parser = h.sequence(h.ch("a"), h.epsilon_p(), h.ch("b"))
|
||||
def test_success(self):
|
||||
self.assertEqual([x.token_data.uint for x in h.h_parse(self.parser, "ab", 2).ast.token_data.seq], [ord(y) for y in ["a", "b"]])
|
||||
self.assertEqual(self.parser.parse("ab"), ("a", "b"))
|
||||
def test_failure(self):
|
||||
pass
|
||||
|
||||
class TestEpsilonP2(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.parser = h.h_sequence__a([h.h_epsilon_p(), h.h_ch("a"), None])
|
||||
cls.parser = h.sequence(h.epsilon_p(), h.ch("a"))
|
||||
def test_success(self):
|
||||
self.assertEqual([x.token_data.uint for x in h.h_parse(self.parser, "a", 1).ast.token_data.seq], [ord(y) for y in ["a"]])
|
||||
self.assertEqual(self.parser.parse("a"), ("a",))
|
||||
def test_failure(self):
|
||||
pass
|
||||
|
||||
class TestEpsilonP3(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.parser = h.h_sequence__a([h.h_ch("a"), h.h_epsilon_p(), None])
|
||||
cls.parser = h.sequence(h.ch("a"), h.epsilon_p())
|
||||
def test_success(self):
|
||||
self.assertEqual([x.token_data.uint for x in h.h_parse(self.parser, "a", 1).ast.token_data.seq], [ord(y) for y in ["a"]])
|
||||
self.assertEqual(self.parser.parse("a"), ("a",))
|
||||
def test_failure(self):
|
||||
pass
|
||||
|
||||
# class TestAttrBool(unittest.TestCase):
|
||||
# @classmethod
|
||||
# def setUpClass(cls):
|
||||
# cls.parser = h.h_attr_bool(h.h_many1(h.h_choice__a([h.h_ch("a"), h.h_ch("b"), None])), lambda x: x[0] == x[1])
|
||||
# def test_success(self):
|
||||
# self.assertEqual(h.h_parse(self.parser, "aa", 2).ast.token_data.seq, ["a", "a"])
|
||||
# self.assertEqual(h.h_parse(self.parser, "bb", 2).ast.token_data.seq, ["b", "b"])
|
||||
# def test_failure(self):
|
||||
# self.assertEqual(h.h_parse(self.parser, "ab", 2), None)
|
||||
class TestAttrBool(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.parser = h.attr_bool(h.many1(h.choice(h.ch("a"), h.ch("b"))),
|
||||
lambda x: x[0] == x[1])
|
||||
def test_success(self):
|
||||
self.assertEqual(self.parser.parse("aa"), ("a", "a"))
|
||||
self.assertEqual(self.parser.parse("bb"), ("b", "b"))
|
||||
def test_failure(self):
|
||||
self.assertEqual(self.parser.parse("ab"), None)
|
||||
|
||||
class TestAnd1(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.parser = h.h_sequence__a([h.h_and(h.h_ch("0")), h.h_ch("0"), None])
|
||||
cls.parser = h.sequence(h.and_(h.ch("0")), h.ch("0"))
|
||||
def test_success(self):
|
||||
self.assertEqual([x.token_data.uint for x in h.h_parse(self.parser, "0", 1).ast.token_data.seq], [ord(y) for y in ["0"]])
|
||||
self.assertEqual(self.parser.parse("0"), ("0",))
|
||||
def test_failure(self):
|
||||
pass
|
||||
|
||||
class TestAnd2(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.parser = h.h_sequence__a([h.h_and(h.h_ch("0")), h.h_ch("1"), None])
|
||||
cls.parser = h.sequence(h.and_(h.ch("0")), h.ch("1"))
|
||||
def test_success(self):
|
||||
pass
|
||||
def test_failure(self):
|
||||
self.assertEqual(h.h_parse(self.parser, "0", 1), None)
|
||||
self.assertEqual(self.parser.parse("0"), None)
|
||||
|
||||
class TestAnd3(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.parser = h.h_sequence__a([h.h_ch("1"), h.h_and(h.h_ch("2")), None])
|
||||
cls.parser = h.sequence(h.ch("1"), h.and_(h.ch("2")))
|
||||
def test_success(self):
|
||||
self.assertEqual([x.token_data.uint for x in h.h_parse(self.parser, "12", 2).ast.token_data.seq], [ord(y) for y in ["1"]])
|
||||
self.assertEqual(self.parser.parse("12"), ('1',))
|
||||
def test_failure(self):
|
||||
pass
|
||||
|
||||
class TestNot1(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.parser = h.h_sequence__a([h.h_ch("a"), h.h_choice__a([h.h_ch("+"), h.h_token("++", 2), None]), h.h_ch("b"), None])
|
||||
cls.parser = h.sequence(h.ch("a"),
|
||||
h.choice(h.ch("+"), h.token("++")),
|
||||
h.ch("b"))
|
||||
def test_success(self):
|
||||
self.assertEqual([x.token_data.uint for x in h.h_parse(self.parser, "a+b", 3).ast.token_data.seq], [ord(y) for y in ["a", "+", "b"]])
|
||||
self.assertEqual(self.parser.parse("a+b"), ("a", "+", "b"))
|
||||
def test_failure(self):
|
||||
self.assertEqual(h.h_parse(self.parser, "a++b", 4), None)
|
||||
self.assertEqual(self.parser.parse("a++b"), None)
|
||||
|
||||
class TestNot2(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.parser = h.h_sequence__a([h.h_ch("a"), h.h_choice__a([h.h_sequence__a([h.h_ch("+"), h.h_not(h.h_ch("+")), None]), h.h_token("++", 2), None]), h.h_ch("b"), None])
|
||||
cls.parser = h.sequence(h.ch("a"), h.choice(h.sequence(h.ch("+"), h.not_(h.ch("+"))),
|
||||
h.token("++")),
|
||||
h.ch("b"))
|
||||
def test_success(self):
|
||||
tree = h.h_parse(self.parser, "a+b", 3).ast.token_data.seq
|
||||
tree[1] = tree[1].token_data.seq[0]
|
||||
self.assertEqual([x.token_data.uint for x in tree], [ord(y) for y in ["a", "+", "b"]])
|
||||
tree = h.h_parse(self.parser, "a++b", 4).ast.token_data.seq
|
||||
tree[0] = chr(tree[0].token_data.uint)
|
||||
tree[1] = tree[1].token_data.bytes
|
||||
tree[2] = chr(tree[2].token_data.uint)
|
||||
self.assertEqual(tree, ["a", "++", "b"])
|
||||
self.assertEqual(self.parser.parse("a+b"), ('a', ('+',), 'b'))
|
||||
self.assertEqual(self.parser.parse("a++b"), ('a', "++", 'b'))
|
||||
def test_failure(self):
|
||||
pass
|
||||
|
||||
|
|
@ -467,32 +466,29 @@ class TestNot2(unittest.TestCase):
|
|||
# #class TestLeftrec(unittest.TestCase):
|
||||
# # @classmethod
|
||||
# # def setUpClass(cls):
|
||||
# # cls.parser = h.h_indirect()
|
||||
# # a = h.h_ch("a")
|
||||
# # h.h_bind_indirect(cls.parser, h.h_choice(h.h_sequence(cls.parser, a), a))
|
||||
# # cls.parser = h.indirect()
|
||||
# # a = h.ch("a")
|
||||
# # h.bind_indirect(cls.parser, h.choice(h.sequence(cls.parser, a), a))
|
||||
# # def test_success(self):
|
||||
# # self.assertEqual(h.h_parse(self.parser, "a", 1).ast.token_data.bytes, "a")
|
||||
# # self.assertEqual(h.h_parse(self.parser, "aa", 2).ast.token_data.seq, ["a", "a"])
|
||||
# # self.assertEqual(h.h_parse(self.parser, "aaa", 3).ast.token_data.seq, ["a", "a", "a"])
|
||||
# # self.assertEqual(self.parser.parse("a"), "a")
|
||||
# # self.assertEqual(self.parser.parse("aa"), ["a", "a"])
|
||||
# # self.assertEqual(self.parser.parse("aaa"), ["a", "a", "a"])
|
||||
# # def test_failure(self):
|
||||
# # pass
|
||||
|
||||
|
||||
class TestRightrec(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.parser = h.h_indirect()
|
||||
a = h.h_ch("a")
|
||||
h.h_bind_indirect(cls.parser, h.h_choice__a([h.h_sequence__a([a, cls.parser, None]), h.h_epsilon_p(), None]))
|
||||
#raise unittest.SkipTest("Bind doesn't work right now")
|
||||
cls.parser = h.indirect()
|
||||
a = h.ch("a")
|
||||
cls.parser.bind(h.choice(h.sequence(a, cls.parser),
|
||||
h.epsilon_p()))
|
||||
def test_success(self):
|
||||
tree = h.h_parse(self.parser, "a", 1).ast.token_data.seq
|
||||
self.assertEqual(tree[0].token_data.uint, ord("a"))
|
||||
tree = h.h_parse(self.parser, "aa", 2).ast.token_data.seq
|
||||
self.assertEqual(tree[0].token_data.uint, ord("a"))
|
||||
self.assertEqual(tree[1].token_data.seq[0].token_data.uint, ord("a"))
|
||||
tree = h.h_parse(self.parser, "aaa", 3).ast.token_data.seq
|
||||
self.assertEqual(tree[0].token_data.uint, ord("a"))
|
||||
self.assertEqual(tree[1].token_data.seq[0].token_data.uint, ord("a"))
|
||||
self.assertEqual(tree[1].token_data.seq[1].token_data.seq[0].token_data.uint, ord("a"))
|
||||
self.assertEqual(self.parser.parse("a"), ('a',))
|
||||
self.assertEqual(self.parser.parse("aa"), ('a', ('a',)))
|
||||
self.assertEqual(self.parser.parse("aaa"), ('a', ('a', ('a',))))
|
||||
def test_failure(self):
|
||||
pass
|
||||
|
||||
|
|
@ -500,15 +496,15 @@ class TestRightrec(unittest.TestCase):
|
|||
# #class TestAmbiguous(unittest.TestCase):
|
||||
# # @classmethod
|
||||
# # def setUpClass(cls):
|
||||
# # cls.parser = h.h_indirect()
|
||||
# # d = h.h_ch("d")
|
||||
# # p = h.h_ch("+")
|
||||
# # h.h_bind_indirect(cls.parser, h.h_choice(h.h_sequence(cls.parser, p, cls.parser), d))
|
||||
# # cls.parser = h.indirect()
|
||||
# # d = h.ch("d")
|
||||
# # p = h.ch("+")
|
||||
# # h.bind_indirect(cls.parser, h.choice(h.sequence(cls.parser, p, cls.parser), d))
|
||||
# # # this is supposed to be flattened
|
||||
# # def test_success(self):
|
||||
# # self.assertEqual(h.h_parse(self.parser, "d", 1).ast.token_data.seq, ["d"])
|
||||
# # self.assertEqual(h.h_parse(self.parser, "d+d", 3).ast.token_data.seq, ["d", "+", "d"])
|
||||
# # self.assertEqual(h.h_parse(self.parser, "d+d+d", 5).ast.token_data.seq, ["d", "+", "d", "+", "d"])
|
||||
# # self.assertEqual(self.parser.parse("d"), ["d"])
|
||||
# # self.assertEqual(self.parser.parse("d+d"), ["d", "+", "d"])
|
||||
# # self.assertEqual(self.parser.parse("d+d+d"), ["d", "+", "d", "+", "d"])
|
||||
# # def test_failure(self):
|
||||
# # self.assertEqual(h.h_parse(self.parser, "d+", 2), None)
|
||||
# # self.assertEqual(self.parser.parse("d+"), None)
|
||||
|
||||
|
|
|
|||
|
|
@ -1,26 +1,53 @@
|
|||
%module hammer
|
||||
|
||||
%nodefaultctor;
|
||||
|
||||
%include "stdint.i"
|
||||
//%include "typemaps.i"
|
||||
//%apply char [ANY] { uint8_t [ANY] };
|
||||
|
||||
#if defined(SWIGPYTHON)
|
||||
%ignore HCountedArray_;
|
||||
%typemap(in) uint8_t* {
|
||||
Py_INCREF($input);
|
||||
$1 = (uint8_t*)PyString_AsString($input);
|
||||
}
|
||||
%typemap(out) uint8_t* {
|
||||
$result = PyString_FromString((char*)$1);
|
||||
%apply (char *STRING, size_t LENGTH) {(uint8_t* str, size_t len)}
|
||||
%apply (uint8_t* str, size_t len) {(const uint8_t* input, size_t length)}
|
||||
%apply (uint8_t* str, size_t len) {(const uint8_t* str, const size_t len)}
|
||||
%apply (uint8_t* str, size_t len) {(const uint8_t* charset, size_t length)}
|
||||
|
||||
|
||||
%rename("_%s") "";
|
||||
// %rename(_h_ch) h_ch;
|
||||
|
||||
%inline {
|
||||
static PyObject *_helper_Placeholder = NULL, *_helper_ParseError = NULL;
|
||||
|
||||
static void register_helpers(PyObject* parse_error, PyObject *placeholder) {
|
||||
_helper_ParseError = parse_error;
|
||||
_helper_Placeholder = placeholder;
|
||||
}
|
||||
}
|
||||
|
||||
%pythoncode %{
|
||||
class Placeholder(object):
|
||||
"""The python equivalent of TT_NONE"""
|
||||
def __str__(self):
|
||||
return "Placeholder"
|
||||
def __repr__(self):
|
||||
return "Placeholder"
|
||||
def __eq__(self, other):
|
||||
return type(self) == type(other)
|
||||
class ParseError(Exception):
|
||||
"""The parse failed; the message may have more information"""
|
||||
pass
|
||||
|
||||
_hammer._register_helpers(ParseError,
|
||||
Placeholder)
|
||||
%}
|
||||
|
||||
%typemap(in) void*[] {
|
||||
if (PyList_Check($input)) {
|
||||
Py_INCREF($input);
|
||||
int size = PyList_Size($input);
|
||||
int i = 0;
|
||||
int res = 0;
|
||||
$1 = (void**)malloc(size*sizeof(HParser*));
|
||||
$1 = (void**)malloc((size+1)*sizeof(HParser*));
|
||||
for (i=0; i<size; i++) {
|
||||
PyObject *o = PyList_GetItem($input, i);
|
||||
res = SWIG_ConvertPtr(o, &($1[i]), SWIGTYPE_p_HParser_, 0 | 0);
|
||||
|
|
@ -28,6 +55,7 @@
|
|||
SWIG_exception_fail(SWIG_ArgError(res), "that wasn't an HParser" );
|
||||
}
|
||||
}
|
||||
$1[size] = NULL;
|
||||
} else {
|
||||
PyErr_SetString(PyExc_TypeError, "__a functions take lists of parsers as their argument");
|
||||
return NULL;
|
||||
|
|
@ -56,6 +84,49 @@
|
|||
PyList_SetItem($result, i, o);
|
||||
}
|
||||
}
|
||||
%typemap(out) struct HParseResult_* {
|
||||
if ($1 == NULL) {
|
||||
// TODO: raise parse failure
|
||||
Py_INCREF(Py_None);
|
||||
$result = Py_None;
|
||||
} else {
|
||||
$result = hpt_to_python($1->ast);
|
||||
}
|
||||
}
|
||||
%typemap(newfree) struct HParseResult_* {
|
||||
h_parse_result_free($input);
|
||||
}
|
||||
%inline %{
|
||||
static int h_tt_python;
|
||||
%}
|
||||
%init %{
|
||||
h_tt_python = h_allocate_token_type("com.upstandinghackers.hammer.python");
|
||||
%}
|
||||
|
||||
|
||||
|
||||
|
||||
%typemap(in) (HPredicate pred, void* user_data) {
|
||||
Py_INCREF($input);
|
||||
$2 = $input;
|
||||
$1 = call_predicate;
|
||||
}
|
||||
|
||||
%typemap(in) (const HAction a, void* user_data) {
|
||||
Py_INCREF($input);
|
||||
$2 = $input;
|
||||
$1 = call_action;
|
||||
}
|
||||
|
||||
%inline %{
|
||||
|
||||
struct HParsedToken_;
|
||||
struct HParseResult_;
|
||||
static PyObject* hpt_to_python(const struct HParsedToken_ *token);
|
||||
|
||||
static struct HParsedToken_* call_action(const struct HParseResult_ *p, void* user_data);
|
||||
static int call_predicate(const struct HParseResult_ *p, void* user_data);
|
||||
%}
|
||||
#else
|
||||
#warning no uint8_t* typemaps defined
|
||||
#endif
|
||||
|
|
@ -65,7 +136,208 @@
|
|||
#include "allocator.h"
|
||||
#include "hammer.h"
|
||||
#include "internal.h"
|
||||
#include "glue.h"
|
||||
%}
|
||||
%include "allocator.h"
|
||||
%include "hammer.h"
|
||||
|
||||
%extend HArena_ {
|
||||
~HArena_() {
|
||||
h_delete_arena($self);
|
||||
}
|
||||
};
|
||||
%extend HParseResult_ {
|
||||
~HParseResult_() {
|
||||
h_parse_result_free($self);
|
||||
}
|
||||
};
|
||||
|
||||
%newobject h_parse;
|
||||
%delobject h_parse_result_free;
|
||||
%newobject h_new_arena;
|
||||
%delobject h_delete_arena;
|
||||
|
||||
#ifdef SWIGPYTHON
|
||||
%inline {
|
||||
static PyObject* hpt_to_python(const HParsedToken *token) {
|
||||
// Caller holds a reference to returned object
|
||||
PyObject *ret;
|
||||
if (token == NULL) {
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
switch (token->token_type) {
|
||||
case TT_NONE:
|
||||
return PyObject_CallFunctionObjArgs(_helper_Placeholder, NULL);
|
||||
break;
|
||||
case TT_BYTES:
|
||||
return PyString_FromStringAndSize((char*)token->token_data.bytes.token, token->token_data.bytes.len);
|
||||
case TT_SINT:
|
||||
// TODO: return PyINT if appropriate
|
||||
return PyLong_FromLong(token->token_data.sint);
|
||||
case TT_UINT:
|
||||
// TODO: return PyINT if appropriate
|
||||
return PyLong_FromUnsignedLong(token->token_data.uint);
|
||||
case TT_SEQUENCE:
|
||||
ret = PyTuple_New(token->token_data.seq->used);
|
||||
for (int i = 0; i < token->token_data.seq->used; i++) {
|
||||
PyTuple_SET_ITEM(ret, i, hpt_to_python(token->token_data.seq->elements[i]));
|
||||
}
|
||||
return ret;
|
||||
default:
|
||||
if (token->token_type == h_tt_python) {
|
||||
ret = (PyObject*)token->token_data.user;
|
||||
Py_INCREF(ret);
|
||||
return ret;
|
||||
} else {
|
||||
return SWIG_NewPointerObj((void*)token, SWIGTYPE_p_HParsedToken_, 0 | 0);
|
||||
// TODO: support registry
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
static struct HParsedToken_* call_action(const struct HParseResult_ *p, void* user_data) {
|
||||
PyObject *callable = user_data;
|
||||
PyObject *ret = PyObject_CallFunctionObjArgs(callable,
|
||||
hpt_to_python(p->ast),
|
||||
NULL);
|
||||
if (ret == NULL) {
|
||||
PyErr_Print();
|
||||
assert(ret != NULL);
|
||||
}
|
||||
// TODO: add reference to ret to parse-local data
|
||||
// For now, just hold onto reference
|
||||
HParsedToken *tok = h_make(p->arena, h_tt_python, ret);
|
||||
return tok;
|
||||
}
|
||||
|
||||
static int call_predicate(const struct HParseResult_ *p, void* user_data) {
|
||||
PyObject *callable = user_data;
|
||||
PyObject *ret = PyObject_CallFunctionObjArgs(callable,
|
||||
hpt_to_python(p->ast),
|
||||
NULL);
|
||||
int rret = 0;
|
||||
if (ret == NULL) {
|
||||
// TODO: throw exception
|
||||
PyErr_Print();
|
||||
assert(ret != NULL);
|
||||
}
|
||||
// TODO: add reference to ret to parse-local data
|
||||
rret = PyObject_IsTrue(ret);
|
||||
Py_DECREF(ret);
|
||||
return rret;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
%rename("%s") "";
|
||||
|
||||
%extend HParser_ {
|
||||
HParseResult* parse(const uint8_t* input, size_t length) {
|
||||
return h_parse($self, input, length);
|
||||
}
|
||||
bool compile(HParserBackend backend) {
|
||||
return h_compile($self, backend, NULL) == 0;
|
||||
}
|
||||
PyObject* __dir__() {
|
||||
PyObject* ret = PyList_New(2);
|
||||
PyList_SET_ITEM(ret, 0, PyString_FromString("parse"));
|
||||
PyList_SET_ITEM(ret, 1, PyString_FromString("compile"));
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
%pythoncode %{
|
||||
|
||||
def action(p, act):
|
||||
return _h_action(p, act)
|
||||
def attr_bool(p, pred):
|
||||
return _h_attr_bool(p, pred)
|
||||
|
||||
def ch(ch):
|
||||
if isinstance(ch, str) or isinstance(ch, unicode):
|
||||
return token(ch)
|
||||
else:
|
||||
return _h_ch(ch)
|
||||
|
||||
def ch_range(c1, c2):
|
||||
dostr = isinstance(c1, str)
|
||||
dostr2 = isinstance(c2, str)
|
||||
if isinstance(c1, unicode) or isinstance(c2, unicode):
|
||||
raise TypeError("ch_range only works on bytes")
|
||||
if dostr != dostr2:
|
||||
raise TypeError("Both arguments to ch_range must be the same type")
|
||||
if dostr:
|
||||
return action(_h_ch_range(c1, c2), chr)
|
||||
else:
|
||||
return _h_ch_range(c1, c2)
|
||||
def epsilon_p(): return _h_epsilon_p()
|
||||
def end_p():
|
||||
return _h_end_p()
|
||||
def in_(charset):
|
||||
return action(_h_in(charset), chr)
|
||||
def not_in(charset):
|
||||
return action(_h_not_in(charset), chr)
|
||||
def not_(p): return _h_not(p)
|
||||
def int_range(p, i1, i2):
|
||||
return _h_int_range(p, i1, i2)
|
||||
def token(string):
|
||||
return _h_token(string)
|
||||
def whitespace(p):
|
||||
return _h_whitespace(p)
|
||||
def xor(p1, p2):
|
||||
return _h_xor(p1, p2)
|
||||
def butnot(p1, p2):
|
||||
return _h_butnot(p1, p2)
|
||||
def and_(p1):
|
||||
return _h_and(p1)
|
||||
def difference(p1, p2):
|
||||
return _h_difference(p1, p2)
|
||||
|
||||
def sepBy(p, sep): return _h_sepBy(p, sep)
|
||||
def sepBy1(p, sep): return _h_sepBy1(p, sep)
|
||||
def many(p): return _h_many(p)
|
||||
def many1(p): return _h_many1(p)
|
||||
def repeat_n(p, n): return _h_repeat_n(p, n)
|
||||
def choice(*args): return _h_choice__a(list(args))
|
||||
def sequence(*args): return _h_sequence__a(list(args))
|
||||
|
||||
def optional(p): return _h_optional(p)
|
||||
def nothing_p(): return _h_nothing_p()
|
||||
def ignore(p): return _h_ignore(p)
|
||||
|
||||
def left(p1, p2): return _h_left(p1, p2)
|
||||
def middle(p1, p2, p3): return _h_middle(p1, p2, p3)
|
||||
def right(p1, p2): return _h_right(p1, p2)
|
||||
|
||||
|
||||
class HIndirectParser(_HParser_):
|
||||
def __init__(self):
|
||||
# Shoves the guts of an _HParser_ into a HIndirectParser.
|
||||
tret = _h_indirect()
|
||||
self.__dict__.clear()
|
||||
self.__dict__.update(tret.__dict__)
|
||||
|
||||
def __dir__(self):
|
||||
return super(HIndirectParser, self).__dir__() + ['bind']
|
||||
def bind(self, parser):
|
||||
_h_bind_indirect(self, parser)
|
||||
|
||||
def indirect():
|
||||
return HIndirectParser()
|
||||
|
||||
def bind_indirect(indirect, new_parser):
|
||||
indirect.bind(new_parser)
|
||||
|
||||
def uint8(): return _h_uint8()
|
||||
def uint16(): return _h_uint16()
|
||||
def uint32(): return _h_uint32()
|
||||
def uint64(): return _h_uint64()
|
||||
def int8(): return _h_int8()
|
||||
def int16(): return _h_int16()
|
||||
def int32(): return _h_int32()
|
||||
def int64(): return _h_int64()
|
||||
|
||||
|
||||
%}
|
||||
|
||||
#endif
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue