diff options
author | Aleksey Kladov <[email protected]> | 2017-12-31 21:13:56 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2017-12-31 21:13:56 +0000 |
commit | 9ed73378ce3343237bae39f6f9837aa18acbc9e7 (patch) | |
tree | 920e1cc93e00d1f69a3f234d7778baa9949ae0e0 /src | |
parent | 8c478a794c667357b0c9a82ffc5c3eb5ca1b1b0d (diff) |
Parser: extract sink trait
Diffstat (limited to 'src')
-rw-r--r-- | src/lib.rs | 2 | ||||
-rw-r--r-- | src/parser/mod.rs | 2 | ||||
-rw-r--r-- | src/tree/file_builder.rs | 53 | ||||
-rw-r--r-- | src/tree/mod.rs | 2 |
4 files changed, 34 insertions, 25 deletions
diff --git a/src/lib.rs b/src/lib.rs index 13872a3d9..d95e26662 100644 --- a/src/lib.rs +++ b/src/lib.rs | |||
@@ -7,6 +7,6 @@ mod parser; | |||
7 | 7 | ||
8 | pub mod syntax_kinds; | 8 | pub mod syntax_kinds; |
9 | pub use text::{TextUnit, TextRange}; | 9 | pub use text::{TextUnit, TextRange}; |
10 | pub use tree::{SyntaxKind, Token, FileBuilder, File, Node}; | 10 | pub use tree::{SyntaxKind, Token, FileBuilder, Sink, File, Node}; |
11 | pub use lexer::{next_token, tokenize}; | 11 | pub use lexer::{next_token, tokenize}; |
12 | pub use parser::parse; | 12 | pub use parser::parse; |
diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 7336c5136..0a136266c 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs | |||
@@ -1,4 +1,4 @@ | |||
1 | use {Token, File, FileBuilder}; | 1 | use {Token, File, FileBuilder, Sink}; |
2 | 2 | ||
3 | use syntax_kinds::*; | 3 | use syntax_kinds::*; |
4 | 4 | ||
diff --git a/src/tree/file_builder.rs b/src/tree/file_builder.rs index 430303ce9..eba850fef 100644 --- a/src/tree/file_builder.rs +++ b/src/tree/file_builder.rs | |||
@@ -1,6 +1,13 @@ | |||
1 | use {SyntaxKind, TextUnit, TextRange}; | 1 | use {SyntaxKind, TextUnit, TextRange}; |
2 | use super::{NodeData, NodeIdx, File}; | 2 | use super::{NodeData, NodeIdx, File}; |
3 | 3 | ||
4 | pub trait Sink { | ||
5 | fn leaf(&mut self, kind: SyntaxKind, len: TextUnit); | ||
6 | fn start_internal(&mut self, kind: SyntaxKind); | ||
7 | fn finish_internal(&mut self); | ||
8 | } | ||
9 | |||
10 | |||
4 | pub struct FileBuilder { | 11 | pub struct FileBuilder { |
5 | text: String, | 12 | text: String, |
6 | nodes: Vec<NodeData>, | 13 | nodes: Vec<NodeData>, |
@@ -8,26 +15,8 @@ pub struct FileBuilder { | |||
8 | pos: TextUnit, | 15 | pos: TextUnit, |
9 | } | 16 | } |
10 | 17 | ||
11 | impl FileBuilder { | 18 | impl Sink for FileBuilder { |
12 | pub fn new(text: String) -> FileBuilder { | 19 | fn leaf(&mut self, kind: SyntaxKind, len: TextUnit) { |
13 | FileBuilder { | ||
14 | text, | ||
15 | nodes: Vec::new(), | ||
16 | in_progress: Vec::new(), | ||
17 | pos: TextUnit::new(0), | ||
18 | } | ||
19 | } | ||
20 | |||
21 | pub fn finish(self) -> File { | ||
22 | assert!(self.in_progress.is_empty()); | ||
23 | assert!(self.pos == (self.text.len() as u32).into()); | ||
24 | File { | ||
25 | text: self.text, | ||
26 | nodes: self.nodes, | ||
27 | } | ||
28 | } | ||
29 | |||
30 | pub fn leaf(&mut self, kind: SyntaxKind, len: TextUnit) { | ||
31 | let leaf = NodeData { | 20 | let leaf = NodeData { |
32 | kind, | 21 | kind, |
33 | range: TextRange::from_len(self.pos, len), | 22 | range: TextRange::from_len(self.pos, len), |
@@ -40,7 +29,7 @@ impl FileBuilder { | |||
40 | self.add_len(id); | 29 | self.add_len(id); |
41 | } | 30 | } |
42 | 31 | ||
43 | pub fn start_internal(&mut self, kind: SyntaxKind) { | 32 | fn start_internal(&mut self, kind: SyntaxKind) { |
44 | let node = NodeData { | 33 | let node = NodeData { |
45 | kind, | 34 | kind, |
46 | range: TextRange::from_len(self.pos, 0.into()), | 35 | range: TextRange::from_len(self.pos, 0.into()), |
@@ -56,12 +45,32 @@ impl FileBuilder { | |||
56 | self.in_progress.push((id, None)) | 45 | self.in_progress.push((id, None)) |
57 | } | 46 | } |
58 | 47 | ||
59 | pub fn finish_internal(&mut self) { | 48 | fn finish_internal(&mut self) { |
60 | let (id, _) = self.in_progress.pop().unwrap(); | 49 | let (id, _) = self.in_progress.pop().unwrap(); |
61 | if !self.in_progress.is_empty() { | 50 | if !self.in_progress.is_empty() { |
62 | self.add_len(id); | 51 | self.add_len(id); |
63 | } | 52 | } |
64 | } | 53 | } |
54 | } | ||
55 | |||
56 | impl FileBuilder { | ||
57 | pub fn new(text: String) -> FileBuilder { | ||
58 | FileBuilder { | ||
59 | text, | ||
60 | nodes: Vec::new(), | ||
61 | in_progress: Vec::new(), | ||
62 | pos: TextUnit::new(0), | ||
63 | } | ||
64 | } | ||
65 | |||
66 | pub fn finish(self) -> File { | ||
67 | assert!(self.in_progress.is_empty()); | ||
68 | assert!(self.pos == (self.text.len() as u32).into()); | ||
69 | File { | ||
70 | text: self.text, | ||
71 | nodes: self.nodes, | ||
72 | } | ||
73 | } | ||
65 | 74 | ||
66 | fn new_node(&mut self, data: NodeData) -> NodeIdx { | 75 | fn new_node(&mut self, data: NodeData) -> NodeIdx { |
67 | let id = NodeIdx(self.nodes.len() as u32); | 76 | let id = NodeIdx(self.nodes.len() as u32); |
diff --git a/src/tree/mod.rs b/src/tree/mod.rs index 6bacb46ef..7a5d429a3 100644 --- a/src/tree/mod.rs +++ b/src/tree/mod.rs | |||
@@ -4,7 +4,7 @@ use syntax_kinds::syntax_info; | |||
4 | use std::fmt; | 4 | use std::fmt; |
5 | 5 | ||
6 | mod file_builder; | 6 | mod file_builder; |
7 | pub use self::file_builder::FileBuilder; | 7 | pub use self::file_builder::{FileBuilder, Sink}; |
8 | 8 | ||
9 | #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] | 9 | #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] |
10 | pub struct SyntaxKind(pub(crate) u32); | 10 | pub struct SyntaxKind(pub(crate) u32); |