mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-14 14:13:58 +00:00
syntax,ssr: Implement statement parsing
This commit is contained in:
parent
354c1daedc
commit
cc081b7e1c
31 changed files with 167 additions and 0 deletions
|
@ -78,6 +78,7 @@ impl ParsedRule {
|
|||
builder.try_add(ast::Item::parse(&raw_pattern), raw_template.map(ast::Item::parse));
|
||||
builder.try_add(ast::Path::parse(&raw_pattern), raw_template.map(ast::Path::parse));
|
||||
builder.try_add(ast::Pat::parse(&raw_pattern), raw_template.map(ast::Pat::parse));
|
||||
builder.try_add(ast::Stmt::parse(&raw_pattern), raw_template.map(ast::Stmt::parse));
|
||||
builder.build()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -212,6 +212,13 @@ impl ast::Attr {
|
|||
}
|
||||
}
|
||||
|
||||
impl ast::Stmt {
|
||||
/// Returns `text`, parsed as statement, but only if it has no errors.
|
||||
pub fn parse(text: &str) -> Result<Self, ()> {
|
||||
parsing::parse_text_fragment(text, parser::FragmentKind::Statement)
|
||||
}
|
||||
}
|
||||
|
||||
/// Matches a `SyntaxNode` against an `ast` type.
|
||||
///
|
||||
/// # Example:
|
||||
|
|
|
@ -102,6 +102,15 @@ fn type_parser_tests() {
|
|||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stmt_parser_tests() {
|
||||
fragment_parser_dir_test(
|
||||
&["parser/fragments/stmt/ok"],
|
||||
&["parser/fragments/stmt/err"],
|
||||
crate::ast::Stmt::parse,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parser_fuzz_tests() {
|
||||
for (_, text) in collect_rust_files(&test_data_dir(), &["parser/fuzz-failures"]) {
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
ERROR
|
|
@ -0,0 +1 @@
|
|||
#[foo]
|
|
@ -0,0 +1 @@
|
|||
ERROR
|
|
@ -0,0 +1 @@
|
|||
foo();
|
|
@ -0,0 +1 @@
|
|||
ERROR
|
|
@ -0,0 +1 @@
|
|||
let x = 10;
|
|
@ -0,0 +1 @@
|
|||
ERROR
|
|
@ -0,0 +1 @@
|
|||
m1!{ let a = 0; };
|
|
@ -0,0 +1 @@
|
|||
ERROR
|
|
@ -0,0 +1 @@
|
|||
m1!{ let a = 0; };
|
|
@ -0,0 +1 @@
|
|||
ERROR
|
|
@ -0,0 +1 @@
|
|||
a(); b(); c()
|
|
@ -0,0 +1 @@
|
|||
ERROR
|
|
@ -0,0 +1 @@
|
|||
(
|
|
@ -0,0 +1 @@
|
|||
ERROR
|
|
@ -0,0 +1 @@
|
|||
;
|
|
@ -0,0 +1 @@
|
|||
ERROR
|
|
@ -0,0 +1 @@
|
|||
1 +
|
|
@ -0,0 +1,9 @@
|
|||
EXPR_STMT@0..5
|
||||
BIN_EXPR@0..5
|
||||
LITERAL@0..1
|
||||
INT_NUMBER@0..1 "1"
|
||||
WHITESPACE@1..2 " "
|
||||
PLUS@2..3 "+"
|
||||
WHITESPACE@3..4 " "
|
||||
LITERAL@4..5
|
||||
INT_NUMBER@4..5 "1"
|
|
@ -0,0 +1 @@
|
|||
1 + 1
|
|
@ -0,0 +1,69 @@
|
|||
EXPR_STMT@0..55
|
||||
BLOCK_EXPR@0..55
|
||||
L_CURLY@0..1 "{"
|
||||
WHITESPACE@1..6 "\n "
|
||||
LET_STMT@6..20
|
||||
LET_KW@6..9 "let"
|
||||
WHITESPACE@9..10 " "
|
||||
IDENT_PAT@10..11
|
||||
NAME@10..11
|
||||
IDENT@10..11 "x"
|
||||
WHITESPACE@11..12 " "
|
||||
EQ@12..13 "="
|
||||
WHITESPACE@13..14 " "
|
||||
CALL_EXPR@14..19
|
||||
PATH_EXPR@14..17
|
||||
PATH@14..17
|
||||
PATH_SEGMENT@14..17
|
||||
NAME_REF@14..17
|
||||
IDENT@14..17 "foo"
|
||||
ARG_LIST@17..19
|
||||
L_PAREN@17..18 "("
|
||||
R_PAREN@18..19 ")"
|
||||
SEMICOLON@19..20 ";"
|
||||
WHITESPACE@20..25 "\n "
|
||||
LET_STMT@25..39
|
||||
LET_KW@25..28 "let"
|
||||
WHITESPACE@28..29 " "
|
||||
IDENT_PAT@29..30
|
||||
NAME@29..30
|
||||
IDENT@29..30 "y"
|
||||
WHITESPACE@30..31 " "
|
||||
EQ@31..32 "="
|
||||
WHITESPACE@32..33 " "
|
||||
CALL_EXPR@33..38
|
||||
PATH_EXPR@33..36
|
||||
PATH@33..36
|
||||
PATH_SEGMENT@33..36
|
||||
NAME_REF@33..36
|
||||
IDENT@33..36 "bar"
|
||||
ARG_LIST@36..38
|
||||
L_PAREN@36..37 "("
|
||||
R_PAREN@37..38 ")"
|
||||
SEMICOLON@38..39 ";"
|
||||
WHITESPACE@39..44 "\n "
|
||||
CALL_EXPR@44..53
|
||||
PATH_EXPR@44..46
|
||||
PATH@44..46
|
||||
PATH_SEGMENT@44..46
|
||||
NAME_REF@44..46
|
||||
IDENT@44..46 "Ok"
|
||||
ARG_LIST@46..53
|
||||
L_PAREN@46..47 "("
|
||||
BIN_EXPR@47..52
|
||||
PATH_EXPR@47..48
|
||||
PATH@47..48
|
||||
PATH_SEGMENT@47..48
|
||||
NAME_REF@47..48
|
||||
IDENT@47..48 "x"
|
||||
WHITESPACE@48..49 " "
|
||||
PLUS@49..50 "+"
|
||||
WHITESPACE@50..51 " "
|
||||
PATH_EXPR@51..52
|
||||
PATH@51..52
|
||||
PATH_SEGMENT@51..52
|
||||
NAME_REF@51..52
|
||||
IDENT@51..52 "y"
|
||||
R_PAREN@52..53 ")"
|
||||
WHITESPACE@53..54 "\n"
|
||||
R_CURLY@54..55 "}"
|
|
@ -0,0 +1,5 @@
|
|||
{
|
||||
let x = foo();
|
||||
let y = bar();
|
||||
Ok(x + y)
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
STRUCT@0..28
|
||||
STRUCT_KW@0..6 "struct"
|
||||
WHITESPACE@6..7 " "
|
||||
NAME@7..10
|
||||
IDENT@7..10 "Foo"
|
||||
WHITESPACE@10..11 " "
|
||||
RECORD_FIELD_LIST@11..28
|
||||
L_CURLY@11..12 "{"
|
||||
WHITESPACE@12..17 "\n "
|
||||
RECORD_FIELD@17..25
|
||||
NAME@17..20
|
||||
IDENT@17..20 "bar"
|
||||
COLON@20..21 ":"
|
||||
WHITESPACE@21..22 " "
|
||||
PATH_TYPE@22..25
|
||||
PATH@22..25
|
||||
PATH_SEGMENT@22..25
|
||||
NAME_REF@22..25
|
||||
IDENT@22..25 "u32"
|
||||
COMMA@25..26 ","
|
||||
WHITESPACE@26..27 "\n"
|
||||
R_CURLY@27..28 "}"
|
|
@ -0,0 +1,3 @@
|
|||
struct Foo {
|
||||
bar: u32,
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
EXPR_STMT@0..5
|
||||
CALL_EXPR@0..5
|
||||
PATH_EXPR@0..3
|
||||
PATH@0..3
|
||||
PATH_SEGMENT@0..3
|
||||
NAME_REF@0..3
|
||||
IDENT@0..3 "foo"
|
||||
ARG_LIST@3..5
|
||||
L_PAREN@3..4 "("
|
||||
R_PAREN@4..5 ")"
|
|
@ -0,0 +1 @@
|
|||
foo()
|
|
@ -0,0 +1,11 @@
|
|||
LET_STMT@0..10
|
||||
LET_KW@0..3 "let"
|
||||
WHITESPACE@3..4 " "
|
||||
IDENT_PAT@4..5
|
||||
NAME@4..5
|
||||
IDENT@4..5 "x"
|
||||
WHITESPACE@5..6 " "
|
||||
EQ@6..7 "="
|
||||
WHITESPACE@7..8 " "
|
||||
LITERAL@8..10
|
||||
INT_NUMBER@8..10 "10"
|
|
@ -0,0 +1 @@
|
|||
let x = 10
|
Loading…
Reference in a new issue