Changed typemap for strings to also parse lengths
This commit is contained in:
parent
8b6d6084c5
commit
954b6e32d3
2 changed files with 195 additions and 171 deletions
|
|
@ -4,11 +4,11 @@ import hammer as h
|
||||||
class TestTokenParser(unittest.TestCase):
|
class TestTokenParser(unittest.TestCase):
|
||||||
@classmethod
|
@classmethod
|
||||||
def setUpClass(cls):
|
def setUpClass(cls):
|
||||||
cls.parser = h.h_token("95\xa2", 3)
|
cls.parser = h.h_token("95\xa2")
|
||||||
def test_success(self):
|
def test_success(self):
|
||||||
self.assertEqual(h.h_parse(self.parser, "95\xa2", 3).ast.token_data.bytes, "95\xa2")
|
self.assertEqual(h.h_parse(self.parser, "95\xa2").ast.token_data.bytes, "95\xa2")
|
||||||
def test_partial_fails(self):
|
def test_partial_fails(self):
|
||||||
self.assertEqual(h.h_parse(self.parser, "95", 2), None)
|
self.assertEqual(h.h_parse(self.parser, "95"), None)
|
||||||
|
|
||||||
class TestChParser(unittest.TestCase):
|
class TestChParser(unittest.TestCase):
|
||||||
@classmethod
|
@classmethod
|
||||||
|
|
@ -16,201 +16,202 @@ class TestChParser(unittest.TestCase):
|
||||||
cls.parser_int = h.h_ch(0xa2)
|
cls.parser_int = h.h_ch(0xa2)
|
||||||
cls.parser_chr = h.h_ch("\xa2")
|
cls.parser_chr = h.h_ch("\xa2")
|
||||||
def test_success(self):
|
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_int, "\xa2").ast.token_data.uint, 0xa2)
|
||||||
self.assertEqual(h.h_parse(self.parser_chr, "\xa2", 1).ast.token_data.uint, ord("\xa2"))
|
self.assertEqual(h.h_parse(self.parser_chr, "\xa2").ast.token_data.uint, ord("\xa2"))
|
||||||
def test_failure(self):
|
def test_failure(self):
|
||||||
self.assertEqual(h.h_parse(self.parser_int, "\xa3", 1), None)
|
self.assertEqual(h.h_parse(self.parser_int, "\xa3"), None)
|
||||||
self.assertEqual(h.h_parse(self.parser_chr, "\xa3", 1), None)
|
self.assertEqual(h.h_parse(self.parser_chr, "\xa3"), None)
|
||||||
|
|
||||||
class TestChRange(unittest.TestCase):
|
class TestChRange(unittest.TestCase):
|
||||||
@classmethod
|
@classmethod
|
||||||
def setUpClass(cls):
|
def setUpClass(cls):
|
||||||
cls.parser = h.h_ch_range("a", "c")
|
cls.parser = h.h_ch_range("a", "c")
|
||||||
def test_success(self):
|
def test_success(self):
|
||||||
self.assertEqual(h.h_parse(self.parser, "b", 1).ast.token_data.uint, ord("b"))
|
self.assertEqual(h.h_parse(self.parser, "b").ast.token_data.uint, ord("b"))
|
||||||
def test_failure(self):
|
def test_failure(self):
|
||||||
self.assertEqual(h.h_parse(self.parser, "d", 1), None)
|
self.assertEqual(h.h_parse(self.parser, "d"), None)
|
||||||
|
|
||||||
class TestInt64(unittest.TestCase):
|
class TestInt64(unittest.TestCase):
|
||||||
@classmethod
|
@classmethod
|
||||||
def setUpClass(cls):
|
def setUpClass(cls):
|
||||||
cls.parser = h.h_int64()
|
cls.parser = h.h_int64()
|
||||||
def test_success(self):
|
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(h.h_parse(self.parser, "\xff\xff\xff\xfe\x00\x00\x00\x00").ast.token_data.sint, -0x200000000)
|
||||||
def test_failure(self):
|
def test_failure(self):
|
||||||
self.assertEqual(h.h_parse(self.parser, "\xff\xff\xff\xfe\x00\x00\x00", 7), None)
|
self.assertEqual(h.h_parse(self.parser, "\xff\xff\xff\xfe\x00\x00\x00"), None)
|
||||||
|
|
||||||
class TestInt32(unittest.TestCase):
|
class TestInt32(unittest.TestCase):
|
||||||
@classmethod
|
@classmethod
|
||||||
def setUpClass(cls):
|
def setUpClass(cls):
|
||||||
cls.parser = h.h_int32()
|
cls.parser = h.h_int32()
|
||||||
def test_success(self):
|
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, "\xff\xfe\x00\x00").ast.token_data.sint, -0x20000)
|
||||||
self.assertEqual(h.h_parse(self.parser, "\x00\x02\x00\x00", 4).ast.token_data.sint, 0x20000)
|
self.assertEqual(h.h_parse(self.parser, "\x00\x02\x00\x00").ast.token_data.sint, 0x20000)
|
||||||
def test_failure(self):
|
def test_failure(self):
|
||||||
self.assertEqual(h.h_parse(self.parser, "\xff\xfe\x00", 3), None)
|
self.assertEqual(h.h_parse(self.parser, "\xff\xfe\x00"), None)
|
||||||
self.assertEqual(h.h_parse(self.parser, "\x00\x02\x00", 3), None)
|
self.assertEqual(h.h_parse(self.parser, "\x00\x02\x00"), None)
|
||||||
|
|
||||||
class TestInt16(unittest.TestCase):
|
class TestInt16(unittest.TestCase):
|
||||||
@classmethod
|
@classmethod
|
||||||
def setUpClass(cls):
|
def setUpClass(cls):
|
||||||
cls.parser = h.h_int16()
|
cls.parser = h.h_int16()
|
||||||
def test_success(self):
|
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, "\xfe\x00").ast.token_data.sint, -0x200)
|
||||||
self.assertEqual(h.h_parse(self.parser, "\x02\x00", 2).ast.token_data.sint, 0x200)
|
self.assertEqual(h.h_parse(self.parser, "\x02\x00").ast.token_data.sint, 0x200)
|
||||||
def test_failure(self):
|
def test_failure(self):
|
||||||
self.assertEqual(h.h_parse(self.parser, "\xfe", 1), None)
|
self.assertEqual(h.h_parse(self.parser, "\xfe"), None)
|
||||||
self.assertEqual(h.h_parse(self.parser, "\x02", 1), None)
|
self.assertEqual(h.h_parse(self.parser, "\x02"), None)
|
||||||
|
|
||||||
class TestInt8(unittest.TestCase):
|
class TestInt8(unittest.TestCase):
|
||||||
@classmethod
|
@classmethod
|
||||||
def setUpClass(cls):
|
def setUpClass(cls):
|
||||||
cls.parser = h.h_int8()
|
cls.parser = h.h_int8()
|
||||||
def test_success(self):
|
def test_success(self):
|
||||||
self.assertEqual(h.h_parse(self.parser, "\x88", 1).ast.token_data.sint, -0x78)
|
self.assertEqual(h.h_parse(self.parser, "\x88").ast.token_data.sint, -0x78)
|
||||||
def test_failure(self):
|
def test_failure(self):
|
||||||
self.assertEqual(h.h_parse(self.parser, "", 0), None)
|
self.assertEqual(h.h_parse(self.parser, ""), None)
|
||||||
|
|
||||||
class TestUint64(unittest.TestCase):
|
class TestUint64(unittest.TestCase):
|
||||||
@classmethod
|
@classmethod
|
||||||
def setUpClass(cls):
|
def setUpClass(cls):
|
||||||
cls.parser = h.h_uint64()
|
cls.parser = h.h_uint64()
|
||||||
def test_success(self):
|
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(h.h_parse(self.parser, "\x00\x00\x00\x02\x00\x00\x00\x00").ast.token_data.uint, 0x200000000)
|
||||||
def test_failure(self):
|
def test_failure(self):
|
||||||
self.assertEqual(h.h_parse(self.parser, "\x00\x00\x00\x02\x00\x00\x00", 7), None)
|
self.assertEqual(h.h_parse(self.parser, "\x00\x00\x00\x02\x00\x00\x00"), None)
|
||||||
|
|
||||||
class TestUint32(unittest.TestCase):
|
class TestUint32(unittest.TestCase):
|
||||||
@classmethod
|
@classmethod
|
||||||
def setUpClass(cls):
|
def setUpClass(cls):
|
||||||
cls.parser = h.h_uint32()
|
cls.parser = h.h_uint32()
|
||||||
def test_success(self):
|
def test_success(self):
|
||||||
self.assertEqual(h.h_parse(self.parser, "\x00\x02\x00\x00", 4).ast.token_data.uint, 0x20000)
|
self.assertEqual(h.h_parse(self.parser, "\x00\x02\x00\x00").ast.token_data.uint, 0x20000)
|
||||||
def test_failure(self):
|
def test_failure(self):
|
||||||
self.assertEqual(h.h_parse(self.parser, "\x00\x02\x00", 3), None)
|
self.assertEqual(h.h_parse(self.parser, "\x00\x02\x00"), None)
|
||||||
|
|
||||||
class TestUint16(unittest.TestCase):
|
class TestUint16(unittest.TestCase):
|
||||||
@classmethod
|
@classmethod
|
||||||
def setUpClass(cls):
|
def setUpClass(cls):
|
||||||
cls.parser = h.h_uint16()
|
cls.parser = h.h_uint16()
|
||||||
def test_success(self):
|
def test_success(self):
|
||||||
self.assertEqual(h.h_parse(self.parser, "\x02\x00", 2).ast.token_data.uint, 0x200)
|
self.assertEqual(h.h_parse(self.parser, "\x02\x00").ast.token_data.uint, 0x200)
|
||||||
def test_failure(self):
|
def test_failure(self):
|
||||||
self.assertEqual(h.h_parse(self.parser, "\x02", 1), None)
|
self.assertEqual(h.h_parse(self.parser, "\x02"), None)
|
||||||
|
|
||||||
class TestUint8(unittest.TestCase):
|
class TestUint8(unittest.TestCase):
|
||||||
@classmethod
|
@classmethod
|
||||||
def setUpClass(cls):
|
def setUpClass(cls):
|
||||||
cls.parser = h.h_uint8()
|
cls.parser = h.h_uint8()
|
||||||
def test_success(self):
|
def test_success(self):
|
||||||
self.assertEqual(h.h_parse(self.parser, "\x78", 1).ast.token_data.uint, 0x78)
|
self.assertEqual(h.h_parse(self.parser, "\x78").ast.token_data.uint, 0x78)
|
||||||
def test_failure(self):
|
def test_failure(self):
|
||||||
self.assertEqual(h.h_parse(self.parser, "", 0), None)
|
self.assertEqual(h.h_parse(self.parser, ""), None)
|
||||||
|
|
||||||
class TestIntRange(unittest.TestCase):
|
class TestIntRange(unittest.TestCase):
|
||||||
@classmethod
|
@classmethod
|
||||||
def setUpClass(cls):
|
def setUpClass(cls):
|
||||||
cls.parser = h.h_int_range(h.h_uint8(), 3, 10)
|
cls.parser = h.h_int_range(h.h_uint8(), 3, 10)
|
||||||
def test_success(self):
|
def test_success(self):
|
||||||
self.assertEqual(h.h_parse(self.parser, "\x05", 1).ast.token_data.uint, 5)
|
self.assertEqual(h.h_parse(self.parser, "\x05").ast.token_data.uint, 5)
|
||||||
def test_failure(self):
|
def test_failure(self):
|
||||||
self.assertEqual(h.h_parse(self.parser, "\x0b", 1), None)
|
self.assertEqual(h.h_parse(self.parser, "\x0b"), None)
|
||||||
|
|
||||||
class TestWhitespace(unittest.TestCase):
|
class TestWhitespace(unittest.TestCase):
|
||||||
@classmethod
|
@classmethod
|
||||||
def setUpClass(cls):
|
def setUpClass(cls):
|
||||||
cls.parser = h.h_whitespace(h.h_ch("a"))
|
cls.parser = h.h_whitespace(h.h_ch("a"))
|
||||||
def test_success(self):
|
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").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").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, " a").ast.token_data.uint, ord("a"))
|
||||||
self.assertEqual(h.h_parse(self.parser, "\ta", 2).ast.token_data.uint, ord("a"))
|
self.assertEqual(h.h_parse(self.parser, "\ta").ast.token_data.uint, ord("a"))
|
||||||
def test_failure(self):
|
def test_failure(self):
|
||||||
self.assertEqual(h.h_parse(self.parser, "_a", 2), None)
|
self.assertEqual(h.h_parse(self.parser, "_a"), None)
|
||||||
|
|
||||||
class TestWhitespaceEnd(unittest.TestCase):
|
class TestWhitespaceEnd(unittest.TestCase):
|
||||||
@classmethod
|
@classmethod
|
||||||
def setUpClass(cls):
|
def setUpClass(cls):
|
||||||
cls.parser = h.h_whitespace(h.h_end_p())
|
cls.parser = h.h_whitespace(h.h_end_p())
|
||||||
def test_success(self):
|
def test_success(self):
|
||||||
self.assertEqual(h.h_parse(self.parser, "", 0).ast, None) # empty string
|
self.assertEqual(h.h_parse(self.parser, "").ast, None) # empty string
|
||||||
self.assertEqual(h.h_parse(self.parser, " ", 2).ast, None) # empty string
|
self.assertEqual(h.h_parse(self.parser, " ").ast, None) # empty string
|
||||||
def test_failure(self):
|
def test_failure(self):
|
||||||
self.assertEqual(h.h_parse(self.parser, " x", 3), None)
|
self.assertEqual(h.h_parse(self.parser, " x"), None)
|
||||||
|
|
||||||
class TestLeft(unittest.TestCase):
|
class TestLeft(unittest.TestCase):
|
||||||
@classmethod
|
@classmethod
|
||||||
def setUpClass(cls):
|
def setUpClass(cls):
|
||||||
cls.parser = h.h_left(h.h_ch("a"), h.h_ch(" "))
|
cls.parser = h.h_left(h.h_ch("a"), h.h_ch(" "))
|
||||||
def test_success(self):
|
def test_success(self):
|
||||||
self.assertEqual(h.h_parse(self.parser, "a ", 2).ast.token_data.uint, ord("a"))
|
self.assertEqual(h.h_parse(self.parser, "a ").ast.token_data.uint, ord("a"))
|
||||||
def test_failure(self):
|
def test_failure(self):
|
||||||
self.assertEqual(h.h_parse(self.parser, "a", 1), None)
|
self.assertEqual(h.h_parse(self.parser, "a"), None)
|
||||||
self.assertEqual(h.h_parse(self.parser, " ", 1), None)
|
self.assertEqual(h.h_parse(self.parser, " "), None)
|
||||||
self.assertEqual(h.h_parse(self.parser, "ab", 2), None)
|
self.assertEqual(h.h_parse(self.parser, "ab"), None)
|
||||||
|
|
||||||
class TestRight(unittest.TestCase):
|
class TestRight(unittest.TestCase):
|
||||||
@classmethod
|
@classmethod
|
||||||
def setUpClass(cls):
|
def setUpClass(cls):
|
||||||
cls.parser = h.h_right(h.h_ch(" "), h.h_ch("a"))
|
cls.parser = h.h_right(h.h_ch(" "), h.h_ch("a"))
|
||||||
def test_success(self):
|
def test_success(self):
|
||||||
self.assertEqual(h.h_parse(self.parser, " a", 2).ast.token_data.uint, ord("a"))
|
self.assertEqual(h.h_parse(self.parser, " a").ast.token_data.uint, ord("a"))
|
||||||
def test_failure(self):
|
def test_failure(self):
|
||||||
self.assertEqual(h.h_parse(self.parser, "a", 1), None)
|
self.assertEqual(h.h_parse(self.parser, "a"), None)
|
||||||
self.assertEqual(h.h_parse(self.parser, " ", 1), None)
|
self.assertEqual(h.h_parse(self.parser, " "), None)
|
||||||
self.assertEqual(h.h_parse(self.parser, "ba", 2), None)
|
self.assertEqual(h.h_parse(self.parser, "ba"), None)
|
||||||
|
|
||||||
class TestMiddle(unittest.TestCase):
|
class TestMiddle(unittest.TestCase):
|
||||||
@classmethod
|
@classmethod
|
||||||
def setUpClass(cls):
|
def setUpClass(cls):
|
||||||
cls.parser = h.h_middle(h.h_ch(" "), h.h_ch("a"), h.h_ch(" "))
|
cls.parser = h.h_middle(h.h_ch(" "), h.h_ch("a"), h.h_ch(" "))
|
||||||
def test_success(self):
|
def test_success(self):
|
||||||
self.assertEqual(h.h_parse(self.parser, " a ", 3).ast.token_data.uint, ord("a"))
|
self.assertEqual(h.h_parse(self.parser, " a ").ast.token_data.uint, ord("a"))
|
||||||
def test_failure(self):
|
def test_failure(self):
|
||||||
self.assertEqual(h.h_parse(self.parser, "a", 1), None)
|
self.assertEqual(h.h_parse(self.parser, "a"), None)
|
||||||
self.assertEqual(h.h_parse(self.parser, " ", 1), None)
|
self.assertEqual(h.h_parse(self.parser, " "), None)
|
||||||
self.assertEqual(h.h_parse(self.parser, " a", 2), None)
|
self.assertEqual(h.h_parse(self.parser, " a"), None)
|
||||||
self.assertEqual(h.h_parse(self.parser, "a ", 2), None)
|
self.assertEqual(h.h_parse(self.parser, "a "), None)
|
||||||
self.assertEqual(h.h_parse(self.parser, " b ", 3), None)
|
self.assertEqual(h.h_parse(self.parser, " b "), None)
|
||||||
self.assertEqual(h.h_parse(self.parser, "ba ", 3), None)
|
self.assertEqual(h.h_parse(self.parser, "ba "), None)
|
||||||
self.assertEqual(h.h_parse(self.parser, " ab", 3), None)
|
self.assertEqual(h.h_parse(self.parser, " ab"), None)
|
||||||
|
|
||||||
# class TestAction(unittest.TestCase):
|
@unittest.skip("Action not implemented yet")
|
||||||
# @classmethod
|
class TestAction(unittest.TestCase):
|
||||||
# def setUpClass(cls):
|
@classmethod
|
||||||
# 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 setUpClass(cls):
|
||||||
# def test_success(self):
|
cls.parser = h.h_action(h.h_sequence__a([h.h_choice__a([h.h_ch("a"), h.h_ch("A")]), h.h_choice__a([h.h_ch("b"), h.h_ch("B")])]), lambda x: [y.upper() for y in x])
|
||||||
# self.assertEqual(h.h_parse(self.parser, "ab", 2).ast.token_data.seq, ["A", "B"])
|
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").ast.token_data.seq, ["A", "B"])
|
||||||
# def test_failure(self):
|
self.assertEqual(h.h_parse(self.parser, "AB").ast.token_data.seq, ["A", "B"])
|
||||||
# self.assertEqual(h.h_parse(self.parser, "XX", 2), None)
|
def test_failure(self):
|
||||||
|
self.assertEqual(h.h_parse(self.parser, "XX"), None)
|
||||||
|
|
||||||
class TestIn(unittest.TestCase):
|
class TestIn(unittest.TestCase):
|
||||||
@classmethod
|
@classmethod
|
||||||
def setUpClass(cls):
|
def setUpClass(cls):
|
||||||
cls.parser = h.h_in("abc", 3)
|
cls.parser = h.h_in("abc")
|
||||||
def test_success(self):
|
def test_success(self):
|
||||||
self.assertEqual(h.h_parse(self.parser, "b", 1).ast.token_data.uint, ord("b"))
|
self.assertEqual(h.h_parse(self.parser, "b").ast.token_data.uint, ord("b"))
|
||||||
def test_failure(self):
|
def test_failure(self):
|
||||||
self.assertEqual(h.h_parse(self.parser, "d", 1), None)
|
self.assertEqual(h.h_parse(self.parser, "d"), None)
|
||||||
|
|
||||||
class TestNotIn(unittest.TestCase):
|
class TestNotIn(unittest.TestCase):
|
||||||
@classmethod
|
@classmethod
|
||||||
def setUpClass(cls):
|
def setUpClass(cls):
|
||||||
cls.parser = h.h_not_in("abc", 3)
|
cls.parser = h.h_not_in("abc")
|
||||||
def test_success(self):
|
def test_success(self):
|
||||||
self.assertEqual(h.h_parse(self.parser, "d", 1).ast.token_data.uint, ord("d"))
|
self.assertEqual(h.h_parse(self.parser, "d").ast.token_data.uint, ord("d"))
|
||||||
def test_failure(self):
|
def test_failure(self):
|
||||||
self.assertEqual(h.h_parse(self.parser, "a", 1), None)
|
self.assertEqual(h.h_parse(self.parser, "a"), None)
|
||||||
|
|
||||||
class TestEndP(unittest.TestCase):
|
class TestEndP(unittest.TestCase):
|
||||||
@classmethod
|
@classmethod
|
||||||
def setUpClass(cls):
|
def setUpClass(cls):
|
||||||
cls.parser = h.h_sequence__a([h.h_ch("a"), h.h_end_p(), None])
|
cls.parser = h.h_sequence__a([h.h_ch("a"), h.h_end_p()])
|
||||||
def test_success(self):
|
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, "a").ast.token_data.seq], [ord(y) for y in ["a"]])
|
||||||
def test_failure(self):
|
def test_failure(self):
|
||||||
self.assertEqual(h.h_parse(self.parser, "aa", 2), None)
|
self.assertEqual(h.h_parse(self.parser, "aa"), None)
|
||||||
|
|
||||||
class TestNothingP(unittest.TestCase):
|
class TestNothingP(unittest.TestCase):
|
||||||
@classmethod
|
@classmethod
|
||||||
|
|
@ -219,243 +220,243 @@ class TestNothingP(unittest.TestCase):
|
||||||
def test_success(self):
|
def test_success(self):
|
||||||
pass
|
pass
|
||||||
def test_failure(self):
|
def test_failure(self):
|
||||||
self.assertEqual(h.h_parse(self.parser, "a", 1), None)
|
self.assertEqual(h.h_parse(self.parser, "a"), None)
|
||||||
|
|
||||||
class TestSequence(unittest.TestCase):
|
class TestSequence(unittest.TestCase):
|
||||||
@classmethod
|
@classmethod
|
||||||
def setUpClass(cls):
|
def setUpClass(cls):
|
||||||
cls.parser = h.h_sequence__a([h.h_ch("a"), h.h_ch("b"), None])
|
cls.parser = h.h_sequence__a([h.h_ch("a"), h.h_ch("b")])
|
||||||
def test_success(self):
|
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, "ab").ast.token_data.seq], [ord(y) for y in ["a", "b"]])
|
||||||
def test_failure(self):
|
def test_failure(self):
|
||||||
self.assertEqual(h.h_parse(self.parser, "a", 1), None)
|
self.assertEqual(h.h_parse(self.parser, "a"), None)
|
||||||
self.assertEqual(h.h_parse(self.parser, "b", 1), None)
|
self.assertEqual(h.h_parse(self.parser, "b"), None)
|
||||||
|
|
||||||
class TestSequenceWhitespace(unittest.TestCase):
|
class TestSequenceWhitespace(unittest.TestCase):
|
||||||
@classmethod
|
@classmethod
|
||||||
def setUpClass(cls):
|
def setUpClass(cls):
|
||||||
cls.parser = h.h_sequence__a([h.h_ch("a"), h.h_whitespace(h.h_ch("b")), None])
|
cls.parser = h.h_sequence__a([h.h_ch("a"), h.h_whitespace(h.h_ch("b"))])
|
||||||
def test_success(self):
|
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, "ab").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").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([x.token_data.uint for x in h.h_parse(self.parser, "a b").ast.token_data.seq], [ord(y) for y in ["a", "b"]])
|
||||||
def test_failure(self):
|
def test_failure(self):
|
||||||
self.assertEqual(h.h_parse(self.parser, "a c", 4), None)
|
self.assertEqual(h.h_parse(self.parser, "a c"), None)
|
||||||
|
|
||||||
class TestChoice(unittest.TestCase):
|
class TestChoice(unittest.TestCase):
|
||||||
@classmethod
|
@classmethod
|
||||||
def setUpClass(cls):
|
def setUpClass(cls):
|
||||||
cls.parser = h.h_choice__a([h.h_ch("a"), h.h_ch("b"), None])
|
cls.parser = h.h_choice__a([h.h_ch("a"), h.h_ch("b")])
|
||||||
def test_success(self):
|
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").ast.token_data.uint, ord("a"))
|
||||||
self.assertEqual(h.h_parse(self.parser, "b", 1).ast.token_data.uint, ord("b"))
|
self.assertEqual(h.h_parse(self.parser, "b").ast.token_data.uint, ord("b"))
|
||||||
def test_failure(self):
|
def test_failure(self):
|
||||||
self.assertEqual(h.h_parse(self.parser, "c", 1), None)
|
self.assertEqual(h.h_parse(self.parser, "c"), None)
|
||||||
|
|
||||||
class TestButNot(unittest.TestCase):
|
class TestButNot(unittest.TestCase):
|
||||||
@classmethod
|
@classmethod
|
||||||
def setUpClass(cls):
|
def setUpClass(cls):
|
||||||
cls.parser = h.h_butnot(h.h_ch("a"), h.h_token("ab", 2))
|
cls.parser = h.h_butnot(h.h_ch("a"), h.h_token("ab"))
|
||||||
def test_success(self):
|
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").ast.token_data.uint, ord("a"))
|
||||||
self.assertEqual(h.h_parse(self.parser, "aa", 2).ast.token_data.uint, ord("a"))
|
self.assertEqual(h.h_parse(self.parser, "aa").ast.token_data.uint, ord("a"))
|
||||||
def test_failure(self):
|
def test_failure(self):
|
||||||
self.assertEqual(h.h_parse(self.parser, "ab", 2), None)
|
self.assertEqual(h.h_parse(self.parser, "ab"), None)
|
||||||
|
|
||||||
class TestButNotRange(unittest.TestCase):
|
class TestButNotRange(unittest.TestCase):
|
||||||
@classmethod
|
@classmethod
|
||||||
def setUpClass(cls):
|
def setUpClass(cls):
|
||||||
cls.parser = h.h_butnot(h.h_ch_range("0", "9"), h.h_ch("6"))
|
cls.parser = h.h_butnot(h.h_ch_range("0", "9"), h.h_ch("6"))
|
||||||
def test_success(self):
|
def test_success(self):
|
||||||
self.assertEqual(h.h_parse(self.parser, "4", 1).ast.token_data.uint, ord("4"))
|
self.assertEqual(h.h_parse(self.parser, "4").ast.token_data.uint, ord("4"))
|
||||||
def test_failure(self):
|
def test_failure(self):
|
||||||
self.assertEqual(h.h_parse(self.parser, "6", 1), None)
|
self.assertEqual(h.h_parse(self.parser, "6"), None)
|
||||||
|
|
||||||
class TestDifference(unittest.TestCase):
|
class TestDifference(unittest.TestCase):
|
||||||
@classmethod
|
@classmethod
|
||||||
def setUpClass(cls):
|
def setUpClass(cls):
|
||||||
cls.parser = h.h_difference(h.h_token("ab", 2), h.h_ch("a"))
|
cls.parser = h.h_difference(h.h_token("ab"), h.h_ch("a"))
|
||||||
def test_success(self):
|
def test_success(self):
|
||||||
self.assertEqual(h.h_parse(self.parser, "ab", 2).ast.token_data.bytes, "ab")
|
self.assertEqual(h.h_parse(self.parser, "ab").ast.token_data.bytes, "ab")
|
||||||
def test_failure(self):
|
def test_failure(self):
|
||||||
self.assertEqual(h.h_parse(self.parser, "a", 1), None)
|
self.assertEqual(h.h_parse(self.parser, "a"), None)
|
||||||
|
|
||||||
class TestXor(unittest.TestCase):
|
class TestXor(unittest.TestCase):
|
||||||
@classmethod
|
@classmethod
|
||||||
def setUpClass(cls):
|
def setUpClass(cls):
|
||||||
cls.parser = h.h_xor(h.h_ch_range("0", "6"), h.h_ch_range("5", "9"))
|
cls.parser = h.h_xor(h.h_ch_range("0", "6"), h.h_ch_range("5", "9"))
|
||||||
def test_success(self):
|
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, "0").ast.token_data.uint, ord("0"))
|
||||||
self.assertEqual(h.h_parse(self.parser, "9", 1).ast.token_data.uint, ord("9"))
|
self.assertEqual(h.h_parse(self.parser, "9").ast.token_data.uint, ord("9"))
|
||||||
def test_failure(self):
|
def test_failure(self):
|
||||||
self.assertEqual(h.h_parse(self.parser, "5", 1), None)
|
self.assertEqual(h.h_parse(self.parser, "5"), None)
|
||||||
self.assertEqual(h.h_parse(self.parser, "a", 1), None)
|
self.assertEqual(h.h_parse(self.parser, "a"), None)
|
||||||
|
|
||||||
class TestMany(unittest.TestCase):
|
class TestMany(unittest.TestCase):
|
||||||
@classmethod
|
@classmethod
|
||||||
def setUpClass(cls):
|
def setUpClass(cls):
|
||||||
cls.parser = h.h_many(h.h_choice__a([h.h_ch("a"), h.h_ch("b"), None]))
|
cls.parser = h.h_many(h.h_choice__a([h.h_ch("a"), h.h_ch("b")]))
|
||||||
def test_success(self):
|
def test_success(self):
|
||||||
self.assertEqual(h.h_parse(self.parser, "", 0).ast.token_data.seq, [])
|
self.assertEqual(h.h_parse(self.parser, "").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, "a").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, "b").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([x.token_data.uint for x in h.h_parse(self.parser, "aabbaba").ast.token_data.seq], [ord(y) for y in ["a", "a", "b", "b", "a", "b", "a"]])
|
||||||
def test_failure(self):
|
def test_failure(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
class TestMany1(unittest.TestCase):
|
class TestMany1(unittest.TestCase):
|
||||||
@classmethod
|
@classmethod
|
||||||
def setUpClass(cls):
|
def setUpClass(cls):
|
||||||
cls.parser = h.h_many1(h.h_choice__a([h.h_ch("a"), h.h_ch("b"), None]))
|
cls.parser = h.h_many1(h.h_choice__a([h.h_ch("a"), h.h_ch("b")]))
|
||||||
def test_success(self):
|
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, "a").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, "b").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([x.token_data.uint for x in h.h_parse(self.parser, "aabbaba").ast.token_data.seq], [ord(y) for y in ["a", "a", "b", "b", "a", "b", "a"]])
|
||||||
def test_failure(self):
|
def test_failure(self):
|
||||||
self.assertEqual(h.h_parse(self.parser, "", 0), None)
|
self.assertEqual(h.h_parse(self.parser, ""), None)
|
||||||
self.assertEqual(h.h_parse(self.parser, "daabbabadef", 11), None)
|
self.assertEqual(h.h_parse(self.parser, "daabbabadef"), None)
|
||||||
|
|
||||||
class TestRepeatN(unittest.TestCase):
|
class TestRepeatN(unittest.TestCase):
|
||||||
@classmethod
|
@classmethod
|
||||||
def setUpClass(cls):
|
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.h_repeat_n(h.h_choice__a([h.h_ch("a"), h.h_ch("b")]), 2)
|
||||||
def test_success(self):
|
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([x.token_data.uint for x in h.h_parse(self.parser, "abdef").ast.token_data.seq], [ord(y) for y in ["a", "b"]])
|
||||||
def test_failure(self):
|
def test_failure(self):
|
||||||
self.assertEqual(h.h_parse(self.parser, "adef", 4), None)
|
self.assertEqual(h.h_parse(self.parser, "adef"), None)
|
||||||
self.assertEqual(h.h_parse(self.parser, "dabdef", 5), None)
|
self.assertEqual(h.h_parse(self.parser, "dabdef"), None)
|
||||||
|
|
||||||
class TestOptional(unittest.TestCase):
|
class TestOptional(unittest.TestCase):
|
||||||
@classmethod
|
@classmethod
|
||||||
def setUpClass(cls):
|
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.h_sequence__a([h.h_ch("a"), h.h_optional(h.h_choice__a([h.h_ch("b"), h.h_ch("c")])), h.h_ch("d")])
|
||||||
def test_success(self):
|
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, "abd").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"]])
|
self.assertEqual([x.token_data.uint for x in h.h_parse(self.parser, "acd").ast.token_data.seq], [ord(y) for y in ["a", "c", "d"]])
|
||||||
### FIXME check this out in repl, what does tree look like
|
### 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([x.token_data.uint for x in h.h_parse(self.parser, "ad").ast.token_data.seq], [ord(y)["a", None, "d"]])
|
||||||
def test_failure(self):
|
def test_failure(self):
|
||||||
self.assertEqual(h.h_parse(self.parser, "aed", 3), None)
|
self.assertEqual(h.h_parse(self.parser, "aed"), None)
|
||||||
self.assertEqual(h.h_parse(self.parser, "ab", 2), None)
|
self.assertEqual(h.h_parse(self.parser, "ab"), None)
|
||||||
self.assertEqual(h.h_parse(self.parser, "ac", 2), None)
|
self.assertEqual(h.h_parse(self.parser, "ac"), None)
|
||||||
|
|
||||||
class TestIgnore(unittest.TestCase):
|
class TestIgnore(unittest.TestCase):
|
||||||
@classmethod
|
@classmethod
|
||||||
def setUpClass(cls):
|
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.h_sequence__a([h.h_ch("a"), h.h_ignore(h.h_ch("b")), h.h_ch("c")])
|
||||||
def test_success(self):
|
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([x.token_data.uint for x in h.h_parse(self.parser, "abc").ast.token_data.seq], [ord(y) for y in ["a", "c"]])
|
||||||
def test_failure(self):
|
def test_failure(self):
|
||||||
self.assertEqual(h.h_parse(self.parser, "ac", 2), None)
|
self.assertEqual(h.h_parse(self.parser, "ac"), None)
|
||||||
|
|
||||||
class TestSepBy(unittest.TestCase):
|
class TestSepBy(unittest.TestCase):
|
||||||
@classmethod
|
@classmethod
|
||||||
def setUpClass(cls):
|
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.h_sepBy(h.h_choice__a([h.h_ch("1"), h.h_ch("2"), h.h_ch("3")]), h.h_ch(","))
|
||||||
def test_success(self):
|
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,2,3").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,2").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, "1,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([x.token_data.uint for x in h.h_parse(self.parser, "3").ast.token_data.seq], [ord(y) for y in ["3"]])
|
||||||
self.assertEqual(h.h_parse(self.parser, "", 0).ast.token_data.seq, [])
|
self.assertEqual(h.h_parse(self.parser, "").ast.token_data.seq, [])
|
||||||
def test_failure(self):
|
def test_failure(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
class TestSepBy1(unittest.TestCase):
|
class TestSepBy1(unittest.TestCase):
|
||||||
@classmethod
|
@classmethod
|
||||||
def setUpClass(cls):
|
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.h_sepBy1(h.h_choice__a([h.h_ch("1"), h.h_ch("2"), h.h_ch("3")]), h.h_ch(","))
|
||||||
def test_success(self):
|
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,2,3").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,2").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, "1,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([x.token_data.uint for x in h.h_parse(self.parser, "3").ast.token_data.seq], [ord(y) for y in ["3"]])
|
||||||
def test_failure(self):
|
def test_failure(self):
|
||||||
self.assertEqual(h.h_parse(self.parser, "", 0), None)
|
self.assertEqual(h.h_parse(self.parser, ""), None)
|
||||||
|
|
||||||
### segfaults
|
### segfaults
|
||||||
class TestEpsilonP1(unittest.TestCase):
|
class TestEpsilonP1(unittest.TestCase):
|
||||||
@classmethod
|
@classmethod
|
||||||
def setUpClass(cls):
|
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.h_sequence__a([h.h_ch("a"), h.h_epsilon_p(), h.h_ch("b")])
|
||||||
def test_success(self):
|
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, "ab").ast.token_data.seq], [ord(y) for y in ["a", "b"]])
|
||||||
def test_failure(self):
|
def test_failure(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
class TestEpsilonP2(unittest.TestCase):
|
class TestEpsilonP2(unittest.TestCase):
|
||||||
@classmethod
|
@classmethod
|
||||||
def setUpClass(cls):
|
def setUpClass(cls):
|
||||||
cls.parser = h.h_sequence__a([h.h_epsilon_p(), h.h_ch("a"), None])
|
cls.parser = h.h_sequence__a([h.h_epsilon_p(), h.h_ch("a")])
|
||||||
def test_success(self):
|
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, "a").ast.token_data.seq], [ord(y) for y in ["a"]])
|
||||||
def test_failure(self):
|
def test_failure(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
class TestEpsilonP3(unittest.TestCase):
|
class TestEpsilonP3(unittest.TestCase):
|
||||||
@classmethod
|
@classmethod
|
||||||
def setUpClass(cls):
|
def setUpClass(cls):
|
||||||
cls.parser = h.h_sequence__a([h.h_ch("a"), h.h_epsilon_p(), None])
|
cls.parser = h.h_sequence__a([h.h_ch("a"), h.h_epsilon_p()])
|
||||||
def test_success(self):
|
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, "a").ast.token_data.seq], [ord(y) for y in ["a"]])
|
||||||
def test_failure(self):
|
def test_failure(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
# class TestAttrBool(unittest.TestCase):
|
# class TestAttrBool(unittest.TestCase):
|
||||||
# @classmethod
|
# @classmethod
|
||||||
# def setUpClass(cls):
|
# 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])
|
# cls.parser = h.h_attr_bool(h.h_many1(h.h_choice__a([h.h_ch("a"), h.h_ch("b")])), lambda x: x[0] == x[1])
|
||||||
# def test_success(self):
|
# 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, "aa").ast.token_data.seq, ["a", "a"])
|
||||||
# self.assertEqual(h.h_parse(self.parser, "bb", 2).ast.token_data.seq, ["b", "b"])
|
# self.assertEqual(h.h_parse(self.parser, "bb").ast.token_data.seq, ["b", "b"])
|
||||||
# def test_failure(self):
|
# def test_failure(self):
|
||||||
# self.assertEqual(h.h_parse(self.parser, "ab", 2), None)
|
# self.assertEqual(h.h_parse(self.parser, "ab"), None)
|
||||||
|
|
||||||
class TestAnd1(unittest.TestCase):
|
class TestAnd1(unittest.TestCase):
|
||||||
@classmethod
|
@classmethod
|
||||||
def setUpClass(cls):
|
def setUpClass(cls):
|
||||||
cls.parser = h.h_sequence__a([h.h_and(h.h_ch("0")), h.h_ch("0"), None])
|
cls.parser = h.h_sequence__a([h.h_and(h.h_ch("0")), h.h_ch("0")])
|
||||||
def test_success(self):
|
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([x.token_data.uint for x in h.h_parse(self.parser, "0").ast.token_data.seq], [ord(y) for y in ["0"]])
|
||||||
def test_failure(self):
|
def test_failure(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
class TestAnd2(unittest.TestCase):
|
class TestAnd2(unittest.TestCase):
|
||||||
@classmethod
|
@classmethod
|
||||||
def setUpClass(cls):
|
def setUpClass(cls):
|
||||||
cls.parser = h.h_sequence__a([h.h_and(h.h_ch("0")), h.h_ch("1"), None])
|
cls.parser = h.h_sequence__a([h.h_and(h.h_ch("0")), h.h_ch("1")])
|
||||||
def test_success(self):
|
def test_success(self):
|
||||||
pass
|
pass
|
||||||
def test_failure(self):
|
def test_failure(self):
|
||||||
self.assertEqual(h.h_parse(self.parser, "0", 1), None)
|
self.assertEqual(h.h_parse(self.parser, "0"), None)
|
||||||
|
|
||||||
class TestAnd3(unittest.TestCase):
|
class TestAnd3(unittest.TestCase):
|
||||||
@classmethod
|
@classmethod
|
||||||
def setUpClass(cls):
|
def setUpClass(cls):
|
||||||
cls.parser = h.h_sequence__a([h.h_ch("1"), h.h_and(h.h_ch("2")), None])
|
cls.parser = h.h_sequence__a([h.h_ch("1"), h.h_and(h.h_ch("2"))])
|
||||||
def test_success(self):
|
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([x.token_data.uint for x in h.h_parse(self.parser, "12").ast.token_data.seq], [ord(y) for y in ["1"]])
|
||||||
def test_failure(self):
|
def test_failure(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
class TestNot1(unittest.TestCase):
|
class TestNot1(unittest.TestCase):
|
||||||
@classmethod
|
@classmethod
|
||||||
def setUpClass(cls):
|
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.h_sequence__a([h.h_ch("a"), h.h_choice__a([h.h_ch("+"), h.h_token("++")]), h.h_ch("b")])
|
||||||
def test_success(self):
|
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([x.token_data.uint for x in h.h_parse(self.parser, "a+b").ast.token_data.seq], [ord(y) for y in ["a", "+", "b"]])
|
||||||
def test_failure(self):
|
def test_failure(self):
|
||||||
self.assertEqual(h.h_parse(self.parser, "a++b", 4), None)
|
self.assertEqual(h.h_parse(self.parser, "a++b"), None)
|
||||||
|
|
||||||
class TestNot2(unittest.TestCase):
|
class TestNot2(unittest.TestCase):
|
||||||
@classmethod
|
@classmethod
|
||||||
def setUpClass(cls):
|
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.h_sequence__a([h.h_ch("a"), h.h_choice__a([h.h_sequence__a([h.h_ch("+"), h.h_not(h.h_ch("+"))]), h.h_token("++")]), h.h_ch("b")])
|
||||||
def test_success(self):
|
def test_success(self):
|
||||||
tree = h.h_parse(self.parser, "a+b", 3).ast.token_data.seq
|
tree = h.h_parse(self.parser, "a+b").ast.token_data.seq
|
||||||
tree[1] = tree[1].token_data.seq[0]
|
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"]])
|
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 = h.h_parse(self.parser, "a++b").ast.token_data.seq
|
||||||
tree[0] = chr(tree[0].token_data.uint)
|
tree[0] = chr(tree[0].token_data.uint)
|
||||||
tree[1] = tree[1].token_data.bytes
|
tree[1] = tree[1].token_data.bytes
|
||||||
tree[2] = chr(tree[2].token_data.uint)
|
tree[2] = chr(tree[2].token_data.uint)
|
||||||
|
|
@ -471,9 +472,9 @@ class TestNot2(unittest.TestCase):
|
||||||
# # a = h.h_ch("a")
|
# # a = h.h_ch("a")
|
||||||
# # h.h_bind_indirect(cls.parser, h.h_choice(h.h_sequence(cls.parser, a), a))
|
# # h.h_bind_indirect(cls.parser, h.h_choice(h.h_sequence(cls.parser, a), a))
|
||||||
# # def test_success(self):
|
# # 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, "a").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, "aa").ast.token_data.seq, ["a", "a"])
|
||||||
# # self.assertEqual(h.h_parse(self.parser, "aaa", 3).ast.token_data.seq, ["a", "a", "a"])
|
# # self.assertEqual(h.h_parse(self.parser, "aaa").ast.token_data.seq, ["a", "a", "a"])
|
||||||
# # def test_failure(self):
|
# # def test_failure(self):
|
||||||
# # pass
|
# # pass
|
||||||
|
|
||||||
|
|
@ -482,14 +483,14 @@ class TestRightrec(unittest.TestCase):
|
||||||
def setUpClass(cls):
|
def setUpClass(cls):
|
||||||
cls.parser = h.h_indirect()
|
cls.parser = h.h_indirect()
|
||||||
a = h.h_ch("a")
|
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]))
|
h.h_bind_indirect(cls.parser, h.h_choice__a([h.h_sequence__a([a, cls.parser]), h.h_epsilon_p()]))
|
||||||
def test_success(self):
|
def test_success(self):
|
||||||
tree = h.h_parse(self.parser, "a", 1).ast.token_data.seq
|
tree = h.h_parse(self.parser, "a").ast.token_data.seq
|
||||||
self.assertEqual(tree[0].token_data.uint, ord("a"))
|
self.assertEqual(tree[0].token_data.uint, ord("a"))
|
||||||
tree = h.h_parse(self.parser, "aa", 2).ast.token_data.seq
|
tree = h.h_parse(self.parser, "aa").ast.token_data.seq
|
||||||
self.assertEqual(tree[0].token_data.uint, ord("a"))
|
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[0].token_data.uint, ord("a"))
|
||||||
tree = h.h_parse(self.parser, "aaa", 3).ast.token_data.seq
|
tree = h.h_parse(self.parser, "aaa").ast.token_data.seq
|
||||||
self.assertEqual(tree[0].token_data.uint, ord("a"))
|
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[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(tree[1].token_data.seq[1].token_data.seq[0].token_data.uint, ord("a"))
|
||||||
|
|
@ -506,9 +507,9 @@ class TestRightrec(unittest.TestCase):
|
||||||
# # h.h_bind_indirect(cls.parser, h.h_choice(h.h_sequence(cls.parser, p, cls.parser), d))
|
# # h.h_bind_indirect(cls.parser, h.h_choice(h.h_sequence(cls.parser, p, cls.parser), d))
|
||||||
# # # this is supposed to be flattened
|
# # # this is supposed to be flattened
|
||||||
# # def test_success(self):
|
# # 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").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").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(h.h_parse(self.parser, "d+d+d").ast.token_data.seq, ["d", "+", "d", "+", "d"])
|
||||||
# # def test_failure(self):
|
# # def test_failure(self):
|
||||||
# # self.assertEqual(h.h_parse(self.parser, "d+", 2), None)
|
# # self.assertEqual(h.h_parse(self.parser, "d+"), None)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -14,13 +14,35 @@
|
||||||
%typemap(out) uint8_t* {
|
%typemap(out) uint8_t* {
|
||||||
$result = PyString_FromString((char*)$1);
|
$result = PyString_FromString((char*)$1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
%typemap(newfree) HParseResult* {
|
||||||
|
h_parse_result_free($1);
|
||||||
|
}
|
||||||
|
|
||||||
|
%newobject h_parse
|
||||||
|
%delobject h_parse_result_free
|
||||||
|
|
||||||
|
/*
|
||||||
|
%typemap(in) (uint8_t* str, size_t len) {
|
||||||
|
if (PyString_Check($input) ||
|
||||||
|
PyUnicode_Check($input)) {
|
||||||
|
PyString_AsStringAndSize($input, (char**)&$1, &$2);
|
||||||
|
} else {
|
||||||
|
PyErr_SetString(PyExc_TypeError, "Argument must be a str or unicode");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
%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)}
|
||||||
%typemap(in) void*[] {
|
%typemap(in) void*[] {
|
||||||
if (PyList_Check($input)) {
|
if (PyList_Check($input)) {
|
||||||
Py_INCREF($input);
|
Py_INCREF($input);
|
||||||
int size = PyList_Size($input);
|
int size = PyList_Size($input);
|
||||||
int i = 0;
|
int i = 0;
|
||||||
int res = 0;
|
int res = 0;
|
||||||
$1 = (void**)malloc(size*sizeof(HParser*));
|
$1 = (void**)malloc((size+1)*sizeof(HParser*));
|
||||||
for (i=0; i<size; i++) {
|
for (i=0; i<size; i++) {
|
||||||
PyObject *o = PyList_GetItem($input, i);
|
PyObject *o = PyList_GetItem($input, i);
|
||||||
res = SWIG_ConvertPtr(o, &($1[i]), SWIGTYPE_p_HParser_, 0 | 0);
|
res = SWIG_ConvertPtr(o, &($1[i]), SWIGTYPE_p_HParser_, 0 | 0);
|
||||||
|
|
@ -28,6 +50,7 @@
|
||||||
SWIG_exception_fail(SWIG_ArgError(res), "that wasn't an HParser" );
|
SWIG_exception_fail(SWIG_ArgError(res), "that wasn't an HParser" );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
$1[size] = NULL;
|
||||||
} else {
|
} else {
|
||||||
PyErr_SetString(PyExc_TypeError, "__a functions take lists of parsers as their argument");
|
PyErr_SetString(PyExc_TypeError, "__a functions take lists of parsers as their argument");
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue