From 9ed73378ce3343237bae39f6f9837aa18acbc9e7 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 1 Jan 2018 00:13:56 +0300 Subject: Parser: extract sink trait --- src/lib.rs | 2 +- src/parser/mod.rs | 2 +- src/tree/file_builder.rs | 53 ++++++++++++++++++++++++++++-------------------- src/tree/mod.rs | 2 +- 4 files changed, 34 insertions(+), 25 deletions(-) (limited to 'src') 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; pub mod syntax_kinds; pub use text::{TextUnit, TextRange}; -pub use tree::{SyntaxKind, Token, FileBuilder, File, Node}; +pub use tree::{SyntaxKind, Token, FileBuilder, Sink, File, Node}; pub use lexer::{next_token, tokenize}; 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 @@ -use {Token, File, FileBuilder}; +use {Token, File, FileBuilder, Sink}; use syntax_kinds::*; 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 @@ use {SyntaxKind, TextUnit, TextRange}; use super::{NodeData, NodeIdx, File}; +pub trait Sink { + fn leaf(&mut self, kind: SyntaxKind, len: TextUnit); + fn start_internal(&mut self, kind: SyntaxKind); + fn finish_internal(&mut self); +} + + pub struct FileBuilder { text: String, nodes: Vec, @@ -8,26 +15,8 @@ pub struct FileBuilder { pos: TextUnit, } -impl FileBuilder { - pub fn new(text: String) -> FileBuilder { - FileBuilder { - text, - nodes: Vec::new(), - in_progress: Vec::new(), - pos: TextUnit::new(0), - } - } - - pub fn finish(self) -> File { - assert!(self.in_progress.is_empty()); - assert!(self.pos == (self.text.len() as u32).into()); - File { - text: self.text, - nodes: self.nodes, - } - } - - pub fn leaf(&mut self, kind: SyntaxKind, len: TextUnit) { +impl Sink for FileBuilder { + fn leaf(&mut self, kind: SyntaxKind, len: TextUnit) { let leaf = NodeData { kind, range: TextRange::from_len(self.pos, len), @@ -40,7 +29,7 @@ impl FileBuilder { self.add_len(id); } - pub fn start_internal(&mut self, kind: SyntaxKind) { + fn start_internal(&mut self, kind: SyntaxKind) { let node = NodeData { kind, range: TextRange::from_len(self.pos, 0.into()), @@ -56,12 +45,32 @@ impl FileBuilder { self.in_progress.push((id, None)) } - pub fn finish_internal(&mut self) { + fn finish_internal(&mut self) { let (id, _) = self.in_progress.pop().unwrap(); if !self.in_progress.is_empty() { self.add_len(id); } } +} + +impl FileBuilder { + pub fn new(text: String) -> FileBuilder { + FileBuilder { + text, + nodes: Vec::new(), + in_progress: Vec::new(), + pos: TextUnit::new(0), + } + } + + pub fn finish(self) -> File { + assert!(self.in_progress.is_empty()); + assert!(self.pos == (self.text.len() as u32).into()); + File { + text: self.text, + nodes: self.nodes, + } + } fn new_node(&mut self, data: NodeData) -> NodeIdx { 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; use std::fmt; mod file_builder; -pub use self::file_builder::FileBuilder; +pub use self::file_builder::{FileBuilder, Sink}; #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct SyntaxKind(pub(crate) u32); -- cgit v1.2.3