Skip to main content

hawdb_sql_syntax/
token.rs

1// Copyright 2026 Nowledge
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use crate::Span;
16
17#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
18pub enum TokenKind {
19    Word,
20    QuotedIdentifier,
21    String,
22    Number,
23    Parameter(u32),
24    LeftParen,
25    RightParen,
26    LeftBracket,
27    RightBracket,
28    LeftBrace,
29    RightBrace,
30    Comma,
31    Dot,
32    Semicolon,
33    Colon,
34    DoubleColon,
35    Plus,
36    Minus,
37    Star,
38    Slash,
39    Percent,
40    Equal,
41    DoubleEqual,
42    Less,
43    Greater,
44    LessOrEqual,
45    GreaterOrEqual,
46    NotEqual,
47    Concat,
48    ArrowLeft,
49    ArrowRight,
50    Pipe,
51    Ampersand,
52    Caret,
53    Question,
54    Tilde,
55    End,
56}
57
58#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
59pub struct Token {
60    pub kind: TokenKind,
61    pub span: Span,
62}
63
64impl Token {
65    pub const fn new(kind: TokenKind, span: Span) -> Self {
66        Self { kind, span }
67    }
68
69    pub fn text(self, input: &str) -> &str {
70        input
71            .get(self.span.start..self.span.end)
72            .unwrap_or_default()
73    }
74
75    pub fn is_keyword(self, input: &str, keyword: &str) -> bool {
76        self.kind == TokenKind::Word && self.text(input).eq_ignore_ascii_case(keyword)
77    }
78}