aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_syntax/src/lib.rs')
-rw-r--r--crates/ra_syntax/src/lib.rs32
1 files changed, 14 insertions, 18 deletions
diff --git a/crates/ra_syntax/src/lib.rs b/crates/ra_syntax/src/lib.rs
index 8af04c136..7f69b86e1 100644
--- a/crates/ra_syntax/src/lib.rs
+++ b/crates/ra_syntax/src/lib.rs
@@ -20,7 +20,6 @@
20//! [Swift]: <https://github.com/apple/swift/blob/13d593df6f359d0cb2fc81cfaac273297c539455/lib/Syntax/README.md> 20//! [Swift]: <https://github.com/apple/swift/blob/13d593df6f359d0cb2fc81cfaac273297c539455/lib/Syntax/README.md>
21 21
22mod syntax_node; 22mod syntax_node;
23mod syntax_text;
24mod syntax_error; 23mod syntax_error;
25mod parsing; 24mod parsing;
26mod validation; 25mod validation;
@@ -38,19 +37,17 @@ use ra_text_edit::AtomTextEdit;
38use crate::syntax_node::GreenNode; 37use crate::syntax_node::GreenNode;
39 38
40pub use crate::{ 39pub use crate::{
40 algo::InsertPosition,
41 ast::{AstNode, AstToken}, 41 ast::{AstNode, AstToken},
42 parsing::{classify_literal, tokenize, Token}, 42 parsing::{classify_literal, tokenize, Token},
43 ptr::{AstPtr, SyntaxNodePtr}, 43 ptr::{AstPtr, SyntaxNodePtr},
44 syntax_error::{Location, SyntaxError, SyntaxErrorKind}, 44 syntax_error::{Location, SyntaxError, SyntaxErrorKind},
45 syntax_node::{ 45 syntax_node::{
46 Direction, InsertPosition, SyntaxElement, SyntaxNode, SyntaxToken, SyntaxTreeBuilder, 46 Direction, NodeOrToken, SyntaxElement, SyntaxNode, SyntaxToken, SyntaxTreeBuilder,
47 WalkEvent,
48 }, 47 },
49 syntax_text::SyntaxText,
50}; 48};
51pub use ra_parser::SyntaxKind; 49pub use ra_parser::{SyntaxKind, T};
52pub use ra_parser::T; 50pub use rowan::{SmolStr, SyntaxText, TextRange, TextUnit, TokenAtOffset, WalkEvent};
53pub use rowan::{SmolStr, TextRange, TextUnit};
54 51
55/// `Parse` is the result of the parsing: a syntax tree and a collection of 52/// `Parse` is the result of the parsing: a syntax tree and a collection of
56/// errors. 53/// errors.
@@ -76,7 +73,7 @@ impl<T> Parse<T> {
76 } 73 }
77 74
78 pub fn syntax_node(&self) -> SyntaxNode { 75 pub fn syntax_node(&self) -> SyntaxNode {
79 SyntaxNode::new(self.green.clone()) 76 SyntaxNode::new_root(self.green.clone())
80 } 77 }
81} 78}
82 79
@@ -146,18 +143,17 @@ impl Parse<SourceFile> {
146pub use crate::ast::SourceFile; 143pub use crate::ast::SourceFile;
147 144
148impl SourceFile { 145impl SourceFile {
149 fn new(green: GreenNode) -> SourceFile { 146 pub fn parse(text: &str) -> Parse<SourceFile> {
150 let root = SyntaxNode::new(green); 147 let (green, mut errors) = parsing::parse_text(text);
148 let root = SyntaxNode::new_root(green.clone());
149
151 if cfg!(debug_assertions) { 150 if cfg!(debug_assertions) {
152 validation::validate_block_structure(&root); 151 validation::validate_block_structure(&root);
153 } 152 }
154 assert_eq!(root.kind(), SyntaxKind::SOURCE_FILE);
155 SourceFile::cast(root).unwrap()
156 }
157 153
158 pub fn parse(text: &str) -> Parse<SourceFile> { 154 errors.extend(validation::validate(&root));
159 let (green, mut errors) = parsing::parse_text(text); 155
160 errors.extend(validation::validate(&SourceFile::new(green.clone()))); 156 assert_eq!(root.kind(), SyntaxKind::SOURCE_FILE);
161 Parse { green, errors: Arc::new(errors), _ty: PhantomData } 157 Parse { green, errors: Arc::new(errors), _ty: PhantomData }
162 } 158 }
163} 159}
@@ -267,8 +263,8 @@ fn api_walkthrough() {
267 match event { 263 match event {
268 WalkEvent::Enter(node) => { 264 WalkEvent::Enter(node) => {
269 let text = match &node { 265 let text = match &node {
270 SyntaxElement::Node(it) => it.text().to_string(), 266 NodeOrToken::Node(it) => it.text().to_string(),
271 SyntaxElement::Token(it) => it.text().to_string(), 267 NodeOrToken::Token(it) => it.text().to_string(),
272 }; 268 };
273 buf += &format!("{:indent$}{:?} {:?}\n", " ", text, node.kind(), indent = indent); 269 buf += &format!("{:indent$}{:?} {:?}\n", " ", text, node.kind(), indent = indent);
274 indent += 2; 270 indent += 2;