Skip to main content

blade_syntax/
kind.rs

1//! Every kind of token and node in a Blade syntax tree.
2
3/// Token kinds come first, then node kinds. The `u16` value is what rowan stores.
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
5#[repr(u16)]
6#[allow(non_camel_case_types)]
7pub enum SyntaxKind {
8    // Tokens
9    /// Anything Blade doesn't treat specially (HTML, CSS, JS, plain text).
10    TEXT,
11    /// Spaces or tabs between a directive name and its arguments, or any run of
12    /// whitespace inside an HTML tag.
13    WHITESPACE,
14    /// `{{`
15    ECHO_OPEN,
16    /// `}}`
17    ECHO_CLOSE,
18    /// `{!!`
19    RAW_ECHO_OPEN,
20    /// `!!}`
21    RAW_ECHO_CLOSE,
22    /// Embedded PHP, kept opaque: echo contents, `@php` bodies, `<?php ?>` bodies.
23    PHP,
24    /// A whole Blade comment, `{{-- ... --}}`.
25    COMMENT,
26    /// Output Blade prints literally: `@{{ ... }}`, `@{!! ... !!}`, `@@name`.
27    ESCAPED,
28    /// A directive name including its `@`, e.g. `@if`.
29    DIRECTIVE_NAME,
30    /// Directive arguments including the parentheses, e.g. `($user)`.
31    DIRECTIVE_ARGS,
32    /// `<?php` or `<?=`
33    PHP_OPEN,
34    /// `?>`
35    PHP_CLOSE,
36    /// `<` starting a start tag.
37    TAG_OPEN,
38    /// `</` starting an end tag.
39    END_TAG_OPEN,
40    /// A tag name, e.g. `div`, `x-slot:title`, `livewire:counter`.
41    TAG_NAME,
42    /// `>` ending a tag.
43    TAG_CLOSE,
44    /// `/>` ending a self-closing tag.
45    TAG_SELF_CLOSE,
46    /// An attribute name, e.g. `class`, `:value`, `wire:model.live`, `@click`.
47    ATTR_NAME,
48    /// `=` between an attribute name and its value.
49    EQ,
50    /// `"` or `'` around an attribute value.
51    QUOTE,
52    /// `<!--`
53    HTML_COMMENT_OPEN,
54    /// `-->`
55    HTML_COMMENT_CLOSE,
56    /// A whole `<!DOCTYPE ...>`.
57    DOCTYPE,
58
59    // Nodes
60    ROOT,
61    ECHO,
62    RAW_ECHO,
63    DIRECTIVE,
64    BLOCK,
65    BODY,
66    VERBATIM,
67    PHP_BLOCK,
68    PHP_TAG,
69    /// A start tag, `<div class="a">` or `<br/>`. Not a whole element: Blade
70    /// directives may open and close elements in different branches.
71    TAG,
72    /// An end tag, `</div>`.
73    END_TAG,
74    /// `name`, `name=value` or `name="value"` inside a tag.
75    ATTRIBUTE,
76    /// An attribute value, with its quotes if it has them.
77    ATTR_VALUE,
78    ERROR,
79}
80
81use SyntaxKind::*;
82
83const ALL: [SyntaxKind; 38] = [
84    TEXT,
85    WHITESPACE,
86    ECHO_OPEN,
87    ECHO_CLOSE,
88    RAW_ECHO_OPEN,
89    RAW_ECHO_CLOSE,
90    PHP,
91    COMMENT,
92    ESCAPED,
93    DIRECTIVE_NAME,
94    DIRECTIVE_ARGS,
95    PHP_OPEN,
96    PHP_CLOSE,
97    TAG_OPEN,
98    END_TAG_OPEN,
99    TAG_NAME,
100    TAG_CLOSE,
101    TAG_SELF_CLOSE,
102    ATTR_NAME,
103    EQ,
104    QUOTE,
105    HTML_COMMENT_OPEN,
106    HTML_COMMENT_CLOSE,
107    DOCTYPE,
108    ROOT,
109    ECHO,
110    RAW_ECHO,
111    DIRECTIVE,
112    BLOCK,
113    BODY,
114    VERBATIM,
115    PHP_BLOCK,
116    PHP_TAG,
117    TAG,
118    END_TAG,
119    ATTRIBUTE,
120    ATTR_VALUE,
121    ERROR,
122];
123
124impl From<SyntaxKind> for rowan::SyntaxKind {
125    fn from(kind: SyntaxKind) -> Self {
126        Self(kind as u16)
127    }
128}
129
130/// Marker type that ties rowan's generic tree to [`SyntaxKind`].
131#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
132pub enum BladeLanguage {}
133
134impl rowan::Language for BladeLanguage {
135    type Kind = SyntaxKind;
136
137    fn kind_from_raw(raw: rowan::SyntaxKind) -> SyntaxKind {
138        ALL[usize::from(raw.0)]
139    }
140
141    fn kind_to_raw(kind: SyntaxKind) -> rowan::SyntaxKind {
142        kind.into()
143    }
144}
145
146pub type SyntaxNode = rowan::SyntaxNode<BladeLanguage>;
147pub type SyntaxToken = rowan::SyntaxToken<BladeLanguage>;
148
149#[cfg(test)]
150mod tests {
151    use super::*;
152    use rowan::Language;
153
154    #[test]
155    fn every_kind_survives_the_raw_round_trip() {
156        for (i, kind) in ALL.iter().enumerate() {
157            assert_eq!(*kind as usize, i);
158            assert_eq!(
159                BladeLanguage::kind_from_raw(BladeLanguage::kind_to_raw(*kind)),
160                *kind
161            );
162        }
163        assert_eq!(ALL.len(), ERROR as usize + 1);
164    }
165}