cstree 0.13.0

Library for generic lossless syntax trees
Documentation
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
use std::{
    fmt,
    hash::{Hash, Hasher},
    iter,
    sync::Arc as StdArc,
};

use text_size::{TextRange, TextSize};

use super::*;
use crate::{
    RawSyntaxKind,
    Syntax,
    green::{GreenNode, GreenToken},
    interning::{Resolver, TokenKey},
    traversal::Direction,
};

/// Syntax tree token.
#[derive(Debug)]
pub struct SyntaxToken<S: Syntax, D: 'static = ()> {
    parent: SyntaxNode<S, D>,
    index:  u32,
    offset: TextSize,
}

impl<S: Syntax, D> Clone for SyntaxToken<S, D> {
    fn clone(&self) -> Self {
        Self {
            parent: self.parent.clone(),
            index:  self.index,
            offset: self.offset,
        }
    }
}

impl<S: Syntax, D> Hash for SyntaxToken<S, D> {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.parent.hash(state);
        self.index.hash(state);
        self.offset.hash(state);
    }
}

impl<S: Syntax, D> PartialEq for SyntaxToken<S, D> {
    fn eq(&self, other: &SyntaxToken<S, D>) -> bool {
        self.parent == other.parent && self.index == other.index && self.offset == other.offset
    }
}

impl<S: Syntax, D> Eq for SyntaxToken<S, D> {}

impl<S: Syntax, D> SyntaxToken<S, D> {
    /// Writes this token's [`Debug`](fmt::Debug) representation into the given `target`.
    pub fn write_debug<R>(&self, resolver: &R, target: &mut impl fmt::Write) -> fmt::Result
    where
        R: Resolver<TokenKey> + ?Sized,
    {
        write!(target, "{:?}@{:?}", self.kind(), self.text_range())?;
        let text = self.resolve_text(resolver);
        if text.len() < 25 {
            return write!(target, " {text:?}");
        }

        for idx in 21..25 {
            if text.is_char_boundary(idx) {
                let text = format!("{} ...", &text[..idx]);
                return write!(target, " {text:?}");
            }
        }
        unreachable!()
    }

    /// Returns this token's [`Debug`](fmt::Debug) representation as a string.
    ///
    /// To avoid allocating for every token, see [`write_debug`](SyntaxToken::write_debug).
    #[inline]
    pub fn debug<R>(&self, resolver: &R) -> String
    where
        R: Resolver<TokenKey> + ?Sized,
    {
        // NOTE: `fmt::Write` methods on `String` never fail
        let mut res = String::new();
        self.write_debug(resolver, &mut res).unwrap();
        res
    }

    /// Writes this token's [`Display`](fmt::Display) representation into the given `target`.
    #[inline]
    pub fn write_display<R>(&self, resolver: &R, target: &mut impl fmt::Write) -> fmt::Result
    where
        R: Resolver<TokenKey> + ?Sized,
    {
        write!(target, "{}", self.resolve_text(resolver))
    }

    /// Returns this token's [`Display`](fmt::Display) representation as a string.
    ///
    /// To avoid allocating for every token, see [`write_display`](SyntaxToken::write_display).
    #[inline]
    pub fn display<R>(&self, resolver: &R) -> String
    where
        R: Resolver<TokenKey> + ?Sized,
    {
        self.resolve_text(resolver).to_string()
    }

    /// If there is a resolver associated with this tree, returns it.
    #[inline]
    pub fn resolver(&self) -> Option<&StdArc<dyn Resolver<TokenKey>>> {
        self.parent.resolver()
    }

    /// Turns this token into a [`ResolvedToken`], but only if there is a resolver
    /// associated with this tree.
    #[inline]
    pub fn try_resolved(&self) -> Option<&ResolvedToken<S, D>> {
        // safety: we only coerce if `resolver` exists
        self.resolver().map(|_| unsafe { ResolvedToken::coerce_ref(self) })
    }

    /// Turns this token into a [`ResolvedToken`].
    /// # Panics
    /// If there is no resolver associated with this tree.
    #[inline]
    pub fn resolved(&self) -> &ResolvedToken<S, D> {
        self.try_resolved().expect("tried to resolve a node without resolver")
    }
}

impl<S: Syntax, D> SyntaxToken<S, D> {
    pub(super) fn new(parent: &SyntaxNode<S, D>, index: u32, offset: TextSize) -> SyntaxToken<S, D> {
        Self {
            parent: parent.clone_uncounted(),
            index,
            offset,
        }
    }

    /// Returns a green tree, equal to the green tree this token
    /// belongs two, except with this token substitute. The complexity
    /// of operation is proportional to the depth of the tree
    pub fn replace_with(&self, replacement: GreenToken) -> GreenNode {
        assert_eq!(self.syntax_kind(), replacement.kind());
        let mut replacement = Some(replacement);
        let parent = self.parent();
        let me = self.index;

        let children = parent.green().children().enumerate().map(|(i, child)| {
            if i as u32 == me {
                replacement.take().unwrap().into()
            } else {
                child.cloned()
            }
        });
        let new_parent = GreenNode::new(parent.syntax_kind(), children);
        parent.replace_with(new_parent)
    }

