mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-26 04:53:34 +00:00
G: slice & array types
This commit is contained in:
parent
ceb94ece2a
commit
555c4ae375
9 changed files with 108 additions and 0 deletions
|
@ -106,6 +106,8 @@ Grammar(
|
|||
"NEVER_TYPE",
|
||||
"PATH_TYPE",
|
||||
"POINTER_TYPE",
|
||||
"ARRAY_TYPE",
|
||||
"SLICE_TYPE",
|
||||
|
||||
"EXTERN_BLOCK",
|
||||
"ENUM_VARIANT",
|
||||
|
|
|
@ -5,6 +5,7 @@ pub(super) fn type_(p: &mut Parser) {
|
|||
L_PAREN => paren_or_tuple_type(p),
|
||||
EXCL => never_type(p),
|
||||
STAR => pointer_type(p),
|
||||
L_BRACK => array_or_slice_type(p),
|
||||
IDENT => path_type(p),
|
||||
_ => {
|
||||
p.error("expected type");
|
||||
|
@ -82,6 +83,38 @@ fn pointer_type(p: &mut Parser) {
|
|||
m.complete(p, POINTER_TYPE);
|
||||
}
|
||||
|
||||
fn array_or_slice_type(p: &mut Parser) {
|
||||
assert!(p.at(L_BRACK));
|
||||
let m = p.start();
|
||||
p.bump();
|
||||
|
||||
type_(p);
|
||||
let kind = match p.current() {
|
||||
// test slice_type
|
||||
// type T = [()];
|
||||
R_BRACK => {
|
||||
p.bump();
|
||||
SLICE_TYPE
|
||||
},
|
||||
|
||||
// test array_type
|
||||
// type T = [(); 92];
|
||||
SEMI => {
|
||||
p.bump();
|
||||
expressions::expr(p);
|
||||
p.expect(R_BRACK);
|
||||
ARRAY_TYPE
|
||||
}
|
||||
// test array_type_missing_semi
|
||||
// type T = [() 92];
|
||||
_ => {
|
||||
p.error("expected `;` or `]`");
|
||||
SLICE_TYPE
|
||||
}
|
||||
};
|
||||
m.complete(p, kind);
|
||||
}
|
||||
|
||||
fn path_type(p: &mut Parser) {
|
||||
assert!(p.at(IDENT));
|
||||
let m = p.start();
|
||||
|
|
|
@ -104,6 +104,8 @@ pub enum SyntaxKind {
|
|||
NEVER_TYPE,
|
||||
PATH_TYPE,
|
||||
POINTER_TYPE,
|
||||
ARRAY_TYPE,
|
||||
SLICE_TYPE,
|
||||
EXTERN_BLOCK,
|
||||
ENUM_VARIANT,
|
||||
NAMED_FIELD,
|
||||
|
@ -234,6 +236,8 @@ impl SyntaxKind {
|
|||
NEVER_TYPE => &SyntaxInfo { name: "NEVER_TYPE" },
|
||||
PATH_TYPE => &SyntaxInfo { name: "PATH_TYPE" },
|
||||
POINTER_TYPE => &SyntaxInfo { name: "POINTER_TYPE" },
|
||||
ARRAY_TYPE => &SyntaxInfo { name: "ARRAY_TYPE" },
|
||||
SLICE_TYPE => &SyntaxInfo { name: "SLICE_TYPE" },
|
||||
EXTERN_BLOCK => &SyntaxInfo { name: "EXTERN_BLOCK" },
|
||||
ENUM_VARIANT => &SyntaxInfo { name: "ENUM_VARIANT" },
|
||||
NAMED_FIELD => &SyntaxInfo { name: "NAMED_FIELD" },
|
||||
|
|
1
tests/data/parser/inline/0023_array_type_missing_semi.rs
Normal file
1
tests/data/parser/inline/0023_array_type_missing_semi.rs
Normal file
|
@ -0,0 +1 @@
|
|||
type T = [() 92];
|
28
tests/data/parser/inline/0023_array_type_missing_semi.txt
Normal file
28
tests/data/parser/inline/0023_array_type_missing_semi.txt
Normal file
|
@ -0,0 +1,28 @@
|
|||
FILE@[0; 18)
|
||||
TYPE_ITEM@[0; 13)
|
||||
TYPE_KW@[0; 4)
|
||||
NAME@[4; 7)
|
||||
WHITESPACE@[4; 5)
|
||||
IDENT@[5; 6) "T"
|
||||
WHITESPACE@[6; 7)
|
||||
EQ@[7; 8)
|
||||
SLICE_TYPE@[8; 13)
|
||||
WHITESPACE@[8; 9)
|
||||
L_BRACK@[9; 10)
|
||||
TUPLE_TYPE@[10; 13)
|
||||
L_PAREN@[10; 11)
|
||||
R_PAREN@[11; 12)
|
||||
WHITESPACE@[12; 13)
|
||||
err: `expected `;` or `]``
|
||||
err: `expected SEMI`
|
||||
ERROR@[13; 15)
|
||||
err: `expected item`
|
||||
INT_NUMBER@[13; 15)
|
||||
ERROR@[15; 16)
|
||||
err: `expected item`
|
||||
R_BRACK@[15; 16)
|
||||
ERROR@[16; 18)
|
||||
err: `expected item, found `;`
|
||||
consider removing this semicolon`
|
||||
SEMI@[16; 17)
|
||||
WHITESPACE@[17; 18)
|
1
tests/data/parser/inline/0024_array_type.rs
Normal file
1
tests/data/parser/inline/0024_array_type.rs
Normal file
|
@ -0,0 +1 @@
|
|||
type T = [(); 92];
|
21
tests/data/parser/inline/0024_array_type.txt
Normal file
21
tests/data/parser/inline/0024_array_type.txt
Normal file
|
@ -0,0 +1,21 @@
|
|||
FILE@[0; 19)
|
||||
TYPE_ITEM@[0; 19)
|
||||
TYPE_KW@[0; 4)
|
||||
NAME@[4; 7)
|
||||
WHITESPACE@[4; 5)
|
||||
IDENT@[5; 6) "T"
|
||||
WHITESPACE@[6; 7)
|
||||
EQ@[7; 8)
|
||||
ARRAY_TYPE@[8; 17)
|
||||
WHITESPACE@[8; 9)
|
||||
L_BRACK@[9; 10)
|
||||
TUPLE_TYPE@[10; 12)
|
||||
L_PAREN@[10; 11)
|
||||
R_PAREN@[11; 12)
|
||||
SEMI@[12; 13)
|
||||
LITERAL@[13; 16)
|
||||
WHITESPACE@[13; 14)
|
||||
INT_NUMBER@[14; 16)
|
||||
R_BRACK@[16; 17)
|
||||
SEMI@[17; 18)
|
||||
WHITESPACE@[18; 19)
|
1
tests/data/parser/inline/0025_slice_type.rs
Normal file
1
tests/data/parser/inline/0025_slice_type.rs
Normal file
|
@ -0,0 +1 @@
|
|||
type T = [()];
|
17
tests/data/parser/inline/0025_slice_type.txt
Normal file
17
tests/data/parser/inline/0025_slice_type.txt
Normal file
|
@ -0,0 +1,17 @@
|
|||
FILE@[0; 15)
|
||||
TYPE_ITEM@[0; 15)
|
||||
TYPE_KW@[0; 4)
|
||||
NAME@[4; 7)
|
||||
WHITESPACE@[4; 5)
|
||||
IDENT@[5; 6) "T"
|
||||
WHITESPACE@[6; 7)
|
||||
EQ@[7; 8)
|
||||
SLICE_TYPE@[8; 13)
|
||||
WHITESPACE@[8; 9)
|
||||
L_BRACK@[9; 10)
|
||||
TUPLE_TYPE@[10; 12)
|
||||
L_PAREN@[10; 11)
|
||||
R_PAREN@[11; 12)
|
||||
R_BRACK@[12; 13)
|
||||
SEMI@[13; 14)
|
||||
WHITESPACE@[14; 15)
|
Loading…
Reference in a new issue