Skip to content

Commit b85a3da

Browse files
committed
parse: Support parsing optional literals
Revert weird renaming of the former `LitError::report`
1 parent 00bc449 commit b85a3da

File tree

3 files changed

+27
-21
lines changed

3 files changed

+27
-21
lines changed

src/librustc_parse/parser/expr.rs

+19-12
Original file line numberDiff line numberDiff line change
@@ -778,13 +778,12 @@ impl<'a> Parser<'a> {
778778

779779
macro_rules! parse_lit {
780780
() => {
781-
match self.parse_lit() {
782-
Ok(literal) => {
781+
match self.parse_opt_lit() {
782+
Some(literal) => {
783783
hi = self.prev_span;
784784
ex = ExprKind::Lit(literal);
785785
}
786-
Err(mut err) => {
787-
err.cancel();
786+
None => {
788787
return Err(self.expected_expression_found());
789788
}
790789
}
@@ -1074,11 +1073,20 @@ impl<'a> Parser<'a> {
10741073
self.maybe_recover_from_bad_qpath(expr, true)
10751074
}
10761075

1077-
/// Matches `lit = true | false | token_lit`.
10781076
pub(super) fn parse_lit(&mut self) -> PResult<'a, Lit> {
1077+
self.parse_opt_lit().ok_or_else(|| {
1078+
let msg = format!("unexpected token: {}", self.this_token_descr());
1079+
self.span_fatal(self.token.span, &msg)
1080+
})
1081+
}
1082+
1083+
/// Matches `lit = true | false | token_lit`.
1084+
/// Returns `None` if the next token is not a literal.
1085+
pub(super) fn parse_opt_lit(&mut self) -> Option<Lit> {
10791086
let mut recovered = None;
10801087
if self.token == token::Dot {
1081-
// Attempt to recover `.4` as `0.4`.
1088+
// Attempt to recover `.4` as `0.4`. We don't currently have any syntax where
1089+
// dot would follow an optional literal, so we do this unconditionally.
10821090
recovered = self.look_ahead(1, |next_token| {
10831091
if let token::Literal(token::Lit { kind: token::Integer, symbol, suffix })
10841092
= next_token.kind {
@@ -1107,11 +1115,10 @@ impl<'a> Parser<'a> {
11071115
match Lit::from_token(token) {
11081116
Ok(lit) => {
11091117
self.bump();
1110-
Ok(lit)
1118+
Some(lit)
11111119
}
11121120
Err(LitError::NotLiteral) => {
1113-
let msg = format!("unexpected token: {}", self.this_token_descr());
1114-
Err(self.span_fatal(token.span, &msg))
1121+
None
11151122
}
11161123
Err(err) => {
11171124
let span = token.span;
@@ -1120,18 +1127,18 @@ impl<'a> Parser<'a> {
11201127
_ => unreachable!(),
11211128
};
11221129
self.bump();
1123-
self.error_literal_from_token(err, lit, span);
1130+
self.report_lit_error(err, lit, span);
11241131
// Pack possible quotes and prefixes from the original literal into
11251132
// the error literal's symbol so they can be pretty-printed faithfully.
11261133
let suffixless_lit = token::Lit::new(lit.kind, lit.symbol, None);
11271134
let symbol = Symbol::intern(&suffixless_lit.to_string());
11281135
let lit = token::Lit::new(token::Err, symbol, lit.suffix);
1129-
Lit::from_lit_token(lit, span).map_err(|_| unreachable!())
1136+
Some(Lit::from_lit_token(lit, span).unwrap_or_else(|_| unreachable!()))
11301137
}
11311138
}
11321139
}
11331140

1134-
fn error_literal_from_token(&self, err: LitError, lit: token::Lit, span: Span) {
1141+
fn report_lit_error(&self, err: LitError, lit: token::Lit, span: Span) {
11351142
// Checks if `s` looks like i32 or u1234 etc.
11361143
fn looks_like_width_suffix(first_chars: &[char], s: &str) -> bool {
11371144
s.len() > 1

src/librustc_parse/parser/item.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ impl<'a> Parser<'a> {
105105
return Ok(Some(self.parse_item_extern_crate(lo, vis, attrs)?));
106106
}
107107

108-
let abi = self.parse_opt_abi()?;
108+
let abi = self.parse_opt_abi();
109109

110110
if self.eat_keyword(kw::Fn) {
111111
// EXTERN FUNCTION ITEM

src/librustc_parse/parser/mod.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -1214,21 +1214,20 @@ impl<'a> Parser<'a> {
12141214
/// Parses `extern string_literal?`.
12151215
fn parse_extern(&mut self) -> PResult<'a, Extern> {
12161216
Ok(if self.eat_keyword(kw::Extern) {
1217-
Extern::from_abi(self.parse_opt_abi()?)
1217+
Extern::from_abi(self.parse_opt_abi())
12181218
} else {
12191219
Extern::None
12201220
})
12211221
}
12221222

12231223
/// Parses a string literal as an ABI spec.
1224-
fn parse_opt_abi(&mut self) -> PResult<'a, Option<StrLit>> {
1225-
if self.token.can_begin_literal_or_bool() {
1226-
let ast::Lit { token: token::Lit { symbol, suffix, .. }, span, kind }
1227-
= self.parse_lit()?;
1224+
fn parse_opt_abi(&mut self) -> Option<StrLit> {
1225+
if let Some(ast::Lit { token: token::Lit { symbol, suffix, .. }, span, kind })
1226+
= self.parse_opt_lit() {
12281227
match kind {
1229-
ast::LitKind::Str(symbol_unescaped, style) => return Ok(Some(StrLit {
1228+
ast::LitKind::Str(symbol_unescaped, style) => return Some(StrLit {
12301229
style, symbol, suffix, span, symbol_unescaped,
1231-
})),
1230+
}),
12321231
ast::LitKind::Err(_) => {}
12331232
_ => {
12341233
self.struct_span_err(span, "non-string ABI literal")
@@ -1242,7 +1241,7 @@ impl<'a> Parser<'a> {
12421241
}
12431242
}
12441243
}
1245-
Ok(None)
1244+
None
12461245
}
12471246

12481247
/// We are parsing `async fn`. If we are on Rust 2015, emit an error.

0 commit comments

Comments
 (0)