changeset 107:722c05bbd5eb

- Implemented parseEnumDeclaration() and added class EnumDeclaration. - Added 'alias srcText identifier' to Token for Identifiers. - Replaced all instances of token.srcText with token.identifier where applicable.
author aziz
date Sun, 08 Jul 2007 15:22:03 +0000
parents 441962b0f526
children 469188935d56
files trunk/src/Declarations.d trunk/src/Parser.d trunk/src/Token.d
diffstat 3 files changed, 85 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/trunk/src/Declarations.d	Sun Jul 08 13:33:02 2007 +0000
+++ b/trunk/src/Declarations.d	Sun Jul 08 15:22:03 2007 +0000
@@ -3,6 +3,8 @@
   License: GPL2
 +/
 module Declarations;
+import Expressions;
+import Types;
 
 class Declaration
 {
@@ -18,3 +20,17 @@
   }
 }
 
+class EnumDeclaration : Declaration
+{
+  string name;
+  Type baseType;
+  string[] members;
+  Expression[] values;
+  this(string name, Type baseType, string[] members, Expression[] values)
+  {
+    this.name = name;
+    this.baseType = baseType;
+    this.members = members;
+    this.values = values;
+  }
+}
--- a/trunk/src/Parser.d	Sun Jul 08 13:33:02 2007 +0000
+++ b/trunk/src/Parser.d	Sun Jul 08 15:22:03 2007 +0000
@@ -155,7 +155,9 @@
         goto case_AttributeSpecifier;
       //goto case T.Import;
     case T.Import:
-      parseImportDeclaration();
+      decl = parseImportDeclaration();
+    case T.Enum:
+      decl = parseEnumDeclaration();
     case T.Module:
       // Error: module is optional and can only appear once at the top of the source file.
     default:
@@ -172,6 +174,8 @@
 
   Declaration parseImportDeclaration()
   {
+    assert(token.type == T.Import || token.type == T.Static);
+
     Declaration decl;
     bool isStatic;
 
@@ -184,6 +188,60 @@
     return decl;
   }
 
+  Declaration parseEnumDeclaration()
+  {
+    assert(token.type == T.Enum);
+
+    string enumName;
+    Type baseType;
+    string[] members;
+    Expression[] values;
+
+    nT();
+    if (token.type == T.Identifier)
+    {
+      enumName = token.srcText;
+      nT();
+    }
+
+    if (token.type == T.Colon)
+    {
+      nT();
+      baseType = parseBasicType();
+    }
+
+    if (token.type == T.LBrace)
+    {
+      nT();
+      do
+      {
+        if (token.type == T.Identifier)
+        {
+          members ~= token.identifier;
+          nT();
+        }
+        else
+          errorIfNot(T.Identifier);
+
+        if (token.type == T.Assign)
+        {
+          values ~= parseAssignExpression();
+          nT();
+        }
+        else
+          values ~= null;
+
+        if (token.type == T.Comma)
+          nT();
+        else if (token.type != T.RBrace)
+          errorIfNot(T.RBrace);
+      } while (token.type != T.RBrace)
+      nT();
+    }
+
+    return new EnumDeclaration(enumName, baseType, members, values);
+  }
+
   /+++++++++++++++++++++++++++++
   + Expression parsing methods +
   +++++++++++++++++++++++++++++/
@@ -464,7 +522,7 @@
       string ident;
       if (token.type == T.Identifier)
       {
-        ident = token.srcText;
+        ident = token.identifier;
         nT();
       }
       else
@@ -547,12 +605,12 @@
     switch (token.type)
     {
     case T.Identifier:
-      e = new IdentifierExpression(token.srcText);
+      e = new IdentifierExpression(token.identifier);
       nT();
       break;
     case T.Dot:
       requireNext(T.Identifier);
-      e = new GlobalIdExpression(token.srcText);
+      e = new GlobalIdExpression(token.identifier);
       break;
     case T.This:
       nT();
@@ -706,7 +764,7 @@
         string ident;
         if (token.type == T.Identifier)
         {
-          ident = token.srcText;
+          ident = token.identifier;
           nT();
         }
         else
@@ -773,7 +831,7 @@
       string ident;
       if (token.type == T.Identifier)
       {
-        ident = token.srcText;
+        ident = token.identifier;
         nT();
       }
       else
@@ -837,7 +895,7 @@
       nT();
       break;
     case T.Identifier, T.Dot:
-      tident = new IdentifierType([token.srcText]);
+      tident = new IdentifierType([token.identifier]);
       nT();
       // TODO: parse template instance
 //       if (token.type == T.Not)
@@ -848,7 +906,7 @@
         nT();
         if (token.type == T.Identifier)
         {
-          tident ~= token.srcText;
+          tident ~= token.identifier;
         }
         else
           errorIfNot(T.Identifier);
@@ -958,7 +1016,7 @@
 
     if (token.type == T.Identifier)
     {
-      ident = token.srcText;
+      ident = token.identifier;
       nT();
       t = parseDeclaratorSuffix(t);
     }
--- a/trunk/src/Token.d	Sun Jul 08 13:33:02 2007 +0000
+++ b/trunk/src/Token.d	Sun Jul 08 15:22:03 2007 +0000
@@ -121,6 +121,8 @@
     real   real_;
   }
 
+  alias srcText identifier;
+
   string srcText()
   {
     assert(start && end);