Skip to content

Commit f499d36

Browse files
committed
libsyntax: Make the parser mutable
1 parent 0df9b85 commit f499d36

File tree

13 files changed

+518
-487
lines changed

13 files changed

+518
-487
lines changed

src/libsyntax/ext/asm.rs

Lines changed: 3 additions & 3 deletions
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;

src/libsyntax/ext/base.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -442,9 +442,9 @@ 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 = ~[];
449449
while *p.token != token::EOF {
450450
if es.len() != 0 && !p.eat(&token::COMMA) {

src/libsyntax/ext/cfg.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ 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", ...)`

src/libsyntax/ext/format.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ 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) {

src/libsyntax/ext/quote.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -579,21 +579,17 @@ 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-
);
590+
let mut p = parse::new_parser_from_tts(cx.parse_sess(),
591+
cx.cfg(),
592+
tts.to_owned());
597593
*p.quote_depth += 1u;
598594

599595
let cx_expr = p.parse_expr();

src/libsyntax/ext/source_util.rs

Lines changed: 7 additions & 3 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 3 additions & 3 deletions
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),

src/libsyntax/ext/tt/macro_rules.rs

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@ use parse::attr::parser_attr;
2424
use parse::token::{get_ident_interner, special_idents, gensym_ident, ident_to_str};
2525
use parse::token::{FAT_ARROW, SEMI, nt_matchers, nt_tt, EOF};
2626
use print;
27+
use std::cell::RefCell;
2728
use util::small_vector::SmallVector;
2829

2930
struct ParserAnyMacro {
30-
parser: @Parser,
31+
parser: RefCell<Parser>,
3132
}
3233

