Skip to content

Commit 4a73426

Browse files
committed
libsyntax: add &self to parser methods
1 parent 3c23589 commit 4a73426

File tree

5 files changed

+303
-261
lines changed

5 files changed

+303
-261
lines changed

src/libsyntax/parse/attr.rs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,24 @@ use core::either::{Either, Left, Right};
2121

2222
// a parser that can parse attributes.
2323
pub trait parser_attr {
24-
fn parse_outer_attributes() -> ~[ast::attribute];
25-
fn parse_attribute(style: ast::attr_style) -> ast::attribute;
26-
fn parse_attribute_naked(style: ast::attr_style, lo: BytePos) ->
27-
ast::attribute;
28-
fn parse_inner_attrs_and_next() ->
24+
fn parse_outer_attributes(&self) -> ~[ast::attribute];
25+
fn parse_attribute(&self, style: ast::attr_style) -> ast::attribute;
26+
fn parse_attribute_naked(
27+
&self,
28+
style: ast::attr_style,
29+
lo: BytePos
30+
) -> ast::attribute;
31+
fn parse_inner_attrs_and_next(&self) ->
2932
(~[ast::attribute], ~[ast::attribute]);
30-
fn parse_meta_item() -> @ast::meta_item;
31-
fn parse_meta_seq() -> ~[@ast::meta_item];
32-
fn parse_optional_meta() -> ~[@ast::meta_item];
33+
fn parse_meta_item(&self) -> @ast::meta_item;
34+
fn parse_meta_seq(&self) -> ~[@ast::meta_item];
35+
fn parse_optional_meta(&self) -> ~[@ast::meta_item];
3336
}
3437

