aboutsummaryrefslogtreecommitdiff
path: root/src/tree
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2018-07-29 13:16:07 +0100
committerAleksey Kladov <[email protected]>2018-07-29 13:16:07 +0100
commit415c891d641fa305e7ddbbbcc78db990dd5d3564 (patch)
tree4b6e1e0aa4b5a732aeae8945e75c9bee3bbf1d65 /src/tree
parentad188d4c3db34f035408afbdd6d2f3c308121f0a (diff)
Reorganize
Diffstat (limited to 'src/tree')
-rw-r--r--src/tree/file_builder.rs87
-rw-r--r--src/tree/mod.rs27
2 files changed, 0 insertions, 114 deletions
diff --git a/src/tree/file_builder.rs b/src/tree/file_builder.rs
deleted file mode 100644
index f5d1751f9..000000000
--- a/src/tree/file_builder.rs
+++ /dev/null
@@ -1,87 +0,0 @@
1//! This module provides a way to construct a `File`.
2//! It is intended to be completely decoupled from the
3//! parser, so as to allow to evolve the tree representation
4//! and the parser algorithm independently.
5//!
6//! The `Sink` trait is the bridge between the parser and the
7//! tree builder: the parser produces a stream of events like
8//! `start node`, `finish node`, and `FileBuilder` converts
9//! this stream to a real tree.
10use std::sync::Arc;
11use {
12 SyntaxKind, TextRange, TextUnit,
13 yellow::GreenNode
14};
15use SError;
16
17pub(crate) trait Sink {
18 fn leaf(&mut self, kind: SyntaxKind, len: TextUnit);
19 fn start_internal(&mut self, kind: SyntaxKind);
20 fn finish_internal(&mut self);
21 fn error(&mut self, err: String);
22}
23
24pub(crate) struct GreenBuilder {
25 text: String,
26 stack: Vec<GreenNode>,
27 pos: TextUnit,
28 root: Option<GreenNode>,
29 errors: Vec<SError>,
30}
31
32impl GreenBuilder {
33 pub(crate) fn new(text: String) -> GreenBuilder {
34 GreenBuilder {
35 text,
36 stack: Vec::new(),
37 pos: 0.into(),
38 root: None,
39 errors: Vec::new(),
40 }
41 }
42
43 pub(crate) fn finish(self) -> (GreenNode, Vec<SError>) {
44 (self.root.unwrap(), self.errors)
45 }
46}
47
48impl Sink for GreenBuilder {
49 fn leaf(&mut self, kind: SyntaxKind, len: TextUnit) {
50 let range = TextRange::offset_len(self.pos, len);
51 self.pos += len;
52 let text = self.text[range].to_owned();
53 let parent = self.stack.last_mut().unwrap();
54 if kind.is_trivia() {
55 parent.push_trivia(kind, text);
56 } else {
57 let node = GreenNode::new_leaf(kind, text);
58 parent.push_child(Arc::new(node));
59 }
60 }
61
62 fn start_internal(&mut self, kind: SyntaxKind) {
63 self.stack.push(GreenNode::new_branch(kind))
64 }
65
66 fn finish_internal(&mut self) {
67 let node = self.stack.pop().unwrap();
68 if let Some(parent) = self.stack.last_mut() {
69 parent.push_child(Arc::new(node))
70 } else {
71 self.root = Some(node);
72 }
73 }
74
75 fn error(&mut self, message: String) {
76 self.errors.push(SError { message, offset: self.pos })
77 }
78}
79impl SyntaxKind {
80 fn is_trivia(self) -> bool {
81 match self {
82 SyntaxKind::WHITESPACE | SyntaxKind::DOC_COMMENT | SyntaxKind::COMMENT => true,
83 _ => false
84 }
85 }
86}
87
diff --git a/src/tree/mod.rs b/src/tree/mod.rs
deleted file mode 100644
index efba82825..000000000
--- a/src/tree/mod.rs
+++ /dev/null
@@ -1,27 +0,0 @@
1mod file_builder;
2
3use ::{TextUnit};
4use std::{fmt};
5pub(crate) use self::file_builder::{Sink, GreenBuilder};
6
7pub use syntax_kinds::SyntaxKind;
8
9impl fmt::Debug for SyntaxKind {
10 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
11 let name = self.info().name;
12 f.write_str(name)
13 }
14}
15
16pub(crate) struct SyntaxInfo {
17 pub name: &'static str,
18}
19
20/// A token of Rust source.
21#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
22pub struct Token {
23 /// The kind of token.
24 pub kind: SyntaxKind,
25 /// The length of the token.
26 pub len: TextUnit,
27}