ludtwig-parser 0.9.0

Lossless parser for HTML / Twig templating syntax.
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
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
//! This module contains all abstract syntax tree (AST) types.
//! All of them implement the [AstNode] trait.
//!
//! Some of them come with extra utility methods, to quickly access some data
//! (e.g. [TwigBlock::name]).
//!
//! An overview of the syntax tree concept can be found
//! at the [crate level documentation](crate#syntax-trees).

use rowan::NodeOrToken;
pub use rowan::ast::AstChildren;
pub use rowan::ast::AstNode;
pub use rowan::ast::support;
use std::fmt::{Debug, Display, Formatter};

use crate::T;

use super::untyped::{
    SyntaxElement, SyntaxKind, SyntaxNode, SyntaxToken, TemplateLanguage, debug_tree,
};

/// So far, we've been working with a homogeneous untyped tree.
/// It's nice to provide generic tree operations, like traversals,
/// but it's a bad fit for semantic analysis.
/// The rowan crate itself does not provide AST facilities directly,
/// but it is possible to layer AST on top of `SyntaxNode` API.
///
/// Let's define AST nodes.
/// It'll be quite a bunch of repetitive code, so we'll use a macro.
///
/// For a real language, you'd want to generate an AST. I find a
/// combination of `serde`, `ron` and `tera` crates invaluable for that!
macro_rules! ast_node {
    ($ast:ident, $kind:path) => {
        #[derive(Clone, PartialEq, Eq, Hash)]
        pub struct $ast {
            pub(crate) syntax: SyntaxNode,
        }

        impl AstNode for $ast {
            type Language = TemplateLanguage;

            fn can_cast(kind: <Self::Language as rowan::Language>::Kind) -> bool
            where
                Self: Sized,
            {
                kind == $kind
            }

            fn cast(node: rowan::SyntaxNode<Self::Language>) -> Option<Self>
            where
                Self: Sized,
            {
                if Self::can_cast(node.kind()) {
                    Some(Self { syntax: node })
                } else {
                    None
                }
            }

            fn syntax(&self) -> &rowan::SyntaxNode<Self::Language> {
                &self.syntax
            }
        }

        impl Display for $ast {
            fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
                write!(f, "{}", self.syntax)?;
                Ok(())
            }
        }

        impl Debug for $ast {
            fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
                write!(f, "{}", debug_tree(&self.syntax))?;
                Ok(())
            }
        }
    };
}

ast_node!(TwigBlock, SyntaxKind::TWIG_BLOCK);
impl TwigBlock {
    /// Name of the twig block
    #[must_use]
    pub fn name(&self) -> Option<SyntaxToken> {
        match self.starting_block() {
            None => None,
            Some(n) => n.name(),
        }
    }

    #[must_use]
    pub fn starting_block(&self) -> Option<TwigStartingBlock> {
        support::child(&self.syntax)
    }

    #[must_use]
    pub fn body(&self) -> Option<Body> {
        support::child(&self.syntax)
    }

    #[must_use]
    pub fn ending_block(&self) -> Option<TwigEndingBlock> {
        support::child(&self.syntax)
    }
}

ast_node!(TwigStartingBlock, SyntaxKind::TWIG_STARTING_BLOCK);
impl TwigStartingBlock {
    /// Name of the twig block
    #[must_use]
    pub fn name(&self) -> Option<SyntaxToken> {
        support::token(&self.syntax, T![word])
    }

    /// Parent complete twig block
    #[must_use]
    pub fn twig_block(&self) -> Option<TwigBlock> {
        match self.syntax.parent() {
            Some(p) => TwigBlock::cast(p),
            None => None,
        }
    }
}

ast_node!(TwigEndingBlock, SyntaxKind::TWIG_ENDING_BLOCK);
impl TwigEndingBlock {
    /// Parent complete twig block
    #[must_use]
    pub fn twig_block(&self) -> Option<TwigBlock> {
        match self.syntax.parent() {
            Some(p) => TwigBlock::cast(p),
            None => None,
        }
    }
}

