From 64d07c1bd475c4945db8d7cd1fa1a61e467b079b Mon Sep 17 00:00:00 2001 From: darksv Date: Mon, 10 Sep 2018 20:14:09 +0200 Subject: Implement reparsing for remaining blocks --- crates/libsyntax2/src/grammar/expressions/atom.rs | 2 +- crates/libsyntax2/src/grammar/expressions/mod.rs | 3 +- crates/libsyntax2/src/grammar/items/mod.rs | 15 ++++-- crates/libsyntax2/src/grammar/items/nominal.rs | 2 +- crates/libsyntax2/src/grammar/items/traits.rs | 4 +- crates/libsyntax2/src/grammar/items/use_item.rs | 2 +- crates/libsyntax2/src/grammar/mod.rs | 17 ++++++- crates/libsyntax2/src/lib.rs | 15 ++++++ crates/libsyntax2/tests/test/main.rs | 61 +++++++++++++++++------ 9 files changed, 93 insertions(+), 28 deletions(-) (limited to 'crates') diff --git a/crates/libsyntax2/src/grammar/expressions/atom.rs b/crates/libsyntax2/src/grammar/expressions/atom.rs index 8335c700f..f01df56bc 100644 --- a/crates/libsyntax2/src/grammar/expressions/atom.rs +++ b/crates/libsyntax2/src/grammar/expressions/atom.rs @@ -287,7 +287,7 @@ fn match_expr(p: &mut Parser) -> CompletedMarker { m.complete(p, MATCH_EXPR) } -fn match_arm_list(p: &mut Parser) { +pub(crate) fn match_arm_list(p: &mut Parser) { assert!(p.at(L_CURLY)); let m = p.start(); p.eat(L_CURLY); diff --git a/crates/libsyntax2/src/grammar/expressions/mod.rs b/crates/libsyntax2/src/grammar/expressions/mod.rs index d5ee91ad8..20e0fa328 100644 --- a/crates/libsyntax2/src/grammar/expressions/mod.rs +++ b/crates/libsyntax2/src/grammar/expressions/mod.rs @@ -2,6 +2,7 @@ mod atom; use super::*; pub(super) use self::atom::{literal, LITERAL_FIRST}; +pub(crate) use self::atom::match_arm_list; const EXPR_FIRST: TokenSet = LHS_FIRST; @@ -419,7 +420,7 @@ fn path_expr(p: &mut Parser, r: Restrictions) -> CompletedMarker { // S { x, y: 32, }; // S { x, y: 32, ..Default::default() }; // } -fn named_field_list(p: &mut Parser) { +pub(crate) fn named_field_list(p: &mut Parser) { assert!(p.at(L_CURLY)); let m = p.start(); p.bump(); diff --git a/crates/libsyntax2/src/grammar/items/mod.rs b/crates/libsyntax2/src/grammar/items/mod.rs index 85d7fe770..8c19aa179 100644 --- a/crates/libsyntax2/src/grammar/items/mod.rs +++ b/crates/libsyntax2/src/grammar/items/mod.rs @@ -5,7 +5,12 @@ mod traits; mod use_item; use super::*; -pub(crate) use self::nominal::named_field_def_list; +pub(crate) use self::{ + expressions::{named_field_list, match_arm_list}, + nominal::{enum_variant_list, named_field_def_list}, + traits::{trait_item_list, impl_item_list}, + use_item::use_tree_list, +}; // test mod_contents // fn foo() {} @@ -223,7 +228,7 @@ fn extern_crate_item(p: &mut Parser) { p.expect(SEMI); } -fn extern_item_list(p: &mut Parser) { +pub(crate) fn extern_item_list(p: &mut Parser) { assert!(p.at(L_CURLY)); let m = p.start(); p.bump(); @@ -295,7 +300,7 @@ fn type_def(p: &mut Parser) { p.expect(SEMI); } -fn mod_item(p: &mut Parser) { +pub(crate) fn mod_item(p: &mut Parser) { assert!(p.at(MOD_KW)); p.bump(); @@ -307,7 +312,7 @@ fn mod_item(p: &mut Parser) { } } -fn mod_item_list(p: &mut Parser) { +pub(crate) fn mod_item_list(p: &mut Parser) { assert!(p.at(L_CURLY)); let m = p.start(); p.bump(); @@ -343,7 +348,7 @@ pub(super) fn macro_call_after_excl(p: &mut Parser) -> BlockLike { flavor } -pub(super) fn token_tree(p: &mut Parser) { +pub(crate) fn token_tree(p: &mut Parser) { let closing_paren_kind = match p.current() { L_CURLY => R_CURLY, L_PAREN => R_PAREN, diff --git a/crates/libsyntax2/src/grammar/items/nominal.rs b/crates/libsyntax2/src/grammar/items/nominal.rs index 3db5b24af..11c43e371 100644 --- a/crates/libsyntax2/src/grammar/items/nominal.rs +++ b/crates/libsyntax2/src/grammar/items/nominal.rs @@ -51,7 +51,7 @@ pub(super) fn enum_def(p: &mut Parser) { } } -fn enum_variant_list(p: &mut Parser) { +pub(crate) fn enum_variant_list(p: &mut Parser) { assert!(p.at(L_CURLY)); let m = p.start(); p.bump(); diff --git a/crates/libsyntax2/src/grammar/items/traits.rs b/crates/libsyntax2/src/grammar/items/traits.rs index 9d21d4d36..c21cfb1a9 100644 --- a/crates/libsyntax2/src/grammar/items/traits.rs +++ b/crates/libsyntax2/src/grammar/items/traits.rs @@ -25,7 +25,7 @@ pub(super) fn trait_def(p: &mut Parser) { // fn foo() {} // fn bar(&self); // } -fn trait_item_list(p: &mut Parser) { +pub(crate) fn trait_item_list(p: &mut Parser) { assert!(p.at(L_CURLY)); let m = p.start(); p.bump(); @@ -74,7 +74,7 @@ pub(super) fn impl_item(p: &mut Parser) { // fn foo() {} // fn bar(&self) {} // } -fn impl_item_list(p: &mut Parser) { +pub(crate) fn impl_item_list(p: &mut Parser) { assert!(p.at(L_CURLY)); let m = p.start(); p.bump(); diff --git a/crates/libsyntax2/src/grammar/items/use_item.rs b/crates/libsyntax2/src/grammar/items/use_item.rs index 2fbf2234a..1ee4349fd 100644 --- a/crates/libsyntax2/src/grammar/items/use_item.rs +++ b/crates/libsyntax2/src/grammar/items/use_item.rs @@ -53,7 +53,7 @@ fn use_tree(p: &mut Parser) { m.complete(p, USE_TREE); } -fn use_tree_list(p: &mut Parser) { +pub(crate) fn use_tree_list(p: &mut Parser) { assert!(p.at(L_CURLY)); let m = p.start(); p.bump(); diff --git a/crates/libsyntax2/src/grammar/mod.rs b/crates/libsyntax2/src/grammar/mod.rs index e19805b9d..2cb11dc1e 100644 --- a/crates/libsyntax2/src/grammar/mod.rs +++ b/crates/libsyntax2/src/grammar/mod.rs @@ -37,8 +37,21 @@ use { SyntaxKind::{self, *}, }; pub(crate) use self::{ - expressions::block, - items::named_field_def_list, + expressions::{ + block, + }, + items::{ + enum_variant_list, + extern_item_list, + impl_item_list, + match_arm_list, + mod_item_list, + named_field_def_list, + named_field_list, + token_tree, + trait_item_list, + use_tree_list, + }, }; pub(crate) fn root(p: &mut Parser) { diff --git a/crates/libsyntax2/src/lib.rs b/crates/libsyntax2/src/lib.rs index d955c01e7..74e8e7338 100644 --- a/crates/libsyntax2/src/lib.rs +++ b/crates/libsyntax2/src/lib.rs @@ -144,6 +144,21 @@ fn find_reparsable_node(node: SyntaxNodeRef, range: TextRange) -> Option<(Syntax let res = match node.kind() { BLOCK => grammar::block, NAMED_FIELD_DEF_LIST => grammar::named_field_def_list, + NAMED_FIELD_LIST => grammar::named_field_list, + ENUM_VARIANT_LIST => grammar::enum_variant_list, + MATCH_ARM_LIST => grammar::match_arm_list, + USE_TREE_LIST => grammar::use_tree_list, + EXTERN_ITEM_LIST => grammar::extern_item_list, + TOKEN_TREE => grammar::token_tree, + ITEM_LIST => { + let parent = node.parent().unwrap(); + match parent.kind() { + IMPL_ITEM => grammar::impl_item_list, + TRAIT_DEF => grammar::trait_item_list, + MODULE => grammar::mod_item_list, + _ => return None, + } + }, _ => return None, }; Some(res) diff --git a/crates/libsyntax2/tests/test/main.rs b/crates/libsyntax2/tests/test/main.rs index 014faa2c6..24058efa9 100644 --- a/crates/libsyntax2/tests/test/main.rs +++ b/crates/libsyntax2/tests/test/main.rs @@ -23,21 +23,6 @@ fn lexer_tests() { }) } -#[test] -fn parser_tests() { - dir_tests(&["parser/inline", "parser/ok", "parser/err"], |text| { - let file = File::parse(text); - dump_tree(file.syntax()) - }) -} - -#[test] -fn parser_fuzz_tests() { - for (_, text) in collect_tests(&["parser/fuzz-failures"]) { - check_fuzz_invariants(&text) - } -} - #[test] fn reparse_test() { fn do_check(before: &str, replace_with: &str) { @@ -73,6 +58,52 @@ fn foo { <|>92<|>; } ", "62"); + do_check(r" +mod foo { + fn <|><|> +} +", "bar"); + do_check(r" +trait Foo { + type <|>Foo<|>; +} +", "Output"); + do_check(r" +impl IntoIterator for Foo { + f<|><|> +} +", "n next("); + do_check(r" +use a::b::{foo,<|>,bar<|>}; + ", "baz"); + do_check(r" +pub enum A { + Foo<|><|> +} +", "\nBar;\n"); + do_check(r" +foo!{a, b<|><|> d} +", ", c[3]"); + do_check(r" +extern { + fn<|>;<|> +} +", " exit(code: c_int)"); +} + +#[test] +fn parser_tests() { + dir_tests(&["parser/inline", "parser/ok", "parser/err"], |text| { + let file = File::parse(text); + dump_tree(file.syntax()) + }) +} + +#[test] +fn parser_fuzz_tests() { + for (_, text) in collect_tests(&["parser/fuzz-failures"]) { + check_fuzz_invariants(&text) + } } -- cgit v1.2.3