diff options
Diffstat (limited to 'src/parser/event.rs')
-rw-r--r-- | src/parser/event.rs | 33 |
1 files changed, 27 insertions, 6 deletions
diff --git a/src/parser/event.rs b/src/parser/event.rs index 83039c664..a8d503b3d 100644 --- a/src/parser/event.rs +++ b/src/parser/event.rs | |||
@@ -1,8 +1,29 @@ | |||
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. | ||
1 | use { | 10 | use { |
2 | Sink, SyntaxKind, Token, | 11 | TextUnit, |
3 | syntax_kinds::TOMBSTONE, | 12 | SyntaxKind::{self, TOMBSTONE}, |
13 | lexer::Token, | ||
4 | }; | 14 | }; |
5 | use super::is_insignificant; | 15 | |
16 | pub(crate) trait Sink { | ||
17 | type Tree; | ||
18 | |||
19 | fn new(text: String) -> Self; | ||
20 | |||
21 | fn leaf(&mut self, kind: SyntaxKind, len: TextUnit); | ||
22 | fn start_internal(&mut self, kind: SyntaxKind); | ||
23 | fn finish_internal(&mut self); | ||
24 | fn error(&mut self, err: String); | ||
25 | fn finish(self) -> Self::Tree; | ||
26 | } | ||
6 | 27 | ||
7 | /// `Parser` produces a flat list of `Event`s. | 28 | /// `Parser` produces a flat list of `Event`s. |
8 | /// They are converted to a tree-structure in | 29 | /// They are converted to a tree-structure in |
@@ -67,7 +88,7 @@ pub(crate) enum Event { | |||
67 | }, | 88 | }, |
68 | } | 89 | } |
69 | 90 | ||
70 | pub(super) fn process(builder: &mut Sink, tokens: &[Token], events: Vec<Event>) { | 91 | pub(super) fn process(builder: &mut impl Sink, tokens: &[Token], events: Vec<Event>) { |
71 | let mut idx = 0; | 92 | let mut idx = 0; |
72 | 93 | ||
73 | let mut holes = Vec::new(); | 94 | let mut holes = Vec::new(); |
@@ -111,7 +132,7 @@ pub(super) fn process(builder: &mut Sink, tokens: &[Token], events: Vec<Event>) | |||
111 | &Event::Finish => { | 132 | &Event::Finish => { |
112 | while idx < tokens.len() { | 133 | while idx < tokens.len() { |
113 | let token = tokens[idx]; | 134 | let token = tokens[idx]; |
114 | if is_insignificant(token.kind) { | 135 | if token.kind.is_trivia() { |
115 | idx += 1; | 136 | idx += 1; |
116 | builder.leaf(token.kind, token.len); | 137 | builder.leaf(token.kind, token.len); |
117 | } else { | 138 | } else { |
@@ -128,7 +149,7 @@ pub(super) fn process(builder: &mut Sink, tokens: &[Token], events: Vec<Event>) | |||
128 | // this should be done in a sensible manner instead | 149 | // this should be done in a sensible manner instead |
129 | loop { | 150 | loop { |
130 | let token = tokens[idx]; | 151 | let token = tokens[idx]; |
131 | if !is_insignificant(token.kind) { | 152 | if !token.kind.is_trivia() { |
132 | break; | 153 | break; |
133 | } | 154 | } |
134 | builder.leaf(token.kind, token.len); | 155 | builder.leaf(token.kind, token.len); |