1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
use std::collections::VecDeque;
use crate::cst::Event;
use crate::cst::SyntaxKind;
use crate::Span;
/// A Concrete Syntax Tree (CST) represented as a stream of events.
///
/// See the documentation for [`crate::cst::CSTStream`], which is the public
/// facing API that exposes the concept to the public.
pub struct SyntaxStream {
events: VecDeque<Event>,
/// Positions within `events` where `Begin` events are located. This allows
/// locating the last `Begin` event without having to traverse `events`
/// right-to-left.
open_begins: VecDeque<usize>,
/// Number of currently active bookmarks.
num_bookmarks: usize,
/// The span of the last token in the stream.
last_token_span: Span,
}
impl SyntaxStream {
/// Creates a new [`SyntaxStream`].
pub(crate) fn new() -> Self {
Self {
events: VecDeque::new(),
open_begins: VecDeque::new(),
num_bookmarks: 0,
last_token_span: Span::default(),
}
}
/// Removes an event from the beginning of the stream and returns it.
///
/// There are some restrictions for this operation:
///
/// * The stream can't have any active bookmarks.
/// * Every `Begin` must already have a matching `End`.
///
/// # Panics
///
/// If any of the conditions mentioned above is not met.
#[inline]
pub(crate) fn pop(&mut self) -> Option<Event> {
// A bookmark is a position in `events`, therefore calling this
// function invalidates any existing bookmark. Make sure that
// there's no existing bookmarks.
assert_eq!(self.num_bookmarks, 0);
// Ensure that all `Begin` events have its corresponding `End`.
assert!(self.open_begins.is_empty());
let event = self.events.pop_front();
if event.is_some() {
for b in self.open_begins.iter_mut() {
*b -= 1;
}
}
event
}
/// Pushes a [`Event::Token`] at the end of the stream.
#[inline]
pub(crate) fn push_token(&mut self, kind: SyntaxKind, span: Span) {
self.last_token_span = span.clone();
self.events.push_back(Event::Token { kind, span })
}
/// Pushes a [`Event::Error`] at the end of the stream.
#[inline]
pub(crate) fn push_error<M: Into<String>>(
&mut self,
message: M,
span: Span,
) {
self.events.push_back(Event::Error { message: message.into(), span })
}
/// Pushes a [`Event::Begin`] at the end of the stream, opening a block
/// that must be closed by a corresponding call to [`SyntaxStream::end`].
pub(crate) fn begin(&mut self, kind: SyntaxKind) {
let pos = self.events.len();
let last_token_end = self.last_token_span.end();
self.events.push_back(Event::Begin {
kind,
// The non-terminal represented by this Event::Begin starts where
// the last token ends. The end is initially set to `last_token_end`
// too, but it will be updated when Event::End is inserted in the
// stream.
span: Span::from(last_token_end..last_token_end),
});
self.open_begins.push_back(pos);
}
/// Pushes a [`Event::End`] at the end of the stream, closing the block
/// that was opened by a previous call to [`SyntaxStream::begin`].
///
/// # Panics
///
/// * If no matching `Begin` exists for this `End`.
pub(crate) fn end(&mut self) {
// Get the index in the `events` vector where the `Event::Begin`
// that corresponds to this `Event::End` resides.
let begin_idx = self
.open_begins
.pop_back()
.expect("`End` without a corresponding `Begin`");
match &mut self.events[begin_idx] {
Event::Begin { kind, span } => {
// Now that we know where it ends, the span associated to
// Event::Begin is updated.
*span = Span::from(span.start()..self.last_token_span.end());
// Push the Event::End that closes the Event::Begin. Both
// have the same kind and span.
let kind = *kind;
let span = span.clone();
self.events.push_back(Event::End { kind, span });
}
_ => unreachable!(),
};
}
/// Similar to [`SyntaxStream::end`], but the kind of the closed block is
/// changed to [`SyntaxKind::ERROR`].
///
/// Notice that if the block being closed is empty, it will be removed
/// altogether.
///
/// # Panics
///
/// * If no matching `Begin` exists for this `End`.
pub(crate) fn end_with_error(&mut self) {
// Get the index in the `events` vector where the `Event::Begin`
// that corresponds to this `Event::End` resides.
let begin_idx = self
.open_begins
.pop_back()
.expect("`End` without a corresponding `Begin`");
// If `Event::Begin` is the last element in `events`, there's no other
// event in between `Event::Begin` and the current `Event::End`. In
// such cases, the `Event::Begin` is removed and the `Event::End` not
// inserted, we don't want empty nodes in the CST.
if begin_idx == self.events.len() - 1 {
self.events.pop_back();
return;
}
match &mut self.events[begin_idx] {
Event::Begin { kind, span } => {
// Change the kind for Event::Begin to SyntaxKind::ERROR.
*kind = SyntaxKind::ERROR;
// Update the span's ending offset for Event::Begin.
*span = Span::from(span.start()..self.last_token_span.end());
let kind = *kind;
let span = span.clone();
self.events.push_back(Event::End { kind, span });
}
_ => unreachable!(),
};
}
/// Returns a bookmark for the current ending position of the stream.
pub(crate) fn bookmark(&mut self) -> Bookmark {
self.num_bookmarks += 1;
Bookmark(self.events.len())
}
/// Truncates the stream at the position indicated by the bookmark,
/// removing any event added after the bookmark was created.
///
/// # Panics
///
/// If the bookmark points to a position that doesn't exist.
pub(crate) fn truncate(&mut self, bookmark: &Bookmark) {
assert!(bookmark.0 <= self.events.len());
self.events.truncate(bookmark.0);
self.last_token_span = self
.events
.iter()
.rev()
.find_map(|event| {
if let Event::Token { span, .. } = event {
Some(span.clone())
} else {
None
}
})
.unwrap_or_default();
}
pub(crate) fn remove_bookmark(&mut self, bookmark: Bookmark) {
assert!(bookmark.0 <= self.events.len());
self.num_bookmarks = self
.num_bookmarks
.checked_sub(1)
.expect("dropping a bookmark twice");
}
#[cfg(test)]
pub(crate) fn last_open_begin(&self) -> Option<(usize, SyntaxKind)> {
let pos = self.open_begins.back()?;
let node = self.events.get(*pos)?;
let kind = match node {
Event::Begin { kind, .. } => kind,
_ => panic!(),
};
Some((*pos, *kind))
}
}
pub(crate) struct Bookmark(usize);
#[cfg(test)]
mod tests {
use super::SyntaxKind;
use super::SyntaxStream;
use crate::cst::Event;
use crate::Span;
#[test]
fn begin_and_end() {
let mut s = SyntaxStream::new();
s.begin(SyntaxKind::RULE_DECL);
assert_eq!(s.last_open_begin(), Some((0, SyntaxKind::RULE_DECL)));
s.begin(SyntaxKind::META_DEF);
assert_eq!(s.last_open_begin(), Some((1, SyntaxKind::META_DEF)));
s.end();
assert_eq!(s.last_open_begin(), Some((0, SyntaxKind::RULE_DECL)));
s.end();
assert_eq!(s.last_open_begin(), None);
}
#[test]
fn bookmarks() {
let mut s = SyntaxStream::new();
let bookmark = s.bookmark();
s.push_token(SyntaxKind::L_BRACE, Span(0..1));
s.push_token(SyntaxKind::R_BRACE, Span(1..2));
s.truncate(&bookmark);
s.push_token(SyntaxKind::L_BRACE, Span(0..1));
s.push_token(SyntaxKind::R_BRACE, Span(1..2));
s.truncate(&bookmark);
s.remove_bookmark(bookmark);
s.begin(SyntaxKind::EXPR);
s.push_token(SyntaxKind::L_PAREN, Span(0..1));
s.push_token(SyntaxKind::R_PAREN, Span(1..2));
s.end();
assert_eq!(
s.pop(),
Some(Event::Begin { kind: SyntaxKind::EXPR, span: Span(0..2) })
);
assert_eq!(
s.pop(),
Some(Event::Token { kind: SyntaxKind::L_PAREN, span: Span(0..1) })
);
assert_eq!(
s.pop(),
Some(Event::Token { kind: SyntaxKind::R_PAREN, span: Span(1..2) })
);
assert_eq!(
s.pop(),
Some(Event::End { kind: SyntaxKind::EXPR, span: Span(0..2) })
);
assert_eq!(s.pop(), None);
}
#[test]
fn end_with_error() {
let mut s = SyntaxStream::new();
s.begin(SyntaxKind::RULE_DECL);
s.begin(SyntaxKind::META_DEF);
s.push_token(SyntaxKind::COLON, Span(0..1));
s.end_with_error();
assert_eq!(s.last_open_begin(), Some((0, SyntaxKind::RULE_DECL)));
let mut s = SyntaxStream::new();
s.begin(SyntaxKind::ERROR);
s.push_token(SyntaxKind::COLON, Span(0..1));
s.end_with_error();
assert_eq!(
s.pop(),
Some(Event::Begin { kind: SyntaxKind::ERROR, span: Span(0..1) })
);
assert_eq!(
s.pop(),
Some(Event::Token { kind: SyntaxKind::COLON, span: Span(0..1) })
);
assert_eq!(
s.pop(),
Some(Event::End { kind: SyntaxKind::ERROR, span: Span(0..1) })
);
}
#[test]
#[should_panic]
fn unmatched_begin_and_end() {
let mut s = SyntaxStream::new();
s.begin(SyntaxKind::META_DEF);
s.begin(SyntaxKind::RULE_DECL);
s.end();
s.end();
s.end();
}
#[test]
#[should_panic]
fn end_without_begin() {
let mut s = SyntaxStream::new();
s.end();
}
}