1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use ast;
use codemap::{BytePos, spanned};
use parse::lexer::reader;
use parse::parser::Parser;
use parse::token::keywords;
use parse::token;
use parse::token::{get_ident_interner};
use opt_vec;
use opt_vec::OptVec;
// SeqSep : a sequence separator (token)
// and whether a trailing separator is allowed.
pub struct SeqSep {
sep: Option<token::Token>,
trailing_sep_allowed: bool
}
pub fn seq_sep_trailing_disallowed(t: token::Token) -> SeqSep {
SeqSep {
sep: Some(t),
trailing_sep_allowed: false,
}
}
pub fn seq_sep_trailing_allowed(t: token::Token) -> SeqSep {
SeqSep {
sep: Some(t),
trailing_sep_allowed: true,
}
}
pub fn seq_sep_none() -> SeqSep {
SeqSep {
sep: None,
trailing_sep_allowed: false,
}
}
// maps any token back to a string. not necessary if you know it's
// an identifier....
pub fn token_to_str(token: &token::Token) -> ~str {
token::to_str(get_ident_interner(), token)
}
impl Parser {
// convert a token to a string using self's reader
pub fn token_to_str(&self, token: &token::Token) -> ~str {
token::to_str(get_ident_interner(), token)
}
// convert the current token to a string using self's reader
pub fn this_token_to_str(&self) -> ~str {
self.token_to_str(self.token)
}
pub fn unexpected_last(&self, t: &token::Token) -> ! {
self.span_fatal(
*self.last_span,
fmt!(
"unexpected token: `%s`",
self.token_to_str(t)
)
);
}
pub fn unexpected(&self) -> ! {
self.fatal(
fmt!(
"unexpected token: `%s`",
self.this_token_to_str()
)
);
}
// expect and consume the token t. Signal an error if
// the next token is not t.
pub fn expect(&self, t: &token::Token) {
if *self.token == *t {
self.bump();
} else {
self.fatal(
fmt!(
"expected `%s` but found `%s`",
self.token_to_str(t),
self.this_token_to_str()
)
)
}
}
pub fn parse_ident(&self) -> ast::ident {
self.check_strict_keywords();
self.check_reserved_keywords();
match *self.token {
token::IDENT(i, _) => {
self.bump();
i
}
token::INTERPOLATED(token::nt_ident(*)) => {
self.bug("ident interpolation not converted to real token");
}
_ => {
self.fatal(
fmt!(
"expected ident, found `%s`",
self.this_token_to_str()
)
);
}
}
}
pub fn parse_path_list_ident(&self) -> ast::path_list_ident {
let lo = self.span.lo;
let ident = self.parse_ident();
let hi = self.last_span.hi;
spanned(lo, hi, ast::path_list_ident_ { name: ident,
id: self.get_id() })
}
// consume token 'tok' if it exists. Returns true if the given
// token was present, false otherwise.
pub fn eat(&self, tok: &token::Token) -> bool {
return if *self.token == *tok { self.bump(); true } else { false };
}
pub fn is_keyword(&self, kw: keywords::Keyword) -> bool {
token::is_keyword(kw, self.token)
}
// if the next token is the given keyword, eat it and return
// true. Otherwise, return false.
pub fn eat_keyword(&self, kw: keywords::Keyword) -> bool {
let is_kw = match *self.token {
token::IDENT(sid, false) => kw.to_ident().name == sid.name,
_ => false
};
if is_kw { self.bump() }
is_kw
}
// if the given word is not a keyword, signal an error.
// if the next token is not the given word, signal an error.
// otherwise, eat it.
pub fn expect_keyword(&self, kw: keywords::Keyword) {
if !self.eat_keyword(kw) {
self.fatal(
fmt!(
"expected `%s`, found `%s`",
self.id_to_str(kw.to_ident()),
self.this_token_to_str()
)
);
}
}
// signal an error if the given string is a strict keyword
pub fn check_strict_keywords(&self) {
if token::is_strict_keyword(self.token) {
self.span_err(*self.last_span,
fmt!("found `%s` in ident position", self.this_token_to_str()));
}
}
// signal an error if the current token is a reserved keyword
pub fn check_reserved_keywords(&self) {
if token::is_reserved_keyword(self.token) {
self.fatal(fmt!("`%s` is a reserved keyword", self.this_token_to_str()));
}
}
// expect and consume a GT. if a >> is seen, replace it
// with a single > and continue. If a GT is not seen,
// signal an error.
pub fn expect_gt(&self) {
if *self.token == token::GT {
self.bump();
} else if *self.token == token::BINOP(token::SHR) {
self.replace_token(
token::GT,
self.span.lo + BytePos(1u),
self.span.hi
);
} else {
let mut s: ~str = ~"expected `";
s += self.token_to_str(&token::GT);
s += "`, found `";
s += self.this_token_to_str();
s += "`";
self.fatal(s);
}
}
// parse a sequence bracketed by '<' and '>', stopping
// before the '>'.
pub fn parse_seq_to_before_gt<T: Copy>(&self,
sep: Option<token::Token>,
f: &fn(&Parser) -> T)
-> OptVec<T> {
let mut first = true;
let mut v = opt_vec::Empty;
while *self.token != token::GT
&& *self.token != token::BINOP(token::SHR) {
match sep {
Some(ref t) => {
if first { first = false; }
else { self.expect(t); }
}
_ => ()
}
v.push(f(self));
}
return v;
}
pub fn parse_seq_to_gt<T: Copy>(&self,
sep: Option<token::Token>,
f: &fn(&Parser) -> T)
-> OptVec<T> {
let v = self.parse_seq_to_before_gt(sep, f);
self.expect_gt();
return v;
}
// parse a sequence, including the closing delimiter. The function
// f must consume tokens until reaching the next separator or
// closing bracket.
pub fn parse_seq_to_end<T: Copy>(&self,
ket: &token::Token,
sep: SeqSep,
f: &fn(&Parser) -> T)
-> ~[T] {
let val = self.parse_seq_to_before_end(ket, sep, f);
self.bump();
val
}
// parse a sequence, not including the closing delimiter. The function
// f must consume tokens until reaching the next separator or
// closing bracket.
pub fn parse_seq_to_before_end<T: Copy>(&self,
ket: &token::Token,
sep: SeqSep,
f: &fn(&Parser) -> T)
-> ~[T] {
let mut first: bool = true;
let mut v: ~[T] = ~[];
while *self.token != *ket {
match sep.sep {
Some(ref t) => {
if first { first = false; }
else { self.expect(t); }
}
_ => ()
}
if sep.trailing_sep_allowed && *self.token == *ket { break; }
v.push(f(self));
}
return v;
}
// parse a sequence, including the closing delimiter. The function
// f must consume tokens until reaching the next separator or
// closing bracket.
pub fn parse_unspanned_seq<T: Copy>(&self,
bra: &token::Token,
ket: &token::Token,
sep: SeqSep,
f: &fn(&Parser) -> T)
-> ~[T] {
self.expect(bra);
let result = self.parse_seq_to_before_end(ket, sep, f);
self.bump();
result
}
// NB: Do not use this function unless you actually plan to place the
// spanned list in the AST.
pub fn parse_seq<T: Copy>(&self,
bra: &token::Token,
ket: &token::Token,
sep: SeqSep,
f: &fn(&Parser) -> T)
-> spanned<~[T]> {
let lo = self.span.lo;
self.expect(bra);
let result = self.parse_seq_to_before_end(ket, sep, f);
let hi = self.span.hi;
self.bump();
spanned(lo, hi, result)
}
}
|