changeset 766:0af3b145a405

Revised TemplateParameterList parsing functions.
author Aziz K?ksal <aziz.koeksal@gmail.com>
date Sat, 16 Feb 2008 21:14:40 +0100
parents bc812843603c
children 1771398583fa
files trunk/src/cmd/DDoc.d trunk/src/dil/ast/Declarations.d trunk/src/dil/ast/DefaultVisitor.d trunk/src/dil/parser/Parser.d
diffstat 4 files changed, 31 insertions(+), 44 deletions(-) [+]
line wrap: on
line diff
--- a/trunk/src/cmd/DDoc.d	Sat Feb 16 20:18:42 2008 +0100
+++ b/trunk/src/cmd/DDoc.d	Sat Feb 16 21:14:40 2008 +0100
@@ -160,7 +160,6 @@
     return Token.textSpan(left, right);
   }
 
-  bool isTemplatized; /// True if an aggregate declaration is templatized.
   TemplateParameters tparams; /// The template parameters of the declaration.
 
   DDocComment cmnt; /// Current comment.
@@ -416,10 +415,9 @@
 
   void writeTemplateParams()
   {
-    if (!isTemplatized)
+    if (!tparams)
       return;
-    write("(", (tparams ? escape(textSpan(tparams.begin, tparams.end)) : ""), ")");
-    isTemplatized = false;
+    write(escape(textSpan(tparams.begin, tparams.end)));
     tparams = null;
   }
 
