Fix array element attribute position

This commit is contained in:
Edwin Cheng 2020-01-16 23:37:43 +08:00
parent b2ed130ffd
commit ed8d5c86e3
5 changed files with 60 additions and 36 deletions

View file

@ -8,6 +8,28 @@ 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) { pub(super) fn outer_attributes(p: &mut Parser) {
while p.at(T![#]) { while p.at(T![#]) {
attribute(p, false) attribute(p, false)

View file

@ -14,9 +14,9 @@ pub(super) enum StmtWithSemi {
const EXPR_FIRST: TokenSet = LHS_FIRST; const EXPR_FIRST: TokenSet = LHS_FIRST;
pub(super) fn expr(p: &mut Parser) -> BlockLike { pub(super) fn expr(p: &mut Parser) -> (Option<CompletedMarker>, BlockLike) {
let r = Restrictions { forbid_structs: false, prefer_stmt: false }; let r = Restrictions { forbid_structs: false, prefer_stmt: false };
expr_bp(p, r, 1).1 expr_bp(p, r, 1)
} }
pub(super) fn expr_stmt(p: &mut Parser) -> (Option<CompletedMarker>, BlockLike) { pub(super) fn expr_stmt(p: &mut Parser) -> (Option<CompletedMarker>, BlockLike) {

View file

@ -192,9 +192,8 @@ fn array_expr(p: &mut Parser) -> CompletedMarker {
// 1, // 1,
// 2, // 2,
// ]; // ];
attributes::outer_attributes(p); attributes::with_outer_attributes(p, |p| expr(p).0);
expr(p);
if p.eat(T![;]) { if p.eat(T![;]) {
expr(p); expr(p);
p.expect(T![']']); p.expect(T![']']);
@ -212,12 +211,15 @@ fn array_expr(p: &mut Parser) -> CompletedMarker {
// #[cfg(test)] // #[cfg(test)]
// 2, // 2,
// ]; // ];
attributes::outer_attributes(p); if !attributes::with_outer_attributes(p, |p| {
if !p.at_ts(EXPR_FIRST) { if !p.at_ts(EXPR_FIRST) {
p.error("expected expression"); p.error("expected expression");
return None;
}
expr(p).0
}) {
break; break;
} }
expr(p);
} }
p.expect(T![']']); p.expect(T![']']);
m.complete(p, ARRAY_EXPR) m.complete(p, ARRAY_EXPR)

View file

@ -27,20 +27,20 @@ SOURCE_FILE@[0; 56)
ARRAY_EXPR@[23; 54) ARRAY_EXPR@[23; 54)
L_BRACK@[23; 24) "[" L_BRACK@[23; 24) "["
WHITESPACE@[24; 28) "\n " WHITESPACE@[24; 28) "\n "
ATTR@[28; 40) LITERAL@[28; 45)
POUND@[28; 29) "#" ATTR@[28; 40)
L_BRACK@[29; 30) "[" POUND@[28; 29) "#"
PATH@[30; 33) L_BRACK@[29; 30) "["
PATH_SEGMENT@[30; 33) PATH@[30; 33)
NAME_REF@[30; 33) PATH_SEGMENT@[30; 33)
IDENT@[30; 33) "cfg" NAME_REF@[30; 33)
TOKEN_TREE@[33; 39) IDENT@[30; 33) "cfg"
L_PAREN@[33; 34) "(" TOKEN_TREE@[33; 39)
IDENT@[34; 38) "test" L_PAREN@[33; 34) "("
R_PAREN@[38; 39) ")" IDENT@[34; 38) "test"
R_BRACK@[39; 40) "]" R_PAREN@[38; 39) ")"
WHITESPACE@[40; 44) "\n " R_BRACK@[39; 40) "]"
LITERAL@[44; 45) WHITESPACE@[40; 44) "\n "
INT_NUMBER@[44; 45) "1" INT_NUMBER@[44; 45) "1"
COMMA@[45; 46) "," COMMA@[45; 46) ","
WHITESPACE@[46; 50) "\n " WHITESPACE@[46; 50) "\n "

View file

@ -31,20 +31,20 @@ SOURCE_FILE@[0; 56)
INT_NUMBER@[28; 29) "1" INT_NUMBER@[28; 29) "1"
COMMA@[29; 30) "," COMMA@[29; 30) ","
WHITESPACE@[30; 34) "\n " WHITESPACE@[30; 34) "\n "
ATTR@[34; 46) LITERAL@[34; 51)
POUND@[34; 35) "#" ATTR@[34; 46)
L_BRACK@[35; 36) "[" POUND@[34; 35) "#"
PATH@[36; 39) L_BRACK@[35; 36) "["
PATH_SEGMENT@[36; 39) PATH@[36; 39)
NAME_REF@[36; 39) PATH_SEGMENT@[36; 39)
IDENT@[36; 39) "cfg" NAME_REF@[36; 39)
TOKEN_TREE@[39; 45) IDENT@[36; 39) "cfg"
L_PAREN@[39; 40) "(" TOKEN_TREE@[39; 45)
IDENT@[40; 44) "test" L_PAREN@[39; 40) "("
R_PAREN@[44; 45) ")" IDENT@[40; 44) "test"
R_BRACK@[45; 46) "]" R_PAREN@[44; 45) ")"
WHITESPACE@[46; 50) "\n " R_BRACK@[45; 46) "]"
LITERAL@[50; 51) WHITESPACE@[46; 50) "\n "
INT_NUMBER@[50; 51) "2" INT_NUMBER@[50; 51) "2"
COMMA@[51; 52) "," COMMA@[51; 52) ","
WHITESPACE@[52; 53) "\n" WHITESPACE@[52; 53) "\n"