# HG changeset patch # User aziz # Date 1185030780 0 # Node ID 481ed2b63a49e552f498760bb46c3b308c3e6560 # Parent f9f5c0949a0612994ed2f877260a9cff93c466e4 - Added contracts to method scan(). - Fixed assert in peek(). - Added unittest for peek(). diff -r f9f5c0949a06 -r 481ed2b63a49 trunk/src/Lexer.d --- 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