1#[derive(Debug, Clone, PartialEq)]
2pub enum Token {
3 Ident(String), DotIdent(String), IntLit(i64), FloatLit(f64), StringLit(String), BoolLit(bool), Param(String), Type, Filter, Order, Limit, Offset, Insert, Update, Delete, Upsert, Returning, Select, Required, Default, Auto, Multi, Link, Index, Unique, On, Conflict, Asc, Desc, And, Or, Not, Exists, Let, As, Match, Group, Join, Inner, LeftKw, RightKw, Outer, Cross, Transaction, Begin, Commit, Rollback, View, Materialized, Refresh, Union, Having, Distinct, In, Between, Like, Count, Avg, Sum, Min, Max, Is, Null, Upper, Lower, Length, Trim, Substring, Concat, Abs, Round, Ceil, Floor, Sqrt, Pow, Now, Extract, DateAdd, DateDiff, JsonType, Cast, Case, When, Then, Else, End, Over, Partition, RowNumber, Rank, DenseRank, Alter, Drop, Add, Column, Explain, Schema, Describe, Eq, Neq, Lt, Gt, Lte, Gte, Assign, Arrow, Pipe, Coalesce, Plus, Minus, Star, Slash, LBrace, RBrace, LParen, RParen, Comma, Colon, Dot, Eof,
148}
149
150impl Token {
151 pub fn display_name(&self) -> String {
154 match self {
155 Token::Ident(s) => format!("identifier '{s}'"),
157 Token::DotIdent(s) => format!("field '.{s}'"),
158 Token::IntLit(v) => format!("number {v}"),
159 Token::FloatLit(v) => format!("decimal number {v}"),
160 Token::StringLit(s) => {
161 let preview = if s.len() > 20 {
162 let end = s.floor_char_boundary(20);
163 format!("{}...", &s[..end])
164 } else {
165 s.clone()
166 };
167 format!("string \"{preview}\"")
168 }
169 Token::BoolLit(v) => format!("{v}"),
170 Token::Param(s) => format!("parameter '${s}'"),
171
172 Token::Type => "'type'".into(),
174 Token::Filter => "'filter'".into(),
175 Token::Order => "'order'".into(),
176 Token::Limit => "'limit'".into(),
177 Token::Offset => "'offset'".into(),
178 Token::Insert => "'insert'".into(),
179 Token::Update => "'update'".into(),
180 Token::Delete => "'delete'".into(),
181 Token::Upsert => "'upsert'".into(),
182 Token::Returning => "'returning'".into(),
183 Token::Select => "'select'".into(),
184 Token::Required => "'required'".into(),
185 Token::Default => "'default'".into(),
186 Token::Auto => "'auto'".into(),
187 Token::Multi => "'multi'".into(),
188 Token::Link => "'link'".into(),
189 Token::Index => "'index'".into(),
190 Token::Unique => "'unique'".into(),
191 Token::On => "'on'".into(),
192 Token::Conflict => "'conflict'".into(),
193 Token::Asc => "'asc'".into(),
194 Token::Desc => "'desc'".into(),
195 Token::And => "'and'".into(),
196 Token::Or => "'or'".into(),
197 Token::Not => "'not'".into(),
198 Token::Exists => "'exists'".into(),
199 Token::Let => "'let'".into(),
200 Token::As => "'as'".into(),
201 Token::Match => "'match'".into(),
202 Token::Group => "'group'".into(),
203 Token::Join => "'join'".into(),
204 Token::Inner => "'inner'".into(),
205 Token::LeftKw => "'left'".into(),
206 Token::RightKw => "'right'".into(),
207 Token::Outer => "'outer'".into(),
208 Token::Cross => "'cross'".into(),
209 Token::Transaction => "'transaction'".into(),
210 Token::Begin => "'begin'".into(),
211 Token::Commit => "'commit'".into(),
212 Token::Rollback => "'rollback'".into(),
213 Token::View => "'view'".into(),
214 Token::Materialized => "'materialized'".into(),
215 Token::Refresh => "'refresh'".into(),
216 Token::Union => "'union'".into(),
217 Token::Having => "'having'".into(),
218 Token::Distinct => "'distinct'".into(),
219 Token::In => "'in'".into(),
220 Token::Between => "'between'".into(),
221 Token::Like => "'like'".into(),
222 Token::Count => "'count'".into(),
223 Token::Avg => "'avg'".into(),
224 Token::Sum => "'sum'".into(),
225 Token::Min => "'min'".into(),
226 Token::Max => "'max'".into(),
227 Token::Is => "'is'".into(),
228 Token::Null => "'null'".into(),
229
230 Token::Upper => "'upper'".into(),
232 Token::Lower => "'lower'".into(),
233 Token::Length => "'length'".into(),
234 Token::Trim => "'trim'".into(),
235 Token::Substring => "'substring'".into(),
236 Token::Concat => "'concat'".into(),
237 Token::Abs => "'abs'".into(),
238 Token::Round => "'round'".into(),
239 Token::Ceil => "'ceil'".into(),
240 Token::Floor => "'floor'".into(),
241 Token::Sqrt => "'sqrt'".into(),
242 Token::Pow => "'pow'".into(),
243 Token::Now => "'now'".into(),
244 Token::Extract => "'extract'".into(),
245 Token::DateAdd => "'date_add'".into(),
246 Token::DateDiff => "'date_diff'".into(),
247 Token::JsonType => "'json_type'".into(),
248 Token::Cast => "'cast'".into(),
249 Token::Case => "'case'".into(),
250 Token::When => "'when'".into(),
251 Token::Then => "'then'".into(),
252 Token::Else => "'else'".into(),
253 Token::End => "'end'".into(),
254
255 Token::Over => "'over'".into(),
257 Token::Partition => "'partition'".into(),
258 Token::RowNumber => "'row_number'".into(),
259 Token::Rank => "'rank'".into(),
260 Token::DenseRank => "'dense_rank'".into(),
261
262 Token::Alter => "'alter'".into(),
264 Token::Drop => "'drop'".into(),
265 Token::Add => "'add'".into(),
266 Token::Column => "'column'".into(),
267 Token::Explain => "'explain'".into(),
268 Token::Schema => "'schema'".into(),
269 Token::Describe => "'describe'".into(),
270
271 Token::Eq => "'='".into(),
273 Token::Neq => "'!='".into(),
274 Token::Lt => "'<'".into(),
275 Token::Gt => "'>'".into(),
276 Token::Lte => "'<='".into(),
277 Token::Gte => "'>='".into(),
278 Token::Assign => "':='".into(),
279 Token::Arrow => "'->'".into(),
280 Token::Pipe => "'|'".into(),
281 Token::Coalesce => "'??'".into(),
282 Token::Plus => "'+'".into(),
283 Token::Minus => "'-'".into(),
284 Token::Star => "'*'".into(),
285 Token::Slash => "'/'".into(),
286
287 Token::LBrace => "'{'".into(),
289 Token::RBrace => "'}'".into(),
290 Token::LParen => "'('".into(),
291 Token::RParen => "')'".into(),
292 Token::Comma => "','".into(),
293 Token::Colon => "':'".into(),
294 Token::Dot => "'.'".into(),
295
296 Token::Eof => "end of input".into(),
297 }
298 }
299
300 pub fn keyword_str(&self) -> Option<&'static str> {
306 Some(match self {
307 Token::Type => "type",
308 Token::Filter => "filter",
309 Token::Order => "order",
310 Token::Limit => "limit",
311 Token::Offset => "offset",
312 Token::Insert => "insert",
313 Token::Update => "update",
314 Token::Delete => "delete",
315 Token::Upsert => "upsert",
316 Token::Returning => "returning",
317 Token::Select => "select",
318 Token::Required => "required",
319 Token::Default => "default",
320 Token::Auto => "auto",
321 Token::Multi => "multi",
322 Token::Link => "link",
323 Token::Index => "index",
324 Token::Unique => "unique",
325 Token::On => "on",
326 Token::Conflict => "conflict",
327 Token::Asc => "asc",
328 Token::Desc => "desc",
329 Token::And => "and",
330 Token::Or => "or",
331 Token::Not => "not",
332 Token::Exists => "exists",
333 Token::Let => "let",
334 Token::As => "as",
335 Token::Match => "match",
336 Token::Group => "group",
337 Token::Join => "join",
338 Token::Inner => "inner",
339 Token::LeftKw => "left",
340 Token::RightKw => "right",
341 Token::Outer => "outer",
342 Token::Cross => "cross",
343 Token::Transaction => "transaction",
344 Token::Begin => "begin",
345 Token::Commit => "commit",
346 Token::Rollback => "rollback",
347 Token::View => "view",
348 Token::Materialized => "materialized",
349 Token::Refresh => "refresh",
350 Token::Union => "union",
351 Token::Having => "having",
352 Token::Distinct => "distinct",
353 Token::In => "in",
354 Token::Between => "between",
355 Token::Like => "like",
356 Token::Count => "count",
357 Token::Avg => "avg",
358 Token::Sum => "sum",
359 Token::Min => "min",
360 Token::Max => "max",
361 Token::Is => "is",
362 Token::Null => "null",
363 Token::Upper => "upper",
364 Token::Lower => "lower",
365 Token::Length => "length",
366 Token::Trim => "trim",
367 Token::Substring => "substring",
368 Token::Concat => "concat",
369 Token::Abs => "abs",
370 Token::Round => "round",
371 Token::Ceil => "ceil",
372 Token::Floor => "floor",
373 Token::Sqrt => "sqrt",
374 Token::Pow => "pow",
375 Token::Now => "now",
376 Token::Extract => "extract",
377 Token::DateAdd => "date_add",
378 Token::DateDiff => "date_diff",
379 Token::JsonType => "json_type",
380 Token::Cast => "cast",
381 Token::Case => "case",
382 Token::When => "when",
383 Token::Then => "then",
384 Token::Else => "else",
385 Token::End => "end",
386 Token::Over => "over",
387 Token::Partition => "partition",
388 Token::RowNumber => "row_number",
389 Token::Rank => "rank",
390 Token::DenseRank => "dense_rank",
391 Token::Alter => "alter",
392 Token::Drop => "drop",
393 Token::Add => "add",
394 Token::Column => "column",
395 Token::Explain => "explain",
396 Token::Schema => "schema",
397 Token::Describe => "describe",
398 _ => return None,
399 })
400 }
401}