ast_node!(HtmlTag, SyntaxKind::HTML_TAG);
impl HtmlTag {
    /// Name of the tag
    #[must_use]
    pub fn name(&self) -> Option<SyntaxToken> {
        match self.starting_tag() {
            Some(n) => n.name(),
            None => None,
        }
    }

    /// Returns true if the tag doesn't have an ending tag
    #[must_use]
    pub fn is_self_closing(&self) -> bool {
        self.ending_tag().is_none()
    }

    /// Attributes of the tag
    #[must_use]
    pub fn attributes(&self) -> AstChildren<HtmlAttribute> {
        match self.starting_tag() {
            Some(n) => n.attributes(),
            // create an iterator for HtmlAttribute over the tag itself, which should yield no results
            None => support::children(&self.syntax),
        }
    }

    /// if the tag is a twig component, e.g. '<twig:my:component />'
    #[must_use]
    pub fn is_twig_component(&self) -> bool {
        match self.starting_tag() {
            Some(n) => n.is_twig_component(),
            None => false,
        }
    }

    #[must_use]
    pub fn starting_tag(&self) -> Option<HtmlStartingTag> {
        support::child(&self.syntax)
    }

    #[must_use]
    pub fn body(&self) -> Option<Body> {
        support::child(&self.syntax)
    }

    #[must_use]
    pub fn ending_tag(&self) -> Option<HtmlEndingTag> {
        support::child(&self.syntax)
    }
}

ast_node!(HtmlStartingTag, SyntaxKind::HTML_STARTING_TAG);
impl HtmlStartingTag {
    /// Name of the tag
    #[must_use]
    pub fn name(&self) -> Option<SyntaxToken> {
        self.syntax
            .children_with_tokens()
            .filter_map(NodeOrToken::into_token)
            .find(|it| it.kind() == T![word] || it.kind() == T![twig component name])
    }

    /// Attributes of the tag
    #[must_use]
    pub fn attributes(&self) -> AstChildren<HtmlAttribute> {
        match support::child::<HtmlAttributeList>(&self.syntax) {
            Some(list) => support::children(&list.syntax),
            // create an iterator for HtmlAttribute over the startingTag itself, which should yield no results
            None => support::children(&self.syntax),
        }
    }

    /// Parent complete html tag
    #[must_use]
    pub fn html_tag(&self) -> Option<HtmlTag> {
        match self.syntax.parent() {
            Some(p) => HtmlTag::cast(p),
            None => None,
        }
    }

    /// if the tag is a twig component, e.g. '<twig:my:component />'
    #[must_use]
    pub fn is_twig_component(&self) -> bool {
        support::token(&self.syntax, T![twig component name]).is_some()
    }
}

ast_node!(HtmlAttribute, SyntaxKind::HTML_ATTRIBUTE);
impl HtmlAttribute {
    /// Name of the attribute (left side of the equal sign)
    #[must_use]
    pub fn name(&self) -> Option<SyntaxToken> {
        support::token(&self.syntax, T![word])
    }

    /// Value of the attribute
    #[must_use]
    pub fn value(&self) -> Option<HtmlString> {
        support::child(&self.syntax)
    }

    /// Parent starting html tag
    #[must_use]
    pub fn html_tag(&self) -> Option<HtmlStartingTag> {
        // first parent is HtmlAttributeList, the parent of that is the tag itself
        match self.syntax.parent()?.parent() {
            Some(p) => HtmlStartingTag::cast(p),
            None => None,
        }
    }
}

ast_node!(HtmlEndingTag, SyntaxKind::HTML_ENDING_TAG);
impl HtmlEndingTag {
    /// Name of the tag
    #[must_use]
    pub fn name(&self) -> Option<SyntaxToken> {
        self.syntax
            .children_with_tokens()
            .filter_map(NodeOrToken::into_token)
            .find(|it| it.kind() == T![word] || it.kind() == T![twig component name])
    }

    /// Parent complete html tag
    #[must_use]
    pub fn html_tag(&self) -> Option<HtmlTag> {
        match self.syntax.parent() {
            Some(p) => HtmlTag::cast(p),
            None => None,
        }
    }

    /// if the tag is a twig component, e.g. '</twig:my:component>'
    #[must_use]
    pub fn is_twig_component(&self) -> bool {
        support::token(&self.syntax, T![twig component name]).is_some()
    }
}

