changeset 86:0459c902a370

- Added code for parsing Assert-, Mixin-, Import-, Typeid- and TypeDotIdExpressions. - Added methods errorIfNot(), requireNext() and require(). - Added error msg MID.ExpectedButFound.
author aziz
date Thu, 05 Jul 2007 18:45:00 +0000
parents d8dc3171440d
children c9544b7d5c7d
files trunk/src/Expressions.d trunk/src/Messages.d trunk/src/Parser.d
diffstat 3 files changed, 101 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/trunk/src/Expressions.d	Thu Jul 05 14:20:05 2007 +0000
+++ b/trunk/src/Expressions.d	Thu Jul 05 18:45:00 2007 +0000
@@ -428,24 +428,49 @@
   }
 }
 
-class AssertExpression
+class AssertExpression : Expression
 {
+  Expression expr, msg;
+  this(Expression expr, Expression msg)
+  {
+    this.expr = expr;
+    this.msg = msg;
+  }
+}
 
+class MixinExpression : Expression
+{
+  Expression expr;
+  this(Expression expr)
+  {
+    this.expr = expr;
+  }
 }
 
-class MixinExpression
+class ImportExpression : Expression
 {
-
+  Expression expr;
+  this(Expression expr)
+  {
+    this.expr = expr;
+  }
 }
 
-class ImportExpression
+class TypeDotIdExpression : Expression
 {
-
+  TOK type;
+  string ident;
+  this(TOK type, string ident)
+  {
+    this.type = type;
+    this.ident = ident;
+  }
 }
 
-class TypeIdExpression
+class TypeidExpression : Expression
 {
-
+  this()
+  {}
 }
 
 class IsExpression : CmpExpression
--- a/trunk/src/Messages.d	Thu Jul 05 14:20:05 2007 +0000
+++ b/trunk/src/Messages.d	Thu Jul 05 18:45:00 2007 +0000
@@ -49,6 +49,9 @@
   HexFloatExponentRequired,
   HexFloatMissingExpDigits,
   FloatExponentDigitExpected,
+
+  // Parser messages
+  ExpectedButFound,
 }
 
 string[] messages = [
@@ -94,4 +97,7 @@
   "the exponent of a hexadecimal float number is required.",
   "missing decimal digits in hexadecimal float exponent.",
   "exponents have to start with a digit.",
+
+  // Parser messages
+  "expected '{1}', but found '{2}'.",
 ];
--- a/trunk/src/Parser.d	Thu Jul 05 14:20:05 2007 +0000
+++ b/trunk/src/Parser.d	Thu Jul 05 18:45:00 2007 +0000
@@ -423,10 +423,13 @@
       break;
     case T.String:
       char[] buffer = lx.token.str;
+      char postfix = lx.token.pf;
       nT();
       while (lx.token.type == T.String)
       {
         string tmp = lx.token.str;
+//         if (postfix != lx.token.pf)
+//           error();
         if (tmp.length > 1)
         {
           buffer[$-1] = tmp[0]; // replace '\0'
@@ -490,19 +493,55 @@
     case T.Function, T.Delegate:
       break;
     case T.Assert:
+      Expression msg;
+      requireNext(T.LParen);
+      e = parseAssignExpression();
+      if (lx.token.type == T.Comma)
+      {
+        nT();
+        msg = parseAssignExpression();
+      }
+      require(T.RParen);
+      e = new AssertExpression(e, msg);
       break;
     case T.Mixin:
+      requireNext(T.LParen);
+      e = parseAssignExpression();
+      require(T.RParen);
+      e = new MixinExpression(e);
       break;
     case T.Import:
+      requireNext(T.LParen);
+      e = parseAssignExpression();
+      require(T.RParen);
+      e = new ImportExpression(e);
       break;
     case T.Typeid:
+      requireNext(T.LParen);
+      e = new TypeidExpression();
       break;
     case T.Is:
       break;
     case T.LParen:
       break;
+    // BasicType . Identifier
+    case T.Void, 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:
+    TOK type = lx.token.type;
+    requireNext(T.Dot);
+
+    string ident;
+    errorIfNot(T.Identifier);
+    if (lx.token.type == T.Identifier)
+    {
+      ident = lx.token.srcText;
+      nT();
+    }
+
+    e = new TypeDotIdExpression(type, ident);
     default:
-      // BasicType . Identifier
     }
     return e;
   }
@@ -537,6 +576,29 @@
     return es;
   }
 
+  void errorIfNot(TOK tok)
+  {
+    if (lx.token.type != tok)
+      error(MID.ExpectedButFound, tok, lx.token.srcText);
+  }
+
+  void require(TOK tok)
+  {
+    if (lx.token.type == tok)
+      nT();
+    else
+      error(MID.ExpectedButFound, tok, lx.token.srcText);
+  }
+
+  void requireNext(TOK tok)
+  {
+    nT();
+    if (lx.token.type == tok)
+      nT();
+    else
+      error(MID.ExpectedButFound, tok, lx.token.srcText);
+  }
+
   void error(MID id, ...)
   {
     errors ~= new Information(Information.Type.Parser, id, lx.loc, arguments(_arguments, _argptr));