Skip to content

Commit 4cb5849

Browse files
committed
Refactor Parser::break_up_float
1 parent 3d20c81 commit 4cb5849

File tree

1 file changed

+25
-27
lines changed
  • compiler/rustc_parse/src/parser

1 file changed

+25
-27
lines changed

Diff for: compiler/rustc_parse/src/parser/expr.rs

+25-27
Original file line numberDiff line numberDiff line change
@@ -1031,14 +1031,16 @@ impl<'a> Parser<'a> {
10311031
self.dcx().emit_err(errors::UnexpectedTokenAfterDot { span, actual })
10321032
}
10331033

1034-
// We need an identifier or integer, but the next token is a float.
1035-
// Break the float into components to extract the identifier or integer.
1034+
/// We need an identifier or integer, but the next token is a float.
1035+
/// Break the float into components to extract the identifier or integer.
1036+
///
1037+
/// See also [`TokenKind::break_two_token_op`] which does similar splitting of `>>` into `>`.
1038+
//
10361039
// FIXME: With current `TokenCursor` it's hard to break tokens into more than 2
1037-
// parts unless those parts are processed immediately. `TokenCursor` should either
1038-
// support pushing "future tokens" (would be also helpful to `break_and_eat`), or
1039-
// we should break everything including floats into more basic proc-macro style
1040-
// tokens in the lexer (probably preferable).
1041-
// See also `TokenKind::break_two_token_op` which does similar splitting of `>>` into `>`.
1040+
// parts unless those parts are processed immediately. `TokenCursor` should either
1041+
// support pushing "future tokens" (would be also helpful to `break_and_eat`), or
1042+
// we should break everything including floats into more basic proc-macro style
1043+
// tokens in the lexer (probably preferable).
10421044
fn break_up_float(&self, float: Symbol, span: Span) -> DestructuredFloat {
10431045
#[derive(Debug)]
10441046
enum FloatComponent {
@@ -1078,34 +1080,30 @@ impl<'a> Parser<'a> {
10781080
DestructuredFloat::Single(Symbol::intern(i), span)
10791081
}
10801082
// 1.
1081-
[IdentLike(i), Punct('.')] => {
1082-
let (ident_span, dot_span) = if can_take_span_apart() {
1083-
let (span, ident_len) = (span.data(), BytePos::from_usize(i.len()));
1084-
let ident_span = span.with_hi(span.lo + ident_len);
1085-
let dot_span = span.with_lo(span.lo + ident_len);
1086-
(ident_span, dot_span)
1083+
[IdentLike(left), Punct('.')] => {
1084+
let (left_span, dot_span) = if can_take_span_apart() {
1085+
let left_span = span.with_hi(span.lo() + BytePos::from_usize(left.len()));
1086+
let dot_span = span.with_lo(left_span.hi());
1087+
(left_span, dot_span)
10871088
} else {
10881089
(span, span)
10891090
};
1090-
let symbol = Symbol::intern(i);
1091-
DestructuredFloat::TrailingDot(symbol, ident_span, dot_span)
1091+
let left = Symbol::intern(left);
1092+
DestructuredFloat::TrailingDot(left, left_span, dot_span)
10921093
}
10931094
// 1.2 | 1.2e3
1094-
[IdentLike(i1), Punct('.'), IdentLike(i2)] => {
1095-
let (ident1_span, dot_span, ident2_span) = if can_take_span_apart() {
1096-
let (span, ident1_len) = (span.data(), BytePos::from_usize(i1.len()));
1097-
let ident1_span = span.with_hi(span.lo + ident1_len);
1098-
let dot_span = span
1099-
.with_lo(span.lo + ident1_len)
1100-
.with_hi(span.lo + ident1_len + BytePos(1));
1101-
let ident2_span = span.with_lo(span.lo + ident1_len + BytePos(1));
1102-
(ident1_span, dot_span, ident2_span)
1095+
[IdentLike(left), Punct('.'), IdentLike(right)] => {
1096+
let (left_span, dot_span, right_span) = if can_take_span_apart() {
1097+
let left_span = span.with_hi(span.lo() + BytePos::from_usize(left.len()));
1098+
let dot_span = span.with_lo(left_span.hi()).with_hi(left_span.hi() + BytePos(1));
1099+
let right_span = span.with_lo(dot_span.hi());
1100+
(left_span, dot_span, right_span)
11031101
} else {
11041102
(span, span, span)
11051103
};
1106-
let symbol1 = Symbol::intern(i1);
1107-
let symbol2 = Symbol::intern(i2);
1108-
DestructuredFloat::MiddleDot(symbol1, ident1_span, dot_span, symbol2, ident2_span)
1104+
let left = Symbol::intern(left);
1105+
let right = Symbol::intern(right);
1106+
DestructuredFloat::MiddleDot(left, left_span, dot_span, right, right_span)
11091107
}
11101108
// 1e+ | 1e- (recovered)
11111109
[IdentLike(_), Punct('+' | '-')] |

0 commit comments

Comments
 (0)