Skip to main content

oak_nginx/kind/
mod.rs

1use oak_core::{ElementType, TokenType, UniversalElementRole, UniversalTokenRole};
2use serde::{Deserialize, Serialize};
3
4#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
5pub enum NginxSyntaxKind {
6    // 节点种类
7    Root,
8    Directive,
9    Block,
10    Parameter,
11    Value,
12    Comment,
13
14    // 词法种类 - 关键字
15    ServerKeyword,     // server
16    LocationKeyword,   // location
17    UpstreamKeyword,   // upstream
18    HttpKeyword,       // http
19    EventsKeyword,     // events
20    ListenKeyword,     // listen
21    ServerNameKeyword, // server_name
22    RootKeyword,       // root
23    IndexKeyword,      // index
24    ProxyPassKeyword,  // proxy_pass
25
26    // 词法种类 - 符号
27    LeftBrace,  // {
28    RightBrace, // }
29    Semicolon,  // ;
30
31    // 词法种类 - 字面量
32    Identifier,
33    String,
34    Number,
35    Path,
36    Url,
37
38    // 词法种类 - 其他
39    Whitespace,
40    Newline,
41    CommentToken,
42    Eof,
43    Error,
44}
45
46impl TokenType for NginxSyntaxKind {
47    const END_OF_STREAM: Self = Self::Eof;
48    type Role = UniversalTokenRole;
49
50    fn role(&self) -> Self::Role {
51        match self {
52            Self::Whitespace | Self::Newline => UniversalTokenRole::Whitespace,
53            Self::CommentToken | Self::Comment => UniversalTokenRole::Comment,
54            Self::Eof => UniversalTokenRole::Eof,
55            _ => UniversalTokenRole::None,
56        }
57    }
58
59    fn is_comment(&self) -> bool {
60        matches!(self, Self::CommentToken | Self::Comment)
61    }
62
63    fn is_whitespace(&self) -> bool {
64        matches!(self, Self::Whitespace | Self::Newline)
65    }
66}
67
68impl ElementType for NginxSyntaxKind {
69    type Role = UniversalElementRole;
70
71    fn role(&self) -> Self::Role {
72        match self {
73            Self::Error => UniversalElementRole::Error,
74            Self::Root => UniversalElementRole::Root,
75            Self::Directive | Self::Block => UniversalElementRole::Detail,
76            _ => UniversalElementRole::None,
77        }
78    }
79}
80
81impl NginxSyntaxKind {
82    pub fn is_element(&self) -> bool {
83        matches!(self, Self::Root | Self::Directive | Self::Block | Self::Parameter | Self::Value | Self::Comment)
84    }
85
86    pub fn is_token(&self) -> bool {
87        !self.is_element()
88    }
89}