comparison trunk/src/dil/Lexer.d @ 344:757c86e2c3cc

- Added member tail and destructor method to Lexer.
author aziz
date Thu, 23 Aug 2007 15:26:04 +0000
parents 95f1b6e43214
children ce9e2e77743f
comparison
equal deleted inserted replaced
343:95f1b6e43214 344:757c86e2c3cc
27 const uint _Z_ = 26; /// Control+Z 27 const uint _Z_ = 26; /// Control+Z
28 28
29 class Lexer 29 class Lexer
30 { 30 {
31 Token* head; /// The head of the doubly linked token list. 31 Token* head; /// The head of the doubly linked token list.
32 Token* tail; /// The tail of the linked list. Set in scan().
32 Token* token; /// Points to the current token in the token list. 33 Token* token; /// Points to the current token in the token list.
33 string text; 34 string text;
34 char* p; /// Points to the current character in the source text. 35 char* p; /// Points to the current character in the source text.
35 char* end; /// Points one character past the end of the source text. 36 char* end; /// Points one character past the end of the source text.
36 37
62 63
63 this.head = new Token; 64 this.head = new Token;
64 this.head.type = TOK.HEAD; 65 this.head.type = TOK.HEAD;
65 this.token = this.head; 66 this.token = this.head;
66 scanShebang(); 67 scanShebang();
68 }
69
70 ~this()
71 {
72 auto token = head.next;
73 do
74 {
75 assert(token.type == TOK.EOF ? token == tail && token.next is null : 1);
76 delete token.prev;
77 token = token.next;
78 } while (token !is null)
79 delete tail;
67 } 80 }
68 81
69 void scanShebang() 82 void scanShebang()
70 { 83 {
71 if (*p == '#' && p[1] == '!') 84 if (*p == '#' && p[1] == '!')
171 if (c == 0 || c == _Z_) 184 if (c == 0 || c == _Z_)
172 { 185 {
173 assert(*p == 0 || *p == _Z_); 186 assert(*p == 0 || *p == _Z_);
174 t.type = TOK.EOF; 187 t.type = TOK.EOF;
175 t.end = p; 188 t.end = p;
189 tail = &t;
176 assert(t.start == t.end); 190 assert(t.start == t.end);
177 return; 191 return;
178 } 192 }
179 193
180 if (c == '\n') 194 if (c == '\n')
1591 private void scanNext(ref Token* t) 1605 private void scanNext(ref Token* t)
1592 { 1606 {
1593 assert(t !is null); 1607 assert(t !is null);
1594 if (t.next) 1608 if (t.next)
1595 t = t.next; 1609 t = t.next;
1596 else if (t.type != TOK.EOF) 1610 else if (t != this.tail)
1597 { 1611 {
1598 Token* new_t = new Token; 1612 Token* new_t = new Token;
1599 scan(*new_t); 1613 scan(*new_t);
1600 new_t.prev = t; 1614 new_t.prev = t;
1601 t.next = new_t; 1615 t.next = new_t;