2 files changed, 32 insertions(+), 19 deletions(-)
viff/sfdl/grammar.py           |   16 ++++++++++++++--
viff/test/sfdl/test_grammar.py |   35 ++++++++++++++++++-----------------


# HG changeset patch
# User Martin Geisler <[EMAIL PROTECTED]>
# Date 1228899835 -3600
# Node ID 9dccd3a382898ec0e2d817eee18ba2deeb3c1d20
# Parent  01b0e928dcbeea1b22329f4356840f3e49fefe30
Restructure struct and parse defined types.

diff --git a/viff/sfdl/grammar.py b/viff/sfdl/grammar.py
--- a/viff/sfdl/grammar.py
+++ b/viff/sfdl/grammar.py
@@ -77,17 +77,20 @@
         self.data_type = data_type = Forward()
         self.struct_field = struct_field = Group(data_type + ident)
         self.struct_type = struct_type = Keyword("struct") \
-            + "{" + delimitedList(struct_field) + "}"
+            + LCURLY + Group(delimitedList(struct_field)) + RCURLY
         self.int_type = int_type = Keyword("Int") + "<" + const_expr + ">"
         self.known_type = known_type \
             = int_type | Keyword("Boolean") | struct_type | Keyword("void")
-        data_type << (known_type | ident) \
+        self.defined_type = defined_type = ident.copy()
+        data_type << (known_type | defined_type) \
             + Optional(LBRACK + const_expr + RBRACK)
 
         self.type_dec = type_dec \
             = Keyword("type") + ident + EQUAL + data_type + SCOLON
 
         int_type.setParseAction(self.parse_int_type)
+        struct_type.setParseAction(self.parse_struct_type)
+        defined_type.setParseAction(self.parse_defined_type)
         data_type.setParseAction(self.parse_data_type)
         type_dec.setParseAction(self.parse_type_dec)
 
@@ -168,6 +171,15 @@
     def parse_int_type(self, s, loc, toks):
         return attrdict(bitsize=toks[2])
 
+    def parse_struct_type(self, s, loc, toks):
+        return attrdict([(b, a) for (a, b) in toks[1]])
+
+    def parse_defined_type(self, s, loc, toks):
+        try:
+            return self.global_scope[toks[0]]
+        except KeyError:
+            raise ParseException(s, loc, "undefined type: %s" % toks[0])
+
     def parse_data_type(self, s, loc, toks):
         if len(toks) == 2:
             return attrdict(length=toks[1])
diff --git a/viff/test/sfdl/test_grammar.py b/viff/test/sfdl/test_grammar.py
--- a/viff/test/sfdl/test_grammar.py
+++ b/viff/test/sfdl/test_grammar.py
@@ -128,22 +128,18 @@
                          [attrdict(bitsize=11)])
 
     def test_struct_type(self):
-        self.assertParse(self.grammar.struct_type, "struct { Boolean x }",
-                         ['struct', '{', ['Boolean', 'x'], '}'])
+        self.assertParse(self.grammar.struct_type, "struct { Int<8> x }",
+                         [attrdict(x=attrdict(bitsize=8))])
+        self.grammar.global_scope['int'] = attrdict(bitsize=8)
+        self.assertParse(self.grammar.struct_type, "struct { int x }",
+                         [attrdict(x=attrdict(bitsize=8))])
+
         self.assertParse(self.grammar.struct_type,
                          "struct { Boolean x, Boolean y }",
-                         ['struct', '{',
-                          ['Boolean', 'x'],
-                          ['Boolean', 'y'],
-                          '}'])
+                         [attrdict(x='Boolean', y='Boolean')])
         self.assertParse(self.grammar.struct_type,
                          "struct { struct { Boolean x } y }",
-                         ['struct', '{',
-                          ['struct',
-                           '{',
-                           ['Boolean', 'x'],
-                           '}', 'y'],
-                          '}'])
+                         [attrdict(y=attrdict(x='Boolean'))])
 
     def test_data_type(self):
         self.assertParse(self.grammar.data_type, "Boolean[4]",
@@ -156,6 +152,7 @@
     def test_type_dec(self):
         self.assertParse(self.grammar.type_dec, "type x = Boolean;",
                          ['type', 'x', 'Boolean'])
+        self.grammar.global_scope["AnotherType"] = attrdict(bitsize=8)
         self.assertParse(self.grammar.type_dec, "type xs = AnotherType[4];",
                          ['type', 'xs', attrdict(length=4)])
         self.assertParse(self.grammar.const_expr, "xs.length", [4])
@@ -309,17 +306,19 @@
     def test_func_var_dec(self):
         self.assertParse(self.grammar.func_var_dec, "var Boolean x;",
                          ['var', 'Boolean', ['x']])
+        self.grammar.global_scope["Byte"] = attrdict(bitsize=8)
         self.assertParse(self.grammar.func_var_dec, "var Byte a, b, c;",
-                         ['var', 'Byte', ['a', 'b', 'c']])
+                         ['var', attrdict(bitsize=8), ['a', 'b', 'c']])
         self.assertNoParse(self.grammar.func_var_dec, "var x;")
 
     def test_func_body(self):
+        self.grammar.global_scope["Byte"] = attrdict(bitsize=8)
         self.assertParse(self.grammar.func_body, "x = 1;", [['x', '1']])
         self.assertParse(self.grammar.func_body, "var Byte x; x = 1;",
-                         [['var', 'Byte', ['x']], ['x', '1']])
+                         [['var', attrdict(bitsize=8), ['x']], ['x', '1']])
         self.assertParse(self.grammar.func_body,
                          "var Byte x; var Boolean y; x = 1;",
-                         [['var', 'Byte', ['x']],
+                         [['var', attrdict(bitsize=8), ['x']],
                           ['var', 'Boolean', ['y']],
                           ['x', '1']])
 
@@ -328,12 +327,14 @@
         self.assertNoParse(self.grammar.func_body, "var Byte x;")
 
     def test_func_dec(self):
+        self.grammar.global_scope["Byte"] = attrdict(bitsize=8)
         self.assertParse(self.grammar.func_dec,
                          "function Byte f() { f = 10; }",
-                         ['function', 'Byte', 'f', ['f', '10']])
+                         ['function', attrdict(bitsize=8), 'f', ['f', '10']])
         self.assertParse(self.grammar.func_dec,
                          "function Byte f(Byte x) { f = x+x; }",
-                         ['function', 'Byte', 'f', ['Byte', 'x'],
+                         ['function', attrdict(bitsize=8), 'f',
+                          [attrdict(bitsize=8), 'x'],
                           ['f', ['x', '+', 'x']]])
 
         self.assertNoParse(self.grammar.func_dec,
_______________________________________________
viff-patches mailing list
[email protected]
http://lists.viff.dk/listinfo.cgi/viff-patches-viff.dk

Reply via email to