changeset 508:943ecc9c133a

Added isIntegralType() to Token and refactored code.
author Aziz K?ksal <aziz.koeksal@gmail.com>
date Wed, 12 Dec 2007 03:25:07 +0100
parents 996041463028
children baa7c4c0be78
files trunk/src/dil/Parser.d trunk/src/dil/Token.d trunk/src/dil/TokensEnum.d
diffstat 3 files changed, 78 insertions(+), 115 deletions(-) [+]
line wrap: on
line diff
--- a/trunk/src/dil/Parser.d	Wed Dec 12 02:41:30 2007 +0100
+++ b/trunk/src/dil/Parser.d	Wed Dec 12 03:25:07 2007 +0100
@@ -345,19 +345,15 @@
       break;
     // Declaration
     case T.Identifier, T.Dot, T.Typeof:
-    // IntegralType
-    case T.Char,   T.Wchar,   T.Dchar, T.Bool,
-         T.Byte,   T.Ubyte,   T.Short, T.Ushort,
-         T.Int,    T.Uint,    T.Long,  T.Ulong,
-         T.Float,  T.Double,  T.Real,
-         T.Ifloat, T.Idouble, T.Ireal,
-         T.Cfloat, T.Cdouble, T.Creal, T.Void:
     case_Declaration:
       return parseVariableOrFunction(this.storageClass, this.protection, this.linkageType);
     /+case T.Module:
       // TODO: Error: module is optional and can appear only once at the top of the source file.
       break;+/
     default:
+      if (token.isIntegralType)
+        goto case_Declaration;
+
       decl = new IllegalDeclaration();
       // Skip to next valid token.
       do
@@ -1659,6 +1655,13 @@
     auto begin = token;
     Statement s;
     Declaration d;
+
+    if (token.isIntegralType)
+    {
+      d = parseVariableOrFunction();
+      goto LreturnDeclarationStatement;
+    }
+
     switch (token.type)
     {
     case T.Align:
@@ -1677,16 +1680,9 @@
 
       d = new AlignDeclaration(size, structDecl ? cast(Declaration)structDecl : new Declarations);
       goto LreturnDeclarationStatement;
-/+ Not applicable for statements.
-//          T.Private,
-//          T.Package,
-//          T.Protected,
-//          T.Public,
-//          T.Export,
-//          T.Deprecated,
-//          T.Override,
-//          T.Abstract,
-+/
+      /+ Not applicable for statements.
+         T.Private, T.Package, T.Protected, T.Public, T.Export,
+         T.Deprecated, T.Override, T.Abstract,+/
     case T.Extern,
          T.Final,
          T.Const,
@@ -1717,16 +1713,7 @@
         goto LreturnDeclarationStatement; // Declaration
       else
         goto case_parseExpressionStatement; // Expression
-    // IntegralType
-    case T.Char,   T.Wchar,   T.Dchar, T.Bool,
-         T.Byte,   T.Ubyte,   T.Short, T.Ushort,
-         T.Int,    T.Uint,    T.Long,  T.Ulong,
-         T.Float,  T.Double,  T.Real,
-         T.Ifloat, T.Idouble, T.Ireal,
-         T.Cfloat, T.Cdouble, T.Creal, T.Void:
-    case_parseDeclaration:
-      d = parseVariableOrFunction();
-      goto LreturnDeclarationStatement;
+
     case T.If:
       s = parseIfStatement();
       break;
@@ -1864,26 +1851,10 @@
     case T.Typeid:
     case T.Is:
     case T.LParen:
-    /+ // IntegralType
-    case T.Char,   T.Wchar,   T.Dchar, T.Bool,
-         T.Byte,   T.Ubyte,   T.Short, T.Ushort,
-         T.Int,    T.Uint,    T.Long,  T.Ulong,
-         T.Float,  T.Double,  T.Real,
-         T.Ifloat, T.Idouble, T.Ireal,
-         T.Cfloat, T.Cdouble, T.Creal, T.Void:+/
     case T.Traits: // D2.0
     // Tokens that can start a UnaryExpression:
-    case T.AndBinary,
-         T.PlusPlus,
-         T.MinusMinus,
-         T.Mul,
-         T.Minus,
-         T.Plus,
-         T.Not,
-         T.Tilde,
-         T.New,
-         T.Delete,
-         T.Cast:
+    case T.AndBinary, T.PlusPlus, T.MinusMinus, T.Mul, T.Minus,
+         T.Plus, T.Not, T.Tilde, T.New, T.Delete, T.Cast:
     case_parseExpressionStatement:
       s = new ExpressionStatement(parseExpression());
       require(T.Semicolon);
@@ -3693,21 +3664,6 @@
         // TODO: create ParenExpression?
       }
       break;