ast_node!(TwigBinaryExpression, SyntaxKind::TWIG_BINARY_EXPRESSION);
impl TwigBinaryExpression {
    #[must_use]
    pub fn operator(&self) -> Option<SyntaxToken> {
        self.syntax
            .children_with_tokens()
            .find_map(|element| match element {
                SyntaxElement::Token(t) if !t.kind().is_trivia() => Some(t),
                _ => None,
            })
    }

    #[must_use]
    pub fn lhs_expression(&self) -> Option<TwigExpression> {
        self.syntax.children().find_map(TwigExpression::cast)
    }

    #[must_use]
    pub fn rhs_expression(&self) -> Option<TwigExpression> {
        self.syntax
            .children()
            .filter_map(TwigExpression::cast)
            .nth(1)
    }
}

ast_node!(
    LudtwigDirectiveRuleList,
    SyntaxKind::LUDTWIG_DIRECTIVE_RULE_LIST
);
impl LudtwigDirectiveRuleList {
    #[must_use]
    pub fn get_rule_names(&self) -> Vec<String> {
        self.syntax
            .children_with_tokens()
            .filter_map(|element| match element {
                NodeOrToken::Token(t) if t.kind() == SyntaxKind::TK_WORD => {
                    Some(t.text().to_string())
                }
                _ => None,
            })
            .collect()
    }
}

ast_node!(
    LudtwigDirectiveFileIgnore,
    SyntaxKind::LUDTWIG_DIRECTIVE_FILE_IGNORE
);
impl LudtwigDirectiveFileIgnore {
    #[must_use]
    pub fn get_rules(&self) -> Vec<String> {
        match support::child::<LudtwigDirectiveRuleList>(&self.syntax) {
            Some(rule_list) => rule_list.get_rule_names(),
            None => vec![],
        }
    }
}

ast_node!(LudtwigDirectiveIgnore, SyntaxKind::LUDTWIG_DIRECTIVE_IGNORE);
impl LudtwigDirectiveIgnore {
    #[must_use]
    pub fn get_rules(&self) -> Vec<String> {
        match support::child::<LudtwigDirectiveRuleList>(&self.syntax) {
            Some(rule_list) => rule_list.get_rule_names(),
            None => vec![],
        }
    }
}

ast_node!(TwigLiteralString, SyntaxKind::TWIG_LITERAL_STRING);
impl TwigLiteralString {
    #[must_use]
    pub fn get_inner(&self) -> Option<TwigLiteralStringInner> {
        support::child(&self.syntax)
    }

    #[must_use]
    pub fn get_opening_quote(&self) -> Option<SyntaxToken> {
        self.syntax
            .children_with_tokens()
            .take_while(|element| {
                if element.as_node().is_some() {
                    return false; // found inner string node
                }

                true
            })
            .find_map(|element| match element {
                // first non trivia token should be a quote
                NodeOrToken::Token(t) if !t.kind().is_trivia() => Some(t),
                _ => None,
            })
    }

    #[must_use]
    pub fn get_closing_quote(&self) -> Option<SyntaxToken> {
        self.syntax
            .children_with_tokens()
            .skip_while(|element| {
                if element.as_node().is_some() {
                    return false; // found inner string node, stop skipping
                }

                true
            })
            .find_map(|element| match element {
                // first non trivia token should be a quote
                NodeOrToken::Token(t) if !t.kind().is_trivia() => Some(t),
                _ => None,
            })
    }
}

ast_node!(
    TwigLiteralStringInner,
    SyntaxKind::TWIG_LITERAL_STRING_INNER
);
impl TwigLiteralStringInner {
    #[must_use]
    pub fn get_interpolations(&self) -> AstChildren<TwigLiteralStringInterpolation> {
        support::children(&self.syntax)
    }
}

ast_node!(HtmlString, SyntaxKind::HTML_STRING);
impl HtmlString {
    #[must_use]
    pub fn get_inner(&self) -> Option<HtmlStringInner> {
        support::child(&self.syntax)
    }

