libsyntax: minor cleanup

This commit is contained in:
Erick Tryzelaar 2013-02-24 15:41:54 -08:00
parent 3635480b15
commit 272c25e938
3 changed files with 246 additions and 138 deletions

View File

@ -151,9 +151,12 @@ fn parse_meta_item() -> @ast::meta_item {
}
fn parse_meta_seq() -> ~[@ast::meta_item] {
return self.parse_seq(token::LPAREN, token::RPAREN,
seq_sep_trailing_disallowed(token::COMMA),
|p| p.parse_meta_item()).node;
self.parse_seq(
token::LPAREN,
token::RPAREN,
seq_sep_trailing_disallowed(token::COMMA),
|p| p.parse_meta_item()
).node
}
fn parse_optional_meta() -> ~[@ast::meta_item] {

View File

@ -29,20 +29,20 @@ pub struct SeqSep {
pub fn seq_sep_trailing_disallowed(t: token::Token) -> SeqSep {
SeqSep {
sep: option::Some(t),
trailing_sep_allowed: false
sep: Some(t),
trailing_sep_allowed: false,
}
}
pub fn seq_sep_trailing_allowed(t: token::Token) -> SeqSep {
SeqSep {
sep: option::Some(t),
trailing_sep_allowed: true
sep: Some(t),
trailing_sep_allowed: true,
}
}
pub fn seq_sep_none() -> SeqSep {
SeqSep {
sep: option::None,
trailing_sep_allowed: false
sep: None,
trailing_sep_allowed: false,
}
}
@ -54,12 +54,20 @@ pub impl Parser {
fn unexpected_last(t: token::Token) -> ! {
self.span_fatal(
*self.last_span,
~"unexpected token: `" + token_to_str(self.reader, t) + ~"`");
fmt!(
"unexpected token: `%s`",
token_to_str(self.reader, t)
)
);
}
fn unexpected() -> ! {
self.fatal(~"unexpected token: `"
+ token_to_str(self.reader, *self.token) + ~"`");
self.fatal(
fmt!(
"unexpected token: `%s`",
token_to_str(self.reader, *self.token)
)
);
}
// expect and consume the token t. Signal an error if
@ -81,12 +89,23 @@ fn parse_ident() -> ast::ident {
self.check_strict_keywords();
self.check_reserved_keywords();
match *self.token {
token::IDENT(i, _) => { self.bump(); return i; }
token::INTERPOLATED(token::nt_ident(*)) => { self.bug(
~"ident interpolation not converted to real token"); }
_ => { self.fatal(~"expected ident, found `"
+ token_to_str(self.reader, *self.token)
+ ~"`"); }
token::IDENT(i, _) => {
self.bump();
i
}
token::INTERPOLATED(token::nt_ident(*)) => {
self.bug(
~"ident interpolation not converted to real token"
);
}
_ => {
self.fatal(
fmt!(
"expected ident, found `%s`",
token_to_str(self.reader, *self.token)
)
);
}
}
}
@ -155,9 +174,13 @@ fn eat_keyword(word: &~str) -> bool {
fn expect_keyword(word: &~str) {
self.require_keyword(word);
if !self.eat_keyword(word) {
self.fatal(~"expected `" + *word + ~"`, found `" +
token_to_str(self.reader, *self.token) +
~"`");
self.fatal(
fmt!(
"expected `%s`, found `%s`",
*word,
token_to_str(self.reader, *self.token)
)
);
}
}
@ -177,7 +200,7 @@ fn check_strict_keywords() {
fn check_strict_keywords_(w: &~str) {
if self.is_strict_keyword(w) {
self.fatal(~"found `" + *w + ~"` in ident position");
self.fatal(fmt!("found `%s` in ident position", *w));
}
}
@ -197,7 +220,7 @@ fn check_reserved_keywords() {
fn check_reserved_keywords_(w: &~str) {
if self.is_reserved_keyword(w) {
self.fatal(~"`" + *w + ~"` is a reserved keyword");
self.fatal(fmt!("`%s` is a reserved keyword", *w));
}
}
@ -207,9 +230,11 @@ fn expect_gt() {
if *self.token == token::GT {
self.bump();
} else if *self.token == token::BINOP(token::SHR) {
self.replace_token(token::GT,
self.span.lo + BytePos(1u),
self.span.hi);
self.replace_token(
token::GT,
self.span.lo + BytePos(1u),
self.span.hi
);
} else {
let mut s: ~str = ~"expected `";
s += token_to_str(self.reader, token::GT);
@ -222,8 +247,10 @@ fn expect_gt() {
// parse a sequence bracketed by '<' and '>', stopping
// before the '>'.
fn parse_seq_to_before_gt<T:Copy>(sep: Option<token::Token>,
f: fn(Parser) -> T) -> ~[T] {
fn parse_seq_to_before_gt<T: Copy>(
sep: Option<token::Token>,
f: fn(Parser) -> T
) -> ~[T] {
let mut first = true;
let mut v = ~[];
while *self.token != token::GT
@ -241,8 +268,10 @@ fn parse_seq_to_before_gt<T:Copy>(sep: Option<token::Token>,
return v;
}
fn parse_seq_to_gt<T:Copy>(sep: Option<token::Token>,
f: fn(Parser) -> T) -> ~[T] {
fn parse_seq_to_gt<T: Copy>(
sep: Option<token::Token>,
f: fn(Parser) -> T
) -> ~[T] {
let v = self.parse_seq_to_before_gt(sep, f);
self.expect_gt();
@ -250,8 +279,10 @@ fn parse_seq_to_gt<T:Copy>(sep: Option<token::Token>,
}
// parse a sequence bracketed by '<' and '>'
fn parse_seq_lt_gt<T:Copy>(sep: Option<token::Token>,
f: fn(Parser) -> T) -> spanned<~[T]> {
fn parse_seq_lt_gt<T: Copy>(
sep: Option<token::Token>,
f: fn(Parser) -> T
) -> spanned<~[T]> {
let lo = self.span.lo;
self.expect(&token::LT);
let result = self.parse_seq_to_before_gt::<T>(sep, f);
@ -263,18 +294,24 @@ fn parse_seq_lt_gt<T:Copy>(sep: Option<token::Token>,
// parse a sequence, including the closing delimiter. The function
// f must consume tokens until reaching the next separator or
// closing bracket.
fn parse_seq_to_end<T:Copy>(ket: token::Token, sep: SeqSep,
f: fn(Parser) -> T) -> ~[T] {
fn parse_seq_to_end<T: Copy>(
ket: token::Token,
sep: SeqSep,
f: fn(Parser) -> T
) -> ~[T] {
let val = self.parse_seq_to_before_end(ket, sep, f);
self.bump();
return val;
val
}
// parse a sequence, not including the closing delimiter. The function
// f must consume tokens until reaching the next separator or
// closing bracket.
fn parse_seq_to_before_end<T:Copy>(ket: token::Token, sep: SeqSep,
f: fn(Parser) -> T) -> ~[T] {
fn parse_seq_to_before_end<T: Copy>(
ket: token::Token,
sep: SeqSep,
f: fn(Parser) -> T
) -> ~[T] {
let mut first: bool = true;
let mut v: ~[T] = ~[];
while *self.token != ket {
@ -288,31 +325,37 @@ fn parse_seq_to_before_end<T:Copy>(ket: token::Token, sep: SeqSep,
if sep.trailing_sep_allowed && *self.token == ket { break; }
v.push(f(self));
}
return v;
v
}
// parse a sequence, including the closing delimiter. The function
// f must consume tokens until reaching the next separator or
// closing bracket.
fn parse_unspanned_seq<T:Copy>(+bra: token::Token,
+ket: token::Token,
sep: SeqSep,
f: fn(Parser) -> T) -> ~[T] {
fn parse_unspanned_seq<T: Copy>(
+bra: token::Token,
+ket: token::Token,
sep: SeqSep,
f: fn(Parser) -> T
) -> ~[T] {
self.expect(&bra);
let result = self.parse_seq_to_before_end::<T>(ket, sep, f);
let result = self.parse_seq_to_before_end(ket, sep, f);
self.bump();
return result;
result
}
// NB: Do not use this function unless you actually plan to place the
// spanned list in the AST.
fn parse_seq<T:Copy>(bra: token::Token, ket: token::Token, sep: SeqSep,
f: fn(Parser) -> T) -> spanned<~[T]> {
fn parse_seq<T: Copy>(
+bra: token::Token,
+ket: token::Token,
sep: SeqSep,
f: fn(Parser) -> T
) -> spanned<~[T]> {
let lo = self.span.lo;
self.expect(&bra);
let result = self.parse_seq_to_before_end::<T>(ket, sep, f);
let result = self.parse_seq_to_before_end(ket, sep, f);
let hi = self.span.hi;
self.bump();
return spanned(lo, hi, result);
spanned(lo, hi, result)
}
}

View File

@ -422,16 +422,21 @@ fn parse_ty_fn_decl() -> fn_decl {
self.expect(&token::GT);
}
let inputs = self.parse_unspanned_seq(
token::LPAREN, token::RPAREN,
token::LPAREN,
token::RPAREN,
seq_sep_trailing_disallowed(token::COMMA),
|p| p.parse_arg_general(false));
|p| p.parse_arg_general(false)
);
let (ret_style, ret_ty) = self.parse_ret_ty();
ast::fn_decl { inputs: inputs, output: ret_ty, cf: ret_style }
}
fn parse_trait_methods() -> ~[trait_method] {
do self.parse_unspanned_seq(token::LBRACE, token::RBRACE,
seq_sep_none()) |p| {
do self.parse_unspanned_seq(
token::LBRACE,
token::RBRACE,
seq_sep_none()
) |p| {
let attrs = p.parse_outer_attributes();
let lo = p.span.lo;
let is_static = p.parse_staticness();
@ -619,9 +624,11 @@ fn parse_ty(colons_before_params: bool) -> @Ty {
ty_ptr(self.parse_mt())
} else if *self.token == token::LBRACE {
let elems = self.parse_unspanned_seq(
token::LBRACE, token::RBRACE,
token::LBRACE,
token::RBRACE,
seq_sep_trailing_allowed(token::COMMA),
|p| p.parse_ty_field());
|p| p.parse_ty_field()
);
if vec::len(elems) == 0u {
self.unexpected_last(token::RBRACE);
}
@ -1175,10 +1182,11 @@ fn parse_bottom_expr() -> @expr {
} else if *self.token == token::COMMA {
// Vector with two or more elements.
self.bump();
let remaining_exprs =
self.parse_seq_to_end(token::RBRACKET,
seq_sep_trailing_allowed(token::COMMA),
|p| p.parse_expr());
let remaining_exprs = self.parse_seq_to_end(
token::RBRACKET,
seq_sep_trailing_allowed(token::COMMA),
|p| p.parse_expr()
);
ex = expr_vec(~[first_expr] + remaining_exprs, mutbl);
} else {
// Vector with one element.
@ -1230,10 +1238,12 @@ fn parse_bottom_expr() -> @expr {
};
let ket = token::flip_delimiter(&*self.token);
let tts = self.parse_unspanned_seq(*self.token,
ket,
seq_sep_none(),
|p| p.parse_token_tree());
let tts = self.parse_unspanned_seq(
*self.token,
ket,
seq_sep_none(),
|p| p.parse_token_tree()
);
let hi = self.span.hi;
return self.mk_mac_expr(lo, hi, mac_invoc_tt(pth, tts));
@ -1310,8 +1320,10 @@ fn parse_dot_or_call_expr_with(e0: @expr) -> @expr {
self.bump();
let tys = if self.eat(&token::MOD_SEP) {
self.expect(&token::LT);
self.parse_seq_to_gt(Some(token::COMMA),
|p| p.parse_ty(false))
self.parse_seq_to_gt(
Some(token::COMMA),
|p| p.parse_ty(false)
)
} else {
~[]
};
@ -1320,9 +1332,11 @@ fn parse_dot_or_call_expr_with(e0: @expr) -> @expr {
match *self.token {
token::LPAREN if self.permits_call() => {
let es = self.parse_unspanned_seq(
token::LPAREN, token::RPAREN,
token::LPAREN,
token::RPAREN,
seq_sep_trailing_disallowed(token::COMMA),
|p| p.parse_expr());
|p| p.parse_expr()
);
hi = self.span.hi;
let nd = expr_method_call(e, i, tys, es, NoSugar);
@ -1342,9 +1356,11 @@ fn parse_dot_or_call_expr_with(e0: @expr) -> @expr {
// expr(...)
token::LPAREN if self.permits_call() => {
let es = self.parse_unspanned_seq(
token::LPAREN, token::RPAREN,
token::LPAREN,
token::RPAREN,
seq_sep_trailing_disallowed(token::COMMA),
|p| p.parse_expr());
|p| p.parse_expr()
);
hi = self.span.hi;
let nd = expr_call(e, es, NoSugar);
@ -1373,7 +1389,7 @@ fn parse_sep_and_zerok() -> (Option<token::Token>, bool) {
|| *self.token == token::BINOP(token::PLUS) {
let zerok = *self.token == token::BINOP(token::STAR);
self.bump();
return (None, zerok);
(None, zerok)
} else {
let sep = *self.token;
self.bump();
@ -1381,7 +1397,7 @@ fn parse_sep_and_zerok() -> (Option<token::Token>, bool) {
|| *self.token == token::BINOP(token::PLUS) {
let zerok = *self.token == token::BINOP(token::STAR);
self.bump();
return (Some(sep), zerok);
(Some(sep), zerok)
} else {
self.fatal(~"expected `*` or `+`");
}
@ -1397,8 +1413,12 @@ fn parse_non_delim_tt_tok(p: Parser) -> token_tree {
match *p.token {
token::RPAREN | token::RBRACE | token::RBRACKET
=> {
p.fatal(~"incorrect close delimiter: `"
+ token_to_str(p.reader, *p.token) + ~"`");
p.fatal(
fmt!(
"incorrect close delimiter: `%s`",
token_to_str(p.reader, *p.token)
)
);
}
/* we ought to allow different depths of unquotation */
token::DOLLAR if *p.quote_depth > 0u => {
@ -1406,9 +1426,12 @@ fn parse_non_delim_tt_tok(p: Parser) -> token_tree {
let sp = *p.span;
if *p.token == token::LPAREN {
let seq = p.parse_seq(token::LPAREN, token::RPAREN,
seq_sep_none(),
|p| p.parse_token_tree());
let seq = p.parse_seq(
token::LPAREN,
token::RPAREN,
seq_sep_none(),
|p| p.parse_token_tree()
);
let (s, z) = p.parse_sep_and_zerok();
tt_seq(mk_sp(sp.lo ,p.span.hi), seq.node, s, z)
} else {
@ -1429,23 +1452,29 @@ fn parse_any_tt_tok(p: Parser) -> token_tree{
}
match *self.token {
token::EOF => {
token::EOF => {
self.fatal(~"file ended in the middle of a macro invocation");
}
token::LPAREN | token::LBRACE | token::LBRACKET => {
// tjc: ??????
let ket = token::flip_delimiter(&*self.token);
tt_delim(vec::append(
// the open delimiter:
~[parse_any_tt_tok(self)],
vec::append(
self.parse_seq_to_before_end(
ket, seq_sep_none(),
|p| p.parse_token_tree()),
// the close delimiter:
~[parse_any_tt_tok(self)])))
}
_ => parse_non_delim_tt_tok(self)
}
token::LPAREN | token::LBRACE | token::LBRACKET => {
// tjc: ??????
let ket = token::flip_delimiter(&*self.token);
tt_delim(
vec::append(
// the open delimiter:
~[parse_any_tt_tok(self)],
vec::append(
self.parse_seq_to_before_end(
ket,
seq_sep_none(),
|p| p.parse_token_tree()
),
// the close delimiter:
~[parse_any_tt_tok(self)]
)
)
)
}
_ => parse_non_delim_tt_tok(self)
}
}
@ -1462,13 +1491,16 @@ fn parse_matchers() -> ~[matcher] {
// the interpolation of matchers
maybe_whole!(self, nt_matchers);
let name_idx = @mut 0u;
return match *self.token {
token::LBRACE | token::LPAREN | token::LBRACKET => {
self.parse_matcher_subseq(name_idx, *self.token,
// tjc: not sure why we need a copy
token::flip_delimiter(&*self.token))
}
_ => self.fatal(~"expected open delimiter")
match *self.token {
token::LBRACE | token::LPAREN | token::LBRACKET => {
self.parse_matcher_subseq(
name_idx,
*self.token,
// tjc: not sure why we need a copy
token::flip_delimiter(&*self.token)
)
}
_ => self.fatal(~"expected open delimiter")
}
}
@ -1476,8 +1508,11 @@ fn parse_matchers() -> ~[matcher] {
// This goofy function is necessary to correctly match parens in matchers.
// Otherwise, `$( ( )` would be a valid matcher, and `$( () )` would be
// invalid. It's similar to common::parse_seq.
fn parse_matcher_subseq(name_idx: @mut uint, bra: token::Token,
ket: token::Token) -> ~[matcher] {
fn parse_matcher_subseq(
name_idx: @mut uint,
bra: token::Token,
ket: token::Token
) -> ~[matcher] {
let mut ret_val = ~[];
let mut lparens = 0u;
@ -1501,9 +1536,11 @@ fn parse_matcher(name_idx: @mut uint) -> matcher {
self.bump();
if *self.token == token::LPAREN {
let name_idx_lo = *name_idx;
let ms = self.parse_matcher_subseq(name_idx,
token::LPAREN,
token::RPAREN);
let ms = self.parse_matcher_subseq(
name_idx,
token::LPAREN,
token::RPAREN
);
if ms.len() == 0u {
self.fatal(~"repetition body must be nonempty");
}
@ -2276,10 +2313,13 @@ fn parse_pat(refutable: bool) -> @pat {
}
_ => {
args = self.parse_unspanned_seq(
token::LPAREN, token::RPAREN,
seq_sep_trailing_disallowed
(token::COMMA),
|p| p.parse_pat(refutable));
token::LPAREN,
token::RPAREN,
seq_sep_trailing_disallowed(
token::COMMA
),
|p| p.parse_pat(refutable)
);
}
},
_ => ()
@ -2423,8 +2463,11 @@ fn check_expected_item(p: Parser, current_attrs: ~[attribute]) {
};
let tts = self.parse_unspanned_seq(
token::LPAREN, token::RPAREN, seq_sep_none(),
|p| p.parse_token_tree());
token::LPAREN,
token::RPAREN,
seq_sep_none(),
|p| p.parse_token_tree()
);
let hi = self.span.hi;
if id == token::special_idents::invalid {
@ -2720,7 +2763,8 @@ fn parse_ty_params() -> ~[ty_param] {
let _lifetimes = self.parse_lifetimes();
self.parse_seq_to_gt(
Some(token::COMMA),
|p| p.parse_ty_param())
|p| p.parse_ty_param()
)
} else { ~[] }
}
@ -2729,8 +2773,11 @@ fn parse_fn_decl(parse_arg_fn: fn(Parser) -> arg_or_capture_item)
{
let args_or_capture_items: ~[arg_or_capture_item] =
self.parse_unspanned_seq(
token::LPAREN, token::RPAREN,
seq_sep_trailing_disallowed(token::COMMA), parse_arg_fn);
token::LPAREN,
token::RPAREN,
seq_sep_trailing_disallowed(token::COMMA),
parse_arg_fn
);
let inputs = either::lefts(args_or_capture_items);
@ -2810,10 +2857,11 @@ fn maybe_parse_self_ty(cnstr: fn(+v: mutability) -> ast::self_ty_,
token::COMMA => {
self.bump();
let sep = seq_sep_trailing_disallowed(token::COMMA);
args_or_capture_items =
self.parse_seq_to_before_end(token::RPAREN,
sep,
parse_arg_fn);
args_or_capture_items = self.parse_seq_to_before_end(
token::RPAREN,
sep,
parse_arg_fn
);
}
token::RPAREN => {
args_or_capture_items = ~[];
@ -2826,10 +2874,11 @@ fn maybe_parse_self_ty(cnstr: fn(+v: mutability) -> ast::self_ty_,
}
} else {
let sep = seq_sep_trailing_disallowed(token::COMMA);
args_or_capture_items =
self.parse_seq_to_before_end(token::RPAREN,
sep,
parse_arg_fn);
args_or_capture_items = self.parse_seq_to_before_end(
token::RPAREN,
sep,
parse_arg_fn
);
}
self.expect(&token::RPAREN);
@ -2854,9 +2903,11 @@ fn parse_fn_block_decl() -> fn_decl {
~[]
} else {
self.parse_unspanned_seq(
token::BINOP(token::OR), token::BINOP(token::OR),
token::BINOP(token::OR),
token::BINOP(token::OR),
seq_sep_trailing_disallowed(token::COMMA),
|p| p.parse_fn_block_arg())
|p| p.parse_fn_block_arg()
)
}
};
let output = if self.eat(&token::RARROW) {
@ -3054,8 +3105,10 @@ fn parse_trait_ref() -> @trait_ref {
fn parse_trait_ref_list(ket: token::Token) -> ~[@trait_ref] {
self.parse_seq_to_before_end(
ket, seq_sep_none(),
|p| p.parse_trait_ref())
ket,
seq_sep_none(),
|p| p.parse_trait_ref()
)
}
fn parse_item_struct() -> item_info {
@ -3102,9 +3155,11 @@ fn parse_item_struct() -> item_info {
} else if *self.token == token::LPAREN {
// It's a tuple-like struct.
is_tuple_like = true;
fields = do self.parse_unspanned_seq(token::LPAREN, token::RPAREN,
seq_sep_trailing_allowed
(token::COMMA)) |p| {
fields = do self.parse_unspanned_seq(
token::LPAREN,
token::RPAREN,
seq_sep_trailing_allowed(token::COMMA)
) |p| {
let lo = p.span.lo;
let struct_field_ = ast::struct_field_ {
kind: unnamed_field,
@ -3667,9 +3722,11 @@ fn parse_enum_def(ty_params: ~[ast::ty_param])
} else if *self.token == token::LPAREN {
all_nullary = false;
let arg_tys = self.parse_unspanned_seq(
token::LPAREN, token::RPAREN,
token::LPAREN,
token::RPAREN,
seq_sep_trailing_disallowed(token::COMMA),
|p| p.parse_ty(false));
|p| p.parse_ty(false)
);
for arg_tys.each |ty| {
args.push(ast::variant_arg {
ty: *ty,
@ -3915,13 +3972,16 @@ fn parse_item_or_view_item(+attrs: ~[attribute], items_allowed: bool,
};
// eat a matched-delimiter token tree:
let tts = match *self.token {
token::LPAREN | token::LBRACE => {
let ket = token::flip_delimiter(&*self.token);
self.parse_unspanned_seq(*self.token, ket,
seq_sep_none(),
|p| p.parse_token_tree())
}
_ => self.fatal(~"expected open delimiter")
token::LPAREN | token::LBRACE => {
let ket = token::flip_delimiter(&*self.token);
self.parse_unspanned_seq(
*self.token,
ket,
seq_sep_none(),
|p| p.parse_token_tree()
)
}
_ => self.fatal(~"expected open delimiter")
};
// single-variant-enum... :
let m = ast::mac_invoc_tt(pth, tts);
@ -4007,9 +4067,11 @@ fn parse_view_path() -> @view_path {
// foo::bar::{a,b,c}
token::LBRACE => {
let idents = self.parse_unspanned_seq(
token::LBRACE, token::RBRACE,
token::LBRACE,
token::RBRACE,
seq_sep_trailing_allowed(token::COMMA),
|p| p.parse_path_list_ident());
|p| p.parse_path_list_ident()
);
let path = @ast::path { span: mk_sp(lo, self.span.hi),
global: false,
idents: path,