Skip to content

Commit

Permalink
Simplify creating a parser from a token tree
Browse files Browse the repository at this point in the history
Closes #15306
  • Loading branch information
Sawyer47 authored and alexcrichton committed Jul 3, 2014
1 parent 4a6fcc5 commit 2f355b7
Show file tree
Hide file tree
Showing 8 changed files with 14 additions and 46 deletions.
7 changes: 1 addition & 6 deletions src/libfourcc/lib.rs
Expand Up @@ -59,7 +59,6 @@ use syntax::codemap::{Span, mk_sp};
use syntax::ext::base;
use syntax::ext::base::{ExtCtxt, MacExpr};
use syntax::ext::build::AstBuilder;
use syntax::parse;
use syntax::parse::token;
use syntax::parse::token::InternedString;
use rustc::plugin::Registry;
Expand Down Expand Up @@ -135,11 +134,7 @@ struct Ident {

fn parse_tts(cx: &ExtCtxt,
tts: &[ast::TokenTree]) -> (Gc<ast::Expr>, Option<Ident>) {
let p = &mut parse::new_parser_from_tts(cx.parse_sess(),
cx.cfg(),
tts.iter()
.map(|x| (*x).clone())
.collect());
let p = &mut cx.new_parser_from_tts(tts);
let ex = p.parse_expr();
let id = if p.token == token::EOF {
None
Expand Down
7 changes: 1 addition & 6 deletions src/libhexfloat/lib.rs
Expand Up @@ -54,7 +54,6 @@ use syntax::codemap::{Span, mk_sp};
use syntax::ext::base;
use syntax::ext::base::{ExtCtxt, MacExpr};
use syntax::ext::build::AstBuilder;
use syntax::parse;
use syntax::parse::token;
use rustc::plugin::Registry;

Expand Down Expand Up @@ -167,11 +166,7 @@ struct Ident {

fn parse_tts(cx: &ExtCtxt,
tts: &[ast::TokenTree]) -> (Gc<ast::Expr>, Option<Ident>) {
let p = &mut parse::new_parser_from_tts(cx.parse_sess(),
cx.cfg(),
tts.iter()
.map(|x| (*x).clone())
.collect());
let p = &mut cx.new_parser_from_tts(tts);
let ex = p.parse_expr();
let id = if p.token == token::EOF {
None
Expand Down
4 changes: 1 addition & 3 deletions src/libregex_macros/lib.rs
Expand Up @@ -32,7 +32,6 @@ use syntax::ast;
use syntax::codemap;
use syntax::ext::build::AstBuilder;
use syntax::ext::base::{ExtCtxt, MacResult, MacExpr, DummyResult};
use syntax::parse;
use syntax::parse::token;
use syntax::print::pprust;

Expand Down Expand Up @@ -615,8 +614,7 @@ fn exec<'t>(which: ::regex::native::MatchKind, input: &'t str,
/// Looks for a single string literal and returns it.
/// Otherwise, logs an error with cx.span_err and returns None.
fn parse(cx: &mut ExtCtxt, tts: &[ast::TokenTree]) -> Option<String> {
let mut parser = parse::new_parser_from_tts(cx.parse_sess(), cx.cfg(),
Vec::from_slice(tts));
let mut parser = cx.new_parser_from_tts(tts);
let entry = cx.expand_expr(parser.parse_expr());
let regex = match entry.node {
ast::ExprLit(lit) => {
Expand Down
8 changes: 1 addition & 7 deletions src/libsyntax/ext/asm.rs
Expand Up @@ -16,7 +16,6 @@ use ast;
use codemap::Span;
use ext::base;
use ext::base::*;
use parse;
use parse::token::InternedString;
use parse::token;

Expand Down Expand Up @@ -48,12 +47,7 @@ static OPTIONS: &'static [&'static str] = &["volatile", "alignstack", "intel"];

pub fn expand_asm(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
-> Box<base::MacResult> {
let mut p = parse::new_parser_from_tts(cx.parse_sess(),
cx.cfg(),
tts.iter()
.map(|x| (*x).clone())
.collect());

let mut p = cx.new_parser_from_tts(tts);
let mut asm = InternedString::new("");
let mut asm_str_style = None;
let mut outputs = Vec::new();
Expand Down
12 changes: 7 additions & 5 deletions src/libsyntax/ext/base.rs
Expand Up @@ -15,6 +15,7 @@ use codemap::{CodeMap, Span, ExpnInfo};
use ext;
use ext::expand;
use parse;
use parse::parser;
use parse::token;
use parse::token::{InternedString, intern, str_to_ident};
use util::small_vector::SmallVector;
Expand Down Expand Up @@ -433,6 +434,11 @@ impl<'a> ExtCtxt<'a> {
}
}

pub fn new_parser_from_tts(&self, tts: &[ast::TokenTree])
-> parser::Parser<'a> {
parse::tts_to_parser(self.parse_sess, Vec::from_slice(tts), self.cfg())
}

pub fn codemap(&self) -> &'a CodeMap { &self.parse_sess.span_diagnostic.cm }
pub fn parse_sess(&self) -> &'a parse::ParseSess { self.parse_sess }
pub fn cfg(&self) -> ast::CrateConfig { self.cfg.clone() }
Expand Down Expand Up @@ -586,11 +592,7 @@ pub fn get_single_str_from_tts(cx: &ExtCtxt,
pub fn get_exprs_from_tts(cx: &mut ExtCtxt,
sp: Span,
tts: &[ast::TokenTree]) -> Option<Vec<Gc<ast::Expr>>> {
let mut p = parse::new_parser_from_tts(cx.parse_sess(),
cx.cfg(),
tts.iter()
.map(|x| (*x).clone())
.collect());
let mut p = cx.new_parser_from_tts(tts);
let mut es = Vec::new();
while p.token != token::EOF {
es.push(cx.expand_expr(p.parse_expr()));
Expand Down
8 changes: 1 addition & 7 deletions src/libsyntax/ext/cfg.rs
Expand Up @@ -24,17 +24,11 @@ use attr::*;
use parse::attr::ParserAttr;
use parse::token::InternedString;
use parse::token;
use parse;


pub fn expand_cfg(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
-> Box<base::MacResult> {
let mut p = parse::new_parser_from_tts(cx.parse_sess(),
cx.cfg(),
tts.iter()
.map(|x| (*x).clone())
.collect());

let mut p = cx.new_parser_from_tts(tts);
let mut cfgs = Vec::new();
// parse `cfg!(meta_item, meta_item(x,y), meta_item="foo", ...)`
while p.token != token::EOF {
Expand Down
7 changes: 1 addition & 6 deletions src/libsyntax/ext/format.rs
Expand Up @@ -16,7 +16,6 @@ use ext::base;
use ext::build::AstBuilder;
use parse::token::InternedString;
use parse::token;
use rsparse = parse;

use parse = fmt_macros;
use std::collections::HashMap;
Expand Down Expand Up @@ -81,11 +80,7 @@ fn parse_args(ecx: &mut ExtCtxt, sp: Span, allow_method: bool,
let mut names = HashMap::<String, Gc<ast::Expr>>::new();
let mut order = Vec::new();

let mut p = rsparse::new_parser_from_tts(ecx.parse_sess(),
ecx.cfg(),
tts.iter()
.map(|x| (*x).clone())
.collect());
let mut p = ecx.new_parser_from_tts(tts);
// Parse the leading function expression (maybe a block, maybe a path)
let invocation = if allow_method {
let e = p.parse_expr();
Expand Down
7 changes: 1 addition & 6 deletions src/libsyntax/ext/quote.rs
Expand Up @@ -15,7 +15,6 @@ use ext::base;
use ext::build::AstBuilder;
use parse::token::*;
use parse::token;
use parse;

use std::gc::Gc;

Expand Down Expand Up @@ -583,11 +582,7 @@ fn expand_tts(cx: &ExtCtxt, sp: Span, tts: &[ast::TokenTree])
// it has to do with transition away from supporting old-style macros, so
// try removing it when enough of them are gone.

let mut p = parse::new_parser_from_tts(cx.parse_sess(),
cx.cfg(),
tts.iter()
.map(|x| (*x).clone())
.collect());
let mut p = cx.new_parser_from_tts(tts);
p.quote_depth += 1u;

let cx_expr = p.parse_expr();
Expand Down

0 comments on commit 2f355b7

Please sign in to comment.