aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2018-08-25 10:10:35 +0100
committerAleksey Kladov <[email protected]>2018-08-25 10:10:35 +0100
commit87cd57d56aeafa1117a6163249d210f39efe8f28 (patch)
treea917b4bff6164902d26ed5334f94c6e76745bfbc
parenta44428fc796cb5afb5b85cd246974e590eef2040 (diff)
Refactor
-rw-r--r--crates/libsyntax2/src/lib.rs21
-rw-r--r--crates/libsyntax2/src/yellow/builder.rs9
-rw-r--r--crates/libsyntax2/tests/test/main.rs6
3 files changed, 19 insertions, 17 deletions
diff --git a/crates/libsyntax2/src/lib.rs b/crates/libsyntax2/src/lib.rs
index ab4a40435..01e155855 100644
--- a/crates/libsyntax2/src/lib.rs
+++ b/crates/libsyntax2/src/lib.rs
@@ -50,16 +50,25 @@ pub use {
50 yellow::{SyntaxNode, SyntaxNodeRef, OwnedRoot, RefRoot, TreeRoot, SyntaxError}, 50 yellow::{SyntaxNode, SyntaxNodeRef, OwnedRoot, RefRoot, TreeRoot, SyntaxError},
51}; 51};
52 52
53use yellow::{GreenNode, SyntaxRoot};
54
53#[derive(Clone, Debug)] 55#[derive(Clone, Debug)]
54pub struct File { 56pub struct File {
55 root: SyntaxNode 57 root: SyntaxNode
56} 58}
57 59
58impl File { 60impl File {
59 pub fn parse(text: &str) -> Self { 61 fn new(root: GreenNode, errors: Vec<SyntaxError>) -> File {
60 let root = ::parse(text); 62 let root = SyntaxRoot::new(root, errors);
63 let root = SyntaxNode::new_owned(root);
64 validate_block_structure(root.borrowed());
61 File { root } 65 File { root }
62 } 66 }
67 pub fn parse(text: &str) -> Self {
68 let tokens = tokenize(&text);
69 let (root, errors) = parser_impl::parse::<yellow::GreenBuilder>(text, &tokens);
70 File::new(root, errors)
71 }
63 pub fn ast(&self) -> ast::Root { 72 pub fn ast(&self) -> ast::Root {
64 ast::Root::cast(self.syntax()).unwrap() 73 ast::Root::cast(self.syntax()).unwrap()
65 } 74 }
@@ -69,14 +78,6 @@ impl File {
69 pub fn errors(&self) -> Vec<SyntaxError> { 78 pub fn errors(&self) -> Vec<SyntaxError> {
70 self.syntax().root.syntax_root().errors.clone() 79 self.syntax().root.syntax_root().errors.clone()
71 } 80 }
72
73}
74
75pub fn parse(text: &str) -> SyntaxNode {
76 let tokens = tokenize(&text);
77 let res = parser_impl::parse::<yellow::GreenBuilder>(text, &tokens);
78 validate_block_structure(res.borrowed());
79 res
80} 81}
81 82
82#[cfg(not(debug_assertions))] 83#[cfg(not(debug_assertions))]
diff --git a/crates/libsyntax2/src/yellow/builder.rs b/crates/libsyntax2/src/yellow/builder.rs
index 5e94e5055..e4ab37899 100644
--- a/crates/libsyntax2/src/yellow/builder.rs
+++ b/crates/libsyntax2/src/yellow/builder.rs
@@ -1,6 +1,6 @@
1use { 1use {
2 parser_impl::Sink, 2 parser_impl::Sink,
3 yellow::{GreenNode, SyntaxError, SyntaxNode, SyntaxRoot}, 3 yellow::{GreenNode, SyntaxError},
4 SyntaxKind, TextRange, TextUnit, 4 SyntaxKind, TextRange, TextUnit,
5}; 5};
6 6
@@ -13,7 +13,7 @@ pub(crate) struct GreenBuilder<'a> {
13} 13}
14 14
15impl<'a> Sink<'a> for GreenBuilder<'a> { 15impl<'a> Sink<'a> for GreenBuilder<'a> {
16 type Tree = SyntaxNode; 16 type Tree = (GreenNode, Vec<SyntaxError>);
17 17
18 fn new(text: &'a str) -> Self { 18 fn new(text: &'a str) -> Self {
19 GreenBuilder { 19 GreenBuilder {
@@ -56,10 +56,9 @@ impl<'a> Sink<'a> for GreenBuilder<'a> {
56 }) 56 })
57 } 57 }
58 58
59 fn finish(mut self) -> SyntaxNode { 59 fn finish(mut self) -> (GreenNode, Vec<SyntaxError>) {
60 assert_eq!(self.children.len(), 1); 60 assert_eq!(self.children.len(), 1);
61 let root = self.children.pop().unwrap(); 61 let root = self.children.pop().unwrap();
62 let root = SyntaxRoot::new(root, self.errors); 62 (root, self.errors)
63 SyntaxNode::new_owned(root)
64 } 63 }
65} 64}
diff --git a/crates/libsyntax2/tests/test/main.rs b/crates/libsyntax2/tests/test/main.rs
index 7e5dc32d9..18c711aee 100644
--- a/crates/libsyntax2/tests/test/main.rs
+++ b/crates/libsyntax2/tests/test/main.rs
@@ -9,6 +9,8 @@ use std::{
9 fmt::Write, 9 fmt::Write,
10}; 10};
11 11
12use libsyntax2::File;
13
12#[test] 14#[test]
13fn lexer_tests() { 15fn lexer_tests() {
14 dir_tests(&["lexer"], |text| { 16 dir_tests(&["lexer"], |text| {
@@ -20,8 +22,8 @@ fn lexer_tests() {
20#[test] 22#[test]
21fn parser_tests() { 23fn parser_tests() {
22 dir_tests(&["parser/inline", "parser/ok", "parser/err"], |text| { 24 dir_tests(&["parser/inline", "parser/ok", "parser/err"], |text| {
23 let file = libsyntax2::parse(text); 25 let file = File::parse(text);
24 libsyntax2::utils::dump_tree(file.borrowed()) 26 libsyntax2::utils::dump_tree(file.syntax())
25 }) 27 })
26} 28}
27 29