    #[must_use]
    pub fn get_opening_quote(&self) -> Option<SyntaxToken> {
        self.syntax
            .children_with_tokens()
            .take_while(|element| {
                if element.as_node().is_some() {
                    return false; // found inner string node
                }

                true
            })
            .find_map(|element| match element {
                // first non trivia token should be a quote
                NodeOrToken::Token(t) if !t.kind().is_trivia() => Some(t),
                _ => None,
            })
    }

    #[must_use]
    pub fn get_closing_quote(&self) -> Option<SyntaxToken> {
        self.syntax
            .children_with_tokens()
            .skip_while(|element| {
                if element.as_node().is_some() {
                    return false; // found inner string node, stop skipping
                }

                true
            })
            .find_map(|element| match element {
                // first non trivia token should be a quote
                NodeOrToken::Token(t) if !t.kind().is_trivia() => Some(t),
                _ => None,
            })
    }
}

ast_node!(TwigExtends, SyntaxKind::TWIG_EXTENDS);
impl TwigExtends {
    #[must_use]
    pub fn get_extends_keyword(&self) -> Option<SyntaxToken> {
        support::token(&self.syntax, T!["extends"])
    }
}

ast_node!(TwigVar, SyntaxKind::TWIG_VAR);
impl TwigVar {
    #[must_use]
    pub fn get_expression(&self) -> Option<TwigExpression> {
        support::child(&self.syntax)
    }
}

ast_node!(TwigLiteralName, SyntaxKind::TWIG_LITERAL_NAME);
impl TwigLiteralName {
    #[must_use]
    pub fn get_name(&self) -> Option<SyntaxToken> {
        support::token(&self.syntax, SyntaxKind::TK_WORD)
    }
}

ast_node!(Body, SyntaxKind::BODY);
ast_node!(TwigExpression, SyntaxKind::TWIG_EXPRESSION);
ast_node!(TwigUnaryExpression, SyntaxKind::TWIG_UNARY_EXPRESSION);
ast_node!(
    TwigParenthesesExpression,
    SyntaxKind::TWIG_PARENTHESES_EXPRESSION
);
ast_node!(
    TwigConditionalExpression,
    SyntaxKind::TWIG_CONDITIONAL_EXPRESSION
);
ast_node!(TwigOperand, SyntaxKind::TWIG_OPERAND);
ast_node!(TwigAccessor, SyntaxKind::TWIG_ACCESSOR);
ast_node!(TwigFilter, SyntaxKind::TWIG_FILTER);
impl TwigFilter {
    /// The expression on the left side of the pipe `|`.
    #[must_use]
    pub fn operand(&self) -> Option<TwigOperand> {
        support::child(&self.syntax)
    }

    /// The filter on the right side of the pipe `|`.
    /// This can be a `TwigLiteralName` or a `TwigFunctionCall` inside the returned `TwigOperand`.
    #[must_use]
    pub fn filter(&self) -> Option<TwigOperand> {
        support::children(&self.syntax).nth(1)
    }
}
ast_node!(TwigIndexLookup, SyntaxKind::TWIG_INDEX_LOOKUP);
ast_node!(TwigIndex, SyntaxKind::TWIG_INDEX);
ast_node!(TwigIndexRange, SyntaxKind::TWIG_INDEX_RANGE);
ast_node!(TwigFunctionCall, SyntaxKind::TWIG_FUNCTION_CALL);
impl TwigFunctionCall {
    /// The name of the function being called.
    /// This is an operand which should contain a `TwigLiteralName`.
    #[must_use]
    pub fn name_operand(&self) -> Option<TwigOperand> {
        support::child(&self.syntax)
    }

