oak_django/kind/
mod.rs

1use oak_core::{SyntaxKind, Token};
2use serde::{Deserialize, Serialize};
3
4pub type DjangoToken = Token<DjangoSyntaxKind>;
5
6/// Django 模板语法种类
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
8pub enum DjangoSyntaxKind {
9    // 基本 kind
10    Identifier,
11    Number,
12    String,
13    Whitespace,
14    Newline,
15
16    // Django 模板标签
17    VariableStart, // {{
18    VariableEnd,   // }}
19    TagStart,      // {%
20    TagEnd,        // %}
21    CommentStart,  // {#
22    CommentEnd,    // #}
23
24    // Django 标签关键字
25    If,
26    Elif,
27    Else,
28    Endif,
29    For,
30    Empty,
31    Endfor,
32    Block,
33    Endblock,
34    Extends,
35    Include,
36    Load,
37    With,
38    Endwith,
39    Autoescape,
40    Endautoescape,
41    Csrf,
42    Url,
43    Static,
44    Now,
45    Cycle,
46    Filter,
47    Endfilter,
48    Spaceless,
49    Endspaceless,
50    Verbatim,
51    Endverbatim,
52
53    // 过滤器和操作符
54    Pipe,    // |
55    Colon,   // :
56    Dot,     // .
57    Comma,   // ,
58    Equal,   // =
59    Plus,    // +
60    Minus,   // -
61    Star,    // *
62    Slash,   // /
63    Percent, // %
64
65    // 比较操作符
66    EqualEqual,   // ==
67    NotEqual,     // !=
68    Less,         // <
69    LessEqual,    // <=
70    Greater,      // >
71    GreaterEqual, // >=
72
73    // 逻辑操作符
74    And,
75    Or,
76    Not,
77    In,
78
79    // 括号
80    LeftParen,    // (
81    RightParen,   // )
82    LeftBracket,  // [
83    RightBracket, // ]
84    Semicolon,    // ;
85
86    // HTML 内容
87    HtmlText,
88    HtmlTag,
89
90    // 注释
91    Comment,
92
93    // 特殊
94    Root,
95    Error,
96    Eof,
97}
98
99impl DjangoSyntaxKind {
100    pub fn is_keyword(&self) -> bool {
101        matches!(
102            self,
103            Self::If
104                | Self::Elif
105                | Self::Else
106                | Self::Endif
107                | Self::For
108                | Self::Empty
109                | Self::Endfor
110                | Self::Block
111                | Self::Endblock
112                | Self::Extends
113                | Self::Include
114                | Self::Load
115                | Self::With
116                | Self::Endwith
117                | Self::Autoescape
118                | Self::Endautoescape
119                | Self::Csrf
120                | Self::Url
121                | Self::Static
122                | Self::Now
123                | Self::Cycle
124                | Self::Filter
125                | Self::Endfilter
126                | Self::Spaceless
127                | Self::Endspaceless
128                | Self::Verbatim
129                | Self::Endverbatim
130                | Self::And
131                | Self::Or
132                | Self::Not
133                | Self::In
134        )
135    }
136
137    pub fn is_trivia(&self) -> bool {
138        matches!(self, Self::Whitespace | Self::Newline | Self::Comment)
139    }
140}
141
142impl SyntaxKind for DjangoSyntaxKind {
143    fn is_trivia(&self) -> bool {
144        matches!(self, Self::Whitespace | Self::Newline | Self::Comment)
145    }
146
147    fn is_comment(&self) -> bool {
148        matches!(self, Self::Comment)
149    }
150
151    fn is_whitespace(&self) -> bool {
152        matches!(self, Self::Whitespace | Self::Newline)
153    }
154
155    fn is_token_type(&self) -> bool {
156        !matches!(self, Self::Error | Self::Eof)
157    }
158
159    fn is_element_type(&self) -> bool {
160        false
161    }
162}