3334
impl ParserAnyMacro {
@@ -38,28 +39,36 @@ impl ParserAnyMacro {
3839
/// fail!(); } )` doesn't get picked up by .parse_expr(), but it's
3940
/// allowed to be there.
4041
fn ensure_complete_parse(&self, allow_semi: bool) {
41-
if allow_semi && *self.parser.token == SEMI {
42-
self.parser.bump()
42+
let mut parser = self.parser.borrow_mut();
43+
if allow_semi && *parser.get().token == SEMI {
44+
parser.get().bump()
4345
}
44-
if *self.parser.token != EOF {
45-
let msg = format!("macro expansion ignores token `{}` and any following",
46-
self.parser.this_token_to_str());
47-
self.parser.span_err(*self.parser.span, msg);
46+
if *parser.get().token != EOF {
47+
let token_str = parser.get().this_token_to_str();
48+
let msg = format!("macro expansion ignores token `{}` and any \
49+
following",
50+
token_str);
51+
let span = *parser.get().span;
52+
parser.get().span_err(span, msg);
4853
}
4954
}
5055
}
5156

5257
impl AnyMacro for ParserAnyMacro {
5358
fn make_expr(&self) -> @ast::Expr {
54-
let ret = self.parser.parse_expr();
59+
let ret = {
60+
let mut parser = self.parser.borrow_mut();
61+
parser.get().parse_expr()
62+
};
5563
self.ensure_complete_parse(true);
5664
ret
5765
}
5866
fn make_items(&self) -> SmallVector<@ast::item> {
5967
let mut ret = SmallVector::zero();
6068
loop {
61-
let attrs = self.parser.parse_outer_attributes();
62-
match self.parser.parse_item(attrs) {
69+
let mut parser = self.parser.borrow_mut();
70+
let attrs = parser.get().parse_outer_attributes();
71+
match parser.get().parse_item(attrs) {
6372
Some(item) => ret.push(item),
6473
None => break
6574
}
@@ -68,8 +77,11 @@ impl AnyMacro for ParserAnyMacro {
6877
ret
6978
}
7079
fn make_stmt(&self) -> @ast::Stmt {
71-
let attrs = self.parser.parse_outer_attributes();
72-
let ret = self.parser.parse_stmt(attrs);
80+
let ret = {
81+
let mut parser = self.parser.borrow_mut();
82+
let attrs = parser.get().parse_outer_attributes();
83+
parser.get().parse_stmt(attrs)
84+
};
7385
self.ensure_complete_parse(true);
7486
ret
7587
}
@@ -142,14 +154,14 @@ fn generic_extension(cx: &ExtCtxt,
142154
// rhs has holes ( `$id` and `$(...)` that need filled)
143155
let trncbr = new_tt_reader(s_d, Some(named_matches),
144156
rhs);
145-
let p = @Parser(cx.parse_sess(),
146-
cx.cfg(),
147-
trncbr as @mut reader);
157+
let p = Parser(cx.parse_sess(),
158+
cx.cfg(),
159+
trncbr as @mut reader);
148160

149161
// Let the context choose how to interpret the result.
150162
// Weird, but useful for X-macros.
151163
return MRAny(@ParserAnyMacro {
152-
parser: p,
164+
parser: RefCell::new(p),
153165
} as @AnyMacro)
154166
}
155167
failure(sp, ref msg) => if sp.lo >= best_fail_spot.lo {

src/libsyntax/parse/attr.rs

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,18 @@ use parse::token::INTERPOLATED;
1717

1818
// a parser that can parse attributes.
1919
pub trait parser_attr {
20-
fn parse_outer_attributes(&self) -> ~[ast::Attribute];
21-
fn parse_attribute(&self, permit_inner: bool) -> ast::Attribute;
22-
fn parse_inner_attrs_and_next(&self) ->
23-
(~[ast::Attribute], ~[ast::Attribute]);
24-
fn parse_meta_item(&self) -> @ast::MetaItem;
25-
fn parse_meta_seq(&self) -> ~[@ast::MetaItem];
26-
fn parse_optional_meta(&self) -> ~[@ast::MetaItem];
20+
fn parse_outer_attributes(&mut self) -> ~[ast::Attribute];
21+
fn parse_attribute(&mut self, permit_inner: bool) -> ast::Attribute;
22+
fn parse_inner_attrs_and_next(&mut self)
23+
-> (~[ast::Attribute], ~[ast::Attribute]);
24+
fn parse_meta_item(&mut self) -> @ast::MetaItem;
25+
fn parse_meta_seq(&mut self) -> ~[@ast::MetaItem];
26+
fn parse_optional_meta(&mut self) -> ~[@ast::MetaItem];
2727
}
2828

2929
impl parser_attr for Parser {
30-
3130
// Parse attributes that appear before an item
32-
fn parse_outer_attributes(&self) -> ~[ast::Attribute] {
31+
fn parse_outer_attributes(&mut self) -> ~[ast::Attribute] {
3332
let mut attrs: ~[ast::Attribute] = ~[];
3433
loop {
3534
debug!("parse_outer_attributes: self.token={:?}",
@@ -66,7 +65,7 @@ impl parser_attr for Parser {
6665
//
6766
// if permit_inner is true, then a trailing `;` indicates an inner
6867
// attribute
69-
fn parse_attribute(&self, permit_inner: bool) -> ast::Attribute {
68+
fn parse_attribute(&mut self, permit_inner: bool) -> ast::Attribute {
7069
debug!("parse_attributes: permit_inner={:?} self.token={:?}",
7170
permit_inner, self.token);
7271
let (span, value) = match *self.token {
@@ -85,8 +84,9 @@ impl parser_attr for Parser {
8584
(mk_sp(lo, hi), meta_item)
8685
}
8786
_ => {
87+
let token_str = self.this_token_to_str();
8888
self.fatal(format!("expected `\\#` but found `{}`",
89-
self.this_token_to_str()));
89+
token_str));
9090
}
9191
};
9292
let style = if permit_inner && *self.token == token::SEMI {
@@ -115,7 +115,7 @@ impl parser_attr for Parser {
115115
// matches inner_attrs* outer_attr?
116116
// you can make the 'next' field an Option, but the result is going to be
117117
// more useful as a vector.
118-
fn parse_inner_attrs_and_next(&self)
118+
fn parse_inner_attrs_and_next(&mut self)
119119
-> (~[ast::Attribute], ~[ast::Attribute]) {
120120
let mut inner_attrs: ~[ast::Attribute] = ~[];
121121
let mut next_outer_attrs: ~[ast::Attribute] = ~[];
@@ -154,9 +154,10 @@ impl parser_attr for Parser {
154154
// matches meta_item = IDENT
155155
// | IDENT = lit
156156
// | IDENT meta_seq
157-
fn parse_meta_item(&self) -> @ast::MetaItem {
157+
fn parse_meta_item(&mut self) -> @ast::MetaItem {
158158
let lo = self.span.lo;
159-
let name = self.id_to_str(self.parse_ident());
159+
let ident = self.parse_ident();
160+
let name = self.id_to_str(ident);
160161
match *self.token {
161162
token::EQ => {
162163
self.bump();
@@ -187,14 +188,14 @@ impl parser_attr for Parser {
187188
}
188189

189190
// matches meta_seq = ( COMMASEP(meta_item) )
190-
fn parse_meta_seq(&self) -> ~[@ast::MetaItem] {
191+
fn parse_meta_seq(&mut self) -> ~[@ast::MetaItem] {
191192
self.parse_seq(&token::LPAREN,
192193
&token::RPAREN,
193194
seq_sep_trailing_disallowed(token::COMMA),
194195
|p| p.parse_meta_item()).node
195196
}
196197

197-
fn parse_optional_meta(&self) -> ~[@ast::MetaItem] {
198+
fn parse_optional_meta(&mut self) -> ~[@ast::MetaItem] {
198199
match *self.token {
199200
token::LPAREN => self.parse_meta_seq(),
200201
_ => ~[]

0 commit comments

Comments
 (0)