From a6224f36200c768d49b6450204fd95edaa559b50 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 18 Jul 2019 22:29:20 +0300 Subject: make Parse generic --- crates/ra_syntax/src/lib.rs | 47 +++++++++++++++++++++++++++++++-------------- 1 file changed, 33 insertions(+), 14 deletions(-) (limited to 'crates/ra_syntax/src/lib.rs') diff --git a/crates/ra_syntax/src/lib.rs b/crates/ra_syntax/src/lib.rs index 0fa2fe382..2eb3fcd57 100644 --- a/crates/ra_syntax/src/lib.rs +++ b/crates/ra_syntax/src/lib.rs @@ -35,7 +35,7 @@ use std::{fmt::Write, sync::Arc}; use ra_text_edit::AtomTextEdit; -use crate::syntax_node::GreenNode; +use crate::syntax_node::{GreenNode, SyntaxNodeWrapper}; pub use crate::{ ast::{AstNode, AstToken}, @@ -57,14 +57,24 @@ pub use rowan::{SmolStr, TextRange, TextUnit}; /// /// Note that we always produce a syntax tree, even for completely invalid /// files. -#[derive(Debug, Clone, PartialEq, Eq)] -pub struct Parse { - tree: TreeArc, +#[derive(Debug, PartialEq, Eq)] +pub struct Parse { + tree: TreeArc, errors: Arc>, } -impl Parse { - pub fn tree(&self) -> &SourceFile { +impl Clone for Parse { + fn clone(&self) -> Parse { + Parse { tree: self.tree.clone(), errors: self.errors.clone() } + } +} + +impl Parse { + fn new(tree: TreeArc, errors: Vec) -> Parse { + Parse { tree, errors: Arc::new(errors) } + } + + pub fn tree(&self) -> &T { &*self.tree } @@ -72,18 +82,16 @@ impl Parse { &*self.errors } - pub fn ok(self) -> Result, Arc>> { + pub fn ok(self) -> Result, Arc>> { if self.errors.is_empty() { Ok(self.tree) } else { Err(self.errors) } } +} - pub fn reparse(&self, edit: &AtomTextEdit) -> Parse { - self.incremental_reparse(edit).unwrap_or_else(|| self.full_reparse(edit)) - } - +impl Parse { pub fn debug_dump(&self) -> String { let mut buf = self.tree.syntax().debug_dump(); for err in self.errors.iter() { @@ -92,7 +100,11 @@ impl Parse { buf } - fn incremental_reparse(&self, edit: &AtomTextEdit) -> Option { + pub fn reparse(&self, edit: &AtomTextEdit) -> Parse { + self.incremental_reparse(edit).unwrap_or_else(|| self.full_reparse(edit)) + } + + fn incremental_reparse(&self, edit: &AtomTextEdit) -> Option> { // FIXME: validation errors are not handled here parsing::incremental_reparse(self.tree.syntax(), edit, self.errors.to_vec()).map( |(green_node, errors, _reparsed_range)| Parse { @@ -102,12 +114,19 @@ impl Parse { ) } - fn full_reparse(&self, edit: &AtomTextEdit) -> Parse { + fn full_reparse(&self, edit: &AtomTextEdit) -> Parse { let text = edit.apply(self.tree.syntax().text().to_string()); SourceFile::parse(&text) } } +impl Parse { + pub fn cast(self) -> Option> { + let node = T::cast(&self.tree)?; + Some(Parse { tree: node.to_owned(), errors: self.errors }) + } +} + /// `SourceFile` represents a parse tree for a single Rust file. pub use crate::ast::SourceFile; @@ -121,7 +140,7 @@ impl SourceFile { TreeArc::cast(root) } - pub fn parse(text: &str) -> Parse { + pub fn parse(text: &str) -> Parse { let (green, mut errors) = parsing::parse_text(text); let tree = SourceFile::new(green); errors.extend(validation::validate(&tree)); -- cgit v1.2.3 From df33e7685bdb0f63bf6aa809b9046708d563a1a7 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 18 Jul 2019 23:19:04 +0300 Subject: use Parse in mbe --- crates/ra_syntax/src/lib.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'crates/ra_syntax/src/lib.rs') diff --git a/crates/ra_syntax/src/lib.rs b/crates/ra_syntax/src/lib.rs index 2eb3fcd57..534c206a6 100644 --- a/crates/ra_syntax/src/lib.rs +++ b/crates/ra_syntax/src/lib.rs @@ -35,7 +35,7 @@ use std::{fmt::Write, sync::Arc}; use ra_text_edit::AtomTextEdit; -use crate::syntax_node::{GreenNode, SyntaxNodeWrapper}; +use crate::syntax_node::GreenNode; pub use crate::{ ast::{AstNode, AstToken}, @@ -43,8 +43,8 @@ pub use crate::{ ptr::{AstPtr, SyntaxNodePtr}, syntax_error::{Location, SyntaxError, SyntaxErrorKind}, syntax_node::{ - Direction, InsertPosition, SyntaxElement, SyntaxNode, SyntaxToken, SyntaxTreeBuilder, - TreeArc, WalkEvent, + Direction, InsertPosition, SyntaxElement, SyntaxNode, SyntaxNodeWrapper, SyntaxToken, + SyntaxTreeBuilder, TreeArc, WalkEvent, }, syntax_text::SyntaxText, }; @@ -91,6 +91,12 @@ impl Parse { } } +impl Parse { + pub fn to_syntax(this: Self) -> Parse { + Parse { tree: this.tree().syntax().to_owned(), errors: this.errors } + } +} + impl Parse { pub fn debug_dump(&self) -> String { let mut buf = self.tree.syntax().debug_dump(); -- cgit v1.2.3