Skip to content

Commit e7aa24d

Browse files
committed
add self.token_to_str and is_any_keyword convenience abstractions
1 parent 7e4cd09 commit e7aa24d

File tree

2 files changed

+55
-25
lines changed

2 files changed

+55
-25
lines changed

src/libsyntax/parse/common.rs

+27-8
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,29 @@ pub fn seq_sep_none() -> SeqSep {
4747
}
4848
}
4949

50+
// maps any token back to a string. not necessary if you know it's
51+
// an identifier....
5052
pub fn token_to_str(reader: @reader, token: &token::Token) -> ~str {
5153
token::to_str(reader.interner(), token)
5254
}
5355

5456
pub impl Parser {
57+
// convert a token to a string using self's reader
58+
fn token_to_str(&self, token: &token::Token) -> ~str {
59+
token::to_str(self.reader.interner(), token)
60+
}
61+
62+
// convert the current token to a string using self's reader
63+
fn this_token_to_str(&self) -> ~str {
64+
self.token_to_str(self.token)
65+
}
66+
5567
fn unexpected_last(&self, t: &token::Token) -> ! {
5668
self.span_fatal(
5769
*self.last_span,
5870
fmt!(
5971
"unexpected token: `%s`",
60-
token_to_str(self.reader, t)
72+
self.token_to_str(t)
6173
)
6274
);
6375
}
@@ -66,7 +78,7 @@ pub impl Parser {
6678
self.fatal(
6779
fmt!(
6880
"unexpected token: `%s`",
69-
token_to_str(self.reader, &copy *self.token)
81+
self.this_token_to_str()
7082
)
7183
);
7284
}
@@ -80,8 +92,8 @@ pub impl Parser {
8092
self.fatal(
8193
fmt!(
8294
"expected `%s` but found `%s`",
83-
token_to_str(self.reader, t),
84-
token_to_str(self.reader, &copy *self.token)
95+
self.token_to_str(t),
96+
self.this_token_to_str()
8597
)
8698
)
8799
}
@@ -104,7 +116,7 @@ pub impl Parser {
104116
self.fatal(
105117
fmt!(
106118
"expected ident, found `%s`",
107-
token_to_str(self.reader, &copy *self.token)
119+
self.this_token_to_str()
108120
)
109121
);
110122
}
@@ -128,12 +140,15 @@ pub impl Parser {
128140
// Storing keywords as interned idents instead of strings would be nifty.
129141

130142
// A sanity check that the word we are asking for is a known keyword
143+
// NOTE: this could be done statically....
131144
fn require_keyword(&self, word: &~str) {
132145
if !self.keywords.contains(word) {
133146
self.bug(fmt!("unknown keyword: %s", *word));
134147
}
135148
}
136149

150+
// return true when this token represents the given string, and is not
151+
// followed immediately by :: .
137152
fn token_is_word(&self, word: &~str, tok: &token::Token) -> bool {
138153
match *tok {
139154
token::IDENT(sid, false) => { *self.id_to_str(sid) == *word }
@@ -150,6 +165,10 @@ pub impl Parser {
150165
self.token_is_keyword(word, &copy *self.token)
151166
}
152167

168+
fn id_is_any_keyword(&self, id: ast::ident) -> bool {
169+
self.keywords.contains(self.id_to_str(id))
170+
}
171+
153172
fn is_any_keyword(&self, tok: &token::Token) -> bool {
154173
match *tok {
155174
token::IDENT(sid, false) => {
@@ -182,7 +201,7 @@ pub impl Parser {
182201
fmt!(
183202
"expected `%s`, found `%s`",
184203
*word,
185-
token_to_str(self.reader, &copy *self.token)
204+
self.this_token_to_str()
186205
)
187206
);
188207
}
@@ -248,9 +267,9 @@ pub impl Parser {
248267
);
249268
} else {
250269
let mut s: ~str = ~"expected `";
251-
s += token_to_str(self.reader, &token::GT);
270+
s += self.token_to_str(&token::GT);
252271
s += ~"`, found `";
253-
s += token_to_str(self.reader, &copy *self.token);
272+
s += self.this_token_to_str();
254273
s += ~"`";
255274
self.fatal(s);
256275
}

src/libsyntax/parse/parser.rs

+28-17
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ use codemap::{span, BytePos, spanned, mk_sp};
6666
use codemap;
6767
use parse::attr::parser_attr;
6868
use parse::classify;
69-
use parse::common::{seq_sep_none, token_to_str};
69+
use parse::common::{seq_sep_none};
7070
use parse::common::{seq_sep_trailing_disallowed, seq_sep_trailing_allowed};
7171
use parse::lexer::reader;
7272
use parse::lexer::TokenAndSpan;
@@ -252,8 +252,11 @@ pub fn Parser(sess: @mut ParseSess,
252252
pub struct Parser {
253253
sess: @mut ParseSess,
254254
cfg: crate_cfg,
255+
// the current token:
255256
token: @mut token::Token,
257+
// the span of the current token:
256258
span: @mut span,
259+
// the span of the prior token:
257260
last_span: @mut span,
258261
buffer: @mut [TokenAndSpan, ..4],
259262
buffer_start: @mut int,
@@ -499,7 +502,7 @@ pub impl Parser {
499502
let hi = p.last_span.hi;
500503
debug!("parse_trait_methods(): trait method signature ends in \
501504
`%s`",
502-
token_to_str(p.reader, &copy *p.token));
505+
self.this_token_to_str());
503506
match *p.token {
504507
token::SEMI => {
505508
p.bump();
@@ -541,7 +544,7 @@ pub impl Parser {
541544
p.fatal(
542545
fmt!(
543546
"expected `;` or `}` but found `%s`",
544-
token_to_str(p.reader, &copy *p.token)
547+
self.this_token_to_str()
545548
)
546549
);
547550
}
@@ -1456,6 +1459,11 @@ pub impl Parser {
14561459
fn parse_token_tree(&self) -> token_tree {
14571460
maybe_whole!(deref self, nt_tt);
14581461

1462+
// this is the fall-through for the 'match' below.
1463+
// invariants: the current token is not a left-delimiter,
1464+
// not an EOF, and not the desired right-delimiter (if
1465+
// it were, parse_seq_to_before_end would have prevented
1466+
// reaching this point.
14591467
fn parse_non_delim_tt_tok(p: &Parser) -> token_tree {
14601468
maybe_whole!(deref p, nt_tt);
14611469
match *p.token {
@@ -1464,7 +1472,7 @@ pub impl Parser {
14641472
p.fatal(
14651473
fmt!(
14661474
"incorrect close delimiter: `%s`",
1467-
token_to_str(p.reader, &copy *p.token)
1475+
p.this_token_to_str()
14681476
)
14691477
);
14701478
}
@@ -1506,18 +1514,17 @@ pub impl Parser {
15061514

15071515
match *self.token {
15081516
token::EOF => {
1509-
self.fatal(~"file ended in the middle of a macro invocation");
1517+
self.fatal(~"file ended with unbalanced delimiters");
15101518
}
15111519
token::LPAREN | token::LBRACE | token::LBRACKET => {
1512-
// tjc: ??????
1513-
let ket = token::flip_delimiter(&*self.token);
1520+
let close_delim = token::flip_delimiter(&*self.token);
15141521
tt_delim(
15151522
vec::append(
15161523
// the open delimiter:
15171524
~[parse_any_tt_tok(self)],
15181525
vec::append(
15191526
self.parse_seq_to_before_end(
1520-
&ket,
1527+
&close_delim,
15211528
seq_sep_none(),
15221529
|p| p.parse_token_tree()
15231530
),
@@ -1531,6 +1538,8 @@ pub impl Parser {
15311538
}
15321539
}
15331540

1541+
// parse a stream of tokens into a list of token_trees,
1542+
// up to EOF.
15341543
fn parse_all_token_trees(&self) -> ~[token_tree] {
15351544
let mut tts = ~[];
15361545
while *self.token != token::EOF {
@@ -2053,6 +2062,7 @@ pub impl Parser {
20532062
return e;
20542063
}
20552064

2065+
// parse the RHS of a local variable declaration (e.g. '= 14;')
20562066
fn parse_initializer(&self) -> Option<@expr> {
20572067
match *self.token {
20582068
token::EQ => {
@@ -2139,7 +2149,7 @@ pub impl Parser {
21392149
self.fatal(
21402150
fmt!(
21412151
"expected `}`, found `%s`",
2142-
token_to_str(self.reader, &copy *self.token)
2152+
self.this_token_to_str()
21432153
)
21442154
);
21452155
}
@@ -2407,6 +2417,7 @@ pub impl Parser {
24072417
pat_ident(binding_mode, name, sub)
24082418
}
24092419

2420+
// parse a local variable declaration
24102421
fn parse_local(&self, is_mutbl: bool,
24112422
allow_init: bool) -> @local {
24122423
let lo = self.span.lo;
@@ -2652,7 +2663,7 @@ pub impl Parser {
26522663
fmt!(
26532664
"expected `;` or `}` after \
26542665
expression but found `%s`",
2655-
token_to_str(self.reader, &t)
2666+
self.token_to_str(&t)
26562667
)
26572668
);
26582669
}
@@ -2867,7 +2878,7 @@ pub impl Parser {
28672878
self.fatal(
28682879
fmt!(
28692880
"expected `self` but found `%s`",
2870-
token_to_str(self.reader, &copy *self.token)
2881+
self.this_token_to_str()
28712882
)
28722883
);
28732884
}
@@ -2991,7 +3002,7 @@ pub impl Parser {
29913002
self.fatal(
29923003
fmt!(
29933004
"expected `,` or `)`, found `%s`",
2994-
token_to_str(self.reader, &copy *self.token)
3005+
self.this_token_to_str()
29953006
)
29963007
);
29973008
}
@@ -3271,7 +3282,7 @@ pub impl Parser {
32713282
fmt!(
32723283
"expected `{`, `(`, or `;` after struct name \
32733284
but found `%s`",
3274-
token_to_str(self.reader, &copy *self.token)
3285+
self.this_token_to_str()
32753286
)
32763287
);
32773288
}
@@ -3321,7 +3332,7 @@ pub impl Parser {
33213332
copy *self.span,
33223333
fmt!(
33233334
"expected `;`, `,`, or '}' but found `%s`",
3324-
token_to_str(self.reader, &copy *self.token)
3335+
self.this_token_to_str()
33253336
)
33263337
);
33273338
}
@@ -3423,7 +3434,7 @@ pub impl Parser {
34233434
self.fatal(
34243435
fmt!(
34253436
"expected item but found `%s`",
3426-
token_to_str(self.reader, &copy *self.token)
3437+
self.this_token_to_str()
34273438
)
34283439
);
34293440
}
@@ -3683,7 +3694,7 @@ pub impl Parser {
36833694
copy *self.span,
36843695
fmt!(
36853696
"expected `{` or `mod` but found `%s`",
3686-
token_to_str(self.reader, &copy *self.token)
3697+
self.this_token_to_str()
36873698
)
36883699
);
36893700
}
@@ -3696,7 +3707,7 @@ pub impl Parser {
36963707
copy *self.span,
36973708
fmt!(
36983709
"expected foreign module name but found `%s`",
3699-
token_to_str(self.reader, &copy *self.token)
3710+
self.this_token_to_str()
37003711
)
37013712
);
37023713
}

0 commit comments

Comments
 (0)