Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 0 additions & 22 deletions crates/ra_parser/src/grammar/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,6 @@ pub(super) fn inner_attributes(p: &mut Parser) {
}
}

pub(super) fn with_outer_attributes(
p: &mut Parser,
f: impl Fn(&mut Parser) -> Option<CompletedMarker>,
) -> bool {
let am = p.start();
let has_attrs = p.at(T![#]);
attributes::outer_attributes(p);
let cm = f(p);
let success = cm.is_some();

match (has_attrs, cm) {
(true, Some(cm)) => {
let kind = cm.kind();
cm.undo_completion(p).abandon(p);
am.complete(p, kind);
}
_ => am.abandon(p),
}

success
}

pub(super) fn outer_attributes(p: &mut Parser) {
while p.at(T![#]) {
attribute(p, false)
Expand Down
59 changes: 27 additions & 32 deletions crates/ra_parser/src/grammar/expressions/atom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,47 +181,42 @@ fn tuple_expr(p: &mut Parser) -> CompletedMarker {
fn array_expr(p: &mut Parser) -> CompletedMarker {
assert!(p.at(T!['[']));
let m = p.start();
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);
let mut n_exprs = 0u32;
let mut has_semi = false;

if p.eat(T![;]) {
expr(p);
p.expect(T![']']);
return m.complete(p, ARRAY_EXPR);
}
p.bump(T!['[']);
while !p.at(EOF) && !p.at(T![']']) {
p.expect(T![,]);
if p.at(T![']']) {
break;
}
n_exprs += 1;

// test array_attrs
// const A: &[i64] = &[1, #[cfg(test)] 2];
let m = p.start();
let has_attrs = p.at(T![#]);
attributes::outer_attributes(p);

// test subsequent_array_member_attributes
// pub const A: &[i64] = &[
// 1,
// #[cfg(test)]
// 2,
// ];
if !attributes::with_outer_attributes(p, |p| {
if !p.at_ts(EXPR_FIRST) {
p.error("expected expression");
return None;
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)
}

Expand Down
33 changes: 13 additions & 20 deletions crates/ra_syntax/test_data/parser/err/0022_bad_exprs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ SOURCE_FILE@[0; 112)
BLOCK@[7; 33)
L_CURLY@[7; 8) "{"
WHITESPACE@[8; 9) " "
EXPR_STMT@[9; 15)
ARRAY_EXPR@[9; 15)
EXPR_STMT@[9; 26)
ARRAY_EXPR@[9; 26)
L_BRACK@[9; 10) "["
LITERAL@[10; 11)
INT_NUMBER@[10; 11) "1"
Expand All @@ -22,17 +22,13 @@ SOURCE_FILE@[0; 112)
LITERAL@[13; 14)
INT_NUMBER@[13; 14) "2"
COMMA@[14; 15) ","
WHITESPACE@[15; 16) " "
EXPR_STMT@[16; 17)
ERROR@[16; 17)
AT@[16; 17) "@"
EXPR_STMT@[17; 18)
ERROR@[17; 18)
WHITESPACE@[15; 16) " "
ERROR@[16; 17)
AT@[16; 17) "@"
COMMA@[17; 18) ","
WHITESPACE@[18; 19) " "
STRUCT_DEF@[19; 26)
STRUCT_KW@[19; 25) "struct"
ERROR@[25; 26)
WHITESPACE@[18; 19) " "
ERROR@[19; 25)
STRUCT_KW@[19; 25) "struct"
COMMA@[25; 26) ","
WHITESPACE@[26; 27) " "
LET_STMT@[27; 31)
Expand Down Expand Up @@ -151,15 +147,12 @@ SOURCE_FILE@[0; 112)
WHITESPACE@[109; 110) " "
R_CURLY@[110; 111) "}"
WHITESPACE@[111; 112) "\n"
error 15: expected expression
error 15: expected R_BRACK
error 15: expected SEMI
error 16: expected expression
error 17: expected SEMI
error 17: expected expression
error 18: expected SEMI
error 25: expected a name
error 26: expected `;`, `{`, or `(`
error 19: expected expression
error 26: expected expression
error 26: expected COMMA
error 26: expected R_BRACK
error 26: expected SEMI
error 30: expected pattern
error 31: expected SEMI
error 52: expected expression
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const A: &[i64] = &[1, #[cfg(test)] 2];
47 changes: 47 additions & 0 deletions crates/ra_syntax/test_data/parser/inline/ok/0150_array_attrs.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
SOURCE_FILE@[0; 40)
CONST_DEF@[0; 39)
CONST_KW@[0; 5) "const"
WHITESPACE@[5; 6) " "
NAME@[6; 7)
IDENT@[6; 7) "A"
COLON@[7; 8) ":"
WHITESPACE@[8; 9) " "
REFERENCE_TYPE@[9; 15)
AMP@[9; 10) "&"
SLICE_TYPE@[10; 15)
L_BRACK@[10; 11) "["
PATH_TYPE@[11; 14)
PATH@[11; 14)
PATH_SEGMENT@[11; 14)
NAME_REF@[11; 14)
IDENT@[11; 14) "i64"
R_BRACK@[14; 15) "]"
WHITESPACE@[15; 16) " "
EQ@[16; 17) "="
WHITESPACE@[17; 18) " "
REF_EXPR@[18; 38)
AMP@[18; 19) "&"
ARRAY_EXPR@[19; 38)
L_BRACK@[19; 20) "["
LITERAL@[20; 21)
INT_NUMBER@[20; 21) "1"
COMMA@[21; 22) ","
WHITESPACE@[22; 23) " "
LITERAL@[23; 37)
ATTR@[23; 35)
POUND@[23; 24) "#"
L_BRACK@[24; 25) "["
PATH@[25; 28)
PATH_SEGMENT@[25; 28)
NAME_REF@[25; 28)
IDENT@[25; 28) "cfg"
TOKEN_TREE@[28; 34)
L_PAREN@[28; 29) "("
IDENT@[29; 33) "test"
R_PAREN@[33; 34) ")"
R_BRACK@[34; 35) "]"
WHITESPACE@[35; 36) " "
INT_NUMBER@[36; 37) "2"
R_BRACK@[37; 38) "]"
SEMI@[38; 39) ";"
WHITESPACE@[39; 40) "\n"