Skip to content

Commit

Permalink
Auto merge of #67112 - Centril:expr-polish, r=estebank
Browse files Browse the repository at this point in the history
Refactor expression parsing thoroughly

Based on #66994 together with which this has refactored basically the entirety of `expr.rs`.

r? @estebank
  • Loading branch information
bors committed Dec 29, 2019
2 parents 25434f8 + 7a246ac commit da3629b
Show file tree
Hide file tree
Showing 31 changed files with 585 additions and 537 deletions.
5 changes: 3 additions & 2 deletions src/librustc_parse/parser/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use super::{Parser, PathStyle, TokenType};
use rustc_errors::PResult;
use syntax::ast;
use syntax::attr;
use syntax::print::pprust;
use syntax::token::{self, Nonterminal};
use syntax::util::comments;
use syntax_pos::{Span, Symbol};
Expand Down Expand Up @@ -154,7 +155,7 @@ impl<'a> Parser<'a> {
(attr_sp, item, style)
}
_ => {
let token_str = self.this_token_to_string();
let token_str = pprust::token_to_string(&self.token);
return Err(self.fatal(&format!("expected `#`, found `{}`", token_str)));
}
};
Expand Down Expand Up @@ -329,7 +330,7 @@ impl<'a> Parser<'a> {
Err(ref mut err) => err.cancel(),
}

let found = self.this_token_to_string();
let found = pprust::token_to_string(&self.token);
let msg = format!("expected unsuffixed literal or identifier, found `{}`", found);
Err(self.diagnostic().struct_span_err(self.token.span, &msg))
}
Expand Down
14 changes: 7 additions & 7 deletions src/librustc_parse/parser/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ impl<'a> Parser<'a> {
pub(super) fn expected_ident_found(&self) -> DiagnosticBuilder<'a> {
let mut err = self.struct_span_err(
self.token.span,
&format!("expected identifier, found {}", self.this_token_descr()),
&format!("expected identifier, found {}", super::token_descr(&self.token)),
);
let valid_follow = &[
TokenKind::Eq,
Expand All @@ -225,7 +225,7 @@ impl<'a> Parser<'a> {
);
}
}
if let Some(token_descr) = self.token_descr() {
if let Some(token_descr) = super::token_descr_opt(&self.token) {
err.span_label(self.token.span, format!("expected identifier, found {}", token_descr));
} else {
err.span_label(self.token.span, "expected identifier");
Expand Down Expand Up @@ -272,7 +272,7 @@ impl<'a> Parser<'a> {
expected.sort_by_cached_key(|x| x.to_string());
expected.dedup();
let expect = tokens_to_string(&expected[..]);
let actual = self.this_token_descr();
let actual = super::token_descr(&self.token);
let (msg_exp, (label_sp, label_exp)) = if expected.len() > 1 {
let short_expect = if expected.len() > 6 {
format!("{} possible tokens", expected.len())
Expand Down Expand Up @@ -815,7 +815,7 @@ impl<'a> Parser<'a> {
t: &TokenKind,
) -> PResult<'a, bool /* recovered */> {
let token_str = pprust::token_kind_to_string(t);
let this_token_str = self.this_token_descr();
let this_token_str = super::token_descr(&self.token);
let (prev_sp, sp) = match (&self.token.kind, self.subparser_name) {
// Point at the end of the macro call when reaching end of macro arguments.
(token::Eof, Some(_)) => {
Expand Down Expand Up @@ -862,7 +862,7 @@ impl<'a> Parser<'a> {
return Ok(());
}
let sm = self.sess.source_map();
let msg = format!("expected `;`, found `{}`", self.this_token_descr());
let msg = format!("expected `;`, found `{}`", super::token_descr(&self.token));
let appl = Applicability::MachineApplicable;
if self.token.span == DUMMY_SP || self.prev_span == DUMMY_SP {
// Likely inside a macro, can't provide meaninful suggestions.
Expand Down Expand Up @@ -1270,7 +1270,7 @@ impl<'a> Parser<'a> {
}

pub(super) fn expected_semi_or_open_brace<T>(&mut self) -> PResult<'a, T> {
let token_str = self.this_token_descr();
let token_str = super::token_descr(&self.token);
let mut err = self.fatal(&format!("expected `;` or `{{`, found {}", token_str));
err.span_label(self.token.span, "expected `;` or `{`");
Err(err)
Expand Down Expand Up @@ -1447,7 +1447,7 @@ impl<'a> Parser<'a> {
}
_ => (
self.token.span,
format!("expected expression, found {}", self.this_token_descr(),),
format!("expected expression, found {}", super::token_descr(&self.token),),
),
};
let mut err = self.struct_span_err(span, &msg);
Expand Down
Loading

0 comments on commit da3629b

Please sign in to comment.