1
Fork 0

syntax: Rename parse_ty -> parse_ty_no_plus, parse_ty_sum -> parse_ty

This commit is contained in:
Vadim Petrochenkov 2017-01-17 02:13:41 +03:00
parent 03620dba25
commit 869b81646d
4 changed files with 44 additions and 40 deletions

View file

@ -540,7 +540,7 @@ impl<'a> Parser<'a> {
} }
ExpansionKind::Expr => Expansion::Expr(self.parse_expr()?), ExpansionKind::Expr => Expansion::Expr(self.parse_expr()?),
ExpansionKind::OptExpr => Expansion::OptExpr(Some(self.parse_expr()?)), ExpansionKind::OptExpr => Expansion::OptExpr(Some(self.parse_expr()?)),
ExpansionKind::Ty => Expansion::Ty(self.parse_ty()?), ExpansionKind::Ty => Expansion::Ty(self.parse_ty_no_plus()?),
ExpansionKind::Pat => Expansion::Pat(self.parse_pat()?), ExpansionKind::Pat => Expansion::Pat(self.parse_pat()?),
}) })
} }

View file

@ -388,7 +388,7 @@ pub fn parse_arm_panic(parser: &mut Parser) -> Arm {
} }
pub fn parse_ty_panic(parser: &mut Parser) -> P<Ty> { pub fn parse_ty_panic(parser: &mut Parser) -> P<Ty> {
panictry!(parser.parse_ty()) panictry!(parser.parse_ty_no_plus())
} }
pub fn parse_stmt_panic(parser: &mut Parser) -> Option<Stmt> { pub fn parse_stmt_panic(parser: &mut Parser) -> Option<Stmt> {

View file

@ -522,7 +522,7 @@ fn parse_nt<'a>(p: &mut Parser<'a>, sp: Span, name: &str) -> Nonterminal {
}, },
"pat" => token::NtPat(panictry!(p.parse_pat())), "pat" => token::NtPat(panictry!(p.parse_pat())),
"expr" => token::NtExpr(panictry!(p.parse_expr())), "expr" => token::NtExpr(panictry!(p.parse_expr())),
"ty" => token::NtTy(panictry!(p.parse_ty())), "ty" => token::NtTy(panictry!(p.parse_ty_no_plus())),
// this could be handled like a token, since it is one // this could be handled like a token, since it is one
"ident" => match p.token { "ident" => match p.token {
token::Ident(sn) => { token::Ident(sn) => {

View file

@ -1128,7 +1128,7 @@ impl<'a> Parser<'a> {
self.expect_keyword(keywords::Const)?; self.expect_keyword(keywords::Const)?;
let ident = self.parse_ident()?; let ident = self.parse_ident()?;
self.expect(&token::Colon)?; self.expect(&token::Colon)?;
let ty = self.parse_ty_sum()?; let ty = self.parse_ty()?;
let default = if self.check(&token::Eq) { let default = if self.check(&token::Eq) {
self.bump(); self.bump();
let expr = self.parse_expr()?; let expr = self.parse_expr()?;
@ -1244,24 +1244,24 @@ impl<'a> Parser<'a> {
/// Parse a possibly mutable type /// Parse a possibly mutable type
pub fn parse_mt(&mut self) -> PResult<'a, MutTy> { pub fn parse_mt(&mut self) -> PResult<'a, MutTy> {
let mutbl = self.parse_mutability()?; let mutbl = self.parse_mutability()?;
let t = self.parse_ty()?; let t = self.parse_ty_no_plus()?;
Ok(MutTy { ty: t, mutbl: mutbl }) Ok(MutTy { ty: t, mutbl: mutbl })
} }
/// Parse optional return type [ -> TY ] in function decl /// Parse optional return type [ -> TY ] in function decl
pub fn parse_ret_ty(&mut self) -> PResult<'a, FunctionRetTy> { pub fn parse_ret_ty(&mut self) -> PResult<'a, FunctionRetTy> {
if self.eat(&token::RArrow) { if self.eat(&token::RArrow) {
Ok(FunctionRetTy::Ty(self.parse_ty()?)) Ok(FunctionRetTy::Ty(self.parse_ty_no_plus()?))
} else { } else {
let pos = self.span.lo; let pos = self.span.lo;
Ok(FunctionRetTy::Default(mk_sp(pos, pos))) Ok(FunctionRetTy::Default(mk_sp(pos, pos)))
} }
} }
/// Parse a type in a context where `T1+T2` is allowed. /// Parse a type.
pub fn parse_ty_sum(&mut self) -> PResult<'a, P<Ty>> { pub fn parse_ty(&mut self) -> PResult<'a, P<Ty>> {
let lo = self.span.lo; let lo = self.span.lo;
let lhs = self.parse_ty()?; let lhs = self.parse_ty_no_plus()?;
if !self.eat(&token::BinOp(token::Plus)) { if !self.eat(&token::BinOp(token::Plus)) {
return Ok(lhs); return Ok(lhs);
@ -1331,8 +1331,12 @@ impl<'a> Parser<'a> {
Ok(P(Ty {id: ast::DUMMY_NODE_ID, node: sum, span: sp})) Ok(P(Ty {id: ast::DUMMY_NODE_ID, node: sum, span: sp}))
} }
/// Parse a type. /// Parse a type in restricted contexts where `+` is not permitted.
pub fn parse_ty(&mut self) -> PResult<'a, P<Ty>> { /// Example 1: `&'a TYPE`
/// `+` is prohibited to maintain operator priority (P(+) < P(&)).
/// Example 2: `value1 as TYPE + value2`
/// `+` is prohibited to avoid interactions with expression grammar.
pub fn parse_ty_no_plus(&mut self) -> PResult<'a, P<Ty>> {
maybe_whole!(self, NtTy, |x| x); maybe_whole!(self, NtTy, |x| x);
let lo = self.span.lo; let lo = self.span.lo;
@ -1346,7 +1350,7 @@ impl<'a> Parser<'a> {
let mut ts = vec![]; let mut ts = vec![];
let mut last_comma = false; let mut last_comma = false;
while self.token != token::CloseDelim(token::Paren) { while self.token != token::CloseDelim(token::Paren) {
ts.push(self.parse_ty_sum()?); ts.push(self.parse_ty()?);
if self.check(&token::Comma) { if self.check(&token::Comma) {
last_comma = true; last_comma = true;
self.bump(); self.bump();
@ -1371,7 +1375,7 @@ impl<'a> Parser<'a> {
} else if self.check(&token::OpenDelim(token::Bracket)) { } else if self.check(&token::OpenDelim(token::Bracket)) {
// VECTOR // VECTOR
self.expect(&token::OpenDelim(token::Bracket))?; self.expect(&token::OpenDelim(token::Bracket))?;
let t = self.parse_ty_sum()?; let t = self.parse_ty()?;
// Parse the `; e` in `[ i32; e ]` // Parse the `; e` in `[ i32; e ]`
// where `e` is a const expression // where `e` is a const expression
@ -1452,7 +1456,7 @@ impl<'a> Parser<'a> {
`*mut T` or `*const T` as appropriate)"); `*mut T` or `*const T` as appropriate)");
Mutability::Immutable Mutability::Immutable
}; };
let t = self.parse_ty()?; let t = self.parse_ty_no_plus()?;
Ok(MutTy { ty: t, mutbl: mutbl }) Ok(MutTy { ty: t, mutbl: mutbl })
} }
@ -1499,7 +1503,7 @@ impl<'a> Parser<'a> {
}) })
}; };
let t = self.parse_ty_sum()?; let t = self.parse_ty()?;
Ok(Arg { Ok(Arg {
ty: t, ty: t,
@ -1517,7 +1521,7 @@ impl<'a> Parser<'a> {
pub fn parse_fn_block_arg(&mut self) -> PResult<'a, Arg> { pub fn parse_fn_block_arg(&mut self) -> PResult<'a, Arg> {
let pat = self.parse_pat()?; let pat = self.parse_pat()?;
let t = if self.eat(&token::Colon) { let t = if self.eat(&token::Colon) {
self.parse_ty_sum()? self.parse_ty()?
} else { } else {
P(Ty { P(Ty {
id: ast::DUMMY_NODE_ID, id: ast::DUMMY_NODE_ID,
@ -1658,7 +1662,7 @@ impl<'a> Parser<'a> {
pub fn parse_qualified_path(&mut self, mode: PathStyle) pub fn parse_qualified_path(&mut self, mode: PathStyle)
-> PResult<'a, (QSelf, ast::Path)> { -> PResult<'a, (QSelf, ast::Path)> {
let span = self.prev_span; let span = self.prev_span;
let self_type = self.parse_ty_sum()?; let self_type = self.parse_ty()?;
let mut path = if self.eat_keyword(keywords::As) { let mut path = if self.eat_keyword(keywords::As) {
self.parse_path(PathStyle::Type)? self.parse_path(PathStyle::Type)?
} else { } else {
@ -1768,10 +1772,10 @@ impl<'a> Parser<'a> {
let inputs = self.parse_seq_to_end( let inputs = self.parse_seq_to_end(
&token::CloseDelim(token::Paren), &token::CloseDelim(token::Paren),
SeqSep::trailing_allowed(token::Comma), SeqSep::trailing_allowed(token::Comma),
|p| p.parse_ty_sum())?; |p| p.parse_ty())?;
let output_ty = if self.eat(&token::RArrow) { let output_ty = if self.eat(&token::RArrow) {
Some(self.parse_ty()?) Some(self.parse_ty_no_plus()?)
} else { } else {
None None
}; };
@ -2981,12 +2985,12 @@ impl<'a> Parser<'a> {
} }
// Special cases: // Special cases:
if op == AssocOp::As { if op == AssocOp::As {
let rhs = self.parse_ty()?; let rhs = self.parse_ty_no_plus()?;
let (lo, hi) = (lhs_span.lo, rhs.span.hi); let (lo, hi) = (lhs_span.lo, rhs.span.hi);
lhs = self.mk_expr(lo, hi, ExprKind::Cast(lhs, rhs), ThinVec::new()); lhs = self.mk_expr(lo, hi, ExprKind::Cast(lhs, rhs), ThinVec::new());
continue continue
} else if op == AssocOp::Colon { } else if op == AssocOp::Colon {
let rhs = self.parse_ty()?; let rhs = self.parse_ty_no_plus()?;
let (lo, hi) = (lhs_span.lo, rhs.span.hi); let (lo, hi) = (lhs_span.lo, rhs.span.hi);
lhs = self.mk_expr(lo, hi, ExprKind::Type(lhs, rhs), ThinVec::new()); lhs = self.mk_expr(lo, hi, ExprKind::Type(lhs, rhs), ThinVec::new());
continue continue
@ -3754,7 +3758,7 @@ impl<'a> Parser<'a> {
let mut ty = None; let mut ty = None;
if self.eat(&token::Colon) { if self.eat(&token::Colon) {
ty = Some(self.parse_ty_sum()?); ty = Some(self.parse_ty()?);
} }
let init = self.parse_initializer()?; let init = self.parse_initializer()?;
Ok(P(ast::Local { Ok(P(ast::Local {
@ -3775,7 +3779,7 @@ impl<'a> Parser<'a> {
-> PResult<'a, StructField> { -> PResult<'a, StructField> {
let name = self.parse_ident()?; let name = self.parse_ident()?;
self.expect(&token::Colon)?; self.expect(&token::Colon)?;
let ty = self.parse_ty_sum()?; let ty = self.parse_ty()?;
Ok(StructField { Ok(StructField {
span: mk_sp(lo, self.prev_span.hi), span: mk_sp(lo, self.prev_span.hi),
ident: Some(name), ident: Some(name),
@ -4250,7 +4254,7 @@ impl<'a> Parser<'a> {
let default = if self.check(&token::Eq) { let default = if self.check(&token::Eq) {
self.bump(); self.bump();
Some(self.parse_ty_sum()?) Some(self.parse_ty()?)
} else { } else {
None None
}; };
@ -4345,7 +4349,7 @@ impl<'a> Parser<'a> {
let mut err = self.diagnostic().struct_span_err(self.span, &msg); let mut err = self.diagnostic().struct_span_err(self.span, &msg);
let span_hi = self.span.hi; let span_hi = self.span.hi;
let span_hi = match self.parse_ty() { let span_hi = match self.parse_ty_no_plus() {
Ok(..) => self.span.hi, Ok(..) => self.span.hi,
Err(ref mut err) => { Err(ref mut err) => {
self.cancel(err); self.cancel(err);
@ -4368,7 +4372,7 @@ impl<'a> Parser<'a> {
if p.look_ahead(1, |t| t == &token::Eq) { if p.look_ahead(1, |t| t == &token::Eq) {
Ok(None) Ok(None)
} else { } else {
Ok(Some(p.parse_ty_sum()?)) Ok(Some(p.parse_ty()?))
} }
} }
)?; )?;
@ -4386,7 +4390,7 @@ impl<'a> Parser<'a> {
let lo = p.span.lo; let lo = p.span.lo;
let ident = p.parse_ident()?; let ident = p.parse_ident()?;
p.expect(&token::Eq)?; p.expect(&token::Eq)?;
let ty = p.parse_ty()?; let ty = p.parse_ty_no_plus()?;
let hi = ty.span.hi; let hi = ty.span.hi;
let span = mk_sp(lo, hi); let span = mk_sp(lo, hi);
return Ok(TypeBinding{id: ast::DUMMY_NODE_ID, return Ok(TypeBinding{id: ast::DUMMY_NODE_ID,
@ -4484,7 +4488,7 @@ impl<'a> Parser<'a> {
vec![] vec![]
}; };
let bounded_ty = self.parse_ty()?; let bounded_ty = self.parse_ty_no_plus()?;
if self.eat(&token::Colon) { if self.eat(&token::Colon) {
let bounds = self.parse_ty_param_bounds()?; let bounds = self.parse_ty_param_bounds()?;
@ -4507,7 +4511,7 @@ impl<'a> Parser<'a> {
parsed_something = true; parsed_something = true;
} else if self.eat(&token::Eq) { } else if self.eat(&token::Eq) {
// let ty = try!(self.parse_ty()); // let ty = try!(self.parse_ty_no_plus());
let hi = self.prev_span.hi; let hi = self.prev_span.hi;
let span = mk_sp(lo, hi); let span = mk_sp(lo, hi);
// where_clause.predicates.push( // where_clause.predicates.push(
@ -4679,7 +4683,7 @@ impl<'a> Parser<'a> {
// self: TYPE // self: TYPE
let eself_ident = expect_ident(self); let eself_ident = expect_ident(self);
if self.eat(&token::Colon) { if self.eat(&token::Colon) {
let ty = self.parse_ty_sum()?; let ty = self.parse_ty()?;
(SelfKind::Explicit(ty, Mutability::Immutable), eself_ident) (SelfKind::Explicit(ty, Mutability::Immutable), eself_ident)
} else { } else {
(SelfKind::Value(Mutability::Immutable), eself_ident) (SelfKind::Value(Mutability::Immutable), eself_ident)
@ -4691,7 +4695,7 @@ impl<'a> Parser<'a> {
self.bump(); self.bump();
let eself_ident = expect_ident(self); let eself_ident = expect_ident(self);
if self.eat(&token::Colon) { if self.eat(&token::Colon) {
let ty = self.parse_ty_sum()?; let ty = self.parse_ty()?;
(SelfKind::Explicit(ty, Mutability::Mutable), eself_ident) (SelfKind::Explicit(ty, Mutability::Mutable), eself_ident)
} else { } else {
(SelfKind::Value(Mutability::Mutable), eself_ident) (SelfKind::Value(Mutability::Mutable), eself_ident)
@ -4848,14 +4852,14 @@ impl<'a> Parser<'a> {
let (name, node) = if self.eat_keyword(keywords::Type) { let (name, node) = if self.eat_keyword(keywords::Type) {
let name = self.parse_ident()?; let name = self.parse_ident()?;
self.expect(&token::Eq)?; self.expect(&token::Eq)?;
let typ = self.parse_ty_sum()?; let typ = self.parse_ty()?;
self.expect(&token::Semi)?; self.expect(&token::Semi)?;
(name, ast::ImplItemKind::Type(typ)) (name, ast::ImplItemKind::Type(typ))
} else if self.is_const_item() { } else if self.is_const_item() {
self.expect_keyword(keywords::Const)?; self.expect_keyword(keywords::Const)?;
let name = self.parse_ident()?; let name = self.parse_ident()?;
self.expect(&token::Colon)?; self.expect(&token::Colon)?;
let typ = self.parse_ty_sum()?; let typ = self.parse_ty()?;
self.expect(&token::Eq)?; self.expect(&token::Eq)?;
let expr = self.parse_expr()?; let expr = self.parse_expr()?;
self.expect(&token::Semi)?; self.expect(&token::Semi)?;
@ -4979,7 +4983,7 @@ impl<'a> Parser<'a> {
}; };
// Parse the trait. // Parse the trait.
let mut ty = self.parse_ty_sum()?; let mut ty = self.parse_ty()?;
// Parse traits, if necessary. // Parse traits, if necessary.
let opt_trait = if could_be_trait && self.eat_keyword(keywords::For) { let opt_trait = if could_be_trait && self.eat_keyword(keywords::For) {
@ -5020,7 +5024,7 @@ impl<'a> Parser<'a> {
ItemKind::DefaultImpl(unsafety, opt_trait.unwrap()), None)) ItemKind::DefaultImpl(unsafety, opt_trait.unwrap()), None))
} else { } else {
if opt_trait.is_some() { if opt_trait.is_some() {
ty = self.parse_ty_sum()?; ty = self.parse_ty()?;
} }
generics.where_clause = self.parse_where_clause()?; generics.where_clause = self.parse_where_clause()?;
@ -5172,7 +5176,7 @@ impl<'a> Parser<'a> {
let mut vis = p.parse_visibility(false)?; let mut vis = p.parse_visibility(false)?;
let ty_is_interpolated = let ty_is_interpolated =
p.token.is_interpolated() || p.look_ahead(1, |t| t.is_interpolated()); p.token.is_interpolated() || p.look_ahead(1, |t| t.is_interpolated());
let mut ty = p.parse_ty_sum()?; let mut ty = p.parse_ty()?;
// Handle `pub(path) type`, in which `vis` will be `pub` and `ty` will be `(path)`. // Handle `pub(path) type`, in which `vis` will be `pub` and `ty` will be `(path)`.
if vis == Visibility::Public && !ty_is_interpolated && if vis == Visibility::Public && !ty_is_interpolated &&
@ -5180,7 +5184,7 @@ impl<'a> Parser<'a> {
ty = if let TyKind::Paren(ref path_ty) = ty.node { ty = if let TyKind::Paren(ref path_ty) = ty.node {
if let TyKind::Path(None, ref path) = path_ty.node { if let TyKind::Path(None, ref path) = path_ty.node {
vis = Visibility::Restricted { path: P(path.clone()), id: path_ty.id }; vis = Visibility::Restricted { path: P(path.clone()), id: path_ty.id };
Some(p.parse_ty_sum()?) Some(p.parse_ty()?)
} else { } else {
None None
} }
@ -5298,7 +5302,7 @@ impl<'a> Parser<'a> {
fn parse_item_const(&mut self, m: Option<Mutability>) -> PResult<'a, ItemInfo> { fn parse_item_const(&mut self, m: Option<Mutability>) -> PResult<'a, ItemInfo> {
let id = self.parse_ident()?; let id = self.parse_ident()?;
self.expect(&token::Colon)?; self.expect(&token::Colon)?;
let ty = self.parse_ty_sum()?; let ty = self.parse_ty()?;
self.expect(&token::Eq)?; self.expect(&token::Eq)?;
let e = self.parse_expr()?; let e = self.parse_expr()?;
self.expect(&token::Semi)?; self.expect(&token::Semi)?;
@ -5539,7 +5543,7 @@ impl<'a> Parser<'a> {
let ident = self.parse_ident()?; let ident = self.parse_ident()?;
self.expect(&token::Colon)?; self.expect(&token::Colon)?;
let ty = self.parse_ty_sum()?; let ty = self.parse_ty()?;
let hi = self.span.hi; let hi = self.span.hi;
self.expect(&token::Semi)?; self.expect(&token::Semi)?;
Ok(ForeignItem { Ok(ForeignItem {
@ -5628,7 +5632,7 @@ impl<'a> Parser<'a> {
let mut tps = self.parse_generics()?; let mut tps = self.parse_generics()?;
tps.where_clause = self.parse_where_clause()?; tps.where_clause = self.parse_where_clause()?;
self.expect(&token::Eq)?; self.expect(&token::Eq)?;
let ty = self.parse_ty_sum()?; let ty = self.parse_ty()?;
self.expect(&token::Semi)?; self.expect(&token::Semi)?;
Ok((ident, ItemKind::Ty(ty, tps), None)) Ok((ident, ItemKind::Ty(ty, tps), None))
} }