diff options
| author | Crazycolorz5 <Crazycolorz5@gmail.com> | 2018-05-25 17:09:32 -0400 |
|---|---|---|
| committer | Crazycolorz5 <Crazycolorz5@gmail.com> | 2018-06-04 22:25:00 -0400 |
| commit | 7a9ffa730740c18d23918add84c40bdf77f17dc0 (patch) | |
| tree | d70d24f98b998bfa42861fd08de062b48db0881c /src/libsyntax/parse | |
| parent | 682033c4e47cf0cca5b4b96b07123da8807254f6 (diff) | |
| download | rust-7a9ffa730740c18d23918add84c40bdf77f17dc0.tar.gz rust-7a9ffa730740c18d23918add84c40bdf77f17dc0.zip | |
Added is_like_plus to token, and used that in place of equality comparison to Plus token.
Diffstat (limited to 'src/libsyntax/parse')
| -rw-r--r-- | src/libsyntax/parse/parser.rs | 31 | ||||
| -rw-r--r-- | src/libsyntax/parse/token.rs | 7 |
2 files changed, 29 insertions, 9 deletions
diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index a4e2b96c5f9..a398962f66a 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -901,6 +901,19 @@ impl<'a> Parser<'a> { _ => false, } } + + + /// Checks to see if the next token is either `+` or `+=`. + /// Otherwise returns false. + fn check_plus(&mut self) -> bool { + if self.token.is_like_plus() { + true + } + else { + self.expected_tokens.push(TokenType::Token(token::BinOp(token::Plus))); + false + } + } /// Expect and consume an `&`. If `&&` is seen, replace it with a single /// `&` and continue. If an `&` is not seen, signal an error. @@ -1533,7 +1546,7 @@ impl<'a> Parser<'a> { if ts.len() == 1 && !last_comma { let ty = ts.into_iter().nth(0).unwrap().into_inner(); - let maybe_bounds = allow_plus && self.token == token::BinOp(token::Plus); + let maybe_bounds = allow_plus && self.check_plus(); match ty.node { // `(TY_BOUND_NOPAREN) + BOUND + ...`. TyKind::Path(None, ref path) if maybe_bounds => { @@ -1602,7 +1615,7 @@ impl<'a> Parser<'a> { self.parse_ty_bare_fn(lifetime_defs)? } else { let path = self.parse_path(PathStyle::Type)?; - let parse_plus = allow_plus && self.check(&token::BinOp(token::Plus)); + let parse_plus = allow_plus && self.check_plus(); self.parse_remaining_bounds(lifetime_defs, path, lo, parse_plus)? } } else if self.eat_keyword(keywords::Impl) { @@ -1619,7 +1632,7 @@ impl<'a> Parser<'a> { impl_dyn_multi = bounds.len() > 1 || self.prev_token_kind == PrevTokenKind::Plus; TyKind::TraitObject(bounds, TraitObjectSyntax::Dyn) } else if self.check(&token::Question) || - self.check_lifetime() && self.look_ahead(1, |t| t == &token::BinOp(token::Plus)) { + self.check_lifetime() && self.look_ahead(1, |t| t.is_like_plus()) { // Bound list (trait object type) TyKind::TraitObject(self.parse_ty_param_bounds_common(allow_plus)?, TraitObjectSyntax::None) @@ -1639,7 +1652,7 @@ impl<'a> Parser<'a> { // Just a type path or bound list (trait object type) starting with a trait. // `Type` // `Trait1 + Trait2 + 'a` - if allow_plus && self.check(&token::BinOp(token::Plus)) { + if allow_plus && self.check_plus() { self.parse_remaining_bounds(Vec::new(), path, lo, true)? } else { TyKind::Path(None, path) @@ -1666,7 +1679,7 @@ impl<'a> Parser<'a> { let poly_trait_ref = PolyTraitRef::new(generic_params, path, lo.to(self.prev_span)); let mut bounds = vec![TraitTyParamBound(poly_trait_ref, TraitBoundModifier::None)]; if parse_plus { - self.bump(); // `+` + self.eat_plus(); // `+` or `+=` gets split and `+` is discarded bounds.append(&mut self.parse_ty_param_bounds()?); } Ok(TyKind::TraitObject(bounds, TraitObjectSyntax::None)) @@ -1687,7 +1700,7 @@ impl<'a> Parser<'a> { fn maybe_recover_from_bad_type_plus(&mut self, allow_plus: bool, ty: &Ty) -> PResult<'a, ()> { // Do not add `+` to expected tokens. - if !allow_plus || self.token != token::BinOp(token::Plus) { + if !allow_plus || !self.token.is_like_plus() { return Ok(()) } @@ -4841,7 +4854,7 @@ impl<'a> Parser<'a> { while self.check_lifetime() { lifetimes.push(self.expect_lifetime()); - if !self.eat(&token::BinOp(token::Plus)) { + if !self.eat_plus() { break } } @@ -4987,7 +5000,7 @@ impl<'a> Parser<'a> { let mut seen_type = false; let mut seen_binding = false; loop { - if self.check_lifetime() && self.look_ahead(1, |t| t != &token::BinOp(token::Plus)) { + if self.check_lifetime() && self.look_ahead(1, |t| !t.is_like_plus()) { // Parse lifetime argument. lifetimes.push(self.expect_lifetime()); if seen_type || seen_binding { @@ -5056,7 +5069,7 @@ impl<'a> Parser<'a> { loop { let lo = self.span; - if self.check_lifetime() && self.look_ahead(1, |t| t != &token::BinOp(token::Plus)) { + if self.check_lifetime() && self.look_ahead(1, |t| !t.is_like_plus()) { let lifetime = self.expect_lifetime(); // Bounds starting with a colon are mandatory, but possibly empty. self.expect(&token::Colon)?; diff --git a/src/libsyntax/parse/token.rs b/src/libsyntax/parse/token.rs index 9770fbca8f8..18665b99e21 100644 --- a/src/libsyntax/parse/token.rs +++ b/src/libsyntax/parse/token.rs @@ -224,6 +224,13 @@ impl Token { _ => false, } } + + pub fn is_like_plus(&self) -> bool { + match *self { + BinOp(Plus) | BinOpEq(Plus) => true, + _ => false, + } + } /// Returns `true` if the token can appear at the start of an expression. pub fn can_begin_expr(&self) -> bool { |
