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    Less,
42    Greater,
43    LessOrEqual,
44    GreaterOrEqual,
45    NotEqual,
46    Concat,
47    ArrowLeft,
48    ArrowRight,
49    Pipe,
50    Ampersand,
51    Caret,
52    Question,
53    End,
54}
55
56#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
57pub struct Token {
58    pub kind: TokenKind,
59    pub span: Span,
60}
61
62impl Token {
63    pub const fn new(kind: TokenKind, span: Span) -> Self {
64        Self { kind, span }
65    }
66
67    pub fn text(self, input: &str) -> &str {
68        input
69            .get(self.span.start..self.span.end)
70            .unwrap_or_default()
71    }
72
73    pub fn is_keyword(self, input: &str, keyword: &str) -> bool {
74        self.kind == TokenKind::Word && self.text(input).eq_ignore_ascii_case(keyword)
75    }
76}