3538
impl parser_attr for Parser {
3639

3740
// Parse attributes that appear before an item
38-
fn parse_outer_attributes() -> ~[ast::attribute] {
41+
fn parse_outer_attributes(&self) -> ~[ast::attribute] {
3942
let mut attrs: ~[ast::attribute] = ~[];
4043
loop {
4144
match *self.token {
@@ -63,13 +66,13 @@ impl parser_attr for Parser {
6366
return attrs;
6467
}
6568

66-
fn parse_attribute(style: ast::attr_style) -> ast::attribute {
69+
fn parse_attribute(&self, style: ast::attr_style) -> ast::attribute {
6770
let lo = self.span.lo;
6871
self.expect(&token::POUND);
6972
return self.parse_attribute_naked(style, lo);
7073
}
7174

72-
fn parse_attribute_naked(style: ast::attr_style, lo: BytePos) ->
75+
fn parse_attribute_naked(&self, style: ast::attr_style, lo: BytePos) ->
7376
ast::attribute {
7477
self.expect(&token::LBRACKET);
7578
let meta_item = self.parse_meta_item();
@@ -89,7 +92,7 @@ impl parser_attr for Parser {
8992

9093
// you can make the 'next' field an Option, but the result is going to be
9194
// more useful as a vector.
92-
fn parse_inner_attrs_and_next() ->
95+
fn parse_inner_attrs_and_next(&self) ->
9396
(~[ast::attribute], ~[ast::attribute]) {
9497
let mut inner_attrs: ~[ast::attribute] = ~[];
9598
let mut next_outer_attrs: ~[ast::attribute] = ~[];
@@ -135,7 +138,7 @@ impl parser_attr for Parser {
135138
(inner_attrs, next_outer_attrs)
136139
}
137140

138-
fn parse_meta_item() -> @ast::meta_item {
141+
fn parse_meta_item(&self) -> @ast::meta_item {
139142
let lo = self.span.lo;
140143
let name = self.id_to_str(self.parse_ident());
141144
match *self.token {
@@ -157,7 +160,7 @@ impl parser_attr for Parser {
157160
}
158161
}
159162

160-
fn parse_meta_seq() -> ~[@ast::meta_item] {
163+
fn parse_meta_seq(&self) -> ~[@ast::meta_item] {
161164
copy self.parse_seq(
162165
&token::LPAREN,
163166
&token::RPAREN,
@@ -166,7 +169,7 @@ impl parser_attr for Parser {
166169
).node
167170
}
168171

169-
fn parse_optional_meta() -> ~[@ast::meta_item] {
172+
fn parse_optional_meta(&self) -> ~[@ast::meta_item] {
170173
match *self.token {
171174
token::LPAREN => self.parse_meta_seq(),
172175
_ => ~[]

src/libsyntax/parse/common.rs

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ pub fn token_to_str(reader: reader, token: &token::Token) -> ~str {
5454
}
5555

5656
pub impl Parser {
57-
fn unexpected_last(t: &token::Token) -> ! {
57+
fn unexpected_last(&self, t: &token::Token) -> ! {
5858
self.span_fatal(
5959
*self.last_span,
6060
fmt!(
@@ -64,7 +64,7 @@ pub impl Parser {
6464
);
6565
}
6666

67-
fn unexpected() -> ! {
67+
fn unexpected(&self) -> ! {
6868
self.fatal(
6969
fmt!(
7070
"unexpected token: `%s`",
@@ -75,7 +75,7 @@ pub impl Parser {
7575

7676
// expect and consume the token t. Signal an error if
7777
// the next token is not t.
78-
fn expect(t: &token::Token) {
78+
fn expect(&self, t: &token::Token) {
7979
if *self.token == *t {
8080
self.bump();
8181
} else {
@@ -89,7 +89,7 @@ pub impl Parser {
8989
}
9090
}
9191

92-
fn parse_ident() -> ast::ident {
92+
fn parse_ident(&self) -> ast::ident {
9393
self.check_strict_keywords();
9494
self.check_reserved_keywords();
9595
match *self.token {
@@ -113,50 +113,50 @@ pub impl Parser {
113113
}
114114
}
115115

116-
fn parse_path_list_ident() -> ast::path_list_ident {
116+
fn parse_path_list_ident(&self) -> ast::path_list_ident {
117117
let lo = self.span.lo;
118118
let ident = self.parse_ident();
119119
let hi = self.span.hi;
120120
spanned(lo, hi, ast::path_list_ident_ { name: ident,
121121
id: self.get_id() })
122122
}
123123

124-
fn parse_value_ident() -> ast::ident {
124+
fn parse_value_ident(&self) -> ast::ident {
125125
return self.parse_ident();
126126
}
127127

128128
// consume token 'tok' if it exists. Returns true if the given
129129
// token was present, false otherwise.
130-
fn eat(tok: &token::Token) -> bool {
130+
fn eat(&self, tok: &token::Token) -> bool {
131131
return if *self.token == *tok { self.bump(); true } else { false };
132132
}
133133

134134
// Storing keywords as interned idents instead of strings would be nifty.
135135

136136
// A sanity check that the word we are asking for is a known keyword
137-
fn require_keyword(word: &~str) {
137+
fn require_keyword(&self, word: &~str) {
138138
if !self.keywords.contains_key(word) {
139139
self.bug(fmt!("unknown keyword: %s", *word));
140140
}
141141
}
142142

143-
pure fn token_is_word(word: &~str, tok: &token::Token) -> bool {
143+
pure fn token_is_word(&self, word: &~str, tok: &token::Token) -> bool {
144144
match *tok {
145145
token::IDENT(sid, false) => { *self.id_to_str(sid) == *word }
146146
_ => { false }
147147
}
148148
}
149149

150-
fn token_is_keyword(word: &~str, tok: &token::Token) -> bool {
150+
fn token_is_keyword(&self, word: &~str, tok: &token::Token) -> bool {
151151
self.require_keyword(word);
152152
self.token_is_word(word, tok)
153153
}
154154

155-
fn is_keyword(word: &~str) -> bool {
155+
fn is_keyword(&self, word: &~str) -> bool {
156156
self.token_is_keyword(word, &copy *self.token)
157157
}
158158

159-
fn is_any_keyword(tok: &token::Token) -> bool {
159+
fn is_any_keyword(&self, tok: &token::Token) -> bool {
160160
match *tok {
161161
token::IDENT(sid, false) => {
162162
self.keywords.contains_key(self.id_to_str(sid))
@@ -165,7 +165,7 @@ pub impl Parser {
165165
}
166166
}
167167

168-
fn eat_keyword(word: &~str) -> bool {
168+
fn eat_keyword(&self, word: &~str) -> bool {
169169
self.require_keyword(word);
170170
let is_kw = match *self.token {
171171
token::IDENT(sid, false) => *word == *self.id_to_str(sid),
@@ -175,7 +175,7 @@ pub impl Parser {
175175
is_kw
176176
}
177177

178-
fn expect_keyword(word: &~str) {
178+
fn expect_keyword(&self, word: &~str) {
179179
self.require_keyword(word);
180180
if !self.eat_keyword(word) {
181181
self.fatal(
@@ -188,11 +188,11 @@ pub impl Parser {
188188
}
189189
}
190190

191-
fn is_strict_keyword(word: &~str) -> bool {
191+
fn is_strict_keyword(&self, word: &~str) -> bool {
192192
self.strict_keywords.contains_key(word)
193193
}
194194

195-
fn check_strict_keywords() {
195+
fn check_strict_keywords(&self) {
196196
match *self.token {
197197
token::IDENT(_, false) => {
198198
let w = token_to_str(self.reader, &copy *self.token);
@@ -202,17 +202,17 @@ pub impl Parser {
202202
}
203203
}
204204

205-
fn check_strict_keywords_(w: &~str) {
205+
fn check_strict_keywords_(&self, w: &~str) {
206206
if self.is_strict_keyword(w) {
207207
self.fatal(fmt!("found `%s` in ident position", *w));
208208
}
209209
}
210210

211-
fn is_reserved_keyword(word: &~str) -> bool {
211+
fn is_reserved_keyword(&self, word: &~str) -> bool {
212212
self.reserved_keywords.contains_key(word)
213213
}
214214

215-
fn check_reserved_keywords() {
215+
fn check_reserved_keywords(&self) {
216216
match *self.token {
217217
token::IDENT(_, false) => {
218218
let w = token_to_str(self.reader, &copy *self.token);
@@ -222,15 +222,15 @@ pub impl Parser {
222222
}
223223
}
224224

225-
fn check_reserved_keywords_(w: &~str) {
225+
fn check_reserved_keywords_(&self, w: &~str) {
226226
if self.is_reserved_keyword(w) {
227227
self.fatal(fmt!("`%s` is a reserved keyword", *w));
228228
}
229229
}
230230

231231
// expect and consume a GT. if a >> is seen, replace it
232232
// with a single > and continue.
233-
fn expect_gt() {
233+
fn expect_gt(&self) {
234234
if *self.token == token::GT {
235235
self.bump();
236236
} else if *self.token == token::BINOP(token::SHR) {
@@ -252,6 +252,7 @@ pub impl Parser {
252252
// parse a sequence bracketed by '<' and '>', stopping
253253
// before the '>'.
254254
fn parse_seq_to_before_gt<T: Copy>(
255+
&self,
255256
sep: Option<token::Token>,
256257
f: fn(&Parser) -> T
257258
) -> OptVec<T> {
@@ -266,12 +267,13 @@ pub impl Parser {
266267
}
267268
_ => ()
268269
}
269-
v.push(f(&self));
270+
v.push(f(self));
270271
}
271272
return v;
272273
}
273274

274275
fn parse_seq_to_gt<T: Copy>(
276+
&self,
275277
sep: Option<token::Token>,
276278
f: fn(&Parser) -> T
277279
) -> OptVec<T> {
@@ -284,6 +286,7 @@ pub impl Parser {
284286
// f must consume tokens until reaching the next separator or
285287
// closing bracket.
286288
fn parse_seq_to_end<T: Copy>(
289+
&self,
287290
ket: &token::Token,
288291
sep: SeqSep,
289292
f: fn(&Parser) -> T
@@ -297,6 +300,7 @@ pub impl Parser {
297300
// f must consume tokens until reaching the next separator or
298301
// closing bracket.
299302
fn parse_seq_to_before_end<T: Copy>(
303+
&self,
300304
ket: &token::Token,
301305
sep: SeqSep,
302306
f: fn(&Parser) -> T
@@ -312,7 +316,7 @@ pub impl Parser {
312316
_ => ()
313317
}
314318
if sep.trailing_sep_allowed && *self.token == *ket { break; }
315-
v.push(f(&self));
319+
v.push(f(self));
316320
}
317321
return v;
318322
}
@@ -321,6 +325,7 @@ pub impl Parser {
321325
// f must consume tokens until reaching the next separator or
322326
// closing bracket.
323327
fn parse_unspanned_seq<T: Copy>(
328+
&self,
324329
bra: &token::Token,
325330
ket: &token::Token,
326331
sep: SeqSep,
@@ -335,6 +340,7 @@ pub impl Parser {
335340
// NB: Do not use this function unless you actually plan to place the
336341
// spanned list in the AST.
337342
fn parse_seq<T: Copy>(
343+
&self,
338344
bra: &token::Token,
339345
ket: &token::Token,
340346
sep: SeqSep,

src/libsyntax/parse/obsolete.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ impl to_bytes::IterBytes for ObsoleteSyntax {
6262

6363
pub impl Parser {
6464
/// Reports an obsolete syntax non-fatal error.
65-
fn obsolete(sp: span, kind: ObsoleteSyntax) {
65+
fn obsolete(&self, sp: span, kind: ObsoleteSyntax) {
6666
let (kind_str, desc) = match kind {
6767
ObsoleteLowerCaseKindBounds => (
6868
"lower-case kind bounds",
@@ -154,12 +154,12 @@ pub impl Parser {
154154

155155
// Reports an obsolete syntax non-fatal error, and returns
156156
// a placeholder expression
157-
fn obsolete_expr(sp: span, kind: ObsoleteSyntax) -> @expr {
157+
fn obsolete_expr(&self, sp: span, kind: ObsoleteSyntax) -> @expr {
158158
self.obsolete(sp, kind);
159159
self.mk_expr(sp.lo, sp.hi, expr_lit(@respan(sp, lit_nil)))
160160
}
161161

162-
priv fn report(sp: span, kind: ObsoleteSyntax, kind_str: &str,
162+
priv fn report(&self, sp: span, kind: ObsoleteSyntax, kind_str: &str,
163163
desc: &str) {
164164
self.span_err(sp, fmt!("obsolete syntax: %s", kind_str));
165165

@@ -169,7 +169,7 @@ pub impl Parser {
169169
}
170170
}
171171

172-
fn token_is_obsolete_ident(ident: &str, token: Token) -> bool {
172+
fn token_is_obsolete_ident(&self, ident: &str, token: Token) -> bool {
173173
match token {
174174
token::IDENT(copy sid, _) => {
175175
str::eq_slice(*self.id_to_str(sid), ident)
@@ -178,11 +178,11 @@ pub impl Parser {
178178
}
179179
}
180180

181-
fn is_obsolete_ident(ident: &str) -> bool {
181+
fn is_obsolete_ident(&self, ident: &str) -> bool {
182182
self.token_is_obsolete_ident(ident, *self.token)
183183
}
184184

185-
fn eat_obsolete_ident(ident: &str) -> bool {
185+
fn eat_obsolete_ident(&self, ident: &str) -> bool {
186186
if self.is_obsolete_ident(ident) {
187187
self.bump();
188188
true
@@ -191,7 +191,7 @@ pub impl Parser {
191191
}
192192
}
193193

194-
fn try_parse_obsolete_struct_ctor() -> bool {
194+
fn try_parse_obsolete_struct_ctor(&self) -> bool {
195195
if self.eat_obsolete_ident("new") {
196196
self.obsolete(*self.last_span, ObsoleteStructCtor);
197197
self.parse_fn_decl(|p| p.parse_arg());
@@ -202,7 +202,7 @@ pub impl Parser {
202202
}
203203
}
204204

205-
fn try_parse_obsolete_with() -> bool {
205+
fn try_parse_obsolete_with(&self) -> bool {
206206
if *self.token == token::COMMA
207207
&& self.token_is_obsolete_ident("with",
208208
self.look_ahead(1u)) {
@@ -217,7 +217,7 @@ pub impl Parser {
217217
}
218218
}
219219

220-
fn try_parse_obsolete_priv_section() -> bool {
220+
fn try_parse_obsolete_priv_section(&self) -> bool {
221221
if self.is_keyword(&~"priv") && self.look_ahead(1) == token::LBRACE {
222222
self.obsolete(copy *self.span, ObsoletePrivSection);
223223
self.eat_keyword(&~"priv");

0 commit comments

Comments
 (0)