diff trunk/src/Parser.d @ 166:56334292a29e

- Implemented parseForStatement(). - Fix: checked for wrong parentheses in parseWhile- and DoWhileStatement().
author aziz
date Fri, 13 Jul 2007 23:23:05 +0000
parents 797074d143d7
children 141b908e32b6
line wrap: on
line diff
--- a/trunk/src/Parser.d	Fri Jul 13 23:02:01 2007 +0000
+++ b/trunk/src/Parser.d	Fri Jul 13 23:23:05 2007 +0000
@@ -1176,6 +1176,31 @@
     return new ScopeStatement(s);
   }
 
+  /+
+    NoScopeStatement:
+        NonEmptyStatement
+        BlockStatement
+    BlockStatement:
+        { }
+        { StatementList }
+  +/
+  Statement parseNoScopeStatement()
+  {
+    Statement s;
+    if (token.type == T.LBrace)
+    {
+      nT();
+      auto ss = new Statements();
+      while (token.type != T.RBrace && token.type != T.EOF)
+        ss ~= parseStatement();
+      require(T.RBrace);
+      s = ss;
+    }
+    else
+      s = parseStatement();
+    return s;
+  }
+
   Statement parseIfStatement()
   {
     assert(token.type == T.If);
@@ -1224,9 +1249,9 @@
   {
     assert(token.type == T.While);
     nT();
-    require(T.RParen);
+    require(T.LParen);
     auto condition = parseExpression();
-    require(T.LParen);
+    require(T.RParen);
     return new WhileStatement(condition, parseScopeStatement());
   }
 
@@ -1236,10 +1261,43 @@
     nT();
     auto doBody = parseScopeStatement();
     require(T.While);
-    require(T.RParen);
+    require(T.LParen);
     auto condition = parseExpression();
+    require(T.RParen);
+    return new DoWhileStatement(condition, doBody);
+  }
+
+  Statement parseForStatement()
+  {
+    assert(token.type == T.For);
+    nT();
     require(T.LParen);
-    return new DoWhileStatement(condition, doBody);
+
+    Statement init, forBody;
+    Expression condition, increment;
+
+    if (token.type != T.Semicolon)
+    {
+      init = parseNoScopeStatement();
+      require(T.Semicolon);
+    }
+    else
+      nT();
+    if (token.type != T.Semicolon)
+    {
+      condition = parseExpression();
+      require(T.Semicolon);
+    }
+    else
+      nT();
+    if (token.type != T.RParen)
+    {
+      increment = parseExpression();
+      require(T.RParen);
+    }
+    else
+      nT();
+    return new ForStatement(init, condition, increment, forBody);
   }
 
   /+++++++++++++++++++++++++++++