From b2be998b7b0beb48806cd3a1552e44100b12ddb6 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 5 Sep 2019 19:53:07 +0300 Subject: better error recovery for use trees --- crates/ra_parser/src/grammar/items.rs | 2 +- crates/ra_parser/src/grammar/items/use_item.rs | 5 +- .../parser/err/0002_duplicate_shebang.txt | 2 +- .../test_data/parser/err/0035_use_recover.rs | 5 ++ .../test_data/parser/err/0035_use_recover.txt | 54 ++++++++++++++++++++++ 5 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 crates/ra_syntax/test_data/parser/err/0035_use_recover.rs create mode 100644 crates/ra_syntax/test_data/parser/err/0035_use_recover.txt diff --git a/crates/ra_parser/src/grammar/items.rs b/crates/ra_parser/src/grammar/items.rs index 6d426206e..b4327b78f 100644 --- a/crates/ra_parser/src/grammar/items.rs +++ b/crates/ra_parser/src/grammar/items.rs @@ -31,7 +31,7 @@ pub(super) enum ItemFlavor { pub(super) const ITEM_RECOVERY_SET: TokenSet = token_set![ FN_KW, STRUCT_KW, ENUM_KW, IMPL_KW, TRAIT_KW, CONST_KW, STATIC_KW, LET_KW, MOD_KW, PUB_KW, - CRATE_KW + CRATE_KW, USE_KW ]; pub(super) fn item_or_macro(p: &mut Parser, stop_on_r_curly: bool, flavor: ItemFlavor) { diff --git a/crates/ra_parser/src/grammar/items/use_item.rs b/crates/ra_parser/src/grammar/items/use_item.rs index c0c7d0ec6..83a65e226 100644 --- a/crates/ra_parser/src/grammar/items/use_item.rs +++ b/crates/ra_parser/src/grammar/items/use_item.rs @@ -101,7 +101,10 @@ fn use_tree(p: &mut Parser) { } _ => { m.abandon(p); - p.err_and_bump("expected one of `*`, `::`, `{`, `self`, `super` or an indentifier"); + p.err_recover( + "expected one of `*`, `::`, `{`, `self`, `super` or an identifier", + ITEM_RECOVERY_SET, + ); return; } } diff --git a/crates/ra_syntax/test_data/parser/err/0002_duplicate_shebang.txt b/crates/ra_syntax/test_data/parser/err/0002_duplicate_shebang.txt index 84867026f..bdb5fa6c5 100644 --- a/crates/ra_syntax/test_data/parser/err/0002_duplicate_shebang.txt +++ b/crates/ra_syntax/test_data/parser/err/0002_duplicate_shebang.txt @@ -28,7 +28,7 @@ SOURCE_FILE@[0; 42) WHITESPACE@[41; 42) "\n" error 23: expected `[` error 23: expected an item -error 27: expected one of `*`, `::`, `{`, `self`, `super` or an indentifier +error 27: expected one of `*`, `::`, `{`, `self`, `super` or an identifier error 28: expected SEMI error 31: expected EXCL error 31: expected `{`, `[`, `(` diff --git a/crates/ra_syntax/test_data/parser/err/0035_use_recover.rs b/crates/ra_syntax/test_data/parser/err/0035_use_recover.rs new file mode 100644 index 000000000..4a2668126 --- /dev/null +++ b/crates/ra_syntax/test_data/parser/err/0035_use_recover.rs @@ -0,0 +1,5 @@ +use foo::bar; +use +use crate::baz; +use +fn f() {} diff --git a/crates/ra_syntax/test_data/parser/err/0035_use_recover.txt b/crates/ra_syntax/test_data/parser/err/0035_use_recover.txt new file mode 100644 index 000000000..636840828 --- /dev/null +++ b/crates/ra_syntax/test_data/parser/err/0035_use_recover.txt @@ -0,0 +1,54 @@ +SOURCE_FILE@[0; 48) + USE_ITEM@[0; 13) + USE_KW@[0; 3) "use" + WHITESPACE@[3; 4) " " + USE_TREE@[4; 12) + PATH@[4; 12) + PATH@[4; 7) + PATH_SEGMENT@[4; 7) + NAME_REF@[4; 7) + IDENT@[4; 7) "foo" + COLONCOLON@[7; 9) "::" + PATH_SEGMENT@[9; 12) + NAME_REF@[9; 12) + IDENT@[9; 12) "bar" + SEMI@[12; 13) ";" + WHITESPACE@[13; 14) "\n" + USE_ITEM@[14; 17) + USE_KW@[14; 17) "use" + WHITESPACE@[17; 18) "\n" + USE_ITEM@[18; 33) + USE_KW@[18; 21) "use" + WHITESPACE@[21; 22) " " + USE_TREE@[22; 32) + PATH@[22; 32) + PATH@[22; 27) + PATH_SEGMENT@[22; 27) + CRATE_KW@[22; 27) "crate" + COLONCOLON@[27; 29) "::" + PATH_SEGMENT@[29; 32) + NAME_REF@[29; 32) + IDENT@[29; 32) "baz" + SEMI@[32; 33) ";" + WHITESPACE@[33; 34) "\n" + USE_ITEM@[34; 37) + USE_KW@[34; 37) "use" + WHITESPACE@[37; 38) "\n" + FN_DEF@[38; 47) + FN_KW@[38; 40) "fn" + WHITESPACE@[40; 41) " " + NAME@[41; 42) + IDENT@[41; 42) "f" + PARAM_LIST@[42; 44) + L_PAREN@[42; 43) "(" + R_PAREN@[43; 44) ")" + WHITESPACE@[44; 45) " " + BLOCK_EXPR@[45; 47) + BLOCK@[45; 47) + L_CURLY@[45; 46) "{" + R_CURLY@[46; 47) "}" + WHITESPACE@[47; 48) "\n" +error 17: expected one of `*`, `::`, `{`, `self`, `super` or an identifier +error 17: expected SEMI +error 37: expected one of `*`, `::`, `{`, `self`, `super` or an identifier +error 37: expected SEMI -- cgit v1.2.3