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;
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<fn(&mut Parser)> {
let res = match node.kind() {
pub(super) fn reparser(
node: SyntaxKind,
first_child: Option<SyntaxKind>,
parent: Option<SyntaxKind>,
) -> Option<fn(&mut Parser)> {
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<fn(&mut Parser)> {
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() {
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)

View file

@ -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 {