@@ -581,12 +579,10 @@
 
   D visit(TemplateDeclaration d)
   {
-    this.isTemplatized = true;
     this.tparams = d.tparams;
     if (d.begin.kind != TOK.Template)
     { // This is a templatized class/interface/struct/union/function.
       super.visit(d.decls);
-      this.isTemplatized = false;
       this.tparams = null;
       return d;
     }
--- a/trunk/src/dil/ast/Declarations.d	Sat Feb 16 20:18:42 2008 +0100
+++ b/trunk/src/dil/ast/Declarations.d	Sat Feb 16 21:14:40 2008 +0100
@@ -205,7 +205,7 @@
   {
     super.hasBody = true;
     mixin(set_kind);
-    addOptChild(tparams);
+    addChild(tparams);
     addChild(decls);
 
     this.name = name;
@@ -237,7 +237,7 @@
   {
     super(name, /+tparams, +/decls);
     mixin(set_kind);
-//     addOptChild(tparams);
+//     addChild(tparams);
     addOptChildren(bases);
     addOptChild(decls);
 
@@ -254,7 +254,7 @@
   {
     super(name, /+tparams, +/decls);
     mixin(set_kind);
-//     addOptChild(tparams);
+//     addChild(tparams);
     addOptChildren(bases);
     addOptChild(decls);
 
@@ -273,7 +273,7 @@
   {
     super(name, /+tparams, +/decls);
     mixin(set_kind);
-//     addOptChild(tparams);
+//     addChild(tparams);
     addOptChild(decls);
   }
 
@@ -291,7 +291,7 @@
   {
     super(name, /+tparams, +/decls);
     mixin(set_kind);
-//     addOptChild(tparams);
+//     addChild(tparams);
     addOptChild(decls);
   }
 
@@ -367,7 +367,7 @@
     super.hasBody = funcBody.funcBody !is null;
     mixin(set_kind);
     addChild(returnType);
-//     addOptChild(tparams);
+//     addChild(tparams);
     addChild(params);
     addChild(funcBody);
 
--- a/trunk/src/dil/ast/DefaultVisitor.d	Sat Feb 16 20:18:42 2008 +0100
+++ b/trunk/src/dil/ast/DefaultVisitor.d	Sat Feb 16 21:14:40 2008 +0100
@@ -47,13 +47,13 @@
       d.value && visitE(d.value);
     static if (is(D == ClassDeclaration) || is( D == InterfaceDeclaration))
     {
-//       d.tparams && visitN(d.tparams);
+//       visitN(d.tparams);
       foreach (base; d.bases)
         visitT(base);
       d.decls && visitD(d.decls);
     }
     static if (is(D == StructDeclaration) || is(D == UnionDeclaration))
-//       d.tparams && visitN(d.tparams),
+//       visitN(d.tparams),
       d.decls && visitD(d.decls);
     static if (is(D == ConstructorDeclaration))
       visitN(d.params), visitS(d.funcBody);
@@ -65,7 +65,7 @@
       visitS(d.funcBody);
     static if (is(D == FunctionDeclaration))
       visitT(d.returnType),
-//       d.tparams && visitN(d.tparams),
+//       visitN(d.tparams),
       visitN(d.params),
       visitS(d.funcBody);
     static if (is(D == VariablesDeclaration))
@@ -85,7 +85,7 @@
       visitE(d.condition),
       d.message && visitE(d.message);
     static if (is(D == TemplateDeclaration))
-      d.tparams && visitN(d.tparams),
+      visitN(d.tparams),
       visitD(d.decls);
     static if (is(D == NewDeclaration) || is(D == DeleteDeclaration))
       visitN(d.params),
--- a/trunk/src/dil/parser/Parser.d	Sat Feb 16 20:18:42 2008 +0100
+++ b/trunk/src/dil/parser/Parser.d	Sat Feb 16 21:14:40 2008 +0100
@@ -484,12 +484,9 @@
         assert(token.kind == T.LParen);
         // It's a function declaration
         TemplateParameters tparams;
-        bool isTemplate;
         if (tokenAfterParenIs(T.LParen))
-        { // ( TemplateParameterList ) ( ParameterList )
+          // ( TemplateParameterList ) ( ParameterList )
           tparams = parseTemplateParameterList();
-          isTemplate = true;
-        }
 
         auto params = parseParameterList();
       version(D2)
@@ -513,7 +510,7 @@
         fd.setStorageClass(stc);
         fd.setLinkageType(linkType);
         fd.setProtection(protection);
-        if (isTemplate)
+        if (tparams)
         {
           auto d = putInsideTemplateDeclaration(begin, ident, fd, tparams);
           d.setStorageClass(stc);
@@ -1059,8 +1056,7 @@
 
     className = requireIdentifier(MSG.ExpectedClassName);
 
-    bool isTemplate = token.kind == T.LParen;
-    if (isTemplate)
+    if (token.kind == T.LParen)
       tparams = parseTemplateParameterList();
 
     if (token.kind == T.Colon)
@@ -1074,7 +1070,7 @@
       error(token, MSG.ExpectedClassBody, token.srcText);
 
     Declaration d = new ClassDeclaration(className, /+tparams, +/bases, decls);
-    if (isTemplate)
+    if (tparams)
       d = putInsideTemplateDeclaration(begin, className, d, tparams);
     return d;
   }
@@ -1125,8 +1121,7 @@
 
     name = requireIdentifier(MSG.ExpectedInterfaceName);
 
-    bool isTemplate = token.kind == T.LParen;
-    if (isTemplate)
+    if (token.kind == T.LParen)
       tparams = parseTemplateParameterList();
 
     if (token.kind == T.Colon)
@@ -1140,7 +1135,7 @@
       error(token, MSG.ExpectedInterfaceBody, token.srcText);
 
     Declaration d = new InterfaceDeclaration(name, /+tparams, +/bases, decls);
-    if (isTemplate)
+    if (tparams)
       d = putInsideTemplateDeclaration(begin, name, d, tparams);
     return d;
   }
@@ -1157,8 +1152,7 @@
 
     name = optionalIdentifier();
 
-    bool isTemplate = name && token.kind == T.LParen;
-    if (isTemplate)
+    if (name && token.kind == T.LParen)
       tparams = parseTemplateParameterList();
 
     if (name && skipped(T.Semicolon))
@@ -1167,8 +1161,8 @@
       decls = parseDeclarationDefinitionsBody();
     else
       error(token, begin.kind == T.Struct ?
-            MSG.ExpectedStructBody :
-            MSG.ExpectedUnionBody, token.srcText);
+                   MSG.ExpectedStructBody :
+                   MSG.ExpectedUnionBody, token.srcText);
 
     Declaration d;
     if (begin.kind == T.Struct)
@@ -1180,7 +1174,7 @@
     else
       d = new UnionDeclaration(name, /+tparams, +/decls);
 
-    if (isTemplate)
+    if (tparams)
       d = putInsideTemplateDeclaration(begin, name, d, tparams);
     return d;
   }
@@ -3935,12 +3929,13 @@
 
   TemplateParameters parseTemplateParameterList()
   {
-    TemplateParameters tparams;
+    auto begin = token;
+    auto tparams = new TemplateParameters;
     require(T.LParen);
     if (token.kind != T.RParen)
-      tparams = parseTemplateParameterList_();
+      parseTemplateParameterList_(tparams);
     require(T.RParen);
-    return tparams;
+    return set(tparams, begin);
   }
 
 version(D2)
@@ -3949,20 +3944,18 @@
   {
     assert(token.kind == T.Comma);
     nT();
-    TemplateParameters tparams;
+    auto begin = token;
+    auto tparams = new TemplateParameters;
     if (token.kind != T.RParen)
-      tparams = parseTemplateParameterList_();
+      parseTemplateParameterList_(tparams);
     else
       error(token, MSG.ExpectedTemplateParameters);
-    return tparams;
+    return set(tparams, begin);
   }
 } // version(D2)
 
-  TemplateParameters parseTemplateParameterList_()
+  void parseTemplateParameterList_(TemplateParameters tparams)
   {
-    auto begin = token;
-    auto tparams = new TemplateParameters;
-
     do
     {
       auto paramBegin = token;
@@ -4047,8 +4040,6 @@
       tparams ~= set(tp, paramBegin);
 
     } while (skipped(T.Comma))
-    set(tparams, begin);
-    return tparams;
   }
 
   void expected(TOK tok)