Initial import

LoxiGen is the work of several developers, not just myself.
diff --git a/utest/c_utils_test.py b/utest/c_utils_test.py
new file mode 100755
index 0000000..5b28e57
--- /dev/null
+++ b/utest/c_utils_test.py
@@ -0,0 +1,66 @@
+#!/usr/bin/python
+# Copyright 2013, Big Switch Networks, Inc.
+#
+# LoxiGen is licensed under the Eclipse Public License, version 1.0 (EPL), with
+# the following special exception:
+#
+# LOXI Exception
+#
+# As a special exception to the terms of the EPL, you may distribute libraries
+# generated by LoxiGen (LoxiGen Libraries) under the terms of your choice, provided
+# that copyright and licensing notices generated by LoxiGen are not altered or removed
+# from the LoxiGen Libraries and the notice provided below is (i) included in
+# the LoxiGen Libraries, if distributed in source code form and (ii) included in any
+# documentation for the LoxiGen Libraries, if distributed in binary form.
+#
+# Notice: "Copyright 2013, Big Switch Networks, Inc. This library was generated by the LoxiGen Compiler."
+#
+# You may not use this file except in compliance with the EPL or LOXI Exception. You may obtain
+# a copy of the EPL at:
+#
+# http://www.eclipse.org/legal/epl-v10.html
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# EPL for the specific language governing permissions and limitations
+# under the EPL.
+
+#
+# Some test code for c_utils.py
+#
+
+import sys
+sys.path.append("../loxi_front_end")
+sys.path.append("..")
+
+from c_parse_utils import *
+
+for filename in [
+    '../canonical/openflow.h-1.0',
+    '../canonical/openflow.h-1.1',
+    '../canonical/openflow.h-1.2']:
+
+    f = open(filename, 'r')
+    all_lines = f.readlines()
+    contents = " ".join(all_lines)
+
+    print "clean_up"
+    print clean_up_input(contents)
+
+#print "structs"
+#for x in extract_structs(c):
+#    print x
+
+    all_enums = extract_enums(contents)
+    print "Got %d enums for %s" % (len(all_enums), filename)
+    for x in all_enums:
+        name, entries =  extract_enum_vals(x)
+        print "Enum name %s has %d entries" % (name, len(entries))
+        for item in entries:
+            print "  key=%s, value=%s" % (item[0], str(item[1]))
+
+    all_defs = extract_defines(contents)
+    print "Got %d defines for %s" % (len(all_defs), filename)
+    for x in all_defs:
+        print "  name=%s, value=%s" % (x[0], str(x[1]))
diff --git a/utest/identifiers_test.py b/utest/identifiers_test.py
new file mode 100755
index 0000000..e8ff2cd
--- /dev/null
+++ b/utest/identifiers_test.py
@@ -0,0 +1,78 @@
+#!/usr/bin/python
+# Copyright 2013, Big Switch Networks, Inc.
+#
+# LoxiGen is licensed under the Eclipse Public License, version 1.0 (EPL), with
+# the following special exception:
+#
+# LOXI Exception
+#
+# As a special exception to the terms of the EPL, you may distribute libraries
+# generated by LoxiGen (LoxiGen Libraries) under the terms of your choice, provided
+# that copyright and licensing notices generated by LoxiGen are not altered or removed
+# from the LoxiGen Libraries and the notice provided below is (i) included in
+# the LoxiGen Libraries, if distributed in source code form and (ii) included in any
+# documentation for the LoxiGen Libraries, if distributed in binary form.
+#
+# Notice: "Copyright 2013, Big Switch Networks, Inc. This library was generated by the LoxiGen Compiler."
+#
+# You may not use this file except in compliance with the EPL or LOXI Exception. You may obtain
+# a copy of the EPL at:
+#
+# http://www.eclipse.org/legal/epl-v10.html
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# EPL for the specific language governing permissions and limitations
+# under the EPL.
+
+#
+# Some test code for identifiers.py
+#
+
+import sys
+sys.path.append("..")
+sys.path.append("../loxi_front_end")
+sys.path.append("../loxi_utils")
+
+from identifiers import *
+
+test_dict = {}
+group_dict = {}
+
+for ver, filename in [
+    (1, '../canonical/openflow.h-1.0'),
+    (2, '../canonical/openflow.h-1.1'),
+    (3, '../canonical/openflow.h-1.2'),
+    (4, '../canonical/openflow.h-1.3')]:
+
+    f = open(filename, 'r')
+    all_lines = f.readlines()
+    contents = " ".join(all_lines)
+
+    add_identifiers(test_dict, group_dict, ver, contents)
+
+version_list = [1,2,3,4]
+print "Merged %d entries from files" % len(test_dict)
+
+for ident, info in test_dict.items():
+    print """
+Name %s:
+  common %s
+  num vals %d
+  all agree %s
+  defined agree %s
+  ofp name %s
+  group %s""" % (ident, str(info["common_value"]), 
+                  len(info["values_by_version"]),
+                  all_versions_agree(test_dict, version_list, ident),
+                  defined_versions_agree(test_dict, version_list, ident),
+                  info["ofp_name"], info["ofp_group"])
+
+    for version, value in info["values_by_version"].items():
+        print "  version %d value %s" % (version, value)
+
+for ident, loxi_list in group_dict.items():
+    print "Group %s:" % ident
+    for loxi_name in loxi_list:
+        print "   %s" % loxi_name
diff --git a/utest/of_h_utils_test.py b/utest/of_h_utils_test.py
new file mode 100755
index 0000000..0a4a6f9
--- /dev/null
+++ b/utest/of_h_utils_test.py
@@ -0,0 +1,54 @@
+#!/usr/bin/python
+# Copyright 2013, Big Switch Networks, Inc.
+#
+# LoxiGen is licensed under the Eclipse Public License, version 1.0 (EPL), with
+# the following special exception:
+#
+# LOXI Exception
+#
+# As a special exception to the terms of the EPL, you may distribute libraries
+# generated by LoxiGen (LoxiGen Libraries) under the terms of your choice, provided
+# that copyright and licensing notices generated by LoxiGen are not altered or removed
+# from the LoxiGen Libraries and the notice provided below is (i) included in
+# the LoxiGen Libraries, if distributed in source code form and (ii) included in any
+# documentation for the LoxiGen Libraries, if distributed in binary form.
+#
+# Notice: "Copyright 2013, Big Switch Networks, Inc. This library was generated by the LoxiGen Compiler."
+#
+# You may not use this file except in compliance with the EPL or LOXI Exception. You may obtain
+# a copy of the EPL at:
+#
+# http://www.eclipse.org/legal/epl-v10.html
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# EPL for the specific language governing permissions and limitations
+# under the EPL.
+
+#
+# Some test code for of_h_utils.py
+#
+
+import sys
+sys.path.append("..")
+sys.path.append("../loxi_front_end")
+sys.path.append("../loxi_utils")
+
+from of_h_utils import *
+
+for filename, version in [
+    ('../canonical/openflow.h-1.0', 1),
+    ('../canonical/openflow.h-1.1', 2),
+    ('../canonical/openflow.h-1.2', 3),
+    ('../canonical/openflow.h-1.3', 4)]:
+
+    f = open(filename, 'r')
+    all_lines = f.readlines()
+    contents = " ".join(all_lines)
+
+    enum_dict = get_enum_dict(version, contents)
+    print "Got %d LOXI entries %s" % (len(enum_dict), filename)
+    for name, entry in enum_dict.items():
+        print "Enum %s:\n  ofp_name: %s.\n  ofp_group: %s.\n  value: %s" % (
+            name, entry.ofp_name, entry.ofp_group, str(entry.value))
diff --git a/utest/test_parser.py b/utest/test_parser.py
new file mode 100755
index 0000000..75e0c5d
--- /dev/null
+++ b/utest/test_parser.py
@@ -0,0 +1,161 @@
+#!/usr/bin/env python
+# Copyright 2013, Big Switch Networks, Inc.
+#
+# LoxiGen is licensed under the Eclipse Public License, version 1.0 (EPL), with
+# the following special exception:
+#
+# LOXI Exception
+#
+# As a special exception to the terms of the EPL, you may distribute libraries
+# generated by LoxiGen (LoxiGen Libraries) under the terms of your choice, provided
+# that copyright and licensing notices generated by LoxiGen are not altered or removed
+# from the LoxiGen Libraries and the notice provided below is (i) included in
+# the LoxiGen Libraries, if distributed in source code form and (ii) included in any
+# documentation for the LoxiGen Libraries, if distributed in binary form.
+#
+# Notice: "Copyright 2013, Big Switch Networks, Inc. This library was generated by the LoxiGen Compiler."
+#
+# You may not use this file except in compliance with the EPL or LOXI Exception. You may obtain
+# a copy of the EPL at:
+#
+# http://www.eclipse.org/legal/epl-v10.html
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# EPL for the specific language governing permissions and limitations
+# under the EPL.
+
+import unittest
+import pyparsing
+import loxi_front_end.parser as parser
+
+class StructTests(unittest.TestCase):
+    def test_empty(self):
+        src = """\
+struct foo { };
+"""
+        ast = parser.parse(src)
+        self.assertEquals(ast.asList(), [['struct', 'foo', []]])
+
+    def test_one_field(self):
+        src = """\
+struct foo {
+    uint32_t bar;
+};
+"""
+        ast = parser.parse(src)
+        self.assertEquals(ast.asList(),
+            [['struct', 'foo', [['uint32_t', 'bar']]]])
+
+    def test_multiple_fields(self):
+        src = """\
+struct foo {
+    uint32_t bar;
+    uint8_t baz;
+    uint64_t abc;
+};
+"""
+        ast = parser.parse(src)
+        self.assertEquals(ast.asList(),
+            [['struct', 'foo',
+                [['uint32_t', 'bar'],
+                 ['uint8_t', 'baz'],
+                 ['uint64_t', 'abc']]]])
+
+    def test_array_type(self):
+        src = """\
+struct foo {
+    uint32_t[4] bar;
+};
+"""
+        ast = parser.parse(src)
+        self.assertEquals(ast.asList(),
+            [['struct', 'foo', [['uint32_t[4]', 'bar']]]])
+
+    def test_list_type(self):
+        src = """\
+struct foo {
+    list(of_action_t) bar;
+};
+"""
+        ast = parser.parse(src)
+        self.assertEquals(ast.asList(),
+            [['struct', 'foo', [['list(of_action_t)', 'bar']]]])
+
+class TestMetadata(unittest.TestCase):
+    def test_version(self):
+        src = """\
+#version 1
+"""
+        ast = parser.parse(src)
+        self.assertEquals(ast.asList(), [['metadata', 'version', '1']])
+
+class TestToplevel(unittest.TestCase):
+    def test_multiple_structs(self):
+        src = """\
+struct foo { };
+struct bar { };
+"""
+        ast = parser.parse(src)
+        self.assertEquals(ast.asList(),
+            [['struct', 'foo', []], ['struct', 'bar', []]])
+
+    def test_comments(self):
+        src = """\
+// comment 1
+struct foo { //comment 2
+// comment 3
+   uint32_t a; //comment 5 
+// comment 6
+};
+// comment 4
+"""
+        ast = parser.parse(src)
+        self.assertEquals(ast.asList(),
+            [['struct', 'foo', [['uint32_t', 'a']]]])
+
+    def test_mixed(self):
+        src = """\
+#version 1
+struct foo { };
+#version 2
+struct bar { };
+"""
+        ast = parser.parse(src)
+        self.assertEquals(ast.asList(),
+            [['metadata', 'version', '1'],
+             ['struct', 'foo', []],
+             ['metadata', 'version', '2'],
+             ['struct', 'bar', []]])
+
+class TestErrors(unittest.TestCase):
+    def syntax_error(self, src, regex):
+        with self.assertRaisesRegexp(pyparsing.ParseSyntaxException, regex):
+            parser.parse(src)
+
+    def test_missing_struct_syntax(self):
+        self.syntax_error('struct { uint32_t bar; };',
+                          'Expected identifier \(at char 7\)')
+        self.syntax_error('struct foo uint32_t bar; };',
+                          'Expected "{" \(at char 11\)')
+        self.syntax_error('struct foo { uint32_t bar; ;',
+                          'Expected "}" \(at char 27\)')
+        self.syntax_error('struct foo { uint32_t bar; }',
+                          'Expected ";" \(at char 28\)')
+
+    def test_invalid_type_name(self):
+        self.syntax_error('struct foo { list<of_action_t> bar; }',
+                          'Expected "\(" \(at char 17\)')
+        self.syntax_error('struct foo { uint32_t[10 bar; }',
+                          'Expected "\]" \(at char 24\)')
+
+    def test_invalid_member_syntax(self):
+        self.syntax_error('struct foo { bar; }',
+                          'Expected identifier \(at char 16\)')
+        self.syntax_error('struct foo { uint32_t bar baz; }',
+                          'Expected ";" \(at char 26\)')
+
+
+if __name__ == '__main__':
+    unittest.main()