fix: Fix parsing of integer/keyword name refs in various places

This commit is contained in:
Lukas Wirth 2024-12-05 14:37:38 +01:00
parent f499faf72b
commit df7ab62a06
33 changed files with 351 additions and 133 deletions

View file

@ -449,7 +449,9 @@ fn postfix_dot_expr<const FLOAT_RECOVERY: bool>(
let nth1 = if FLOAT_RECOVERY { 0 } else { 1 };
let nth2 = if FLOAT_RECOVERY { 1 } else { 2 };
if p.nth(nth1) == IDENT && (p.nth(nth2) == T!['('] || p.nth_at(nth2, T![::])) {
if PATH_NAME_REF_KINDS.contains(p.nth(nth1))
&& (p.nth(nth2) == T!['('] || p.nth_at(nth2, T![::]))
{
return Ok(method_call_expr::<FLOAT_RECOVERY>(p, lhs));
}
@ -510,21 +512,26 @@ fn index_expr(p: &mut Parser<'_>, lhs: CompletedMarker) -> CompletedMarker {
// y.bar::<T>(1, 2,);
// x.0.0.call();
// x.0. call();
// x.0()
// }
fn method_call_expr<const FLOAT_RECOVERY: bool>(
p: &mut Parser<'_>,
lhs: CompletedMarker,
) -> CompletedMarker {
if FLOAT_RECOVERY {
assert!(p.nth(0) == IDENT && (p.nth(1) == T!['('] || p.nth_at(1, T![::])));
assert!(p.at_ts(PATH_NAME_REF_KINDS) && (p.nth(1) == T!['('] || p.nth_at(1, T![::])));
} else {
assert!(p.at(T![.]) && p.nth(1) == IDENT && (p.nth(2) == T!['('] || p.nth_at(2, T![::])));
assert!(
p.at(T![.])
&& PATH_NAME_REF_KINDS.contains(p.nth(1))
&& (p.nth(2) == T!['('] || p.nth_at(2, T![::]))
);
}
let m = lhs.precede(p);
if !FLOAT_RECOVERY {
p.bump(T![.]);
}
name_ref(p);
name_ref_mod_path(p);
generic_args::opt_generic_arg_list_expr(p);
if p.at(T!['(']) {
arg_list(p);
@ -543,6 +550,8 @@ fn method_call_expr<const FLOAT_RECOVERY: bool>(
// test field_expr
// fn foo() {
// x.self;
// x.Self;
// x.foo;
// x.0.bar;
// x.0.1;
@ -560,8 +569,8 @@ fn field_expr<const FLOAT_RECOVERY: bool>(
if !FLOAT_RECOVERY {
p.bump(T![.]);
}
if p.at(IDENT) || p.at(INT_NUMBER) {
name_ref_or_index(p);
if p.at_ts(PATH_NAME_REF_OR_INDEX_KINDS) {
name_ref_mod_path_or_index(p);
} else if p.at(FLOAT_NUMBER) {
return match p.split_float(m) {
(true, m) => {
@ -679,34 +688,37 @@ pub(crate) fn record_expr_field_list(p: &mut Parser<'_>) {
IDENT | INT_NUMBER if p.nth_at(1, T![::]) => {
// test_err record_literal_missing_ellipsis_recovery
// fn main() {
// S { S::default() }
// S { S::default() };
// S { 0::default() };
// }
m.abandon(p);
p.expect(T![..]);
expr(p);
}
IDENT | INT_NUMBER if p.nth_at(1, T![..]) => {
// test_err record_literal_before_ellipsis_recovery
// fn main() {
// S { field ..S::default() }
// S { 0 ..S::default() }
// }
name_ref_or_index(p);
p.error("expected `:`");
m.complete(p, RECORD_EXPR_FIELD);
}
IDENT | INT_NUMBER => {
if p.nth_at(1, T![..]) {
// test_err record_literal_before_ellipsis_recovery
// fn main() {
// S { field ..S::default() }
// }
// test_err record_literal_field_eq_recovery
// fn main() {
// S { field = foo }
// S { 0 = foo }
// }
if p.nth_at(1, T![:]) {
name_ref_or_index(p);
p.error("expected `:`");
} else {
// test_err record_literal_field_eq_recovery
// fn main() {
// S { field = foo }
// }
if p.nth_at(1, T![:]) {
name_ref_or_index(p);
p.bump(T![:]);
} else if p.nth_at(1, T![=]) {
name_ref_or_index(p);
p.err_and_bump("expected `:`");
}
expr(p);
p.bump(T![:]);
} else if p.nth_at(1, T![=]) {
name_ref_or_index(p);
p.err_and_bump("expected `:`");
}
expr(p);
m.complete(p, RECORD_EXPR_FIELD);
}
T![.] if p.at(T![..]) => {