Run rustfmt on /libsyntax/ext/tt/macro_parser.rs
This commit is contained in:
parent
bacb5c58df
commit
ac0c16d3b5
1 changed files with 113 additions and 77 deletions
|
@ -90,8 +90,8 @@ use codemap::Spanned;
|
||||||
use errors::FatalError;
|
use errors::FatalError;
|
||||||
use ext::tt::quoted::{self, TokenTree};
|
use ext::tt::quoted::{self, TokenTree};
|
||||||
use parse::{Directory, ParseSess};
|
use parse::{Directory, ParseSess};
|
||||||
use parse::parser::{PathStyle, Parser};
|
use parse::parser::{Parser, PathStyle};
|
||||||
use parse::token::{self, DocComment, Token, Nonterminal};
|
use parse::token::{self, DocComment, Nonterminal, Token};
|
||||||
use print::pprust;
|
use print::pprust;
|
||||||
use symbol::keywords;
|
use symbol::keywords;
|
||||||
use tokenstream::TokenStream;
|
use tokenstream::TokenStream;
|
||||||
|
@ -100,7 +100,7 @@ use util::small_vector::SmallVector;
|
||||||
use std::mem;
|
use std::mem;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::collections::hash_map::Entry::{Vacant, Occupied};
|
use std::collections::hash_map::Entry::{Occupied, Vacant};
|
||||||
|
|
||||||
// To avoid costly uniqueness checks, we require that `MatchSeq` always has
|
// To avoid costly uniqueness checks, we require that `MatchSeq` always has
|
||||||
// a nonempty body.
|
// a nonempty body.
|
||||||
|
@ -182,7 +182,7 @@ fn initial_matcher_pos(ms: Vec<TokenTree>, lo: BytePos) -> Box<MatcherPos> {
|
||||||
match_lo: 0,
|
match_lo: 0,
|
||||||
match_cur: 0,
|
match_cur: 0,
|
||||||
match_hi: match_idx_hi,
|
match_hi: match_idx_hi,
|
||||||
sp_lo: lo
|
sp_lo: lo,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -206,25 +206,27 @@ fn initial_matcher_pos(ms: Vec<TokenTree>, lo: BytePos) -> Box<MatcherPos> {
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub enum NamedMatch {
|
pub enum NamedMatch {
|
||||||
MatchedSeq(Rc<Vec<NamedMatch>>, syntax_pos::Span),
|
MatchedSeq(Rc<Vec<NamedMatch>>, syntax_pos::Span),
|
||||||
MatchedNonterminal(Rc<Nonterminal>)
|
MatchedNonterminal(Rc<Nonterminal>),
|
||||||
}
|
}
|
||||||
|
|
||||||
fn nameize<I: Iterator<Item=NamedMatch>>(sess: &ParseSess, ms: &[TokenTree], mut res: I)
|
fn nameize<I: Iterator<Item = NamedMatch>>(
|
||||||
-> NamedParseResult {
|
sess: &ParseSess,
|
||||||
fn n_rec<I: Iterator<Item=NamedMatch>>(sess: &ParseSess, m: &TokenTree, res: &mut I,
|
ms: &[TokenTree],
|
||||||
ret_val: &mut HashMap<Ident, Rc<NamedMatch>>)
|
mut res: I,
|
||||||
-> Result<(), (syntax_pos::Span, String)> {
|
) -> NamedParseResult {
|
||||||
|
fn n_rec<I: Iterator<Item = NamedMatch>>(
|
||||||
|
sess: &ParseSess,
|
||||||
|
m: &TokenTree,
|
||||||
|
res: &mut I,
|
||||||
|
ret_val: &mut HashMap<Ident, Rc<NamedMatch>>,
|
||||||
|
) -> Result<(), (syntax_pos::Span, String)> {
|
||||||
match *m {
|
match *m {
|
||||||
TokenTree::Sequence(_, ref seq) => {
|
TokenTree::Sequence(_, ref seq) => for next_m in &seq.tts {
|
||||||
for next_m in &seq.tts {
|
n_rec(sess, next_m, res.by_ref(), ret_val)?
|
||||||
n_rec(sess, next_m, res.by_ref(), ret_val)?
|
},
|
||||||
}
|
TokenTree::Delimited(_, ref delim) => for next_m in &delim.tts {
|
||||||
}
|
n_rec(sess, next_m, res.by_ref(), ret_val)?;
|
||||||
TokenTree::Delimited(_, ref delim) => {
|
},
|
||||||
for next_m in &delim.tts {
|
|
||||||
n_rec(sess, next_m, res.by_ref(), ret_val)?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
TokenTree::MetaVarDecl(span, _, id) if id.name == keywords::Invalid.name() => {
|
TokenTree::MetaVarDecl(span, _, id) if id.name == keywords::Invalid.name() => {
|
||||||
if sess.missing_fragment_specifiers.borrow_mut().remove(&span) {
|
if sess.missing_fragment_specifiers.borrow_mut().remove(&span) {
|
||||||
return Err((span, "missing fragment specifier".to_string()));
|
return Err((span, "missing fragment specifier".to_string()));
|
||||||
|
@ -250,7 +252,7 @@ fn nameize<I: Iterator<Item=NamedMatch>>(sess: &ParseSess, ms: &[TokenTree], mut
|
||||||
let mut ret_val = HashMap::new();
|
let mut ret_val = HashMap::new();
|
||||||
for m in ms {
|
for m in ms {
|
||||||
match n_rec(sess, m, res.by_ref(), &mut ret_val) {
|
match n_rec(sess, m, res.by_ref(), &mut ret_val) {
|
||||||
Ok(_) => {},
|
Ok(_) => {}
|
||||||
Err((sp, msg)) => return Error(sp, msg),
|
Err((sp, msg)) => return Error(sp, msg),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -265,18 +267,21 @@ pub enum ParseResult<T> {
|
||||||
/// indicates that no rules expected the given token.
|
/// indicates that no rules expected the given token.
|
||||||
Failure(syntax_pos::Span, Token),
|
Failure(syntax_pos::Span, Token),
|
||||||
/// Fatal error (malformed macro?). Abort compilation.
|
/// Fatal error (malformed macro?). Abort compilation.
|
||||||
Error(syntax_pos::Span, String)
|
Error(syntax_pos::Span, String),
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_failure_msg(tok: Token) -> String {
|
pub fn parse_failure_msg(tok: Token) -> String {
|
||||||
match tok {
|
match tok {
|
||||||
token::Eof => "unexpected end of macro invocation".to_string(),
|
token::Eof => "unexpected end of macro invocation".to_string(),
|
||||||
_ => format!("no rules expected the token `{}`", pprust::token_to_string(&tok)),
|
_ => format!(
|
||||||
|
"no rules expected the token `{}`",
|
||||||
|
pprust::token_to_string(&tok)
|
||||||
|
),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Perform a token equality check, ignoring syntax context (that is, an unhygienic comparison)
|
/// Perform a token equality check, ignoring syntax context (that is, an unhygienic comparison)
|
||||||
fn token_name_eq(t1 : &Token, t2 : &Token) -> bool {
|
fn token_name_eq(t1: &Token, t2: &Token) -> bool {
|
||||||
if let (Some(id1), Some(id2)) = (t1.ident(), t2.ident()) {
|
if let (Some(id1), Some(id2)) = (t1.ident(), t2.ident()) {
|
||||||
id1.name == id2.name
|
id1.name == id2.name
|
||||||
} else if let (&token::Lifetime(id1), &token::Lifetime(id2)) = (t1, t2) {
|
} else if let (&token::Lifetime(id1), &token::Lifetime(id2)) = (t1, t2) {
|
||||||
|
@ -290,14 +295,15 @@ fn create_matches(len: usize) -> Vec<Rc<Vec<NamedMatch>>> {
|
||||||
(0..len).into_iter().map(|_| Rc::new(Vec::new())).collect()
|
(0..len).into_iter().map(|_| Rc::new(Vec::new())).collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn inner_parse_loop(sess: &ParseSess,
|
fn inner_parse_loop(
|
||||||
cur_items: &mut SmallVector<Box<MatcherPos>>,
|
sess: &ParseSess,
|
||||||
next_items: &mut Vec<Box<MatcherPos>>,
|
cur_items: &mut SmallVector<Box<MatcherPos>>,
|
||||||
eof_items: &mut SmallVector<Box<MatcherPos>>,
|
next_items: &mut Vec<Box<MatcherPos>>,
|
||||||
bb_items: &mut SmallVector<Box<MatcherPos>>,
|
eof_items: &mut SmallVector<Box<MatcherPos>>,
|
||||||
token: &Token,
|
bb_items: &mut SmallVector<Box<MatcherPos>>,
|
||||||
span: syntax_pos::Span)
|
token: &Token,
|
||||||
-> ParseResult<()> {
|
span: syntax_pos::Span,
|
||||||
|
) -> ParseResult<()> {
|
||||||
while let Some(mut item) = cur_items.pop() {
|
while let Some(mut item) = cur_items.pop() {
|
||||||
// When unzipped trees end, remove them
|
// When unzipped trees end, remove them
|
||||||
while item.idx >= item.top_elts.len() {
|
while item.idx >= item.top_elts.len() {
|
||||||
|
@ -306,7 +312,7 @@ fn inner_parse_loop(sess: &ParseSess,
|
||||||
item.top_elts = elts;
|
item.top_elts = elts;
|
||||||
item.idx = idx + 1;
|
item.idx = idx + 1;
|
||||||
}
|
}
|
||||||
None => break
|
None => break,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -341,11 +347,16 @@ fn inner_parse_loop(sess: &ParseSess,
|
||||||
// Check if we need a separator
|
// Check if we need a separator
|
||||||
if idx == len && item.sep.is_some() {
|
if idx == len && item.sep.is_some() {
|
||||||
// We have a separator, and it is the current token.
|
// We have a separator, and it is the current token.
|
||||||
if item.sep.as_ref().map(|sep| token_name_eq(token, sep)).unwrap_or(false) {
|
if item.sep
|
||||||
|
.as_ref()
|
||||||
|
.map(|sep| token_name_eq(token, sep))
|
||||||
|
.unwrap_or(false)
|
||||||
|
{
|
||||||
item.idx += 1;
|
item.idx += 1;
|
||||||
next_items.push(item);
|
next_items.push(item);
|
||||||
}
|
}
|
||||||
} else { // we don't need a separator
|
} else {
|
||||||
|
// we don't need a separator
|
||||||
item.match_cur = item.match_lo;
|
item.match_cur = item.match_lo;
|
||||||
item.idx = 0;
|
item.idx = 0;
|
||||||
cur_items.push(item);
|
cur_items.push(item);
|
||||||
|
@ -418,12 +429,13 @@ fn inner_parse_loop(sess: &ParseSess,
|
||||||
Success(())
|
Success(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse(sess: &ParseSess,
|
pub fn parse(
|
||||||
tts: TokenStream,
|
sess: &ParseSess,
|
||||||
ms: &[TokenTree],
|
tts: TokenStream,
|
||||||
directory: Option<Directory>,
|
ms: &[TokenTree],
|
||||||
recurse_into_modules: bool)
|
directory: Option<Directory>,
|
||||||
-> NamedParseResult {
|
recurse_into_modules: bool,
|
||||||
|
) -> NamedParseResult {
|
||||||
let mut parser = Parser::new(sess, tts, directory, recurse_into_modules, true);
|
let mut parser = Parser::new(sess, tts, directory, recurse_into_modules, true);
|
||||||
let mut cur_items = SmallVector::one(initial_matcher_pos(ms.to_owned(), parser.span.lo()));
|
let mut cur_items = SmallVector::one(initial_matcher_pos(ms.to_owned(), parser.span.lo()));
|
||||||
let mut next_items = Vec::new(); // or proceed normally
|
let mut next_items = Vec::new(); // or proceed normally
|
||||||
|
@ -433,9 +445,16 @@ pub fn parse(sess: &ParseSess,
|
||||||
let mut eof_items = SmallVector::new();
|
let mut eof_items = SmallVector::new();
|
||||||
assert!(next_items.is_empty());
|
assert!(next_items.is_empty());
|
||||||
|
|
||||||
match inner_parse_loop(sess, &mut cur_items, &mut next_items, &mut eof_items, &mut bb_items,
|
match inner_parse_loop(
|
||||||
&parser.token, parser.span) {
|
sess,
|
||||||
Success(_) => {},
|
&mut cur_items,
|
||||||
|
&mut next_items,
|
||||||
|
&mut eof_items,
|
||||||
|
&mut bb_items,
|
||||||
|
&parser.token,
|
||||||
|
parser.span,
|
||||||
|
) {
|
||||||
|
Success(_) => {}
|
||||||
Failure(sp, tok) => return Failure(sp, tok),
|
Failure(sp, tok) => return Failure(sp, tok),
|
||||||
Error(sp, msg) => return Error(sp, msg),
|
Error(sp, msg) => return Error(sp, msg),
|
||||||
}
|
}
|
||||||
|
@ -446,43 +465,56 @@ pub fn parse(sess: &ParseSess,
|
||||||
/* error messages here could be improved with links to orig. rules */
|
/* error messages here could be improved with links to orig. rules */
|
||||||
if token_name_eq(&parser.token, &token::Eof) {
|
if token_name_eq(&parser.token, &token::Eof) {
|
||||||
if eof_items.len() == 1 {
|
if eof_items.len() == 1 {
|
||||||
let matches = eof_items[0].matches.iter_mut().map(|dv| {
|
let matches = eof_items[0]
|
||||||
Rc::make_mut(dv).pop().unwrap()
|
.matches
|
||||||
});
|
.iter_mut()
|
||||||
|
.map(|dv| Rc::make_mut(dv).pop().unwrap());
|
||||||
return nameize(sess, ms, matches);
|
return nameize(sess, ms, matches);
|
||||||
} else if eof_items.len() > 1 {
|
} else if eof_items.len() > 1 {
|
||||||
return Error(parser.span, "ambiguity: multiple successful parses".to_string());
|
return Error(
|
||||||
|
parser.span,
|
||||||
|
"ambiguity: multiple successful parses".to_string(),
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
return Failure(parser.span, token::Eof);
|
return Failure(parser.span, token::Eof);
|
||||||
}
|
}
|
||||||
} else if (!bb_items.is_empty() && !next_items.is_empty()) || bb_items.len() > 1 {
|
} else if (!bb_items.is_empty() && !next_items.is_empty()) || bb_items.len() > 1 {
|
||||||
let nts = bb_items.iter().map(|item| match item.top_elts.get_tt(item.idx) {
|
let nts = bb_items
|
||||||
TokenTree::MetaVarDecl(_, bind, name) => {
|
.iter()
|
||||||
format!("{} ('{}')", name, bind)
|
.map(|item| match item.top_elts.get_tt(item.idx) {
|
||||||
}
|
TokenTree::MetaVarDecl(_, bind, name) => format!("{} ('{}')", name, bind),
|
||||||
_ => panic!()
|
_ => panic!(),
|
||||||
}).collect::<Vec<String>>().join(" or ");
|
})
|
||||||
|
.collect::<Vec<String>>()
|
||||||
|
.join(" or ");
|
||||||
|
|
||||||
return Error(parser.span, format!(
|
return Error(
|
||||||
"local ambiguity: multiple parsing options: {}",
|
parser.span,
|
||||||
match next_items.len() {
|
format!(
|
||||||
0 => format!("built-in NTs {}.", nts),
|
"local ambiguity: multiple parsing options: {}",
|
||||||
1 => format!("built-in NTs {} or 1 other option.", nts),
|
match next_items.len() {
|
||||||
n => format!("built-in NTs {} or {} other options.", nts, n),
|
0 => format!("built-in NTs {}.", nts),
|
||||||
}
|
1 => format!("built-in NTs {} or 1 other option.", nts),
|
||||||
));
|
n => format!("built-in NTs {} or {} other options.", nts, n),
|
||||||
|
}
|
||||||
|
),
|
||||||
|
);
|
||||||
} else if bb_items.is_empty() && next_items.is_empty() {
|
} else if bb_items.is_empty() && next_items.is_empty() {
|
||||||
return Failure(parser.span, parser.token);
|
return Failure(parser.span, parser.token);
|
||||||
} else if !next_items.is_empty() {
|
} else if !next_items.is_empty() {
|
||||||
/* Now process the next token */
|
/* Now process the next token */
|
||||||
cur_items.extend(next_items.drain(..));
|
cur_items.extend(next_items.drain(..));
|
||||||
parser.bump();
|
parser.bump();
|
||||||
} else /* bb_items.len() == 1 */ {
|
} else
|
||||||
|
/* bb_items.len() == 1 */
|
||||||
|
{
|
||||||
let mut item = bb_items.pop().unwrap();
|
let mut item = bb_items.pop().unwrap();
|
||||||
if let TokenTree::MetaVarDecl(span, _, ident) = item.top_elts.get_tt(item.idx) {
|
if let TokenTree::MetaVarDecl(span, _, ident) = item.top_elts.get_tt(item.idx) {
|
||||||
let match_cur = item.match_cur;
|
let match_cur = item.match_cur;
|
||||||
item.push_match(match_cur,
|
item.push_match(
|
||||||
MatchedNonterminal(Rc::new(parse_nt(&mut parser, span, &ident.name.as_str()))));
|
match_cur,
|
||||||
|
MatchedNonterminal(Rc::new(parse_nt(&mut parser, span, &ident.name.as_str()))),
|
||||||
|
);
|
||||||
item.idx += 1;
|
item.idx += 1;
|
||||||
item.match_cur += 1;
|
item.match_cur += 1;
|
||||||
} else {
|
} else {
|
||||||
|
@ -512,20 +544,21 @@ fn may_begin_with(name: &str, token: &Token) -> bool {
|
||||||
"expr" => token.can_begin_expr(),
|
"expr" => token.can_begin_expr(),
|
||||||
"ty" => token.can_begin_type(),
|
"ty" => token.can_begin_type(),
|
||||||
"ident" => token.is_ident(),
|
"ident" => token.is_ident(),
|
||||||
"vis" => match *token { // The follow-set of :vis + "priv" keyword + interpolated
|
"vis" => match *token {
|
||||||
|
// The follow-set of :vis + "priv" keyword + interpolated
|
||||||
Token::Comma | Token::Ident(_) | Token::Interpolated(_) => true,
|
Token::Comma | Token::Ident(_) | Token::Interpolated(_) => true,
|
||||||
_ => token.can_begin_type(),
|
_ => token.can_begin_type(),
|
||||||
},
|
},
|
||||||
"block" => match *token {
|
"block" => match *token {
|
||||||
Token::OpenDelim(token::Brace) => true,
|
Token::OpenDelim(token::Brace) => true,
|
||||||
Token::Interpolated(ref nt) => match nt.0 {
|
Token::Interpolated(ref nt) => match nt.0 {
|
||||||
token::NtItem(_) |
|
token::NtItem(_)
|
||||||
token::NtPat(_) |
|
| token::NtPat(_)
|
||||||
token::NtTy(_) |
|
| token::NtTy(_)
|
||||||
token::NtIdent(_) |
|
| token::NtIdent(_)
|
||||||
token::NtMeta(_) |
|
| token::NtMeta(_)
|
||||||
token::NtPath(_) |
|
| token::NtPath(_)
|
||||||
token::NtVis(_) => false, // none of these may start with '{'.
|
| token::NtVis(_) => false, // none of these may start with '{'.
|
||||||
_ => true,
|
_ => true,
|
||||||
},
|
},
|
||||||
_ => false,
|
_ => false,
|
||||||
|
@ -591,12 +624,15 @@ fn parse_nt<'a>(p: &mut Parser<'a>, sp: Span, name: &str) -> Nonterminal {
|
||||||
"ident" => match p.token {
|
"ident" => match p.token {
|
||||||
token::Ident(sn) => {
|
token::Ident(sn) => {
|
||||||
p.bump();
|
p.bump();
|
||||||
token::NtIdent(Spanned::<Ident>{node: sn, span: p.prev_span})
|
token::NtIdent(Spanned::<Ident> {
|
||||||
|
node: sn,
|
||||||
|
span: p.prev_span,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
let token_str = pprust::token_to_string(&p.token);
|
let token_str = pprust::token_to_string(&p.token);
|
||||||
p.fatal(&format!("expected ident, found {}",
|
p.fatal(&format!("expected ident, found {}", &token_str[..]))
|
||||||
&token_str[..])).emit();
|
.emit();
|
||||||
FatalError.raise()
|
FatalError.raise()
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -606,6 +642,6 @@ fn parse_nt<'a>(p: &mut Parser<'a>, sp: Span, name: &str) -> Nonterminal {
|
||||||
"lifetime" => token::NtLifetime(p.expect_lifetime()),
|
"lifetime" => token::NtLifetime(p.expect_lifetime()),
|
||||||
// this is not supposed to happen, since it has been checked
|
// this is not supposed to happen, since it has been checked
|
||||||
// when compiling the macro.
|
// when compiling the macro.
|
||||||
_ => p.span_bug(sp, "invalid fragment specifier")
|
_ => p.span_bug(sp, "invalid fragment specifier"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue