vhdl_syntax 0.1.0

Library to work with VHDL files
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
//! Public API for abstract and untyped nodes.
//!
//! Every language element is either represented by a [SyntaxToken] or a [SyntaxNode].
//! The SyntaxToken wraps a [Token] and therefore allows access
//! to the original source text and the kind of token. A SyntaxNode simply groups these tokens
//! and other sub-nodes to generate a tree - the abstract syntax tree.
//! For example, for the following design-file, the syntax tree might look like the following:
//! ```vhdl
//! entity foo is
//! end foo;
//! ```
//!
//! ```no-check
//!            DesignFile
//!                 |
//!          EntityDeclaration
//!                 |
//!     ------------------------------
//!     |      |    |     |     |    |
//! `Entity` `foo` `is` `end` `foo` `;`
//! ```
//!
//! # Tree traversal
//! Traversal cen be accommodated by the many methods on `SyntaxNode`, such as
//! [SyntaxNode::children], [SyntaxNode::parent] or [SyntaxNode::ancestors]. To traverse the tree
//! in textual pre-order, for example, to search for a node of a certain type, use
//! [Preorder](crate::syntax::Preorder).
//!
//! # Mutability
//! Once created, a [SyntaxNode] is immutable. To change the tree, for example, for refactorings,
//! use the [Rewriter]. This creates a new tree from the old tree while replacing selected nodes
//! with new ones.
//!
//! # Efficiency
//! This implementation takes inspiration from and is a mixture between
//! [rowan](https://github.com/rust-analyzer/rowan) and swift's
//! [libsyntax](https://github.com/swiftlang/swift-syntax). The basic idea is that there is
//! a simple, underlying tree (the green tree) that contains all relevant data. `SyntaxNode`s,
//! that allow for traversal of siblings or parents, are generated on-the fly while traversing
//! the tree. This allows the actual tree to be quite small, yet traversal is still reasonably
//! efficient.
//!
//! Note that currently the main effort is on creating a public and well-tested API.
//! Many known optimization possibilities are currently ignored.
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at http://mozilla.org/MPL/2.0/.
//
// Copyright (c)  2025, Lukas Scheller lukasscheller@icloud.com

use crate::latin_1::{Latin1Str, Latin1String, Utf8ToLatin1Error};
use crate::syntax::child::Child;
use crate::syntax::green::{GreenChild, GreenNode, GreenToken};
use crate::syntax::node_kind::NodeKind;
use crate::syntax::rewrite::{RewriteAction, Rewriter};
use crate::tokens::{Token, TokenKind, Trivia};
use std::fmt::Debug;
use std::io::{self, Write};
use std::iter;
use std::sync::Arc;

pub type SyntaxElement = Child<SyntaxNode, SyntaxToken>;

impl SyntaxElement {
    pub(crate) fn green(&self) -> GreenChild {
        match self {
            SyntaxElement::Node(node) => GreenChild::Node(node.green().clone()),
            SyntaxElement::Token(token) => GreenChild::Token(token.green().clone()),
        }
    }
}

impl SyntaxElement {
    pub fn next_sibling_or_token(&self) -> Option<SyntaxElement> {
        match self {
            Child::Node(node) => node.next_sibling_or_token(),
            Child::Token(token) => token.next_sibling_or_token(),
        }
    }

    pub fn parent(&self) -> Option<SyntaxNode> {
        match self {
            Child::Node(node) => node.parent(),
            Child::Token(token) => Some(token.parent()),
        }
    }
}

impl From<SyntaxNode> for SyntaxElement {
    fn from(value: SyntaxNode) -> Self {
        Self::Node(value)
    }
}

impl From<SyntaxToken> for SyntaxElement {
    fn from(value: SyntaxToken) -> Self {
        Self::Token(value)
    }
}

#[derive(Clone, Eq, PartialEq)]
pub struct SyntaxToken(Arc<SyntaxTokenData>);

impl Debug for SyntaxToken {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("SyntaxToken")
            .field("kind", &self.green().kind())
            .field("leading_trivia", &self.green().leading_trivia())
            .field("text", &self.green().text())
            .finish()
    }
}

impl SyntaxToken {
    pub(crate) fn new(
        offset: usize,
        index: usize,
        parent: SyntaxNode,
        green: GreenToken,
    ) -> SyntaxToken {
        SyntaxToken(Arc::new(SyntaxTokenData {
            offset,
            index,
            parent,
            green,
        }))
    }

