From a450142aca947b9364e498897f522f854f19781d Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 26 Aug 2018 09:12:18 +0300 Subject: fix stray curly --- crates/libsyntax2/src/grammar/items/mod.rs | 7 +++- crates/libsyntax2/src/lib.rs | 4 +- crates/libsyntax2/src/parser_api.rs | 4 +- .../data/parser/err/0007_stray_curly_in_file.txt | 6 +-- .../tests/data/parser/err/0015_curly_in_params.txt | 2 +- .../data/parser/err/0017_incomplete_binexpr.rs | 4 ++ .../data/parser/err/0017_incomplete_binexpr.txt | 47 ++++++++++++++++++++++ crates/libsyntax2/tests/test/main.rs | 1 + 8 files changed, 67 insertions(+), 8 deletions(-) create mode 100644 crates/libsyntax2/tests/data/parser/err/0017_incomplete_binexpr.rs create mode 100644 crates/libsyntax2/tests/data/parser/err/0017_incomplete_binexpr.txt (limited to 'crates/libsyntax2') diff --git a/crates/libsyntax2/src/grammar/items/mod.rs b/crates/libsyntax2/src/grammar/items/mod.rs index 32d0778c4..a285892df 100644 --- a/crates/libsyntax2/src/grammar/items/mod.rs +++ b/crates/libsyntax2/src/grammar/items/mod.rs @@ -43,7 +43,12 @@ pub(super) fn item_or_macro(p: &mut Parser, stop_on_r_curly: bool, flavor: ItemF m.abandon(p); if p.at(L_CURLY) { error_block(p, "expected an item"); - } else if !p.at(EOF) && !(stop_on_r_curly && p.at(R_CURLY)) { + } else if p.at(R_CURLY) && !stop_on_r_curly { + let e = p.start(); + p.error("unmatched `}`"); + p.bump(); + e.complete(p, ERROR); + } else if !p.at(EOF) && !p.at(R_CURLY) { p.err_and_bump("expected an item"); } else { p.error("expected an item"); diff --git a/crates/libsyntax2/src/lib.rs b/crates/libsyntax2/src/lib.rs index 9ba9970c9..93057dd6a 100644 --- a/crates/libsyntax2/src/lib.rs +++ b/crates/libsyntax2/src/lib.rs @@ -127,12 +127,12 @@ fn validate_block_structure(root: SyntaxNodeRef) { assert_eq!( node.parent(), pair.parent(), - "unpaired curleys:\n{}", + "\nunpaired curleys:\n{}", utils::dump_tree(root), ); assert!( node.next_sibling().is_none() && pair.prev_sibling().is_none(), - "floating curlys at {:?}\nfile:\n{}\nerror:\n{}\n", + "\nfloating curlys at {:?}\nfile:\n{}\nerror:\n{}\n", node, root.text(), node.text(), diff --git a/crates/libsyntax2/src/parser_api.rs b/crates/libsyntax2/src/parser_api.rs index bb34fe973..0a3b29b70 100644 --- a/crates/libsyntax2/src/parser_api.rs +++ b/crates/libsyntax2/src/parser_api.rs @@ -141,7 +141,9 @@ impl<'t> Parser<'t> { pub(crate) fn err_and_bump(&mut self, message: &str) { let m = self.start(); self.error(message); - self.bump(); + if !self.at(SyntaxKind::L_CURLY) && !self.at(SyntaxKind::R_CURLY) { + self.bump(); + } m.complete(self, ERROR); } } diff --git a/crates/libsyntax2/tests/data/parser/err/0007_stray_curly_in_file.txt b/crates/libsyntax2/tests/data/parser/err/0007_stray_curly_in_file.txt index 81e82f7e2..802c69b31 100644 --- a/crates/libsyntax2/tests/data/parser/err/0007_stray_curly_in_file.txt +++ b/crates/libsyntax2/tests/data/parser/err/0007_stray_curly_in_file.txt @@ -1,7 +1,7 @@ ROOT@[0; 31) ERROR@[0; 1) R_CURLY@[0; 1) - err: `expected an item` + err: `unmatched `}`` WHITESPACE@[1; 3) STRUCT_DEF@[3; 12) STRUCT_KW@[3; 9) @@ -10,7 +10,7 @@ ROOT@[0; 31) IDENT@[10; 11) "S" SEMI@[11; 12) WHITESPACE@[12; 14) - err: `expected an item` + err: `unmatched `}`` ERROR@[14; 15) R_CURLY@[14; 15) WHITESPACE@[15; 17) @@ -26,7 +26,7 @@ ROOT@[0; 31) L_CURLY@[25; 26) R_CURLY@[26; 27) WHITESPACE@[27; 29) - err: `expected an item` + err: `unmatched `}`` ERROR@[29; 30) R_CURLY@[29; 30) WHITESPACE@[30; 31) diff --git a/crates/libsyntax2/tests/data/parser/err/0015_curly_in_params.txt b/crates/libsyntax2/tests/data/parser/err/0015_curly_in_params.txt index dbc19abea..5f736a978 100644 --- a/crates/libsyntax2/tests/data/parser/err/0015_curly_in_params.txt +++ b/crates/libsyntax2/tests/data/parser/err/0015_curly_in_params.txt @@ -9,7 +9,7 @@ ROOT@[0; 14) err: `expected value parameter` err: `expected R_PAREN` err: `expected a block` - err: `expected an item` + err: `unmatched `}`` ERROR@[7; 8) R_CURLY@[7; 8) err: `expected an item` diff --git a/crates/libsyntax2/tests/data/parser/err/0017_incomplete_binexpr.rs b/crates/libsyntax2/tests/data/parser/err/0017_incomplete_binexpr.rs new file mode 100644 index 000000000..17bd49777 --- /dev/null +++ b/crates/libsyntax2/tests/data/parser/err/0017_incomplete_binexpr.rs @@ -0,0 +1,4 @@ +fn foo(foo: i32) { + let bar = 92; + 1 + +} diff --git a/crates/libsyntax2/tests/data/parser/err/0017_incomplete_binexpr.txt b/crates/libsyntax2/tests/data/parser/err/0017_incomplete_binexpr.txt new file mode 100644 index 000000000..db9a2f175 --- /dev/null +++ b/crates/libsyntax2/tests/data/parser/err/0017_incomplete_binexpr.txt @@ -0,0 +1,47 @@ +ROOT@[0; 47) + FN_DEF@[0; 46) + FN_KW@[0; 2) + WHITESPACE@[2; 3) + NAME@[3; 6) + IDENT@[3; 6) "foo" + PARAM_LIST@[6; 16) + L_PAREN@[6; 7) + PARAM@[7; 15) + BIND_PAT@[7; 10) + NAME@[7; 10) + IDENT@[7; 10) "foo" + COLON@[10; 11) + WHITESPACE@[11; 12) + PATH_TYPE@[12; 15) + PATH@[12; 15) + PATH_SEGMENT@[12; 15) + NAME_REF@[12; 15) + IDENT@[12; 15) "i32" + R_PAREN@[15; 16) + WHITESPACE@[16; 17) + BLOCK@[17; 46) + L_CURLY@[17; 18) + WHITESPACE@[18; 23) + LET_STMT@[23; 36) + LET_KW@[23; 26) + WHITESPACE@[26; 27) + BIND_PAT@[27; 30) + NAME@[27; 30) + IDENT@[27; 30) "bar" + WHITESPACE@[30; 31) + EQ@[31; 32) + WHITESPACE@[32; 33) + LITERAL@[33; 35) + INT_NUMBER@[33; 35) "92" + SEMI@[35; 36) + WHITESPACE@[36; 41) + BIN_EXPR@[41; 45) + LITERAL@[41; 42) + INT_NUMBER@[41; 42) "1" + WHITESPACE@[42; 43) + PLUS@[43; 44) + WHITESPACE@[44; 45) + err: `expected expression` + ERROR@[45; 45) + R_CURLY@[45; 46) + WHITESPACE@[46; 47) diff --git a/crates/libsyntax2/tests/test/main.rs b/crates/libsyntax2/tests/test/main.rs index 596f32216..9958c7ece 100644 --- a/crates/libsyntax2/tests/test/main.rs +++ b/crates/libsyntax2/tests/test/main.rs @@ -26,6 +26,7 @@ fn lexer_tests() { #[test] fn parser_tests() { dir_tests(&["parser/inline", "parser/ok", "parser/err"], |text| { + eprintln!("\n{}\n", text); let file = File::parse(text); dump_tree(file.syntax()) }) -- cgit v1.2.3