From cd0d2866fc61e03aeca7bd7d1bc652e0443d2b3d Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 21 Feb 2019 12:12:04 +0300 Subject: make grammar independent of syntax tree --- crates/ra_syntax/src/parsing/grammar.rs | 26 +++++++++++++------------- crates/ra_syntax/src/parsing/reparsing.rs | 6 +++++- 2 files changed, 18 insertions(+), 14 deletions(-) (limited to 'crates') diff --git a/crates/ra_syntax/src/parsing/grammar.rs b/crates/ra_syntax/src/parsing/grammar.rs index 7ca9c223c..800d5a4a2 100644 --- a/crates/ra_syntax/src/parsing/grammar.rs +++ b/crates/ra_syntax/src/parsing/grammar.rs @@ -37,7 +37,6 @@ mod type_params; mod types; use crate::{ - SyntaxNode, SyntaxKind::{self, *}, parsing::{ token_set::TokenSet, @@ -52,8 +51,12 @@ pub(super) fn root(p: &mut Parser) { m.complete(p, SOURCE_FILE); } -pub(super) fn reparser(node: &SyntaxNode) -> Option { - let res = match node.kind() { +pub(super) fn reparser( + node: SyntaxKind, + first_child: Option, + parent: Option, +) -> Option { + let res = match node { BLOCK => expressions::block, NAMED_FIELD_DEF_LIST => items::named_field_def_list, NAMED_FIELD_LIST => items::named_field_list, @@ -61,16 +64,13 @@ pub(super) fn reparser(node: &SyntaxNode) -> Option { 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, - } - } + TOKEN_TREE if first_child? == L_CURLY => items::token_tree, + ITEM_LIST => match parent? { + IMPL_BLOCK => items::impl_item_list, + TRAIT_DEF => items::trait_item_list, + MODULE => items::mod_item_list, + _ => return None, + }, _ => return None, }; Some(res) diff --git a/crates/ra_syntax/src/parsing/reparsing.rs b/crates/ra_syntax/src/parsing/reparsing.rs index f4c2251d7..2c860b3df 100644 --- a/crates/ra_syntax/src/parsing/reparsing.rs +++ b/crates/ra_syntax/src/parsing/reparsing.rs @@ -83,7 +83,11 @@ fn find_reparsable_node( range: TextRange, ) -> Option<(&SyntaxNode, fn(&mut Parser))> { let node = algo::find_covering_node(node, range); - node.ancestors().find_map(|node| grammar::reparser(node).map(|r| (node, r))) + node.ancestors().find_map(|node| { + let first_child = node.first_child().map(|it| it.kind()); + let parent = node.parent().map(|it| it.kind()); + grammar::reparser(node.kind(), first_child, parent).map(|r| (node, r)) + }) } fn is_balanced(tokens: &[Token]) -> bool { -- cgit v1.2.3