    pub fn offset(&self) -> usize {
        self.0.offset
    }

    pub fn token(&self) -> &Token {
        self.green().token()
    }

    pub fn byte_len(&self) -> usize {
        self.green().byte_len()
    }

    pub fn kind(&self) -> TokenKind {
        self.green().kind()
    }

    /// Returns all trivia between this token and the previous one, resp. only the leading trivia
    /// of this token, if there is no previous token.
    pub fn leading_trivia(&self) -> &Trivia {
        self.green().leading_trivia()
    }

    /// Returns all trailing trivia between this token and the next one, resp. only the trailing
    /// trivia of this token, if there is no next token.
    /// TODO: After trivia-interning, we should be able to return `&Trivia` here, similar to `leading_trivia`
    pub fn trailing_trivia(&self) -> Trivia {
        self.next_token()
            .map(|tok| tok.leading_trivia().to_owned())
            .unwrap_or_default()
    }

    pub fn text(&self) -> &Latin1Str {
        self.green().text()
    }

    pub fn parent(&self) -> SyntaxNode {
        self.0.parent.clone()
    }

    pub fn text_pos(&self) -> usize {
        self.0.offset
    }

    pub fn ancestors(&self) -> impl Iterator<Item = SyntaxNode> {
        iter::successors(Some(self.parent()), SyntaxNode::parent)
    }

    fn siblings(&self) -> impl Iterator<Item = SyntaxElement> + use<'_> {
        self.0.parent.children_with_tokens()
    }

    pub fn prev_sibling_or_token(&self) -> Option<SyntaxElement> {
        self.siblings().nth(self.index().checked_sub(1)?)
    }

    pub fn next_sibling_or_token(&self) -> Option<SyntaxElement> {
        self.siblings().nth(self.index().checked_add(1)?)
    }

    pub fn prev_token(&self) -> Option<SyntaxToken> {
        match self.prev_sibling_or_token() {
            Some(element) => element.last_token(),
            None => self
                .ancestors()
                .find_map(|it| it.prev_sibling_or_token())
                .and_then(|element| element.last_token()),
        }
    }

    pub fn next_token(&self) -> Option<SyntaxToken> {
        match self.next_sibling_or_token() {
            Some(element) => element.first_token(),
            None => self
                .ancestors()
                .find_map(|node| node.next_sibling_or_token())
                .and_then(|element| element.first_token()),
        }
    }

    pub fn clone_with_text(&self, text: impl Into<Box<Latin1Str>>) -> SyntaxToken {
        let token = Token::new(self.kind(), text, self.green().leading_trivia().clone());
        self.clone_with_token(token)
    }

    pub fn clone_with_leading_trivia(&self, trivia: Trivia) -> SyntaxToken {
        let token = Token::new(self.kind(), self.green().text(), trivia);
        self.clone_with_token(token)
    }

    /// Clones all content (trivia, kind) of this token but changes the textual representation.
    ///
    /// If the text is not valid `Latin1`, a `Utf8ToLatin1Error` will be returned.
    pub fn clone_with_utf8_text(
        &self,
        text: impl AsRef<str>,
    ) -> Result<SyntaxToken, Utf8ToLatin1Error> {
        Ok(self.clone_with_text(Latin1String::from_utf8(text.as_ref())?))
    }

    pub fn clone_with_token(&self, token: Token) -> SyntaxToken {
        let green = GreenToken::new(token);
        SyntaxToken::new(self.offset(), self.index(), self.parent(), green)
    }

    pub(crate) fn green(&self) -> &GreenToken {
        &self.0.green
    }

    fn index(&self) -> usize {
        self.0.index
    }

    pub fn write_to(&self, writer: &mut impl Write) -> io::Result<()> {
        self.green().write_to(writer)
    }

    /// Returns `true` when this token is the last child of its parent
    pub fn is_first_sibling(&self) -> bool {
        self.prev_sibling_or_token().is_none()
    }

    /// Returns `true` when this token is the last child of its parent
    pub fn is_last_sibling(&self) -> bool {
        self.next_sibling_or_token().is_none()
    }
}

#[derive(Debug, Eq, PartialEq)]
pub struct SyntaxTokenData {
    offset: usize,
    index: usize,
    parent: SyntaxNode,
    green: GreenToken,
}

#[derive(Clone, Eq, PartialEq)]
pub struct SyntaxNode(Arc<SyntaxNodeData>);

impl Debug for SyntaxNode {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("SyntaxNode")
            .field("kind", &self.kind())
            .field("children", &self.children_with_tokens().collect::<Vec<_>>())
            .finish()
    }
}

