changeset 113:20d8ae8a3fd9

- Implemented parseAggregateDeclaration for Struct- and UnionDeclarations.
author aziz
date Sun, 08 Jul 2007 21:24:03 +0000
parents 004d98df65af
children 83bb5190c0fc
files trunk/src/Declarations.d trunk/src/Parser.d
diffstat 2 files changed, 69 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/trunk/src/Declarations.d	Sun Jul 08 20:34:05 2007 +0000
+++ b/trunk/src/Declarations.d	Sun Jul 08 21:24:03 2007 +0000
@@ -80,3 +80,25 @@
     this.decls = decls;
   }
 }
+
+class StructDeclaration : Declaration
+{
+  string name;
+  Declaration[] decls;
+  this(string name, Declaration[] decls)
+  {
+    this.name = name;
+    this.decls = decls;
+  }
+}
+
+class UnionDeclaration : Declaration
+{
+  string name;
+  Declaration[] decls;
+  this(string name, Declaration[] decls)
+  {
+    this.name = name;
+    this.decls = decls;
+  }
+}
--- a/trunk/src/Parser.d	Sun Jul 08 20:34:05 2007 +0000
+++ b/trunk/src/Parser.d	Sun Jul 08 21:24:03 2007 +0000
@@ -165,6 +165,9 @@
     case T.Interface:
       decl = parseInterfaceDeclaration();
       break;
+    case T.Struct, T.Union:
+      decl = parseAggregateDeclaration();
+      break;
     case T.Module:
       // Error: module is optional and can only appear once at the top of the source file.
       break;
@@ -292,6 +295,8 @@
     else
       errorIfNot(T.LBrace); // TODO: better error msg
 
+    // TODO: error if decls.length == 0
+
     return new ClassDeclaration(className, bases, decls);
   }
 
@@ -375,9 +380,51 @@
     else
       errorIfNot(T.LBrace); // TODO: better error msg
 
+    // TODO: error if decls.length == 0
+
     return new InterfaceDeclaration(name, bases, decls);
   }
 
+  Declaration parseAggregateDeclaration()
+  {
+    assert(token.type == T.Struct || token.type == T.Union);
+
+    TOK tok = token.type;
+
+    string name;
+    Declaration[] decls;
+
+    nT();
+    if (token.type == T.Identifier)
+    {
+      name = token.identifier;
+      nT();
+    }
+
+    if (token.type == T.LParen)
+    {
+      // TODO: parse template parameters
+    }
+
+    if (token.type == T.Semicolon)
+      nT();
+    else if (token.type == T.LBrace)
+    {
+      nT();
+      decls = parseDeclarationDefinitions();
+      require(T.RBrace);
+    }
+    else
+      errorIfNot(T.LBrace); // TODO: better error msg
+
+    // TODO: error if decls.length == 0
+
+    if (tok == T.Struct)
+      return new StructDeclaration(name, decls);
+    else
+      return new UnionDeclaration(name, decls);
+  }
+
   /+++++++++++++++++++++++++++++
   + Expression parsing methods +
   +++++++++++++++++++++++++++++/