    /// The arguments of the function call.
    #[must_use]
    pub fn arguments(&self) -> Option<TwigArguments> {
        support::child(&self.syntax)
    }
}
ast_node!(TwigArrowFunction, SyntaxKind::TWIG_ARROW_FUNCTION);
ast_node!(TwigArguments, SyntaxKind::TWIG_ARGUMENTS);
ast_node!(TwigNamedArgument, SyntaxKind::TWIG_NAMED_ARGUMENT);
ast_node!(
    TwigLiteralStringInterpolation,
    SyntaxKind::TWIG_LITERAL_STRING_INTERPOLATION
);
ast_node!(TwigLiteralNumber, SyntaxKind::TWIG_LITERAL_NUMBER);
ast_node!(TwigLiteralArray, SyntaxKind::TWIG_LITERAL_ARRAY);
ast_node!(TwigLiteralArrayInner, SyntaxKind::TWIG_LITERAL_ARRAY_INNER);
ast_node!(TwigLiteralNull, SyntaxKind::TWIG_LITERAL_NULL);
ast_node!(TwigLiteralBoolean, SyntaxKind::TWIG_LITERAL_BOOLEAN);
ast_node!(TwigLiteralHash, SyntaxKind::TWIG_LITERAL_HASH);
ast_node!(TwigLiteralHashItems, SyntaxKind::TWIG_LITERAL_HASH_ITEMS);
ast_node!(TwigLiteralHashPair, SyntaxKind::TWIG_LITERAL_HASH_PAIR);
ast_node!(TwigLiteralHashKey, SyntaxKind::TWIG_LITERAL_HASH_KEY);
ast_node!(TwigLiteralHashValue, SyntaxKind::TWIG_LITERAL_HASH_VALUE);
ast_node!(TwigComment, SyntaxKind::TWIG_COMMENT);
ast_node!(TwigIf, SyntaxKind::TWIG_IF);
ast_node!(TwigIfBlock, SyntaxKind::TWIG_IF_BLOCK);
ast_node!(TwigElseIfBlock, SyntaxKind::TWIG_ELSE_IF_BLOCK);
ast_node!(TwigElseBlock, SyntaxKind::TWIG_ELSE_BLOCK);
ast_node!(TwigEndIfBlock, SyntaxKind::TWIG_ENDIF_BLOCK);
ast_node!(TwigSet, SyntaxKind::TWIG_SET);
ast_node!(TwigSetBlock, SyntaxKind::TWIG_SET_BLOCK);
ast_node!(TwigEndSetBlock, SyntaxKind::TWIG_ENDSET_BLOCK);
ast_node!(TwigAssignment, SyntaxKind::TWIG_ASSIGNMENT);
ast_node!(TwigFor, SyntaxKind::TWIG_FOR);
ast_node!(TwigForBlock, SyntaxKind::TWIG_FOR_BLOCK);
ast_node!(TwigForElseBlock, SyntaxKind::TWIG_FOR_ELSE_BLOCK);
ast_node!(TwigEndForBlock, SyntaxKind::TWIG_ENDFOR_BLOCK);
ast_node!(TwigInclude, SyntaxKind::TWIG_INCLUDE);
ast_node!(TwigIncludeWith, SyntaxKind::TWIG_INCLUDE_WITH);
ast_node!(TwigUse, SyntaxKind::TWIG_USE);
ast_node!(TwigOverride, SyntaxKind::TWIG_OVERRIDE);
ast_node!(TwigApply, SyntaxKind::TWIG_APPLY);
ast_node!(
    TwigApplyStartingBlock,
    SyntaxKind::TWIG_APPLY_STARTING_BLOCK
);
ast_node!(TwigApplyEndingBlock, SyntaxKind::TWIG_APPLY_ENDING_BLOCK);
ast_node!(TwigAutoescape, SyntaxKind::TWIG_AUTOESCAPE);
ast_node!(
    TwigAutoescapeStartingBlock,
    SyntaxKind::TWIG_AUTOESCAPE_STARTING_BLOCK
);
ast_node!(
    TwigAutoescapeEndingBlock,
    SyntaxKind::TWIG_AUTOESCAPE_ENDING_BLOCK
);
ast_node!(TwigDeprecated, SyntaxKind::TWIG_DEPRECATED);
ast_node!(TwigDo, SyntaxKind::TWIG_DO);
ast_node!(TwigEmbed, SyntaxKind::TWIG_EMBED);
ast_node!(
    TwigEmbedStartingBlock,
    SyntaxKind::TWIG_EMBED_STARTING_BLOCK
);
ast_node!(TwigEmbedEndingBlock, SyntaxKind::TWIG_EMBED_ENDING_BLOCK);
ast_node!(TwigFlush, SyntaxKind::TWIG_FLUSH);
ast_node!(TwigFrom, SyntaxKind::TWIG_FROM);
ast_node!(TwigImport, SyntaxKind::TWIG_IMPORT);
ast_node!(TwigSandbox, SyntaxKind::TWIG_SANDBOX);
ast_node!(
    TwigSandboxStartingBlock,
    SyntaxKind::TWIG_SANDBOX_STARTING_BLOCK
);
ast_node!(
    TwigSandboxEndingBlock,
    SyntaxKind::TWIG_SANDBOX_ENDING_BLOCK
);
ast_node!(TwigVerbatim, SyntaxKind::TWIG_VERBATIM);
ast_node!(
    TwigVerbatimStartingBlock,
    SyntaxKind::TWIG_VERBATIM_STARTING_BLOCK
);
ast_node!(
    TwigVerbatimEndingBlock,
    SyntaxKind::TWIG_VERBATIM_ENDING_BLOCK
);
ast_node!(TwigMacro, SyntaxKind::TWIG_MACRO);
ast_node!(
    TwigMacroStartingBlock,
    SyntaxKind::TWIG_MACRO_STARTING_BLOCK
);
ast_node!(TwigMacroEndingBlock, SyntaxKind::TWIG_MACRO_ENDING_BLOCK);
ast_node!(TwigWith, SyntaxKind::TWIG_WITH);
ast_node!(TwigWithStartingBlock, SyntaxKind::TWIG_WITH_STARTING_BLOCK);
ast_node!(TwigWithEndingBlock, SyntaxKind::TWIG_WITH_ENDING_BLOCK);
ast_node!(TwigCache, SyntaxKind::TWIG_CACHE);
ast_node!(TwigCacheTTL, SyntaxKind::TWIG_CACHE_TTL);
ast_node!(TwigCacheTags, SyntaxKind::TWIG_CACHE_TAGS);
ast_node!(
    TwigCacheStartingBlock,
    SyntaxKind::TWIG_CACHE_STARTING_BLOCK
);
ast_node!(TwigCacheEndingBlock, SyntaxKind::TWIG_CACHE_ENDING_BLOCK);
ast_node!(TwigProps, SyntaxKind::TWIG_PROPS);
ast_node!(TwigPropDeclaration, SyntaxKind::TWIG_PROP_DECLARATION);
ast_node!(TwigComponent, SyntaxKind::TWIG_COMPONENT);
ast_node!(
    TwigComponentStartingBlock,
    SyntaxKind::TWIG_COMPONENT_STARTING_BLOCK
);
ast_node!(
    TwigComponentEndingBlock,
    SyntaxKind::TWIG_COMPONENT_ENDING_BLOCK
);
ast_node!(ShopwareTwigExtends, SyntaxKind::SHOPWARE_TWIG_SW_EXTENDS);
ast_node!(ShopwareTwigInclude, SyntaxKind::SHOPWARE_TWIG_SW_INCLUDE);
ast_node!(
    ShopwareSilentFeatureCall,
    SyntaxKind::SHOPWARE_SILENT_FEATURE_CALL
);
ast_node!(
    ShopwareSilentFeatureCallStartingBlock,
    SyntaxKind::SHOPWARE_SILENT_FEATURE_CALL_STARTING_BLOCK
);
ast_node!(
    ShopwareSilentFeatureCallEndingBlock,
    SyntaxKind::SHOPWARE_SILENT_FEATURE_CALL_ENDING_BLOCK
);
ast_node!(ShopwareReturn, SyntaxKind::SHOPWARE_RETURN);
ast_node!(ShopwareIcon, SyntaxKind::SHOPWARE_ICON);
ast_node!(ShopwareIconStyle, SyntaxKind::SHOPWARE_ICON_STYLE);
ast_node!(ShopwareThumbnails, SyntaxKind::SHOPWARE_THUMBNAILS);
ast_node!(ShopwareThumbnailsWith, SyntaxKind::SHOPWARE_THUMBNAILS_WITH);
ast_node!(HtmlDoctype, SyntaxKind::HTML_DOCTYPE);
ast_node!(HtmlAttributeList, SyntaxKind::HTML_ATTRIBUTE_LIST);
ast_node!(HtmlStringInner, SyntaxKind::HTML_STRING_INNER);
ast_node!(HtmlText, SyntaxKind::HTML_TEXT);
ast_node!(HtmlRawText, SyntaxKind::HTML_RAW_TEXT);
ast_node!(HtmlComment, SyntaxKind::HTML_COMMENT);
ast_node!(Error, SyntaxKind::ERROR);
ast_node!(Root, SyntaxKind::ROOT);
ast_node!(TwigTrans, SyntaxKind::TWIG_TRANS);
ast_node!(
    TwigTransStartingBlock,
    SyntaxKind::TWIG_TRANS_STARTING_BLOCK
);
ast_node!(TwigTransEndingBlock, SyntaxKind::TWIG_TRANS_ENDING_BLOCK);

