Skip to content

Commit d906fba

Browse files
committed
Make token tree lexers and their stacks unique.
1 parent d03c04b commit d906fba

File tree

1 file changed

+50
-37
lines changed

1 file changed

+50
-37
lines changed

src/libsyntax/parse/lexer.rs

Lines changed: 50 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ type tt_frame = @{
2727
up: tt_frame_up
2828
};
2929

30-
type tt_reader = @{
30+
type tt_reader = ~{
3131
span_diagnostic: diagnostic::span_handler,
3232
interner: @interner::interner<@str>,
3333
mut cur: tt_frame,
@@ -39,28 +39,29 @@ type tt_reader = @{
3939
fn new_tt_reader(span_diagnostic: diagnostic::span_handler,
4040
itr: @interner::interner<@str>, src: [ast::token_tree])
4141
-> tt_reader {
42-
let r = @{span_diagnostic: span_diagnostic, interner: itr,
42+
let r = ~{span_diagnostic: span_diagnostic, interner: itr,
4343
mut cur: @{readme: src, mut idx: 0u,
4444
up: tt_frame_up(option::none)},
4545
mut cur_tok: token::EOF, /* dummy value, never read */
4646
mut cur_chpos: 0u /* dummy value, never read */
4747
};
48-
(r as reader).next_token(); /* get cur_tok and cur_chpos set up */
48+
//tt_next_token(r); /* get cur_tok and cur_chpos set up */
4949
ret r;
5050
}
5151

5252
pure fn dup_tt_frame(&&f: tt_frame) -> tt_frame {
5353
@{readme: f.readme, mut idx: f.idx,
5454
up: alt f.up {
55-
tt_frame_up(o_f) {
56-
tt_frame_up(option::map(o_f, dup_tt_frame))
55+
tt_frame_up(some(up_frame)) {
56+
tt_frame_up(some(dup_tt_frame(up_frame)))
5757
}
58+
tt_frame_up(none) { tt_frame_up(none) }
5859
}
5960
}
6061
}
6162

6263
pure fn dup_tt_reader(&&r: tt_reader) -> tt_reader {
63-
@{span_diagnostic: r.span_diagnostic, interner: r.interner,
64+
~{span_diagnostic: r.span_diagnostic, interner: r.interner,
6465
mut cur: dup_tt_frame(r.cur),
6566
mut cur_tok: r.cur_tok, mut cur_chpos: r.cur_chpos}
6667
}
@@ -114,38 +115,13 @@ impl string_reader_as_reader of reader for string_reader {
114115
impl tt_reader_as_reader of reader for tt_reader {
115116
fn is_eof() -> bool { self.cur_tok == token::EOF }
116117
fn next_token() -> {tok: token::token, chpos: uint} {
117-
let ret_val = { tok: self.cur_tok, chpos: self.cur_chpos };
118-
if self.cur.idx >= vec::len(self.cur.readme) {
119-
/* done with this set; pop */
120-
alt self.cur.up {
121-
tt_frame_up(option::none) {
122-
self.cur_tok = token::EOF;
123-
ret ret_val;
124-
}
125-
tt_frame_up(option::some(tt_f)) {
126-
self.cur = tt_f;
127-
/* the above `if` would need to be a `while` if we didn't know
128-
that the last thing in a `tt_delim` is always a `tt_flat` */
129-
self.cur.idx += 1u;
130-
}
131-
}
132-
}
133-
/* if `tt_delim`s could be 0-length, we'd need to be able to switch
134-
between popping and pushing until we got to an actual `tt_flat` */
135-
loop { /* because it's easiest, this handles `tt_delim` not starting
136-
with a `tt_flat`, even though it won't happen */
137-
alt self.cur.readme[self.cur.idx] {
138-
tt_delim(tts) {
139-
self.cur = @{readme: tts, mut idx: 0u,
140-
up: tt_frame_up(option::some(self.cur)) };
141-
}
142-
tt_flat(chpos, tok) {
143-
self.cur_chpos = chpos; self.cur_tok = tok;
144-
self.cur.idx += 1u;
145-
ret ret_val;
146-
}
147-
}
118+
/* weird resolve bug: if the following `if`, or any of its
119+
statements are removed, we get resolution errors */
120+
if false {
121+
let _ignore_me = 0;
122+
let _me_too = self.cur.readme[self.cur.idx];
148123
}
124+
tt_next_token(self)
149125
}
150126
fn fatal(m: str) -> ! {
151127
self.span_diagnostic.span_fatal(
@@ -155,6 +131,43 @@ impl tt_reader_as_reader of reader for tt_reader {
155131
fn interner() -> @interner::interner<@str> { self.interner }
156132
}
157133

134+
fn tt_next_token(&&r: tt_reader) -> {tok: token::token, chpos: uint} {
135+
let ret_val = { tok: r.cur_tok, chpos: r.cur_chpos };
136+
if r.cur.idx >= vec::len(r.cur.readme) {
137+
/* done with this set; pop */
138+
alt r.cur.up {
139+
tt_frame_up(option::none) {
140+
r.cur_tok = token::EOF;
141+
ret ret_val;
142+
}
143+
tt_frame_up(option::some(tt_f)) {
144+
r.cur <- tt_f;
145+
/* the above `if` would need to be a `while` if we didn't know
146+
that the last thing in a `tt_delim` is always a `tt_flat` */
147+
r.cur.idx += 1u;
148+
}
149+
}
150+
}
151+
/* if `tt_delim`s could be 0-length, we'd need to be able to switch
152+
between popping and pushing until we got to an actual `tt_flat` */
153+
loop { /* because it's easiest, this handles `tt_delim` not starting
154+
with a `tt_flat`, even though it won't happen */
155+
alt r.cur.readme[r.cur.idx] {
156+
tt_delim(tts) {
157+
/* TODO: this copy should be a unary move, once they exist */
158+
r.cur = @{readme: tts, mut idx: 0u,
159+
up: tt_frame_up(option::some(copy r.cur)) };
160+
}
161+
tt_flat(chpos, tok) {
162+
r.cur_chpos = chpos; r.cur_tok = tok;
163+
r.cur.idx += 1u;
164+
ret ret_val;
165+
}
166+
}
167+
}
168+
169+
}
170+
158171
fn get_str_from(rdr: string_reader, start: uint) -> str unsafe {
159172
// I'm pretty skeptical about this subtraction. What if there's a
160173
// multi-byte character before the mark?

0 commit comments

Comments
 (0)