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