changeset 305:df237b3b5f09

- Added class Declarations. - Changed many methods to return Declarations instead of Declaration[]. Changed members of classes accordingly.
author aziz
date Tue, 14 Aug 2007 12:18:01 +0000
parents fc7b9a3a99d4
children 9c866aadcb5b
files trunk/src/Declarations.d trunk/src/Expressions.d trunk/src/Parser.d trunk/src/SyntaxTree.d trunk/src/main.d
diffstat 5 files changed, 87 insertions(+), 65 deletions(-) [+]
line wrap: on
line diff
--- a/trunk/src/Declarations.d	Tue Aug 14 11:03:04 2007 +0000
+++ b/trunk/src/Declarations.d	Tue Aug 14 12:18:01 2007 +0000
@@ -19,6 +19,25 @@
   }
 }
 
+class Declarations : Declaration
+{
+  this()
+  {
+    super(true);
+    mixin(set_kind);
+  }
+
+  void opCatAssign(Declaration d)
+  {
+    this.children ~= d;
+  }
+
+  void opCatAssign(Declarations ds)
+  {
+    this.children ~= ds.children;
+  }
+}
+
 class EmptyDeclaration : Declaration
 {
   this()
@@ -120,8 +139,8 @@
   Token* name;
   TemplateParameters tparams;
   BaseClass[] bases;
-  Declaration[] decls;
-  this(Token* name, TemplateParameters tparams, BaseClass[] bases, Declaration[] decls, bool hasBody)
+  Declarations decls;
+  this(Token* name, TemplateParameters tparams, BaseClass[] bases, Declarations decls, bool hasBody)
   {
     super(hasBody);
     mixin(set_kind);
@@ -129,8 +148,8 @@
       this.children = [tparams];
     if (bases.length)
       this.children ~= bases;
-    if (decls.length)
-      this.children ~= decls;
+    this.children ~= decls;
+
     this.name = name;
     this.tparams = tparams;
     this.bases = bases;
@@ -143,8 +162,8 @@
   Token* name;
   TemplateParameters tparams;
   BaseClass[] bases;
-  Declaration[] decls;
-  this(Token* name, TemplateParameters tparams, BaseClass[] bases, Declaration[] decls, bool hasBody)
+  Declarations decls;
+  this(Token* name, TemplateParameters tparams, BaseClass[] bases, Declarations decls, bool hasBody)
   {
     super(hasBody);
     mixin(set_kind);
@@ -152,8 +171,8 @@
       this.children = [tparams];
     if (bases.length)
       this.children ~= bases;
-    if (decls.length)
-      this.children ~= decls;
+    this.children ~= decls;
+
     this.name = name;
     this.tparams = tparams;
     this.bases = bases;
@@ -165,15 +184,15 @@
 {
   Token* name;
   TemplateParameters tparams;
-  Declaration[] decls;
-  this(Token* name, TemplateParameters tparams, Declaration[] decls, bool hasBody)
+  Declarations decls;
+  this(Token* name, TemplateParameters tparams, Declarations decls, bool hasBody)
   {
     super(hasBody);
     mixin(set_kind);
     if (tparams)
       this.children = [tparams];
-    if (decls.length)
-      this.children ~= decls;
+    this.children ~= decls;
+
     this.name = name;
     this.tparams = tparams;
     this.decls = decls;
@@ -184,15 +203,15 @@
 {
   Token* name;
   TemplateParameters tparams;
-  Declaration[] decls;
-  this(Token* name, TemplateParameters tparams, Declaration[] decls, bool hasBody)
+  Declarations decls;
+  this(Token* name, TemplateParameters tparams, Declarations decls, bool hasBody)
   {
     super(hasBody);
     mixin(set_kind);
     if (tparams)
       this.children = [tparams];
-    if (decls.length)
-      this.children ~= decls;
+    this.children ~= decls;
+
     this.name = name;
     this.tparams = tparams;
     this.decls = decls;
@@ -321,13 +340,13 @@
 {
   Token* spec;
   Token* cond;
-  Declaration[] decls, elseDecls;
+  Declaration decls, elseDecls;
 
-  this(Token* spec, Token* cond, Declaration[] decls, Declaration[] elseDecls)
+  this(Token* spec, Token* cond, Declaration decls, Declaration elseDecls)
   {
-    super(decls.length != 0);
+    super(true /+decls.length != 0+/);
     mixin(set_kind);
-    this.children = decls ~ elseDecls;
+    this.children = [decls, elseDecls];
     this.spec = spec;
     this.cond = cond;
     this.decls = decls;
@@ -339,13 +358,13 @@
 {
   Token* spec;
   Token* cond;
-  Declaration[] decls, elseDecls;
+  Declaration decls, elseDecls;
 
-  this(Token* spec, Token* cond, Declaration[] decls, Declaration[] elseDecls)
+  this(Token* spec, Token* cond, Declaration decls, Declaration elseDecls)
   {
-    super(decls.length != 0);
+    super(true /+decls.length != 0+/);
     mixin(set_kind);
-    this.children = decls ~ elseDecls;
+    this.children = [decls, elseDecls];
     this.spec = spec;
     this.cond = cond;
     this.decls = decls;
@@ -356,12 +375,12 @@
 class StaticIfDeclaration : Declaration
 {
   Expression condition;
-  Declaration[] ifDecls, elseDecls;
-  this(Expression condition, Declaration[] ifDecls, Declaration[] elseDecls)
+  Declaration ifDecls, elseDecls;
+  this(Expression condition, Declaration ifDecls, Declaration elseDecls)
   {
     super(true);
     mixin(set_kind);
-    this.children = [condition] ~ cast(Node[])(ifDecls ~ elseDecls);
+    this.children = [cast(Node)condition, ifDecls, elseDecls];
     this.condition = condition;
     this.ifDecls = ifDecls;
     this.elseDecls = elseDecls;
@@ -387,12 +406,12 @@
 {
   Token* name;
   TemplateParameters tparams;
-  Declaration[] decls;
-  this(Token* name, TemplateParameters tparams, Declaration[] decls)
+  Declarations decls;
+  this(Token* name, TemplateParameters tparams, Declarations decls)
   {
     super(true);
     mixin(set_kind);
-    this.children = [tparams] ~ cast(Node[])decls;
+    this.children = [cast(Node)tparams, decls];
     this.name = name;
     this.tparams = tparams;
     this.decls = decls;
@@ -430,12 +449,12 @@
 class AttributeDeclaration : Declaration
 {
   TOK attribute;
-  Declaration[] decls;
-  this(TOK attribute, Declaration[] decls)
+  Declaration decls;
+  this(TOK attribute, Declaration decls)
   {
     super(true);
     mixin(set_kind);
-    this.children = decls;
+    this.children = [decls];
     this.attribute = attribute;
     this.decls = decls;
   }
@@ -444,7 +463,7 @@
 class ExternDeclaration : AttributeDeclaration
 {
   Linkage linkage;
-  this(Linkage linkage, Declaration[] decls)
+  this(Linkage linkage, Declaration decls)
   {
     super(TOK.Extern, decls);
     mixin(set_kind);
@@ -455,7 +474,7 @@
 class AlignDeclaration : AttributeDeclaration
 {
   int size;
-  this(int size, Declaration[] decls)
+  this(int size, Declaration decls)
   {
     super(TOK.Align, decls);
     mixin(set_kind);
@@ -467,7 +486,7 @@
 {
   Token* ident;
   Expression[] args;
-  this(Token* ident, Expression[] args, Declaration[] decls)
+  this(Token* ident, Expression[] args, Declaration decls)
   {
     super(TOK.Pragma, decls);
     mixin(set_kind);
--- a/trunk/src/Expressions.d	Tue Aug 14 11:03:04 2007 +0000
+++ b/trunk/src/Expressions.d	Tue Aug 14 12:18:01 2007 +0000
@@ -497,8 +497,8 @@
   Expression[] newArgs;
   BaseClass[] bases;
   Expression[] ctorArgs;
-  Declaration[] decls;
-  this(/*Expression e, */Expression[] newArgs, BaseClass[] bases, Expression[] ctorArgs, Declaration[] decls)
+  Declarations decls;
+  this(/*Expression e, */Expression[] newArgs, BaseClass[] bases, Expression[] ctorArgs, Declarations decls)
   {
     /*super(e);*/
     mixin(set_kind);
@@ -508,8 +508,8 @@
       this.children ~= bases;
     if (ctorArgs.length)
       this.children ~= ctorArgs;
-    if (decls.length)
-      this.children ~= decls;
+    this.children ~= decls;
+
     this.newArgs = newArgs;
     this.bases = bases;
     this.ctorArgs = ctorArgs;
--- a/trunk/src/Parser.d	Tue Aug 14 11:03:04 2007 +0000
+++ b/trunk/src/Parser.d	Tue Aug 14 12:18:01 2007 +0000
@@ -107,9 +107,9 @@
   + Declaration parsing methods +
   ++++++++++++++++++++++++++++++/
 
-  Declaration[] parseModule()
+  Declarations parseModule()
   {
-    Declaration[] decls;
+    auto decls = new Declarations;
 
     if (token.type == T.Module)
     {
@@ -127,9 +127,9 @@
     return decls;
   }
 
-  Declaration[] parseDeclarationDefinitions()
+  Declarations parseDeclarationDefinitions()
   {
-    Declaration[] decls;
+    auto decls = new Declarations;
     while (token.type != T.EOF)
       decls ~= parseDeclarationDefinition();
     return decls;
@@ -140,9 +140,9 @@
         { }
         { DeclDefs }
   */
-  Declaration[] parseDeclarationDefinitionsBlock()
+  Declarations parseDeclarationDefinitionsBlock()
   {
-    Declaration[] decls;
+    auto decls = new Declarations;
     require(T.LBrace);
     while (token.type != T.RBrace && token.type != T.EOF)
       decls ~= parseDeclarationDefinition();
@@ -307,23 +307,25 @@
         { DeclDefs }
         DeclDef
   */
-  Declaration[] parseDeclarationsBlock()
+  Declaration parseDeclarationsBlock()
   {
-    Declaration[] decls;
+    Declaration d;
     switch (token.type)
     {
     case T.LBrace:
-      decls = parseDeclarationDefinitionsBlock();
+      d = parseDeclarationDefinitionsBlock();
       break;
     case T.Colon:
       nT();
+      auto decls = new Declarations;
       while (token.type != T.RBrace && token.type != T.EOF)
         decls ~= parseDeclarationDefinition();
+      d = decls;
       break;
     default:
-      decls ~= parseDeclarationDefinition();
+      d = parseDeclarationDefinition();
     }
-    return decls;
+    return d;
   }
 
   Declaration parseDeclaration(StorageClass stc = StorageClass.None)
@@ -548,7 +550,7 @@
         stc |= tmp;
     }
 
-    Declaration[] parse()
+    Declaration parse()
     {
       Declaration decl;
       switch (token.type)
@@ -644,11 +646,11 @@
         decl = parseDeclaration(stc);
         break;
       default:
-        return parseDeclarationsBlock();
+        decl = parseDeclarationsBlock();
       }
-      return [decl];
+      return decl;
     }
-    return parse()[0];
+    return parse();
   }
 
   Token* parseAlignAttribute()
@@ -694,7 +696,7 @@
       nT();
       Token* ident;
       Expression[] args;
-      Declaration[] decls;
+      Declaration decls;
 
       require(T.LParen);
       ident = requireId();
@@ -894,7 +896,7 @@
     Token* className;
     TemplateParameters tparams;
     BaseClass[] bases;
-    Declaration[] decls;
+    Declarations decls;
     bool hasBody;
 
     nT(); // Skip class keyword.
@@ -969,7 +971,7 @@
     Token* name;
     TemplateParameters tparams;
     BaseClass[] bases;
-    Declaration[] decls;
+    Declarations decls;
     bool hasBody;
 
     nT(); // Skip interface keyword.
@@ -1008,7 +1010,7 @@
 
     Token* name;
     TemplateParameters tparams;
-    Declaration[] decls;
+    Declarations decls;
     bool hasBody;
 
     nT(); // Skip struct or union keyword.
@@ -1116,7 +1118,7 @@
                  // debug = Identifier ;
     Token* cond; // debug ( Integer )
                  // debug ( Identifier )
-    Declaration[] decls, elseDecls;
+    Declaration decls, elseDecls;
 
     void parseIdentOrInt(ref Token* tok)
     {
@@ -1175,7 +1177,7 @@
                  // version = Identifier ;
     Token* cond; // version ( Integer )
                  // version ( Identifier )
-    Declaration[] decls, elseDecls;
+    Declaration decls, elseDecls;
 
     void parseIdentOrInt(ref Token* tok)
     {
@@ -1230,7 +1232,7 @@
     nT(); // Skip if keyword.
 
     Expression condition;
-    Declaration[] ifDecls, elseDecls;
+    Declaration ifDecls, elseDecls;
 
     require(T.LParen);
     condition = parseAssignExpression();
@@ -1526,7 +1528,7 @@
         structDecl = parseAggregateDeclaration();
       else
         expected(T.Struct);
-      d = new AlignDeclaration(size, structDecl ? [structDecl] : null);
+      d = new AlignDeclaration(size, structDecl ? structDecl : new Declarations);
       goto case_DeclarationStatement;
 /+ Not applicable for statements.
 //          T.Private,
--- a/trunk/src/SyntaxTree.d	Tue Aug 14 11:03:04 2007 +0000
+++ b/trunk/src/SyntaxTree.d	Tue Aug 14 12:18:01 2007 +0000
@@ -17,6 +17,7 @@
 enum NodeKind
 {
   // Declarations:
+  Declarations,
   EmptyDeclaration,
   IllegalDeclaration,
   ModuleDeclaration,
--- a/trunk/src/main.d	Tue Aug 14 11:03:04 2007 +0000
+++ b/trunk/src/main.d	Tue Aug 14 12:18:01 2007 +0000
@@ -29,7 +29,7 @@
   auto srctext = cast(char[]) std.file.read(args[1]);
   auto parser = new Parser(srctext, args[1]);
   parser.start();
-  auto decls = parser.parseModule();
+  auto root = parser.parseModule();
 
 void print(Node[] decls, char[] indent)
 {
@@ -40,7 +40,7 @@
     print(decl.children, indent ~ "  ");
   }
 }
-print(decls, "");
+print(root.children, "");
 foreach (error; parser.errors)
 {
   writefln(`%s(%d)P: %s`, parser.lx.fileName, error.loc, error.getMsg);