frontend: support specifying type member values in the input file
diff --git a/loxi_front_end/frontend.py b/loxi_front_end/frontend.py
index 256ae70..7d0424f 100644
--- a/loxi_front_end/frontend.py
+++ b/loxi_front_end/frontend.py
@@ -33,10 +33,11 @@
class InputError(Exception):
pass
-# TODO handle type members
def create_member(m_ast):
if m_ast[0] == 'pad':
return OFPadMember(length=m_ast[1])
+ elif m_ast[0] == 'type':
+ return OFTypeMember(name=m_ast[2], oftype=m_ast[1], value=m_ast[3])
elif m_ast[0] == 'data':
if m_ast[2] == 'length' or m_ast[2] == 'len': # Should be moved to parser
return OFLengthMember(name=m_ast[2], oftype=m_ast[1])
diff --git a/loxi_front_end/parser.py b/loxi_front_end/parser.py
index ab6bfc4..503a05a 100644
--- a/loxi_front_end/parser.py
+++ b/loxi_front_end/parser.py
@@ -51,8 +51,9 @@
# Structs
pad_member = P.Group(kw('pad') - s('(') - integer - s(')'))
+type_member = P.Group(tag('type') + any_type + identifier + s('==') + integer)
data_member = P.Group(tag('data') + any_type - identifier)
-struct_member = pad_member | data_member;
+struct_member = pad_member | type_member | data_member;
struct = kw('struct') - identifier - s('{') + \
P.Group(P.ZeroOrMore(struct_member - s(';'))) + \
s('}') - s(';')
diff --git a/utest/test_frontend.py b/utest/test_frontend.py
index 2119363..1f284b0 100755
--- a/utest/test_frontend.py
+++ b/utest/test_frontend.py
@@ -52,7 +52,7 @@
struct of_echo_reply {
uint8_t version;
- uint8_t type;
+ uint8_t type == 3;
uint16_t length;
uint32_t xid;
of_octets_t data;
@@ -86,7 +86,7 @@
['metadata', 'version', '2'],
['struct', 'of_echo_reply', [
['data', 'uint8_t', 'version'],
- ['data', 'uint8_t', 'type'],
+ ['type', 'uint8_t', 'type', 3],
['data', 'uint16_t', 'length'],
['data', 'uint32_t', 'xid'],
['data', 'of_octets_t', 'data']]],
@@ -107,7 +107,7 @@
expected_classes = [
OFClass('of_echo_reply', [
OFDataMember('version', 'uint8_t'), # XXX
- OFDataMember('type', 'uint8_t'), # XXX
+ OFTypeMember('type', 'uint8_t', 3),
OFLengthMember('length', 'uint16_t'),
OFDataMember('xid', 'uint32_t'),
OFDataMember('data', 'of_octets_t')]),
diff --git a/utest/test_parser.py b/utest/test_parser.py
index a4a0e63..cb22a9c 100755
--- a/utest/test_parser.py
+++ b/utest/test_parser.py
@@ -83,7 +83,7 @@
self.assertEquals(ast,
[['struct', 'foo', [['data', 'list(of_action_t)', 'bar']]]])
- def test_pad_type(self):
+ def test_pad_member(self):
src = """\
struct foo {
pad(1);
@@ -93,6 +93,16 @@
self.assertEquals(ast,
[['struct', 'foo', [['pad', 1]]]])
+ def test_type_member(self):
+ src = """\
+struct foo {
+ uint16_t foo == 0x10;
+};
+"""
+ ast = parser.parse(src)
+ self.assertEquals(ast,
+ [['struct', 'foo', [['type', 'uint16_t', 'foo', 0x10]]]])
+
class EnumTests(unittest.TestCase):
def test_empty(self):
src = """\