changeset 207:481ed2b63a49

- Added contracts to method scan(). - Fixed assert in peek(). - Added unittest for peek().
author aziz
date Sat, 21 Jul 2007 15:13:00 +0000
parents f9f5c0949a06
children 0a9bccf74046
files trunk/src/Lexer.d
diffstat 1 files changed, 30 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/trunk/src/Lexer.d	Mon Jul 16 11:06:01 2007 +0000
+++ b/trunk/src/Lexer.d	Sat Jul 21 15:13:00 2007 +0000
@@ -57,9 +57,17 @@
   }
 
   public void scan(out Token t)
+  in
   {
-    assert(p < end);
-
+    assert(text.ptr <= p && p < end);
+  }
+  out
+  {
+    assert(text.ptr <= t.start && t.start < end);
+    assert(text.ptr < t.end && t.end <= end, std.string.format(t.type));
+  }
+  body
+  {
     uint c = *p;
 
     while (1)
@@ -1459,10 +1467,10 @@
     // so as to avoid getting the same error more than once.
     reportErrors = false;
     char* save = p;
-    if (t.end) // For successive peeks.
+    if (t.end !is null) // For successive peeks.
     {
       p = t.end;
-      assert(text.ptr <= p && p < end);
+      assert(text.ptr < p && p <= end);
     }
     scan(t);
     p = save;
@@ -1475,6 +1483,23 @@
       errors ~= new Information(InfoType.Lexer, id, loc, arguments(_arguments, _argptr));
   }
 
+  unittest
+  {
+    string sourceText = "unittest { }";
+    auto lx = new Lexer(sourceText, null);
+
+    Token next;
+    lx.peek(next);
+    assert(next == TOK.Unittest);
+    lx.peek(next);
+    assert(next == TOK.LBrace);
+    lx.peek(next);
+    assert(next == TOK.RBrace);
+    lx.peek(next);
+    assert(next == TOK.EOF);
+    writefln("end of peek() unittest");
+  }
+
   public TOK nextToken()
   {
     scan(this.token);
@@ -1563,7 +1588,7 @@
   assert(tokens.length == toks.length );
 
   foreach (i, t; tokens)
-    assert(t.span == toks[i], std.string.format("Lexed '%s' but expected '%s'", t.span, toks[i]));
+    assert(t.srcText == toks[i], std.string.format("Lexed '%s' but expected '%s'", t.srcText, toks[i]));
 }
 
 unittest