Skip to content

Commit 27ce4d3

Browse files
committed
auto merge of #11226 : pcwalton/rust/mutable-parser, r=pcwalton
r? @alexcrichton
2 parents 2e98a93 + f3eee0e commit 27ce4d3

14 files changed

+881
-847
lines changed

src/libsyntax/ext/asm.rs

+23-23
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ fn next_state(s: State) -> Option<State> {
3939

4040
pub fn expand_asm(cx: &mut ExtCtxt, sp: Span, tts: &[ast::token_tree])
4141
-> base::MacResult {
42-
let p = parse::new_parser_from_tts(cx.parse_sess(),
43-
cx.cfg(),
44-
tts.to_owned());
42+
let mut p = parse::new_parser_from_tts(cx.parse_sess(),
43+
cx.cfg(),
44+
tts.to_owned());
4545

4646
let mut asm = @"";
4747
let mut asm_str_style = None;
@@ -66,9 +66,9 @@ pub fn expand_asm(cx: &mut ExtCtxt, sp: Span, tts: &[ast::token_tree])
6666
asm_str_style = Some(style);
6767
}
6868
Outputs => {
69-
while *p.token != token::EOF &&
70-
*p.token != token::COLON &&
71-
*p.token != token::MOD_SEP {
69+
while p.token != token::EOF &&
70+
p.token != token::COLON &&
71+
p.token != token::MOD_SEP {
7272

7373
if outputs.len() != 0 {
7474
p.eat(&token::COMMA);
@@ -77,10 +77,10 @@ pub fn expand_asm(cx: &mut ExtCtxt, sp: Span, tts: &[ast::token_tree])
7777
let (constraint, _str_style) = p.parse_str();
7878

7979
if constraint.starts_with("+") {
80-
cx.span_unimpl(*p.last_span,
80+
cx.span_unimpl(p.last_span,
8181
"'+' (read+write) output operand constraint modifier");
8282
} else if !constraint.starts_with("=") {
83-
cx.span_err(*p.last_span, "output operand constraint lacks '='");
83+
cx.span_err(p.last_span, "output operand constraint lacks '='");
8484
}
8585

8686
p.expect(&token::LPAREN);
@@ -91,9 +91,9 @@ pub fn expand_asm(cx: &mut ExtCtxt, sp: Span, tts: &[ast::token_tree])
9191
}
9292
}
9393
Inputs => {
94-
while *p.token != token::EOF &&
95-
*p.token != token::COLON &&
96-
*p.token != token::MOD_SEP {
94+
while p.token != token::EOF &&
95+
p.token != token::COLON &&
96+
p.token != token::MOD_SEP {
9797

9898
if inputs.len() != 0 {
9999
p.eat(&token::COMMA);
@@ -102,9 +102,9 @@ pub fn expand_asm(cx: &mut ExtCtxt, sp: Span, tts: &[ast::token_tree])
102102
let (constraint, _str_style) = p.parse_str();
103103

104104
if constraint.starts_with("=") {
105-
cx.span_err(*p.last_span, "input operand constraint contains '='");
105+
cx.span_err(p.last_span, "input operand constraint contains '='");
106106
} else if constraint.starts_with("+") {
107-
cx.span_err(*p.last_span, "input operand constraint contains '+'");
107+
cx.span_err(p.last_span, "input operand constraint contains '+'");
108108
}
109109

110110
p.expect(&token::LPAREN);
@@ -116,9 +116,9 @@ pub fn expand_asm(cx: &mut ExtCtxt, sp: Span, tts: &[ast::token_tree])
116116
}
117117
Clobbers => {
118118
let mut clobs = ~[];
119-
while *p.token != token::EOF &&
120-
*p.token != token::COLON &&
121-
*p.token != token::MOD_SEP {
119+
while p.token != token::EOF &&
120+
p.token != token::COLON &&
121+
p.token != token::MOD_SEP {
122122

123123
if clobs.len() != 0 {
124124
p.eat(&token::COMMA);
@@ -142,16 +142,16 @@ pub fn expand_asm(cx: &mut ExtCtxt, sp: Span, tts: &[ast::token_tree])
142142
dialect = ast::asm_intel;
143143
}
144144

145-
if *p.token == token::COMMA {
145+
if p.token == token::COMMA {
146146
p.eat(&token::COMMA);
147147
}
148148
}
149149
}
150150

151-
while *p.token == token::COLON ||
152-
*p.token == token::MOD_SEP ||
153-
*p.token == token::EOF {
154-
state = if *p.token == token::COLON {
151+
while p.token == token::COLON ||
152+
p.token == token::MOD_SEP ||
153+
p.token == token::EOF {
154+
state = if p.token == token::COLON {
155155
p.bump();
156156
match next_state(state) {
157157
Some(x) => x,
@@ -160,7 +160,7 @@ pub fn expand_asm(cx: &mut ExtCtxt, sp: Span, tts: &[ast::token_tree])
160160
break
161161
}
162162
}
163-
} else if *p.token == token::MOD_SEP {
163+
} else if p.token == token::MOD_SEP {
164164
p.bump();
165165
let s = match next_state(state) {
166166
Some(x) => x,
@@ -176,7 +176,7 @@ pub fn expand_asm(cx: &mut ExtCtxt, sp: Span, tts: &[ast::token_tree])
176176
break
177177
}
178178
}
179-
} else if *p.token == token::EOF {
179+
} else if p.token == token::EOF {
180180
continue_ = false;
181181
break;
182182
} else {

src/libsyntax/ext/base.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -442,11 +442,11 @@ pub fn get_single_str_from_tts(cx: &ExtCtxt,
442442
pub fn get_exprs_from_tts(cx: &ExtCtxt,
443443
sp: Span,
444444
tts: &[ast::token_tree]) -> ~[@ast::Expr] {
445-
let p = parse::new_parser_from_tts(cx.parse_sess(),
446-
cx.cfg(),
447-
tts.to_owned());
445+
let mut p = parse::new_parser_from_tts(cx.parse_sess(),
446+
cx.cfg(),
447+
tts.to_owned());
448448
let mut es = ~[];
449-
while *p.token != token::EOF {
449+
while p.token != token::EOF {
450450
if es.len() != 0 && !p.eat(&token::COMMA) {
451451
cx.span_fatal(sp, "expected token: `,`");
452452
}

src/libsyntax/ext/cfg.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@ use parse::token;
2626
use parse::attr::parser_attr;
2727

2828
pub fn expand_cfg(cx: &mut ExtCtxt, sp: Span, tts: &[ast::token_tree]) -> base::MacResult {
29-
let p = parse::new_parser_from_tts(cx.parse_sess(), cx.cfg(), tts.to_owned());
29+
let mut p = parse::new_parser_from_tts(cx.parse_sess(),
30+
cx.cfg(),
31+
tts.to_owned());
3032

3133
let mut cfgs = ~[];
3234
// parse `cfg!(meta_item, meta_item(x,y), meta_item="foo", ...)`
33-
while *p.token != token::EOF {
35+
while p.token != token::EOF {
3436
cfgs.push(p.parse_meta_item());
3537
if p.eat(&token::EOF) { break } // trailing comma is optional,.
3638
p.expect(&token::COMMA);

src/libsyntax/ext/format.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -53,46 +53,46 @@ struct Context<'a> {
5353
impl<'a> Context<'a> {
5454
/// Parses the arguments from the given list of tokens, returning None if
5555
/// there's a parse error so we can continue parsing other format! expressions.
56-
fn parse_args(&mut self, sp: Span,
57-
tts: &[ast::token_tree]) -> (@ast::Expr, Option<@ast::Expr>) {
58-
let p = rsparse::new_parser_from_tts(self.ecx.parse_sess(),
59-
self.ecx.cfg(),
60-
tts.to_owned());
56+
fn parse_args(&mut self, sp: Span, tts: &[ast::token_tree])
57+
-> (@ast::Expr, Option<@ast::Expr>) {
58+
let mut p = rsparse::new_parser_from_tts(self.ecx.parse_sess(),
59+
self.ecx.cfg(),
60+
tts.to_owned());
6161
// Parse the leading function expression (maybe a block, maybe a path)
6262
let extra = p.parse_expr();
6363
if !p.eat(&token::COMMA) {
6464
self.ecx.span_err(sp, "expected token: `,`");
6565
return (extra, None);
6666
}
6767

68-
if *p.token == token::EOF {
68+
if p.token == token::EOF {
6969
self.ecx.span_err(sp, "requires at least a format string argument");
7070
return (extra, None);
7171
}
7272
let fmtstr = p.parse_expr();
7373
let mut named = false;
74-
while *p.token != token::EOF {
74+
while p.token != token::EOF {
7575
if !p.eat(&token::COMMA) {
7676
self.ecx.span_err(sp, "expected token: `,`");
7777
return (extra, None);
7878
}
79-
if *p.token == token::EOF { break } // accept trailing commas
80-
if named || (token::is_ident(p.token) &&
79+
if p.token == token::EOF { break } // accept trailing commas
80+
if named || (token::is_ident(&p.token) &&
8181
p.look_ahead(1, |t| *t == token::EQ)) {
8282
named = true;
83-
let ident = match *p.token {
83+
let ident = match p.token {
8484
token::IDENT(i, _) => {
8585
p.bump();
8686
i
8787
}
8888
_ if named => {
89-
self.ecx.span_err(*p.span,
89+
self.ecx.span_err(p.span,
9090
"expected ident, positional arguments \
9191
cannot follow named arguments");
9292
return (extra, None);
9393
}
9494
_ => {
95-
self.ecx.span_err(*p.span,
95+
self.ecx.span_err(p.span,
9696
format!("expected ident for named \
9797
argument, but found `{}`",
9898
p.this_token_to_str()));

src/libsyntax/ext/quote.rs

+6-10
Original file line numberDiff line numberDiff line change
@@ -579,22 +579,18 @@ fn mk_tts(cx: &ExtCtxt, sp: Span, tts: &[ast::token_tree])
579579
ss
580580
}
581581

582-
fn expand_tts(cx: &ExtCtxt,
583-
sp: Span,
584-
tts: &[ast::token_tree]) -> (@ast::Expr, @ast::Expr) {
585-
582+
fn expand_tts(cx: &ExtCtxt, sp: Span, tts: &[ast::token_tree])
583+
-> (@ast::Expr, @ast::Expr) {
586584
// NB: It appears that the main parser loses its mind if we consider
587585
// $foo as a tt_nonterminal during the main parse, so we have to re-parse
588586
// under quote_depth > 0. This is silly and should go away; the _guess_ is
589587
// it has to do with transition away from supporting old-style macros, so
590588
// try removing it when enough of them are gone.
591589

592-
let p = parse::new_parser_from_tts(
593-
cx.parse_sess(),
594-
cx.cfg(),
595-
tts.to_owned()
596-
);
597-
*p.quote_depth += 1u;
590+
let mut p = parse::new_parser_from_tts(cx.parse_sess(),
591+
cx.cfg(),
592+
tts.to_owned());
593+
p.quote_depth += 1u;
598594

599595
let cx_expr = p.parse_expr();
600596
if !p.eat(&token::COMMA) {

src/libsyntax/ext/source_util.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,13 @@ pub fn expand_include(cx: &mut ExtCtxt, sp: Span, tts: &[ast::token_tree])
8181
-> base::MacResult {
8282
let file = get_single_str_from_tts(cx, sp, tts, "include!");
8383
// The file will be added to the code map by the parser
84-
let p = parse::new_sub_parser_from_file(
85-
cx.parse_sess(), cx.cfg(),
86-
&res_rel_file(cx, sp, &Path::new(file)), sp);
84+
let mut p =
85+
parse::new_sub_parser_from_file(cx.parse_sess(),
86+
cx.cfg(),
87+
&res_rel_file(cx,
88+
sp,
89+
&Path::new(file)),
90+
sp);
8791
base::MRExpr(p.parse_expr())
8892
}
8993

src/libsyntax/ext/trace_macros.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub fn expand_trace_macros(cx: &mut ExtCtxt,
2626
None,
2727
tt.to_owned());
2828
let rdr = tt_rdr as @mut reader;
29-
let rust_parser = Parser(sess, cfg.clone(), rdr.dup());
29+
let mut rust_parser = Parser(sess, cfg.clone(), rdr.dup());
3030

3131
if rust_parser.is_keyword(keywords::True) {
3232
cx.set_trace_macros(true);
@@ -38,7 +38,7 @@ pub fn expand_trace_macros(cx: &mut ExtCtxt,
3838

3939
rust_parser.bump();
4040

41-
let rust_parser = Parser(sess, cfg, rdr.dup());
41+
let mut rust_parser = Parser(sess, cfg, rdr.dup());
4242
let result = rust_parser.parse_expr();
4343
base::MRExpr(result)
4444
}

src/libsyntax/ext/tt/macro_parser.rs

+10-8
Original file line numberDiff line numberDiff line change
@@ -403,13 +403,13 @@ pub fn parse(
403403
}
404404
rdr.next_token();
405405
} else /* bb_eis.len() == 1 */ {
406-
let rust_parser = Parser(sess, cfg.clone(), rdr.dup());
406+
let mut rust_parser = Parser(sess, cfg.clone(), rdr.dup());
407407
408408
let mut ei = bb_eis.pop();
409409
match ei.elts[ei.idx].node {
410410
match_nonterminal(_, ref name, idx) => {
411411
ei.matches[idx].push(@matched_nonterminal(
412-
parse_nt(&rust_parser, ident_to_str(name))));
412+
parse_nt(&mut rust_parser, ident_to_str(name))));
413413
ei.idx += 1u;
414414
}
415415
_ => fail!()
@@ -426,7 +426,7 @@ pub fn parse(
426426
}
427427
}
428428
429-
pub fn parse_nt(p: &Parser, name: &str) -> nonterminal {
429+
pub fn parse_nt(p: &mut Parser, name: &str) -> nonterminal {
430430
match name {
431431
"item" => match p.parse_item(~[]) {
432432
Some(i) => token::nt_item(i),
@@ -438,19 +438,21 @@ pub fn parse_nt(p: &Parser, name: &str) -> nonterminal {
438438
"expr" => token::nt_expr(p.parse_expr()),
439439
"ty" => token::nt_ty(p.parse_ty(false /* no need to disambiguate*/)),
440440
// this could be handled like a token, since it is one
441-
"ident" => match *p.token {
441+
"ident" => match p.token {
442442
token::IDENT(sn,b) => { p.bump(); token::nt_ident(~sn,b) }
443-
_ => p.fatal(~"expected ident, found "
444-
+ token::to_str(get_ident_interner(), p.token))
443+
_ => {
444+
let token_str = token::to_str(get_ident_interner(), &p.token);
445+
p.fatal(~"expected ident, found " + token_str)
446+
}
445447
},
446448
"path" => {
447449
token::nt_path(~p.parse_path(LifetimeAndTypesWithoutColons).path)
448450
}
449451
"attr" => token::nt_attr(@p.parse_attribute(false)),
450452
"tt" => {
451-
*p.quote_depth += 1u; //but in theory, non-quoted tts might be useful
453+
p.quote_depth += 1u; //but in theory, non-quoted tts might be useful
452454
let res = token::nt_tt(@p.parse_token_tree());
453-
*p.quote_depth -= 1u;
455+
p.quote_depth -= 1u;
454456
res
455457
}
456458
"matchers" => token::nt_matchers(p.parse_matchers()),

0 commit comments

Comments
 (0)