From dea6ed73fac6f3f4daf38b1cb6df4c8fb68872b7 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 28 Aug 2018 19:35:09 +0300 Subject: better pattern recovery --- crates/libeditor/src/scope/mod_scope.rs | 2 + crates/libsyntax2/src/ast/generated.rs | 6 ++ crates/libsyntax2/src/grammar.ron | 2 +- crates/libsyntax2/src/grammar/patterns.rs | 6 +- .../tests/data/parser/err/0019_let_recover.rs | 8 +++ .../tests/data/parser/err/0019_let_recover.txt | 76 ++++++++++++++++++++-- 6 files changed, 92 insertions(+), 8 deletions(-) diff --git a/crates/libeditor/src/scope/mod_scope.rs b/crates/libeditor/src/scope/mod_scope.rs index 0e51108d9..aa8dd93a7 100644 --- a/crates/libeditor/src/scope/mod_scope.rs +++ b/crates/libeditor/src/scope/mod_scope.rs @@ -13,6 +13,8 @@ impl ModuleScope { ast::ModuleItem::StructDef(item) => Entry::new(item), ast::ModuleItem::EnumDef(item) => Entry::new(item), ast::ModuleItem::FnDef(item) => Entry::new(item), + ast::ModuleItem::ConstDef(item) => Entry::new(item), + ast::ModuleItem::StaticDef(item) => Entry::new(item), ast::ModuleItem::TraitDef(item) => Entry::new(item), ast::ModuleItem::ExternCrateItem(_) | ast::ModuleItem::ImplItem(_) | diff --git a/crates/libsyntax2/src/ast/generated.rs b/crates/libsyntax2/src/ast/generated.rs index 2b400b847..999023e3d 100644 --- a/crates/libsyntax2/src/ast/generated.rs +++ b/crates/libsyntax2/src/ast/generated.rs @@ -893,6 +893,8 @@ pub enum ModuleItem<'a> { ImplItem(ImplItem<'a>), UseItem(UseItem<'a>), ExternCrateItem(ExternCrateItem<'a>), + ConstDef(ConstDef<'a>), + StaticDef(StaticDef<'a>), } impl<'a> AstNode<'a> for ModuleItem<'a> { @@ -905,6 +907,8 @@ impl<'a> AstNode<'a> for ModuleItem<'a> { IMPL_ITEM => Some(ModuleItem::ImplItem(ImplItem { syntax })), USE_ITEM => Some(ModuleItem::UseItem(UseItem { syntax })), EXTERN_CRATE_ITEM => Some(ModuleItem::ExternCrateItem(ExternCrateItem { syntax })), + CONST_DEF => Some(ModuleItem::ConstDef(ConstDef { syntax })), + STATIC_DEF => Some(ModuleItem::StaticDef(StaticDef { syntax })), _ => None, } } @@ -917,6 +921,8 @@ impl<'a> AstNode<'a> for ModuleItem<'a> { ModuleItem::ImplItem(inner) => inner.syntax(), ModuleItem::UseItem(inner) => inner.syntax(), ModuleItem::ExternCrateItem(inner) => inner.syntax(), + ModuleItem::ConstDef(inner) => inner.syntax(), + ModuleItem::StaticDef(inner) => inner.syntax(), } } } diff --git a/crates/libsyntax2/src/grammar.ron b/crates/libsyntax2/src/grammar.ron index 8055a4687..f8fa7b694 100644 --- a/crates/libsyntax2/src/grammar.ron +++ b/crates/libsyntax2/src/grammar.ron @@ -334,7 +334,7 @@ Grammar( ), "ModuleItem": ( enum: ["StructDef", "EnumDef", "FnDef", "TraitDef", "ImplItem", - "UseItem", "ExternCrateItem" ] + "UseItem", "ExternCrateItem", "ConstDef", "StaticDef" ] ), "TupleExpr": (), diff --git a/crates/libsyntax2/src/grammar/patterns.rs b/crates/libsyntax2/src/grammar/patterns.rs index 114964651..065570b99 100644 --- a/crates/libsyntax2/src/grammar/patterns.rs +++ b/crates/libsyntax2/src/grammar/patterns.rs @@ -22,6 +22,10 @@ pub(super) fn pattern(p: &mut Parser) { } } +const PAT_RECOVERY_SET: TokenSet = + token_set![LET_KW, IF_KW, WHILE_KW, LOOP_KW, MATCH_KW]; + + fn atom_pat(p: &mut Parser) -> Option { let la0 = p.nth(0); let la1 = p.nth(1); @@ -52,7 +56,7 @@ fn atom_pat(p: &mut Parser) -> Option { L_PAREN => tuple_pat(p), L_BRACK => slice_pat(p), _ => { - p.err_and_bump("expected pattern"); + p.err_recover("expected pattern", PAT_RECOVERY_SET); return None; } }; diff --git a/crates/libsyntax2/tests/data/parser/err/0019_let_recover.rs b/crates/libsyntax2/tests/data/parser/err/0019_let_recover.rs index 52bddd494..48bf3d68b 100644 --- a/crates/libsyntax2/tests/data/parser/err/0019_let_recover.rs +++ b/crates/libsyntax2/tests/data/parser/err/0019_let_recover.rs @@ -1,4 +1,12 @@ fn foo() { let foo = let bar = 1; + let + let baz = 92; + let + if true {} + let + while true {} + let + loop {} } diff --git a/crates/libsyntax2/tests/data/parser/err/0019_let_recover.txt b/crates/libsyntax2/tests/data/parser/err/0019_let_recover.txt index 9ffa9d781..6e74771f1 100644 --- a/crates/libsyntax2/tests/data/parser/err/0019_let_recover.txt +++ b/crates/libsyntax2/tests/data/parser/err/0019_let_recover.txt @@ -1,5 +1,5 @@ -ROOT@[0; 44) - FN_DEF@[0; 43) +ROOT@[0; 139) + FN_DEF@[0; 138) FN_KW@[0; 2) WHITESPACE@[2; 3) NAME@[3; 6) @@ -8,7 +8,7 @@ ROOT@[0; 44) L_PAREN@[6; 7) R_PAREN@[7; 8) WHITESPACE@[8; 9) - BLOCK@[9; 43) + BLOCK@[9; 138) L_CURLY@[9; 10) WHITESPACE@[10; 15) LET_STMT@[15; 24) @@ -34,6 +34,70 @@ ROOT@[0; 44) LITERAL@[39; 40) INT_NUMBER@[39; 40) "1" SEMI@[40; 41) - WHITESPACE@[41; 42) - R_CURLY@[42; 43) - WHITESPACE@[43; 44) + WHITESPACE@[41; 46) + LET_STMT@[46; 49) + LET_KW@[46; 49) + err: `expected pattern` + err: `expected SEMI` + WHITESPACE@[49; 54) + LET_STMT@[54; 67) + LET_KW@[54; 57) + WHITESPACE@[57; 58) + BIND_PAT@[58; 61) + NAME@[58; 61) + IDENT@[58; 61) "baz" + WHITESPACE@[61; 62) + EQ@[62; 63) + WHITESPACE@[63; 64) + LITERAL@[64; 66) + INT_NUMBER@[64; 66) "92" + SEMI@[66; 67) + WHITESPACE@[67; 72) + LET_STMT@[72; 75) + LET_KW@[72; 75) + err: `expected pattern` + err: `expected SEMI` + WHITESPACE@[75; 80) + EXPR_STMT@[80; 90) + IF_EXPR@[80; 90) + IF_KW@[80; 82) + WHITESPACE@[82; 83) + CONDITION@[83; 87) + LITERAL@[83; 87) + TRUE_KW@[83; 87) + WHITESPACE@[87; 88) + BLOCK@[88; 90) + L_CURLY@[88; 89) + R_CURLY@[89; 90) + WHITESPACE@[90; 95) + LET_STMT@[95; 98) + LET_KW@[95; 98) + err: `expected pattern` + err: `expected SEMI` + WHITESPACE@[98; 103) + EXPR_STMT@[103; 116) + WHILE_EXPR@[103; 116) + WHILE_KW@[103; 108) + WHITESPACE@[108; 109) + CONDITION@[109; 113) + LITERAL@[109; 113) + TRUE_KW@[109; 113) + WHITESPACE@[113; 114) + BLOCK@[114; 116) + L_CURLY@[114; 115) + R_CURLY@[115; 116) + WHITESPACE@[116; 121) + LET_STMT@[121; 124) + LET_KW@[121; 124) + err: `expected pattern` + err: `expected SEMI` + WHITESPACE@[124; 129) + LOOP_EXPR@[129; 136) + LOOP_KW@[129; 133) + WHITESPACE@[133; 134) + BLOCK@[134; 136) + L_CURLY@[134; 135) + R_CURLY@[135; 136) + WHITESPACE@[136; 137) + R_CURLY@[137; 138) + WHITESPACE@[138; 139) -- cgit v1.2.3