#[derive(Debug, Eq, PartialEq)]
pub struct SyntaxNodeData {
    offset: usize,
    index: usize,
    parent: Option<SyntaxNode>,
    green: GreenNode,
}

impl SyntaxNode {
    pub fn parent(&self) -> Option<SyntaxNode> {
        self.0.parent.clone()
    }

    pub fn kind(&self) -> NodeKind {
        self.0.green.kind()
    }

    pub fn offset(&self) -> usize {
        self.0.offset
    }

    pub fn byte_len(&self) -> usize {
        self.green().byte_len()
    }

    pub fn children_with_tokens(
        &self,
    ) -> impl Iterator<Item = Child<SyntaxNode, SyntaxToken>> + use<'_> {
        let parent_offset = self.offset();
        self.green()
            .children()
            .enumerate()
            .scan(0usize, move |run, (i, child)| {
                let child_offset = parent_offset + *run;
                *run += child.byte_len();
                Some(match child {
                    Child::Token(t) => {
                        Child::Token(SyntaxToken::new(child_offset, i, self.clone(), t.clone()))
                    }
                    Child::Node(n) => Child::Node(SyntaxNode::new_child(
                        child_offset,
                        i,
                        self.clone(),
                        n.clone(),
                    )),
                })
            })
    }

    pub fn children(&self) -> impl Iterator<Item = SyntaxNode> + use<'_> {
        self.children_with_tokens()
            .filter_map(|child| child.as_node())
    }

    pub fn first_token(&self) -> Option<SyntaxToken> {
        self.children_with_tokens()
            .filter_map(|node| match node {
                SyntaxElement::Node(n) => n.first_token(),
                SyntaxElement::Token(t) => Some(t),
            })
            .next()
    }

    pub fn tokens(&self) -> impl Iterator<Item = SyntaxToken> + use<'_> {
        self.children_with_tokens()
            .filter_map(|element| element.as_token())
    }

    pub fn first_child(&self) -> Option<SyntaxNode> {
        self.children().next()
    }

    pub fn first_child_or_token(&self) -> Option<SyntaxElement> {
        self.children_with_tokens().next()
    }

    pub fn nth_child(&self, n: usize) -> Option<SyntaxNode> {
        self.children().nth(n)
    }

    pub fn nth_child_or_token(&self, n: usize) -> Option<Child<SyntaxNode, SyntaxToken>> {
        self.children_with_tokens().nth(n)
    }

    pub fn prev_sibling(&self) -> Option<Child<SyntaxNode, SyntaxToken>> {
        self.parent()?
            .nth_child_or_token(self.index().checked_sub(1)?)
    }

    pub fn next_sibling(&self) -> Option<SyntaxNode> {
        self.parent()?.nth_child(self.0.index + 1)
    }

    pub fn last_token(&self) -> Option<SyntaxToken> {
        self.last_child_or_token()?.last_token()
    }

    pub fn last_child_or_token(&self) -> Option<SyntaxElement> {
        self.children_with_tokens().last()
    }

    pub fn ancestors(&self) -> impl Iterator<Item = SyntaxNode> {
        iter::successors(Some(self.clone()), SyntaxNode::parent)
    }

    pub fn rewrite(&self, rewrite: impl FnMut(&SyntaxElement) -> RewriteAction) -> SyntaxNode {
        Rewriter::new(rewrite).rewrite(self.clone())
    }

    pub fn rewrite_nodes(&self, rewrite: impl Fn(&SyntaxNode) -> RewriteAction) -> SyntaxNode {
        Rewriter::new(|element| match element {
            SyntaxElement::Node(node) => rewrite(node),
            SyntaxElement::Token(_) => RewriteAction::Leave,
        })
        .rewrite(self.clone())
    }

    pub fn rewrite_tokens(&self, rewrite: impl Fn(&SyntaxToken) -> RewriteAction) -> SyntaxNode {
        Rewriter::new(|element| match element {
            SyntaxElement::Node(_) => RewriteAction::Leave,
            SyntaxElement::Token(token) => rewrite(token),
        })
        .rewrite(self.clone())
    }

    pub(crate) fn new_root(green: GreenNode) -> SyntaxNode {
        SyntaxNode(Arc::new(SyntaxNodeData {
            offset: 0,
            index: 0,
            parent: None,
            green,
        }))
    }

    pub(crate) fn green(&self) -> &GreenNode {
        &self.0.green
    }

    fn new_child(offset: usize, index: usize, parent: SyntaxNode, green: GreenNode) -> SyntaxNode {
        SyntaxNode(Arc::new(SyntaxNodeData {
            offset,
            index,
            parent: Some(parent),
            green,
        }))
    }

    fn index(&self) -> usize {
        self.0.index
    }

    fn prev_sibling_or_token(&self) -> Option<SyntaxElement> {
        self.parent()?
            .children_with_tokens()
            .nth(self.index().checked_sub(1)?)
    }

    fn next_sibling_or_token(&self) -> Option<SyntaxElement> {
        self.parent()?
            .children_with_tokens()
            .nth(self.index().checked_add(1)?)
    }

    #[cfg(test)]
    pub(crate) fn test_text(&self) -> String {
        self.green().test_text(0)
    }

    pub fn write_to(&self, writer: &mut impl Write) -> io::Result<()> {
        self.green().write_to(writer)
    }
}

