Skip to main content

powdb_query/
token.rs

1#[derive(Debug, Clone, PartialEq)]
2pub enum Token {
3    // Identifiers and literals
4    Ident(String),     // User, name, email
5    DotIdent(String),  // .name, .age (field access)
6    IntLit(i64),       // 42
7    FloatLit(f64),     // 3.14
8    StringLit(String), // "hello"
9    BoolLit(bool),     // true, false
10    Param(String),     // $age, $name (query parameter)
11
12    // Keywords
13    Type,         // type
14    Filter,       // filter
15    Order,        // order
16    Limit,        // limit
17    Offset,       // offset
18    Insert,       // insert
19    Update,       // update
20    Delete,       // delete
21    Upsert,       // upsert
22    Select,       // select (alias for projection)
23    Required,     // required
24    Multi,        // multi
25    Link,         // link
26    Index,        // index
27    On,           // on
28    Conflict,     // conflict
29    Asc,          // asc
30    Desc,         // desc
31    And,          // and
32    Or,           // or
33    Not,          // not
34    Exists,       // exists
35    Let,          // let
36    As,           // as
37    Match,        // match
38    Group,        // group
39    Join,         // join
40    Inner,        // inner
41    LeftKw,       // left  (keyword — avoids clashing with ast::JoinKind::LeftOuter naming)
42    RightKw,      // right
43    Outer,        // outer
44    Cross,        // cross
45    Transaction,  // transaction
46    View,         // view
47    Materialized, // materialized
48    Refresh,      // refresh
49    Union,        // union
50    Having,       // having
51    Distinct,     // distinct
52    In,           // in
53    Between,      // between
54    Like,         // like
55    Count,        // count
56    Avg,          // avg
57    Sum,          // sum
58    Min,          // min
59    Max,          // max
60    Is,           // is
61    Null,         // null
62
63    // String functions
64    Upper,     // upper
65    Lower,     // lower
66    Length,    // length
67    Trim,      // trim
68    Substring, // substring
69    Concat,    // concat
70
71    // Math functions
72    Abs,   // abs
73    Round, // round
74    Ceil,  // ceil
75    Floor, // floor
76    Sqrt,  // sqrt
77    Pow,   // pow
78
79    // Date/time functions
80    Now,      // now
81    Extract,  // extract
82    DateAdd,  // date_add
83    DateDiff, // date_diff
84
85    // Type conversion
86    Cast, // cast
87
88    // CASE WHEN
89    Case, // case
90    When, // when
91    Then, // then
92    Else, // else
93    End,  // end
94
95    // Window functions
96    Over,      // over
97    Partition, // partition
98    RowNumber, // row_number
99    Rank,      // rank
100    DenseRank, // dense_rank
101
102    // DDL
103    Alter,   // alter
104    Drop,    // drop
105    Add,     // add
106    Column,  // column
107    Explain, // explain
108
109    // Operators
110    Eq,       // =
111    Neq,      // !=
112    Lt,       // <
113    Gt,       // >
114    Lte,      // <=
115    Gte,      // >=
116    Assign,   // :=
117    Arrow,    // ->
118    Pipe,     // |
119    Coalesce, // ??
120    Plus,     // +
121    Minus,    // -
122    Star,     // *
123    Slash,    // /
124
125    // Delimiters
126    LBrace, // {
127    RBrace, // }
128    LParen, // (
129    RParen, // )
130    Comma,  // ,
131    Colon,  // :
132    Dot,    // .
133
134    // Special
135    Eof,
136}
137
138impl Token {
139    /// Human-readable name for error messages. Avoids exposing raw Rust Debug
140    /// format like `IntLit(42)` to end users.
141    pub fn display_name(&self) -> String {
142        match self {
143            // Literals
144            Token::Ident(s) => format!("identifier '{s}'"),
145            Token::DotIdent(s) => format!("field '.{s}'"),
146            Token::IntLit(v) => format!("number {v}"),
147            Token::FloatLit(v) => format!("decimal number {v}"),
148            Token::StringLit(s) => {
149                let preview = if s.len() > 20 {
150                    format!("{}...", &s[..20])
151                } else {
152                    s.clone()
153                };
154                format!("string \"{preview}\"")
155            }
156            Token::BoolLit(v) => format!("{v}"),
157            Token::Param(s) => format!("parameter '${s}'"),
158
159            // Keywords
160            Token::Type => "'type'".into(),
161            Token::Filter => "'filter'".into(),
162            Token::Order => "'order'".into(),
163            Token::Limit => "'limit'".into(),
164            Token::Offset => "'offset'".into(),
165            Token::Insert => "'insert'".into(),
166            Token::Update => "'update'".into(),
167            Token::Delete => "'delete'".into(),
168            Token::Upsert => "'upsert'".into(),
169            Token::Select => "'select'".into(),
170            Token::Required => "'required'".into(),
171            Token::Multi => "'multi'".into(),
172            Token::Link => "'link'".into(),
173            Token::Index => "'index'".into(),
174            Token::On => "'on'".into(),
175            Token::Conflict => "'conflict'".into(),
176            Token::Asc => "'asc'".into(),
177            Token::Desc => "'desc'".into(),
178            Token::And => "'and'".into(),
179            Token::Or => "'or'".into(),
180            Token::Not => "'not'".into(),
181            Token::Exists => "'exists'".into(),
182            Token::Let => "'let'".into(),
183            Token::As => "'as'".into(),
184            Token::Match => "'match'".into(),
185            Token::Group => "'group'".into(),
186            Token::Join => "'join'".into(),
187            Token::Inner => "'inner'".into(),
188            Token::LeftKw => "'left'".into(),
189            Token::RightKw => "'right'".into(),
190            Token::Outer => "'outer'".into(),
191            Token::Cross => "'cross'".into(),
192            Token::Transaction => "'transaction'".into(),
193            Token::View => "'view'".into(),
194            Token::Materialized => "'materialized'".into(),
195            Token::Refresh => "'refresh'".into(),
196            Token::Union => "'union'".into(),
197            Token::Having => "'having'".into(),
198            Token::Distinct => "'distinct'".into(),
199            Token::In => "'in'".into(),
200            Token::Between => "'between'".into(),
201            Token::Like => "'like'".into(),
202            Token::Count => "'count'".into(),
203            Token::Avg => "'avg'".into(),
204            Token::Sum => "'sum'".into(),
205            Token::Min => "'min'".into(),
206            Token::Max => "'max'".into(),
207            Token::Is => "'is'".into(),
208            Token::Null => "'null'".into(),
209
210            // Functions
211            Token::Upper => "'upper'".into(),
212            Token::Lower => "'lower'".into(),
213            Token::Length => "'length'".into(),
214            Token::Trim => "'trim'".into(),
215            Token::Substring => "'substring'".into(),
216            Token::Concat => "'concat'".into(),
217            Token::Abs => "'abs'".into(),
218            Token::Round => "'round'".into(),
219            Token::Ceil => "'ceil'".into(),
220            Token::Floor => "'floor'".into(),
221            Token::Sqrt => "'sqrt'".into(),
222            Token::Pow => "'pow'".into(),
223            Token::Now => "'now'".into(),
224            Token::Extract => "'extract'".into(),
225            Token::DateAdd => "'date_add'".into(),
226            Token::DateDiff => "'date_diff'".into(),
227            Token::Cast => "'cast'".into(),
228            Token::Case => "'case'".into(),
229            Token::When => "'when'".into(),
230            Token::Then => "'then'".into(),
231            Token::Else => "'else'".into(),
232            Token::End => "'end'".into(),
233
234            // Window
235            Token::Over => "'over'".into(),
236            Token::Partition => "'partition'".into(),
237            Token::RowNumber => "'row_number'".into(),
238            Token::Rank => "'rank'".into(),
239            Token::DenseRank => "'dense_rank'".into(),
240
241            // DDL
242            Token::Alter => "'alter'".into(),
243            Token::Drop => "'drop'".into(),
244            Token::Add => "'add'".into(),
245            Token::Column => "'column'".into(),
246            Token::Explain => "'explain'".into(),
247
248            // Operators
249            Token::Eq => "'='".into(),
250            Token::Neq => "'!='".into(),
251            Token::Lt => "'<'".into(),
252            Token::Gt => "'>'".into(),
253            Token::Lte => "'<='".into(),
254            Token::Gte => "'>='".into(),
255            Token::Assign => "':='".into(),
256            Token::Arrow => "'->'".into(),
257            Token::Pipe => "'|'".into(),
258            Token::Coalesce => "'??'".into(),
259            Token::Plus => "'+'".into(),
260            Token::Minus => "'-'".into(),
261            Token::Star => "'*'".into(),
262            Token::Slash => "'/'".into(),
263
264            // Delimiters
265            Token::LBrace => "'{'".into(),
266            Token::RBrace => "'}'".into(),
267            Token::LParen => "'('".into(),
268            Token::RParen => "')'".into(),
269            Token::Comma => "','".into(),
270            Token::Colon => "':'".into(),
271            Token::Dot => "'.'".into(),
272
273            Token::Eof => "end of input".into(),
274        }
275    }
276}