Skip to content

libsyntax: Make the parser mutable #11226

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Jan 3, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 23 additions & 23 deletions src/libsyntax/ext/asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ fn next_state(s: State) -> Option<State> {

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

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

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

if constraint.starts_with("+") {
cx.span_unimpl(*p.last_span,
cx.span_unimpl(p.last_span,
"'+' (read+write) output operand constraint modifier");
} else if !constraint.starts_with("=") {
cx.span_err(*p.last_span, "output operand constraint lacks '='");
cx.span_err(p.last_span, "output operand constraint lacks '='");
}

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

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

if constraint.starts_with("=") {
cx.span_err(*p.last_span, "input operand constraint contains '='");
cx.span_err(p.last_span, "input operand constraint contains '='");
} else if constraint.starts_with("+") {
cx.span_err(*p.last_span, "input operand constraint contains '+'");
cx.span_err(p.last_span, "input operand constraint contains '+'");
}

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

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

if *p.token == token::COMMA {
if p.token == token::COMMA {
p.eat(&token::COMMA);
}
}
}

while *p.token == token::COLON ||
*p.token == token::MOD_SEP ||
*p.token == token::EOF {
state = if *p.token == token::COLON {
while p.token == token::COLON ||
p.token == token::MOD_SEP ||
p.token == token::EOF {
state = if p.token == token::COLON {
p.bump();
match next_state(state) {
Some(x) => x,
Expand All @@ -160,7 +160,7 @@ pub fn expand_asm(cx: &mut ExtCtxt, sp: Span, tts: &[ast::token_tree])
break
}
}
} else if *p.token == token::MOD_SEP {
} else if p.token == token::MOD_SEP {
p.bump();
let s = match next_state(state) {
Some(x) => x,
Expand All @@ -176,7 +176,7 @@ pub fn expand_asm(cx: &mut ExtCtxt, sp: Span, tts: &[ast::token_tree])
break
}
}
} else if *p.token == token::EOF {
} else if p.token == token::EOF {
continue_ = false;
break;
} else {
Expand Down
8 changes: 4 additions & 4 deletions src/libsyntax/ext/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -442,11 +442,11 @@ pub fn get_single_str_from_tts(cx: &ExtCtxt,
pub fn get_exprs_from_tts(cx: &ExtCtxt,
sp: Span,
tts: &[ast::token_tree]) -> ~[@ast::Expr] {
let p = parse::new_parser_from_tts(cx.parse_sess(),
cx.cfg(),
tts.to_owned());
let mut p = parse::new_parser_from_tts(cx.parse_sess(),
cx.cfg(),
tts.to_owned());
let mut es = ~[];
while *p.token != token::EOF {
while p.token != token::EOF {
if es.len() != 0 && !p.eat(&token::COMMA) {
cx.span_fatal(sp, "expected token: `,`");
}
Expand Down
6 changes: 4 additions & 2 deletions src/libsyntax/ext/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ use parse::token;
use parse::attr::parser_attr;

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

let mut cfgs = ~[];
// parse `cfg!(meta_item, meta_item(x,y), meta_item="foo", ...)`
while *p.token != token::EOF {
while p.token != token::EOF {
cfgs.push(p.parse_meta_item());
if p.eat(&token::EOF) { break } // trailing comma is optional,.
p.expect(&token::COMMA);
Expand Down
24 changes: 12 additions & 12 deletions src/libsyntax/ext/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,46 +53,46 @@ struct Context<'a> {
impl<'a> Context<'a> {
/// Parses the arguments from the given list of tokens, returning None if
/// there's a parse error so we can continue parsing other format! expressions.
fn parse_args(&mut self, sp: Span,
tts: &[ast::token_tree]) -> (@ast::Expr, Option<@ast::Expr>) {
let p = rsparse::new_parser_from_tts(self.ecx.parse_sess(),
self.ecx.cfg(),
tts.to_owned());
fn parse_args(&mut self, sp: Span, tts: &[ast::token_tree])
-> (@ast::Expr, Option<@ast::Expr>) {
let mut p = rsparse::new_parser_from_tts(self.ecx.parse_sess(),
self.ecx.cfg(),
tts.to_owned());
// Parse the leading function expression (maybe a block, maybe a path)
let extra = p.parse_expr();
if !p.eat(&token::COMMA) {
self.ecx.span_err(sp, "expected token: `,`");
return (extra, None);
}

if *p.token == token::EOF {
if p.token == token::EOF {
self.ecx.span_err(sp, "requires at least a format string argument");
return (extra, None);
}
let fmtstr = p.parse_expr();
let mut named = false;
while *p.token != token::EOF {
while p.token != token::EOF {
if !p.eat(&token::COMMA) {
self.ecx.span_err(sp, "expected token: `,`");
return (extra, None);
}
if *p.token == token::EOF { break } // accept trailing commas
if named || (token::is_ident(p.token) &&
if p.token == token::EOF { break } // accept trailing commas
if named || (token::is_ident(&p.token) &&
p.look_ahead(1, |t| *t == token::EQ)) {
named = true;
let ident = match *p.token {
let ident = match p.token {
token::IDENT(i, _) => {
p.bump();
i
}
_ if named => {
self.ecx.span_err(*p.span,
self.ecx.span_err(p.span,
"expected ident, positional arguments \
cannot follow named arguments");
return (extra, None);
}
_ => {
self.ecx.span_err(*p.span,
self.ecx.span_err(p.span,
format!("expected ident for named \
argument, but found `{}`",
p.this_token_to_str()));
Expand Down
16 changes: 6 additions & 10 deletions src/libsyntax/ext/quote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -579,22 +579,18 @@ fn mk_tts(cx: &ExtCtxt, sp: Span, tts: &[ast::token_tree])
ss
}

fn expand_tts(cx: &ExtCtxt,
sp: Span,
tts: &[ast::token_tree]) -> (@ast::Expr, @ast::Expr) {

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

let p = parse::new_parser_from_tts(
cx.parse_sess(),
cx.cfg(),
tts.to_owned()
);
*p.quote_depth += 1u;
let mut p = parse::new_parser_from_tts(cx.parse_sess(),
cx.cfg(),
tts.to_owned());
p.quote_depth += 1u;

let cx_expr = p.parse_expr();
if !p.eat(&token::COMMA) {
Expand Down
10 changes: 7 additions & 3 deletions src/libsyntax/ext/source_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,13 @@ pub fn expand_include(cx: &mut ExtCtxt, sp: Span, tts: &[ast::token_tree])
-> base::MacResult {
let file = get_single_str_from_tts(cx, sp, tts, "include!");
// The file will be added to the code map by the parser
let p = parse::new_sub_parser_from_file(
cx.parse_sess(), cx.cfg(),
&res_rel_file(cx, sp, &Path::new(file)), sp);
let mut p =
parse::new_sub_parser_from_file(cx.parse_sess(),
cx.cfg(),
&res_rel_file(cx,
sp,
&Path::new(file)),
sp);
base::MRExpr(p.parse_expr())
}

Expand Down
4 changes: 2 additions & 2 deletions src/libsyntax/ext/trace_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub fn expand_trace_macros(cx: &mut ExtCtxt,
None,
tt.to_owned());
let rdr = tt_rdr as @mut reader;
let rust_parser = Parser(sess, cfg.clone(), rdr.dup());
let mut rust_parser = Parser(sess, cfg.clone(), rdr.dup());

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

rust_parser.bump();

let rust_parser = Parser(sess, cfg, rdr.dup());
let mut rust_parser = Parser(sess, cfg, rdr.dup());
let result = rust_parser.parse_expr();
base::MRExpr(result)
}
18 changes: 10 additions & 8 deletions src/libsyntax/ext/tt/macro_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,13 +403,13 @@ pub fn parse(
}
rdr.next_token();
} else /* bb_eis.len() == 1 */ {
let rust_parser = Parser(sess, cfg.clone(), rdr.dup());
let mut rust_parser = Parser(sess, cfg.clone(), rdr.dup());

let mut ei = bb_eis.pop();
match ei.elts[ei.idx].node {
match_nonterminal(_, ref name, idx) => {
ei.matches[idx].push(@matched_nonterminal(
parse_nt(&rust_parser, ident_to_str(name))));
parse_nt(&mut rust_parser, ident_to_str(name))));
ei.idx += 1u;
}
_ => fail!()
Expand All @@ -426,7 +426,7 @@ pub fn parse(
}
}

pub fn parse_nt(p: &Parser, name: &str) -> nonterminal {
pub fn parse_nt(p: &mut Parser, name: &str) -> nonterminal {
match name {
"item" => match p.parse_item(~[]) {
Some(i) => token::nt_item(i),
Expand All @@ -438,19 +438,21 @@ pub fn parse_nt(p: &Parser, name: &str) -> nonterminal {
"expr" => token::nt_expr(p.parse_expr()),
"ty" => token::nt_ty(p.parse_ty(false /* no need to disambiguate*/)),
// this could be handled like a token, since it is one
"ident" => match *p.token {
"ident" => match p.token {
token::IDENT(sn,b) => { p.bump(); token::nt_ident(~sn,b) }
_ => p.fatal(~"expected ident, found "
+ token::to_str(get_ident_interner(), p.token))
_ => {
let token_str = token::to_str(get_ident_interner(), &p.token);
p.fatal(~"expected ident, found " + token_str)
}
},
"path" => {
token::nt_path(~p.parse_path(LifetimeAndTypesWithoutColons).path)
}
"attr" => token::nt_attr(@p.parse_attribute(false)),
"tt" => {
*p.quote_depth += 1u; //but in theory, non-quoted tts might be useful
p.quote_depth += 1u; //but in theory, non-quoted tts might be useful
let res = token::nt_tt(@p.parse_token_tree());
*p.quote_depth -= 1u;
p.quote_depth -= 1u;
res
}
"matchers" => token::nt_matchers(p.parse_matchers()),
Expand Down
Loading