impl SyntaxElement {
    pub fn last_token(&self) -> Option<SyntaxToken> {
        match self {
            Child::Token(token) => Some(token.clone()),
            Child::Node(node) => node.last_token(),
        }
    }

    pub fn first_token(&self) -> Option<SyntaxToken> {
        match self {
            SyntaxElement::Node(node) => node.first_token(),
            SyntaxElement::Token(token) => Some(token.clone()),
        }
    }

    pub fn offset(&self) -> usize {
        match self {
            Child::Token(token) => token.text_pos(),
            Child::Node(node) => node.offset(),
        }
    }

    pub fn byte_len(&self) -> usize {
        match self {
            Child::Token(token) => token.byte_len(),
            Child::Node(node) => node.byte_len(),
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::syntax::green::{GreenChild, GreenNode, GreenNodeData};
    use crate::syntax::node::{SyntaxElement, SyntaxNode};
    use crate::syntax::node_kind::NodeKind::*;
    use crate::syntax::rewrite::RewriteAction;
    use crate::tokens::Tokenize;
    use crate::tokens::{Keyword, Token, TokenKind, Trivia, TriviaPiece};
    use pretty_assertions::assert_eq;
    use std::collections::VecDeque;

    #[test]
    fn no_leading_trivia() {
        let token = Token::simple(TokenKind::Keyword(Keyword::Entity), b"entity");
        let mut green_node = GreenNodeData::new(EntityDeclaration);
        green_node.push_token(token);
        let node = SyntaxNode::new_root(GreenNode::new(green_node));
        assert_eq!(
            node.first_token().unwrap().leading_trivia(),
            &Trivia::default()
        );
    }

    #[test]
    fn leading_trivia_no_previous_token() {
        let token = Token::new(
            TokenKind::Keyword(Keyword::Entity),
            b"entity",
            Trivia::from([TriviaPiece::Spaces(2), TriviaPiece::LineFeeds(1)]),
        );
        let mut green_node = GreenNodeData::new(EntityDeclaration);
        green_node.push_token(token);
        let node = SyntaxNode::new_root(GreenNode::new(green_node));
        assert_eq!(
            node.first_token().unwrap().leading_trivia(),
            &Trivia::from([TriviaPiece::Spaces(2), TriviaPiece::LineFeeds(1)])
        );
    }

    #[test]
    fn leading_trivia_with_previous_token() {
        let tokens = [
            Token::new(
                TokenKind::Keyword(Keyword::Entity),
                b"entity",
                Trivia::from([TriviaPiece::Spaces(2), TriviaPiece::LineFeeds(1)]),
            ),
            Token::new(
                TokenKind::Identifier,
                b"foo",
                Trivia::from([TriviaPiece::Spaces(2), TriviaPiece::LineFeeds(1)]),
            ),
        ];
        let mut green_node = GreenNodeData::new(EntityDeclaration);
        green_node.push_tokens(tokens);
        let node = SyntaxNode::new_root(GreenNode::new(green_node));
        assert_eq!(
            node.tokens().nth(1).unwrap().leading_trivia(),
            &Trivia::from([TriviaPiece::Spaces(2), TriviaPiece::LineFeeds(1)])
        );
    }

    #[test]
    fn no_trailing_trivia() {
        let token = Token::simple(TokenKind::Keyword(Keyword::Entity), b"entity");
        let mut green_node = GreenNodeData::new(EntityDeclaration);
        green_node.push_token(token);
        let node = SyntaxNode::new_root(GreenNode::new(green_node));
        assert_eq!(
            node.first_token().unwrap().trailing_trivia(),
            Trivia::default()
        );
    }

    #[test]
    fn trailing_trivia_with_next_token() {
        let tokens = [
            Token::new(
                TokenKind::Keyword(Keyword::Entity),
                b"entity",
                Trivia::from([TriviaPiece::Spaces(2), TriviaPiece::LineFeeds(1)]),
            ),
            Token::new(
                TokenKind::Identifier,
                b"foo",
                Trivia::from([TriviaPiece::Spaces(2), TriviaPiece::LineFeeds(1)]),
            ),
        ];
        let mut green_node = GreenNodeData::new(EntityDeclaration);
        green_node.push_tokens(tokens);
        let node = SyntaxNode::new_root(GreenNode::new(green_node));
        assert_eq!(
            node.first_token().unwrap().trailing_trivia(),
            Trivia::from([TriviaPiece::Spaces(2), TriviaPiece::LineFeeds(1)])
        );
    }

    #[test]
    fn no_rewrite_is_noop() {
        let orig_tokens = "entity foo is end foo".tokenize().collect::<Vec<_>>();
        let mut data = GreenNodeData::new(EntityDeclaration);
        data.push_tokens(orig_tokens.clone());
        let node = SyntaxNode::new_root(GreenNode::new(data));
        let new_node = node.rewrite(|_| RewriteAction::Leave);
        let new_tokens = new_node
            .tokens()
            .map(|syntax_token| syntax_token.token().clone())
            .collect::<Vec<_>>();
        assert_eq!(new_tokens, orig_tokens);
    }

    #[test]
    fn rewrite_tokens() {
        let mut data = GreenNodeData::new(EntityDeclaration);
        data.push_tokens("entity foo is end foo;".tokenize());
        let node = SyntaxNode::new_root(GreenNode::new(data));
        let new_node = node.rewrite_tokens(|tok| {
            if tok.text() == "foo" {
                RewriteAction::Change(SyntaxElement::Token(tok.clone_with_text(b"bar")))
            } else {
                RewriteAction::Leave
            }
        });
        let new_tokens = new_node
            .tokens()
            .map(|syntax_token| syntax_token.token().clone())
            .collect::<VecDeque<_>>();
        assert_eq!(
            new_tokens,
            "entity bar is end bar;".tokenize().collect::<Vec<_>>()
        );
    }

    #[test]
    fn rewrite_does_not_modify_self() {
        let orig_tokens = "entity foo is end foo".tokenize().collect::<Vec<_>>();
        let mut data = GreenNodeData::new(EntityDeclaration);
        data.push_tokens(orig_tokens.clone());
        let node = SyntaxNode::new_root(GreenNode::new(data));
        let new_node = node.rewrite_nodes(|node| match node.kind() {
            EntityDeclaration => panic!("Should not modify self"),
            _ => RewriteAction::Leave,
        });
        let new_tokens = new_node
            .tokens()
            .map(|syntax_token| syntax_token.token().clone())
            .collect::<VecDeque<_>>();
        assert_eq!(new_tokens, orig_tokens);
    }

    #[test]
    fn next_token() {
        let mut top = GreenNodeData::new(DesignFile);
        let mut n1 = GreenNodeData::new(EntityDeclaration);
        n1.push_tokens([
            Token::simple(TokenKind::Keyword(Keyword::Entity), b"entity"),
            Token::simple(TokenKind::Identifier, b"foo"),
        ]);
        let n1 = GreenNode::new(n1);
        let mut n2 = GreenNodeData::new(ArchitectureBody);
        n2.push_tokens([
            Token::simple(TokenKind::Keyword(Keyword::Architecture), b"architecture"),
            Token::simple(TokenKind::Identifier, b"bar"),
        ]);
        let n2 = GreenNode::new(n2);

        top.push_children([GreenChild::Node(n1), GreenChild::Node(n2)]);

        let s = SyntaxNode::new_root(GreenNode::new(top));
        let first_token = s.first_token().expect("Node must have first token");
        assert!(first_token.kind() == TokenKind::Keyword(Keyword::Entity));
        // Same node
        let second_token = first_token
            .next_token()
            .expect("Node must have second token");
        assert!(second_token.kind() == TokenKind::Identifier);
        // Neighbor node
        let third_token = second_token
            .next_token()
            .expect("Node must have second token");
        assert!(third_token.kind() == TokenKind::Keyword(Keyword::Architecture));
    }
}