From 9f6883fbf10567e4177dbe77857c970b6dc61f48 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 20 Feb 2019 16:02:06 +0300 Subject: move reparsers to grammar --- crates/ra_syntax/src/parsing/grammar.rs | 34 +++++++++++++++++++++++-------- crates/ra_syntax/src/parsing/reparsing.rs | 26 +---------------------- 2 files changed, 27 insertions(+), 33 deletions(-) diff --git a/crates/ra_syntax/src/parsing/grammar.rs b/crates/ra_syntax/src/parsing/grammar.rs index bf86443de..bcdcd9f57 100644 --- a/crates/ra_syntax/src/parsing/grammar.rs +++ b/crates/ra_syntax/src/parsing/grammar.rs @@ -36,14 +36,8 @@ mod type_args; mod type_params; mod types; -pub(crate) use self::{ - 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, - }, -}; use crate::{ + SyntaxNode, SyntaxKind::{self, *}, parsing::{ token_set::TokenSet, @@ -51,13 +45,37 @@ use crate::{ }, }; -pub(crate) fn root(p: &mut Parser) { +pub(super) fn root(p: &mut Parser) { let m = p.start(); p.eat(SHEBANG); items::mod_contents(p, false); m.complete(p, SOURCE_FILE); } +pub(super) fn reparser(node: &SyntaxNode) -> Option { + let res = match node.kind() { + BLOCK => expressions::block, + NAMED_FIELD_DEF_LIST => items::named_field_def_list, + NAMED_FIELD_LIST => items::named_field_list, + ENUM_VARIANT_LIST => items::enum_variant_list, + MATCH_ARM_LIST => items::match_arm_list, + USE_TREE_LIST => items::use_tree_list, + EXTERN_ITEM_LIST => items::extern_item_list, + TOKEN_TREE if node.first_child().unwrap().kind() == L_CURLY => items::token_tree, + ITEM_LIST => { + let parent = node.parent().unwrap(); + match parent.kind() { + IMPL_BLOCK => items::impl_item_list, + TRAIT_DEF => items::trait_item_list, + MODULE => items::mod_item_list, + _ => return None, + } + } + _ => return None, + }; + Some(res) +} + #[derive(Clone, Copy, PartialEq, Eq)] enum BlockLike { Block, diff --git a/crates/ra_syntax/src/parsing/reparsing.rs b/crates/ra_syntax/src/parsing/reparsing.rs index 994e7e212..0a24dae0e 100644 --- a/crates/ra_syntax/src/parsing/reparsing.rs +++ b/crates/ra_syntax/src/parsing/reparsing.rs @@ -81,31 +81,7 @@ fn is_contextual_kw(text: &str) -> bool { type ParseFn = fn(&mut Parser); fn find_reparsable_node(node: &SyntaxNode, range: TextRange) -> Option<(&SyntaxNode, ParseFn)> { let node = algo::find_covering_node(node, range); - return node.ancestors().filter_map(|node| reparser(node).map(|r| (node, r))).next(); - - fn reparser(node: &SyntaxNode) -> Option { - 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 if node.first_child().unwrap().kind() == L_CURLY => grammar::token_tree, - ITEM_LIST => { - let parent = node.parent().unwrap(); - match parent.kind() { - IMPL_BLOCK => grammar::impl_item_list, - TRAIT_DEF => grammar::trait_item_list, - MODULE => grammar::mod_item_list, - _ => return None, - } - } - _ => return None, - }; - Some(res) - } + node.ancestors().find_map(grammar::reparser).map(|r| (node, r)) } fn is_balanced(tokens: &[Token]) -> bool { -- cgit v1.2.3