    /// The internal representation of the kind of this token.
    #[inline]
    pub fn syntax_kind(&self) -> RawSyntaxKind {
        self.green().kind()
    }

    /// The kind of this token in terms of your language.
    #[inline]
    pub fn kind(&self) -> S {
        S::from_raw(self.syntax_kind())
    }

    /// The range this token covers in the source text, in bytes.
    #[inline]
    pub fn text_range(&self) -> TextRange {
        TextRange::at(self.offset, self.green().text_len())
    }

    /// Uses the provided resolver to return the source text of this token.
    ///
    /// If no text is explicitly associated with the token, returns its [`static_text`](SyntaxToken::static_text)
    /// instead.
    #[inline]
    pub fn resolve_text<'i, I>(&self, resolver: &'i I) -> &'i str
    where
        I: Resolver<TokenKey> + ?Sized,
    {
        // one of the two must be present upon construction
        self.static_text().or_else(|| self.green().text(resolver)).unwrap()
    }

    /// If the [syntax kind](Syntax) of this token always represents the same text, returns
    /// that text.
    ///
    /// # Examples
    /// If there is a syntax kind `Plus` that represents just the `+` operator and we implement
    /// [`Syntax::static_text`] for it, we can retrieve this text in the resulting syntax tree.
    ///
    /// ```
    /// # use cstree::testing::*;
    /// # use cstree::build::*;
    /// let mut builder: GreenNodeBuilder<MySyntax> = GreenNodeBuilder::new();
    /// # builder.start_node(Root);
    /// # builder.token(Identifier, "x");
    /// # builder.token(Whitespace, " ");
    /// # builder.token(Plus, "+");
    /// # builder.token(Whitespace, " ");
    /// # builder.token(Int, "3");
    /// # builder.finish_node();
    /// let tree = parse(&mut builder, "x + 3");
    /// # let tree: SyntaxNode<MySyntax> = SyntaxNode::new_root(builder.finish().0);
    /// let plus = tree
    ///     .children_with_tokens()
    ///     .nth(2) // `x`, then a space, then `+`
    ///     .unwrap()
    ///     .into_token()
    ///     .unwrap();
    /// assert_eq!(plus.static_text(), Some("+"));
    /// ```
    #[inline(always)]
    pub fn static_text(&self) -> Option<&'static str> {
        S::static_text(self.kind())
    }

    /// Returns `true` if `self` and `other` represent equal source text.
    ///
    /// This method is different from the `PartialEq` and `Eq` implementations in that it compares
    /// only the token text and not its source position.
    /// It is more efficient than comparing the result of
    /// [`resolve_text`](SyntaxToken::resolve_text) because it compares the tokens' interned
    /// [`text_key`s](SyntaxToken::text_key) (if their text is not static) or their kind (if it is).
    /// Therefore, it also does not require a [`Resolver`].
    ///
    /// **Note** that the result of the comparison may be wrong when comparing two tokens from
    /// different trees that use different interners.
    ///  
    /// # Examples
    /// ```
    /// # use cstree::testing::*;
    /// let mut builder: GreenNodeBuilder<MySyntax> = GreenNodeBuilder::new();
    /// # builder.start_node(Root);
    /// # builder.token(Identifier, "x");
    /// # builder.token(Whitespace, " ");
    /// # builder.token(Plus, "+");
    /// # builder.token(Whitespace, " ");
    /// # builder.token(Identifier, "x");
    /// # builder.token(Whitespace, " ");
    /// # builder.token(Plus, "+");
    /// # builder.token(Int, "3");
    /// # builder.finish_node();
    /// let tree = parse(&mut builder, "x + x + 3");
    /// # let tree: SyntaxNode<MySyntax> = SyntaxNode::new_root(builder.finish().0);
    /// let mut tokens = tree.children_with_tokens();
    /// let tokens = tokens.by_ref();
    /// let first_x = tokens.next().unwrap().into_token().unwrap();
    ///
    /// // For the other tokens, skip over the whitespace between them
    /// let first_plus = tokens.skip(1).next().unwrap().into_token().unwrap();
    /// let second_x = tokens.skip(1).next().unwrap().into_token().unwrap();
    /// let second_plus = tokens.skip(1).next().unwrap().into_token().unwrap();
    /// assert!(first_x.text_eq(&second_x));
    /// assert!(first_plus.text_eq(&second_plus));
    /// ```
    #[inline]
    pub fn text_eq(&self, other: &Self) -> bool {
        if let Some(k1) = self.green().text_key() {
            match other.green().text_key() {
                Some(k2) => return k1 == k2,
                None => return false, // a kind with static text cannot be equal to one with non-static text
            }
        }

        debug_assert!(self.static_text().is_some());
        debug_assert!(other.static_text().is_some());
        self.syntax_kind() == other.syntax_kind()
    }

    /// Returns the interned key of text covered by this token, if any.
    /// This key may be used for comparisons with other keys of strings interned by the same interner.
    ///
    /// See also [`resolve_text`](SyntaxToken::resolve_text) and [`text_eq`](SyntaxToken::text_eq).
    ///
    /// # Examples
    /// If you intern strings inside of your application, like inside a compiler, you can use
    /// token's text keys to cross-reference between the syntax tree and the rest of your
    /// implementation by re-using the interner in both.
    /// ```
    /// # use cstree::testing::*;
    /// use cstree::interning::{TokenInterner, TokenKey, new_interner};
    /// struct TypeTable {
    ///     // ...
    /// }
    /// impl TypeTable {
    ///     fn type_of(&self, ident: TokenKey) -> &str {
    ///         // ...
    /// #     ""
    ///     }
    /// }
    /// # struct State {
    /// #   interner: TokenInterner,
    /// #   type_table: TypeTable,
    /// # }
    /// let interner = new_interner();
    /// let mut state = State {
    ///     interner,
    ///     type_table: TypeTable{ /* stuff */},
    /// };
    /// let mut builder: GreenNodeBuilder<MySyntax, TokenInterner> =
    ///     GreenNodeBuilder::with_interner(&mut state.interner);
    /// # let input = "";
    /// # builder.start_node(Root);
    /// # builder.token(Identifier, "x");
    /// # builder.finish_node();
    /// let tree = parse(&mut builder, "x");
    /// # let tree: SyntaxNode<MySyntax> = SyntaxNode::new_root(builder.finish().0);
    /// let type_table = &state.type_table;
    /// let ident = tree
    ///     .children_with_tokens()
    ///     .next()
    ///     .unwrap()
    ///     .into_token()
    ///     .unwrap();
    /// let typ = type_table.type_of(ident.text_key().unwrap());
    /// ```
    #[inline]
    pub fn text_key(&self) -> Option<TokenKey> {
        self.green().text_key()
    }

    /// Returns the unterlying green tree token of this token.
    #[inline]
    pub fn green(&self) -> &GreenToken {
        self.parent
            .green()
            .children()
            .nth(self.index as usize)
            .unwrap()
            .as_token()
            .unwrap()
    }

    /// The parent node of this token.
    #[inline]
    pub fn parent(&self) -> &SyntaxNode<S, D> {
        &self.parent
    }

    /// Returns an iterator along the chain of parents of this token.
    #[inline]
    pub fn ancestors(&self) -> impl Iterator<Item = &SyntaxNode<S, D>> {
        self.parent().ancestors()
    }

    /// The tree element to the right of this one, i.e. the next child of this token's parent after this token.
    #[inline]
    pub fn next_sibling_or_token(&self) -> Option<SyntaxElementRef<'_, S, D>> {
        self.parent()
            .next_child_or_token_after(self.index as usize, self.text_range().end())
    }

    /// The tree element to the left of this one, i.e. the previous child of this token's parent after this token.
    #[inline]
    pub fn prev_sibling_or_token(&self) -> Option<SyntaxElementRef<'_, S, D>> {
        self.parent()
            .prev_child_or_token_before(self.index as usize, self.text_range().start())
    }

    /// Returns an iterator over all siblings of this token in the given `direction`, i.e. all of this
    /// token's parent's children from this token on to the left or the right.
    /// The first item in the iterator will always be this token.
    #[inline]
    pub fn siblings_with_tokens(&self, direction: Direction) -> impl Iterator<Item = SyntaxElementRef<'_, S, D>> {
        let me: SyntaxElementRef<'_, S, D> = self.into();
        iter::successors(Some(me), move |el| match direction {
            Direction::Next => el.next_sibling_or_token(),
            Direction::Prev => el.prev_sibling_or_token(),
        })
    }

    /// Returns the next token in the tree.
    /// This is not necessary a direct sibling of this token, but will always be further right in the tree.
    #[inline]
    pub fn next_token(&self) -> Option<&SyntaxToken<S, D>> {
        match self.next_sibling_or_token() {
            Some(element) => element.first_token(),
            None => self
                .parent()
                .ancestors()
                .find_map(|it| it.next_sibling_or_token())
                .and_then(|element| element.first_token()),
        }
    }

    /// Returns the previous token in the tree.
    /// This is not necessary a direct sibling of this token, but will always be further left in the tree.
    #[inline]
    pub fn prev_token(&self) -> Option<&SyntaxToken<S, D>> {
        match self.prev_sibling_or_token() {
            Some(element) => element.last_token(),
            None => self
                .parent()
                .ancestors()
                .find_map(|it| it.prev_sibling_or_token())
                .and_then(|element| element.last_token()),
        }
    }
}