changeset 117:79857de26e86

- Moved class Parameter to module Types. Added struct Parameters. - Implemented parseConstructorDeclaration(). - Added method stub parseStatements(). - Restructured parseBaseClasses() a bit.
author aziz
date Mon, 09 Jul 2007 16:36:05 +0000
parents f0c1883cdd4c
children 379f33cbd521
files trunk/src/Declarations.d trunk/src/Parser.d trunk/src/Types.d
diffstat 3 files changed, 92 insertions(+), 43 deletions(-) [+]
line wrap: on
line diff
--- a/trunk/src/Declarations.d	Mon Jul 09 13:40:01 2007 +0000
+++ b/trunk/src/Declarations.d	Mon Jul 09 16:36:05 2007 +0000
@@ -5,6 +5,7 @@
 module Declarations;
 import Expressions;
 import Types;
+import Statements;
 
 class Declaration
 {
@@ -130,3 +131,15 @@
     this.decls = decls;
   }
 }
+
+class ConstructorDeclaration : Declaration
+{
+  Parameters parameters;
+  Statement[] statements;
+  this(Parameters parameters, Statement[] statements)
+  {
+    super(true);
+    this.parameters = parameters;
+    this.statements = statements;
+  }
+}
--- a/trunk/src/Parser.d	Mon Jul 09 13:40:01 2007 +0000
+++ b/trunk/src/Parser.d	Mon Jul 09 16:36:05 2007 +0000
@@ -12,32 +12,6 @@
 import Expressions;
 import Types;
 
-class Parameter
-{
-  StorageClass stc;
-  Type type;
-  string ident;
-  Expression assignExpr;
-
-  this(StorageClass stc, Type type, string ident, Expression assignExpr)
-  {
-    this.stc = stc;
-    this.type = type;
-    this.ident = ident;
-    this.assignExpr = assignExpr;
-  }
-
-  bool isVariadic()
-  {
-    return !!(stc & StorageClass.Variadic);
-  }
-
-  bool isOnlyVariadic()
-  {
-    return stc == StorageClass.Variadic;
-  }
-}
-
 private alias TOK T;
 
 class Parser
@@ -156,6 +130,9 @@
     case T.Struct, T.Union:
       decl = parseAggregateDeclaration();
       break;
+    case T.This:
+      decl = parseConstructorDeclaration();
+      break;
     case T.Module:
       // Error: module is optional and can only appear once at the top of the source file.
       break;
@@ -164,6 +141,11 @@
     return null;
   }
 
+  Statement[] parseStatements()
+  {
+    return null;
+  }
+
   Declaration parseAttributeSpecifier()
   {
     // Attribute :
@@ -357,13 +339,13 @@
     assert(token.type == T.Colon);
 
     BaseClass[] bases;
-    Protection prot;
 
-    nT();
+    nT(); // Skip colon
 
     while (1)
     {
-      prot = Protection.Public;
+      Protection prot = Protection.Public;
+      string name;
       switch (token.type)
       {
       case T.Identifier: goto LisIdentifier;
@@ -377,17 +359,16 @@
         return bases;
       }
       nT();
-      string ident;
       if (token.type == T.Identifier)
       {
       LisIdentifier:
-        ident = token.identifier;
+        name = token.identifier;
         nT();
         // TODO: handle template instantiations: class Foo : Bar!(int)
       }
       else
         errorIfNot(T.Identifier);
-      bases ~= new BaseClass(prot, ident);
+      bases ~= new BaseClass(prot, name);
       if (token.type != T.Comma)
         break;
     }
@@ -481,6 +462,17 @@
       return new UnionDeclaration(name, decls, hasDefinition);
   }
 
+  Declaration parseConstructorDeclaration()
+  {
+    assert(token.type == T.This);
+    nT(); // Skip 'this' keyword.
+    auto parameters = parseParameters();
+    require(T.LBrace);
+    auto statements = parseStatements();
+    require(T.RBrace);
+    return new ConstructorDeclaration(parameters, statements);
+  }
+
   /+++++++++++++++++++++++++++++
   + Expression parsing methods +
   +++++++++++++++++++++++++++++/
@@ -1236,13 +1228,13 @@
     return t;
   }
 
-  Parameter[] parseParameters()
-  out(args)
+  Parameters parseParameters()
+  out(params)
   {
-    if (args.length > 1)
-      foreach (arg; args[0..$-1])
+    if (params.length > 1)
+      foreach (param; params.items[0..$-1])
       {
-        if (arg.isVariadic())
+        if (param.isVariadic())
           assert(0, "variadic arguments can only appear at the end of the parameter list.");
       }
   }
@@ -1253,10 +1245,10 @@
     if (token.type == T.RParen)
     {
       nT();
-      return null;
+      return Parameters.init;
     }
 
-    Parameter[] args;
+    Parameters params;
     StorageClass stc;
 
     while (1)
@@ -1269,7 +1261,7 @@
       case T.Ref:  stc = StorageClass.Ref;  nT(); goto default;
       case T.Lazy: stc = StorageClass.Lazy; nT(); goto default;
       case T.Ellipses:
-        args ~= new Parameter(StorageClass.Variadic, null, null, null);
+        params ~= new Parameter(StorageClass.Variadic, null, null, null);
         break;
       default:
         string ident;
@@ -1285,12 +1277,12 @@
         if (token.type == T.Ellipses)
         {
           stc |= StorageClass.Variadic;
-          args ~= new Parameter(stc, type, ident, assignExpr);
+          params ~= new Parameter(stc, type, ident, assignExpr);
           nT();
           break;
         }
 
-        args ~= new Parameter(stc, type, ident, assignExpr);
+        params ~= new Parameter(stc, type, ident, assignExpr);
         if (token.type == T.Comma)
         {
           nT();
@@ -1301,7 +1293,7 @@
       break;
     }
     require(T.RParen);
-    return args;
+    return params;
   }
 
   void errorIfNot(TOK tok)
--- a/trunk/src/Types.d	Mon Jul 09 13:40:01 2007 +0000
+++ b/trunk/src/Types.d	Mon Jul 09 16:36:05 2007 +0000
@@ -27,6 +27,50 @@
   Variadic     = 1<<16,
 }
 
+class Parameter
+{
+  StorageClass stc;
+  Type type;
+  string ident;
+  Expression assignExpr;
+
+  this(StorageClass stc, Type type, string ident, Expression assignExpr)
+  {
+    this.stc = stc;
+    this.type = type;
+    this.ident = ident;
+    this.assignExpr = assignExpr;
+  }
+
+  bool isVariadic()
+  {
+    return !!(stc & StorageClass.Variadic);
+  }
+
+  bool isOnlyVariadic()
+  {
+    return stc == StorageClass.Variadic;
+  }
+}
+
+struct Parameters
+{
+  Parameter[] items;
+
+  bool hasVariadic()
+  {
+    if (items.length != 0)
+      return items[$-1].isVariadic();
+    return false;
+  }
+
+  void opCatAssign(Parameter param)
+  { items ~= param; }
+
+  size_t length()
+  { return items.length; }
+}
+
 class Type
 {
   TOK type;