comparison trunk/src/Lexer.d @ 39:69b940398d7b

- Added unittest to test correct parsing of operator tokens.
author aziz
date Tue, 26 Jun 2007 10:06:01 +0000
parents 640c45aaaaee
children 9d5ceb0f8be9
comparison
equal deleted inserted replaced
38:640c45aaaaee 39:69b940398d7b
168 this(string text, string fileName) 168 this(string text, string fileName)
169 { 169 {
170 this.fileName = fileName; 170 this.fileName = fileName;
171 171
172 this.text = text; 172 this.text = text;
173 this.text.length = this.text.length + 1; 173 if (text[$-1] != 0)
174 this.text[$-1] = 0; 174 {
175 this.text.length = this.text.length + 1;
176 this.text[$-1] = 0;
177 }
175 178
176 this.p = this.text.ptr; 179 this.p = this.text.ptr;
177 this.end = this.p + this.text.length; 180 this.end = this.p + this.text.length;
178 181
179 loadKeywords(); 182 loadKeywords();
937 tokens ~= this.token; 940 tokens ~= this.token;
938 tokens ~= this.token; 941 tokens ~= this.token;
939 return tokens; 942 return tokens;
940 } 943 }
941 } 944 }
945
946 unittest
947 {
948 string[] ops = [">", ">=", ">>", ">>=", ">>>", ">>>=", "<", "<=", "<>", "<>=", "<<", "<<=", "!", "!<", "!>", "!<=", "!>=", "!<>", "!<>=", ".", "..", "...", "&", "&&", "&=", "+", "++", "+=", "-", "--", "-=", "=", "==", "~", "~=", "*", "*=", "^", "^=", "%", "%="];
949
950 char[] src;
951
952 foreach (op; ops)
953 src ~= op ~ " ";
954
955 auto lx = new Lexer(src, "");
956 auto tokens = lx.getTokens();
957
958 tokens = tokens[0..$-1]; // exclude TOK.EOF
959
960 assert(tokens.length == 41 );
961
962 foreach (i, t; tokens)
963 assert(t.span == ops[i], std.string.format("Lexed '%s' but expected '%s'", t.span, ops[i]));
964 }