#[cfg(test)]
mod tests {
    use super::*;
    use crate::parse;
    use expect_test::expect;

    fn parse_and_extract<T: AstNode<Language = TemplateLanguage>>(input: &str) -> T {
        let (tree, errors) = parse(input).split();
        assert_eq!(errors, vec![]);
        support::child(&tree).unwrap()
    }

    #[test]
    fn simple_html_tag() {
        let raw = r#"<div class="hello">world {{ 42 }}</div>"#;
        let html_tag: HtmlTag = parse_and_extract(raw);

        assert_eq!(format!("{html_tag}"), raw.to_string());
        expect![[r#"
            HTML_TAG@0..39
              HTML_STARTING_TAG@0..19
                TK_LESS_THAN@0..1 "<"
                TK_WORD@1..4 "div"
                HTML_ATTRIBUTE_LIST@4..18
                  HTML_ATTRIBUTE@4..18
                    TK_WHITESPACE@4..5 " "
                    TK_WORD@5..10 "class"
                    TK_EQUAL@10..11 "="
                    HTML_STRING@11..18
                      TK_DOUBLE_QUOTES@11..12 "\""
                      HTML_STRING_INNER@12..17
                        TK_WORD@12..17 "hello"
                      TK_DOUBLE_QUOTES@17..18 "\""
                TK_GREATER_THAN@18..19 ">"
              BODY@19..33
                HTML_TEXT@19..24
                  TK_WORD@19..24 "world"
                TWIG_VAR@24..33
                  TK_WHITESPACE@24..25 " "
                  TK_OPEN_CURLY_CURLY@25..27 "{{"
                  TWIG_EXPRESSION@27..30
                    TWIG_LITERAL_NUMBER@27..30
                      TK_WHITESPACE@27..28 " "
                      TK_NUMBER@28..30 "42"
                  TK_WHITESPACE@30..31 " "
                  TK_CLOSE_CURLY_CURLY@31..33 "}}"
              HTML_ENDING_TAG@33..39
                TK_LESS_THAN_SLASH@33..35 "</"
                TK_WORD@35..38 "div"
                TK_GREATER_THAN@38..39 ">""#]]
        .assert_eq(&format!("{html_tag:?}"));

        assert!(!html_tag.is_self_closing());
        assert_eq!(
            html_tag.name().map(|t| t.to_string()),
            Some("div".to_string())
        );
        assert_eq!(
            html_tag.starting_tag().map(|t| t.to_string()),
            Some(r#"<div class="hello">"#.to_string())
        );
        assert_eq!(
            html_tag.body().map(|t| t.to_string()),
            Some("world {{ 42 }}".to_string())
        );
        assert_eq!(
            html_tag.ending_tag().map(|t| t.to_string()),
            Some("</div>".to_string())
        );
        assert_eq!(html_tag.attributes().count(), 1);
        assert_eq!(
            html_tag
                .attributes()
                .next()
                .and_then(|t| t.name())
                .map(|t| t.to_string()),
            Some("class".to_string())
        );
        assert_eq!(
            html_tag
                .attributes()
                .next()
                .and_then(|t| t.value())
                .and_then(|t| t.get_inner())
                .map(|t| t.to_string()),
            Some("hello".to_string())
        );
    }
}