-    // IntegralType . Identifier
-    case T.Char,   T.Wchar,   T.Dchar, T.Bool,
-         T.Byte,   T.Ubyte,   T.Short, T.Ushort,
-         T.Int,    T.Uint,    T.Long,  T.Ulong,
-         T.Float,  T.Double,  T.Real,
-         T.Ifloat, T.Idouble, T.Ireal,
-         T.Cfloat, T.Cdouble, T.Creal, T.Void:
-      auto type = new IntegralType(token.type);
-      nT();
-      set(type, begin);
-      require(T.Dot);
-      auto ident = requireIdentifier(MSG.ExpectedIdAfterTypeDot);
-
-      e = new TypeDotIdExpression(type, ident);
-      break;
     version(D2)
     {
     case T.Traits:
@@ -3723,20 +3679,30 @@
       break;
     }
     default:
-      if (token.isSpecialToken)
+      if (token.isIntegralType)
+      { // IntegralType . Identifier
+        auto type = new IntegralType(token.type);
+        nT();
+        set(type, begin);
+        require(T.Dot);
+        auto ident = requireIdentifier(MSG.ExpectedIdAfterTypeDot);
+        e = new TypeDotIdExpression(type, ident);
+      }
+      else if (token.isSpecialToken)
       {
         e = new SpecialTokenExpression(token);
         nT();
-        break;
       }
-
-      error(MID.ExpectedButFound, "Expression", token.srcText);
-      e = new EmptyExpression();
-      if (!trying)
+      else
       {
-        // Insert a dummy token and don't consume current one.
-        begin = lx.insertEmptyTokenBefore(token);
-        this.prevToken = begin;
+        error(MID.ExpectedButFound, "Expression", token.srcText);
+        e = new EmptyExpression();
+        if (!trying)
+        {
+          // Insert a dummy token and don't consume current one.
+          begin = lx.insertEmptyTokenBefore(token);
+          this.prevToken = begin;
+        }
       }
     }
     set(e, begin);
@@ -3790,24 +3756,18 @@
   {
     auto begin = token;
     Type t;
-//     IdentifierType tident;
-
+
+    if (token.isIntegralType)
+    {
+      t = new IntegralType(token.type);
+      nT();
+    }
+    else
     switch (token.type)
     {
-    case T.Char,   T.Wchar,   T.Dchar, T.Bool,
-         T.Byte,   T.Ubyte,   T.Short, T.Ushort,
-         T.Int,    T.Uint,    T.Long,  T.Ulong,
-         T.Float,  T.Double,  T.Real,
-         T.Ifloat, T.Idouble, T.Ireal,
-         T.Cfloat, T.Cdouble, T.Creal, T.Void:
-      t = new IntegralType(token.type);
-      nT();
-      set(t, begin);
-      break;
     case T.Identifier, T.Typeof, T.Dot:
       t = parseDotListType();
       assert(!isNodeSet(t));
-      set(t, begin);
       break;
     version(D2)
     {
@@ -3818,7 +3778,6 @@
       t = parseType();
       require(T.RParen);
       t = new ConstType(t);
-      set(t, begin);
       break;
     case T.Invariant:
       // invariant ( Type )
@@ -3827,16 +3786,14 @@
       t = parseType();
       require(T.RParen);
       t = new InvariantType(t);
-      set(t, begin);
       break;
     } // version(D2)
     default:
       error(MID.ExpectedButFound, "BasicType", token.srcText);
       t = new UndefinedType();
       nT();
-      set(t, begin);
     }
-    return t;
+    return set(t, begin);
   }
 
   Type parseBasicType2(Type t)
--- a/trunk/src/dil/Token.d	Wed Dec 12 02:41:30 2007 +0100
+++ b/trunk/src/dil/Token.d	Wed Dec 12 03:25:07 2007 +0100
@@ -130,6 +130,12 @@
     return KeywordsBegin <= type && type <= KeywordsEnd;
   }
 
+  /// Returns true if this is an integral type token.
+  bool isIntegralType()
+  {
+    return IntegralTypeBegin <= type && type <= IntegralTypeEnd;
+  }
+
   /// Returns true if this is a whitespace token.
   bool isWhitespace()
   {
@@ -311,15 +317,11 @@
         T.Auto, T.Scope, T.Alias, T.Typedef, T.Import, T.Enum, T.Class,
         T.Interface, T.Struct, T.Union, T.This, T.Tilde, T.Unittest, T.Debug,
         T.Version, T.Template, T.New, T.Delete, T.Mixin, T.Semicolon,
-        T.Identifier, T.Dot, T.Typeof,
-        T.Char,   T.Wchar,   T.Dchar, T.Bool,
-        T.Byte,   T.Ubyte,   T.Short, T.Ushort,
-        T.Int,    T.Uint,    T.Long,  T.Ulong,
-        T.Float,  T.Double,  T.Real,
-        T.Ifloat, T.Idouble, T.Ireal,
-        T.Cfloat, T.Cdouble, T.Creal, T.Void:
+        T.Identifier, T.Dot, T.Typeof:
     return true;
   default:
+    if (IntegralTypeBegin <= tok && tok <= IntegralTypeEnd)
+      return true;
   }
   return false;
 }
