Skip to main content

oak_nginx/lexer/
token_type.rs

1use oak_core::{Token, TokenType, UniversalTokenRole};
2
3/// Represents a token in an Nginx configuration file.
4pub type NginxToken = Token<NginxTokenType>;
5
6/// Token types for Nginx configuration.
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
8#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
9pub enum NginxTokenType {
10    /// The root of the configuration.
11    Root,
12    /// A configuration directive (e.g., `worker_processes 1;`).
13    Directive,
14    /// A configuration block (e.g., `server { ... }`).
15    Block,
16    /// A parameter within a directive.
17    Parameter,
18    /// A value within a directive or parameter.
19    Value,
20    /// A comment.
21    Comment,
22
23    /// The `server` keyword.
24    ServerKeyword, // server
25    /// The `location` keyword.
26    LocationKeyword, // location
27    /// The `upstream` keyword.
28    UpstreamKeyword, // upstream
29    /// The `http` keyword.
30    HttpKeyword, // http
31    /// The `events` keyword.
32    EventsKeyword, // events
33    /// The `listen` keyword.
34    ListenKeyword, // listen
35    /// The `server_name` keyword.
36    ServerNameKeyword, // server_name
37    /// The `root` keyword.
38    RootKeyword, // root
39    /// The `index` keyword.
40    IndexKeyword, // index
41    /// The `proxy_pass` keyword.
42    ProxyPassKeyword, // proxy_pass
43
44    /// Left brace `{`.
45    LeftBrace, // {
46    /// Right brace `}`.
47    RightBrace, // }
48    /// Semicolon `;`.
49    Semicolon, // ;
50
51    /// An identifier.
52    Identifier,
53    /// A string literal.
54    String,
55    /// A numeric literal.
56    Number,
57    /// A file path.
58    Path,
59    /// A URL.
60    Url,
61
62    /// Whitespace characters.
63    Whitespace,
64    /// Newline characters.
65    Newline,
66    /// A comment token.
67    CommentToken,
68    /// End of stream.
69    Eof,
70    /// An error token.
71    Error,
72}
73
74impl NginxTokenType {
75    /// Returns true if the token type represents a structural element.
76    pub fn is_element(&self) -> bool {
77        matches!(self, Self::Root | Self::Directive | Self::Block | Self::Parameter | Self::Value | Self::Comment)
78    }
79
80    /// Returns true if the token type represents a lexical token.
81    pub fn is_token(&self) -> bool {
82        !self.is_element()
83    }
84}
85
86impl TokenType for NginxTokenType {
87    type Role = UniversalTokenRole;
88    const END_OF_STREAM: Self = Self::Eof;
89
90    fn role(&self) -> Self::Role {
91        match self {
92            Self::ServerKeyword | Self::LocationKeyword | Self::UpstreamKeyword | Self::HttpKeyword | Self::EventsKeyword | Self::ListenKeyword | Self::ServerNameKeyword | Self::RootKeyword | Self::IndexKeyword | Self::ProxyPassKeyword => {
93                UniversalTokenRole::Keyword
94            }
95
96            Self::LeftBrace | Self::RightBrace | Self::Semicolon => UniversalTokenRole::Punctuation,
97
98            Self::Identifier => UniversalTokenRole::Name,
99            Self::String | Self::Number | Self::Path | Self::Url => UniversalTokenRole::Literal,
100
101            Self::Whitespace | Self::Newline => UniversalTokenRole::Whitespace,
102            Self::CommentToken => UniversalTokenRole::Comment,
103            Self::Error => UniversalTokenRole::Error,
104            _ => UniversalTokenRole::None,
105        }
106    }
107}