make grammar independent of syntax tree

This commit is contained in:
Aleksey Kladov 2019-02-21 12:12:04 +03:00
parent 1b2e70df99
commit cd0d2866fc
2 changed files with 18 additions and 14 deletions

View File

@ -37,7 +37,6 @@ mod type_params;
mod types; mod types;
use crate::{ use crate::{
SyntaxNode,
SyntaxKind::{self, *}, SyntaxKind::{self, *},
parsing::{ parsing::{
token_set::TokenSet, token_set::TokenSet,
@ -52,8 +51,12 @@ pub(super) fn root(p: &mut Parser) {
m.complete(p, SOURCE_FILE); m.complete(p, SOURCE_FILE);
} }
pub(super) fn reparser(node: &SyntaxNode) -> Option<fn(&mut Parser)> { pub(super) fn reparser(
let res = match node.kind() { node: SyntaxKind,
first_child: Option<SyntaxKind>,
parent: Option<SyntaxKind>,
) -> Option<fn(&mut Parser)> {
let res = match node {
BLOCK => expressions::block, BLOCK => expressions::block,
NAMED_FIELD_DEF_LIST => items::named_field_def_list, NAMED_FIELD_DEF_LIST => items::named_field_def_list,
NAMED_FIELD_LIST => items::named_field_list, NAMED_FIELD_LIST => items::named_field_list,
@ -61,16 +64,13 @@ pub(super) fn reparser(node: &SyntaxNode) -> Option<fn(&mut Parser)> {
MATCH_ARM_LIST => items::match_arm_list, MATCH_ARM_LIST => items::match_arm_list,
USE_TREE_LIST => items::use_tree_list, USE_TREE_LIST => items::use_tree_list,
EXTERN_ITEM_LIST => items::extern_item_list, EXTERN_ITEM_LIST => items::extern_item_list,
TOKEN_TREE if node.first_child().unwrap().kind() == L_CURLY => items::token_tree, TOKEN_TREE if first_child? == L_CURLY => items::token_tree,
ITEM_LIST => { ITEM_LIST => match parent? {
let parent = node.parent().unwrap(); IMPL_BLOCK => items::impl_item_list,
match parent.kind() { TRAIT_DEF => items::trait_item_list,
IMPL_BLOCK => items::impl_item_list, MODULE => items::mod_item_list,
TRAIT_DEF => items::trait_item_list, _ => return None,
MODULE => items::mod_item_list, },
_ => return None,
}
}
_ => return None, _ => return None,
}; };
Some(res) Some(res)

View File

@ -83,7 +83,11 @@ fn find_reparsable_node(
range: TextRange, range: TextRange,
) -> Option<(&SyntaxNode, fn(&mut Parser))> { ) -> Option<(&SyntaxNode, fn(&mut Parser))> {
let node = algo::find_covering_node(node, range); 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 { fn is_balanced(tokens: &[Token]) -> bool {