mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-30 22:01:37 +00:00
Simplify array parsing
This commit is contained in:
parent
d3c4fbbbc4
commit
ab0a11b1de
3 changed files with 45 additions and 69 deletions
|
@ -181,29 +181,19 @@ fn tuple_expr(p: &mut Parser) -> CompletedMarker {
|
|||
fn array_expr(p: &mut Parser) -> CompletedMarker {
|
||||
assert!(p.at(T!['[']));
|
||||
let m = p.start();
|
||||
|
||||
let mut n_exprs = 0u32;
|
||||
let mut has_semi = false;
|
||||
|
||||
p.bump(T!['[']);
|
||||
if p.eat(T![']']) {
|
||||
return m.complete(p, ARRAY_EXPR);
|
||||
}
|
||||
|
||||
// test first_array_member_attributes
|
||||
// pub const A: &[i64] = &[
|
||||
// #[cfg(test)]
|
||||
// 1,
|
||||
// 2,
|
||||
// ];
|
||||
attributes::with_outer_attributes(p, |p| expr(p).0);
|
||||
|
||||
if p.eat(T![;]) {
|
||||
expr(p);
|
||||
p.expect(T![']']);
|
||||
return m.complete(p, ARRAY_EXPR);
|
||||
}
|
||||
while !p.at(EOF) && !p.at(T![']']) {
|
||||
p.expect(T![,]);
|
||||
if p.at(T![']']) {
|
||||
break;
|
||||
}
|
||||
n_exprs += 1;
|
||||
// test first_array_member_attributes
|
||||
// pub const A: &[i64] = &[
|
||||
// #[cfg(test)]
|
||||
// 1,
|
||||
// 2,
|
||||
// ];
|
||||
|
||||
// test subsequent_array_member_attributes
|
||||
// pub const A: &[i64] = &[
|
||||
|
@ -211,17 +201,32 @@ fn array_expr(p: &mut Parser) -> CompletedMarker {
|
|||
// #[cfg(test)]
|
||||
// 2,
|
||||
// ];
|
||||
if !attributes::with_outer_attributes(p, |p| {
|
||||
if !p.at_ts(EXPR_FIRST) {
|
||||
p.error("expected expression");
|
||||
return None;
|
||||
let m = p.start();
|
||||
let has_attrs = p.at(T![#]);
|
||||
attributes::outer_attributes(p);
|
||||
|
||||
let cm = expr(p).0;
|
||||
|
||||
match (has_attrs, cm) {
|
||||
(true, Some(cm)) => {
|
||||
let kind = cm.kind();
|
||||
cm.undo_completion(p).abandon(p);
|
||||
m.complete(p, kind);
|
||||
}
|
||||
expr(p).0
|
||||
}) {
|
||||
_ => m.abandon(p),
|
||||
}
|
||||
|
||||
if n_exprs == 1 && p.eat(T![;]) {
|
||||
has_semi = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if has_semi || !p.at(T![']']) && !p.expect(T![,]) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
p.expect(T![']']);
|
||||
|
||||
m.complete(p, ARRAY_EXPR)
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue