Refactor Parser::break_up_float

This commit is contained in:
Lieselotte 2024-09-14 13:17:16 +02:00
parent 3d20c810b0
commit 4cb5849f01
No known key found for this signature in database
GPG Key ID: 68A9A951C7E1F283

View File

@ -1031,14 +1031,16 @@ fn error_unexpected_after_dot(&self) -> ErrorGuaranteed {
self.dcx().emit_err(errors::UnexpectedTokenAfterDot { span, actual }) self.dcx().emit_err(errors::UnexpectedTokenAfterDot { span, actual })
} }
// We need an identifier or integer, but the next token is a float. /// We need an identifier or integer, but the next token is a float.
// Break the float into components to extract the identifier or integer. /// Break the float into components to extract the identifier or integer.
///
/// See also [`TokenKind::break_two_token_op`] which does similar splitting of `>>` into `>`.
//
// FIXME: With current `TokenCursor` it's hard to break tokens into more than 2 // FIXME: With current `TokenCursor` it's hard to break tokens into more than 2
// parts unless those parts are processed immediately. `TokenCursor` should either // parts unless those parts are processed immediately. `TokenCursor` should either
// support pushing "future tokens" (would be also helpful to `break_and_eat`), or // support pushing "future tokens" (would be also helpful to `break_and_eat`), or
// we should break everything including floats into more basic proc-macro style // we should break everything including floats into more basic proc-macro style
// tokens in the lexer (probably preferable). // tokens in the lexer (probably preferable).
// See also `TokenKind::break_two_token_op` which does similar splitting of `>>` into `>`.
fn break_up_float(&self, float: Symbol, span: Span) -> DestructuredFloat { fn break_up_float(&self, float: Symbol, span: Span) -> DestructuredFloat {
#[derive(Debug)] #[derive(Debug)]
enum FloatComponent { enum FloatComponent {
@ -1078,34 +1080,30 @@ enum FloatComponent {
DestructuredFloat::Single(Symbol::intern(i), span) DestructuredFloat::Single(Symbol::intern(i), span)
} }
// 1. // 1.
[IdentLike(i), Punct('.')] => { [IdentLike(left), Punct('.')] => {
let (ident_span, dot_span) = if can_take_span_apart() { let (left_span, dot_span) = if can_take_span_apart() {
let (span, ident_len) = (span.data(), BytePos::from_usize(i.len())); let left_span = span.with_hi(span.lo() + BytePos::from_usize(left.len()));
let ident_span = span.with_hi(span.lo + ident_len); let dot_span = span.with_lo(left_span.hi());
let dot_span = span.with_lo(span.lo + ident_len); (left_span, dot_span)
(ident_span, dot_span)
} else { } else {
(span, span) (span, span)
}; };
let symbol = Symbol::intern(i); let left = Symbol::intern(left);
DestructuredFloat::TrailingDot(symbol, ident_span, dot_span) DestructuredFloat::TrailingDot(left, left_span, dot_span)
} }
// 1.2 | 1.2e3 // 1.2 | 1.2e3
[IdentLike(i1), Punct('.'), IdentLike(i2)] => { [IdentLike(left), Punct('.'), IdentLike(right)] => {
let (ident1_span, dot_span, ident2_span) = if can_take_span_apart() { let (left_span, dot_span, right_span) = if can_take_span_apart() {
let (span, ident1_len) = (span.data(), BytePos::from_usize(i1.len())); let left_span = span.with_hi(span.lo() + BytePos::from_usize(left.len()));
let ident1_span = span.with_hi(span.lo + ident1_len); let dot_span = span.with_lo(left_span.hi()).with_hi(left_span.hi() + BytePos(1));
let dot_span = span let right_span = span.with_lo(dot_span.hi());
.with_lo(span.lo + ident1_len) (left_span, dot_span, right_span)
.with_hi(span.lo + ident1_len + BytePos(1));
let ident2_span = span.with_lo(span.lo + ident1_len + BytePos(1));
(ident1_span, dot_span, ident2_span)
} else { } else {
(span, span, span) (span, span, span)
}; };
let symbol1 = Symbol::intern(i1); let left = Symbol::intern(left);
let symbol2 = Symbol::intern(i2); let right = Symbol::intern(right);
DestructuredFloat::MiddleDot(symbol1, ident1_span, dot_span, symbol2, ident2_span) DestructuredFloat::MiddleDot(left, left_span, dot_span, right, right_span)
} }
// 1e+ | 1e- (recovered) // 1e+ | 1e- (recovered)
[IdentLike(_), Punct('+' | '-')] | [IdentLike(_), Punct('+' | '-')] |