there's the uint8_t problem (mostly) sorted

This commit is contained in:
Meredith L. Patterson 2013-11-18 21:50:28 -06:00
parent e8b2c17026
commit d1b71779e1
2 changed files with 19 additions and 7 deletions

View file

@ -6,7 +6,7 @@ class TestTokenParser(unittest.TestCase):
def setUpClass(cls):
cls.parser = h.h_token("95\xa2", 3)
def test_success(self):
self.assertEqual(h.h_parse(self.parser, "95\xa2", 3).ast.token_data.bytes.token, "95\xa2")
self.assertEqual(h.h_parse(self.parser, "95\xa2", 3).ast.token_data.bytes, "95\xa2")
def test_partial_fails(self):
self.assertEqual(h.h_parse(self.parser, "95", 2), None)
@ -226,7 +226,7 @@ class TestSequence(unittest.TestCase):
def setUpClass(cls):
cls.parser = h.h_sequence(h.h_ch("a"), h.h_ch("b"))
def test_success(self):
self.assertEqual(h.h_parse(self.parser, "ab").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, "a", 1), None)
self.assertEqual(h.h_parse(self.parser, "b", 1), None)
@ -255,7 +255,7 @@ class TestChoice(unittest.TestCase):
class TestButNot(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.parser = h.h_butnot(h.h_ch("a"), h.h_token("ab"))
cls.parser = h.h_butnot(h.h_ch("a"), h.h_token("ab", 2))
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.bytes, "a")
@ -439,7 +439,7 @@ class TestAnd3(unittest.TestCase):
class TestNot1(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.parser = h.h_sequence(h.h_ch("a"), h.h_choice(h.h_ch("+"), h.h_token("++")), h.h_ch("b"))
cls.parser = h.h_sequence(h.h_ch("a"), h.h_choice(h.h_ch("+"), h.h_token("++", 2)), h.h_ch("b"))
def test_success(self):
self.assertEqual(h.h_parse(self.parser, "a+b", 3).ast.token_data.seq, ["a", "+", "b"])
def test_failure(self):
@ -448,7 +448,7 @@ class TestNot1(unittest.TestCase):
class TestNot2(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.parser = h.h_sequence(h.h_ch("a"), h.h_choice(h.h_sequence(h.h_ch("+"), h.h_not(h.h_ch("+"))), h.h_token("++")), h.h_ch("b"))
cls.parser = h.h_sequence(h.h_ch("a"), h.h_choice(h.h_sequence(h.h_ch("+"), h.h_not(h.h_ch("+"))), h.h_token("++", 2)), h.h_ch("b"))
def test_success(self):
self.assertEqual(h.h_parse(self.parser, "a+b", 3).ast.token_data.seq, ["a", ["+"], "b"])
self.assertEqual(h.h_parse(self.parser, "a++b", 4).ast.token_data.seq, ["a", "++", "b"])

View file

@ -11,6 +11,20 @@
%typemap(out) uint8_t* {
$result = PyString_FromString((char*)$1);
}
%typemap(in) uint8_t {
if (PyInt_Check($input)) {
$1 = PyInt_AsLong($input);
}
else if (!PyString_Check($input)) {
PyErr_SetString(PyExc_ValueError, "Expecting a string");
return NULL;
} else {
$1 = *(uint8_t*)PyString_AsString($input);
}
}
%typemap(out) HBytes* {
$result = PyString_FromStringAndSize((char*)$1->token, $1->len);
}
#else
#warning no uint8_t* typemaps defined
#endif
@ -24,5 +38,3 @@
%include "allocator.h"
%include "hammer.h"