Skip to content
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

Simplify creating a parser from a token tree #15366

Closed
wants to merge 1 commit into from
Closed
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
7 changes: 1 addition & 6 deletions src/libfourcc/lib.rs
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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