@@ -342,15 +344,10 @@
         T.Function, T.Delegate, T.Assert, T.Import, T.Typeid, T.Is, T.LParen,
         T.Traits/*D2.0*/, T.AndBinary, T.PlusPlus, T.MinusMinus, T.Mul,
         T.Minus, T.Plus, T.Not, T.Tilde, T.New, T.Delete, T.Cast:
-  case  T.Char,   T.Wchar,   T.Dchar, T.Bool,
-        T.Byte,   T.Ubyte,   T.Short, T.Ushort,
-        T.Int,    T.Uint,    T.Long,  T.Ulong,
-        T.Float,  T.Double,  T.Real,
-        T.Ifloat, T.Idouble, T.Ireal,
-        T.Cfloat, T.Cdouble, T.Creal, T.Void:
     return true;
   default:
-    if (SpecialTokensBegin <= tok && tok <= SpecialTokensEnd)
+    if (IntegralTypeBegin <= tok && tok <= IntegralTypeEnd ||
+        SpecialTokensBegin <= tok && tok <= SpecialTokensEnd)
       return true;
   }
   return false;
--- a/trunk/src/dil/TokensEnum.d	Wed Dec 12 02:41:30 2007 +0100
+++ b/trunk/src/dil/TokensEnum.d	Wed Dec 12 03:25:07 2007 +0100
@@ -87,20 +87,27 @@
   /* Keywords:
      NB.: Token.isKeyword() depends on this list being contiguous.
   */
-  Abstract,Alias,Align,Asm,Assert,Auto,Body,
-  Bool,Break,Byte,Case,Cast,Catch,Cdouble,
-  Cent,Cfloat,Char,Class,Const,Continue,Creal,
-  Dchar,Debug,Default,Delegate,Delete,Deprecated,Do,
-  Double,Else,Enum,Export,Extern,False,Final,
-  Finally,Float,For,Foreach,Foreach_reverse,Function,Goto,
-  Idouble,If,Ifloat,Import,In,Inout,Int,
-  Interface,Invariant,Ireal,Is,Lazy,Long,Macro/+D2.0+/,
-  Mixin,Module,New,Null,Out,Override,Package,
-  Pragma,Private,Protected,Public,Real,Ref/+D2.0+/,Return,
-  Scope,Short,Static,Struct,Super,Switch,Synchronized,
-  Template,This,Throw,Traits/+D2.0+/,True,Try,Typedef,Typeid,
-  Typeof,Ubyte,Ucent,Uint,Ulong,Union,Unittest,
-  Ushort,Version,Void,Volatile,Wchar,While,With,
+  Abstract, Alias, Align, Asm, Assert, Auto, Body,
+  Break, Case, Cast, Catch,
+  Cent, Class, Const, Continue,
+  Debug, Default, Delegate, Delete, Deprecated, Do,
+  Else, Enum, Export, Extern, False, Final,
+  Finally, For, Foreach, Foreach_reverse, Function, Goto,
+  If, Import, In, Inout,
+  Interface, Invariant, Is, Lazy, Macro/+D2.0+/,
+  Mixin, Module, New, Null, Out, Override, Package,
+  Pragma, Private, Protected, Public, Ref/+D2.0+/, Return,
+  Scope, Static, Struct, Super, Switch, Synchronized,
+  Template, This, Throw, Traits/+D2.0+/, True, Try, Typedef, Typeid,
+  Typeof, Union, Unittest,
+  Version, Volatile, While, With,
+  // Integral types.
+  Char,   Wchar,   Dchar, Bool, Ucent,
+  Byte,   Ubyte,   Short, Ushort,
+  Int,    Uint,    Long,  Ulong,
+  Float,  Double,  Real,
+  Ifloat, Idouble, Ireal,
+  Cfloat, Cdouble, Creal, Void,
 
   HEAD, // start of linked list
   EOF,
@@ -108,7 +115,9 @@
 }
 
 alias TOK.Abstract KeywordsBegin;
-alias TOK.With KeywordsEnd;
+alias TOK.Void KeywordsEnd;
+alias TOK.Char IntegralTypeBegin;
+alias TOK.Void IntegralTypeEnd;
 alias TOK.FILE SpecialTokensBegin;
 alias TOK.VERSION SpecialTokensEnd;