1use super::{
2 Alphanumeric,
3 CustomToTokens,
4 Parse,
5 StatementStream,
6};
7use scylladb_parse_macros::{
8 ParseFromStr,
9 ToTokens,
10};
11use std::{
12 fmt::Display,
13 str::FromStr,
14};
15
16#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, ToTokens)]
17pub enum ReservedKeyword {
18 ADD,
19 ALLOW,
20 ALTER,
21 AND,
22 APPLY,
23 ASC,
24 AUTHORIZE,
25 BATCH,
26 BEGIN,
27 BY,
28 COLUMNFAMILY,
29 CREATE,
30 DELETE,
31 DESC,
32 DESCRIBE,
33 DROP,
34 ENTRIES,
35 EXECUTE,
36 FROM,
37 FULL,
38 GRANT,
39 IF,
40 IN,
41 INDEX,
42 INFINITY,
43 INSERT,
44 INTO,
45 KEYSPACE,
46 LIMIT,
47 MODIFY,
48 NAN,
49 NORECURSIVE,
50 NOT,
51 NULL,
52 OF,
53 ON,
54 OR,
55 ORDER,
56 PRIMARY,
57 RENAME,
58 REPLACE,
59 REVOKE,
60 SCHEMA,
61 SELECT,
62 SET,
63 TABLE,
64 TO,
65 TRUNCATE,
66 UNLOGGED,
67 UPDATE,
68 USE,
69 USING,
70 VIEW,
71 WHERE,
72 WITH,
73}
74
75macro_rules! keyword {
76 ($t:ident) => {
77 #[derive(ParseFromStr, Copy, Clone, Debug)]
78 pub struct $t;
79 impl Parse for $t {
80 type Output = Self;
81 fn parse(s: &mut StatementStream<'_>) -> anyhow::Result<Self::Output> {
82 let this = stringify!($t);
83 if let Some(token) = s.nextn(this.len()) {
84 if token.to_uppercase().as_str() != this {
85 anyhow::bail!("Expected keyword '{}', found {}", this, s.info_with_token(token))
86 }
87 Ok($t)
88 } else {
89 anyhow::bail!("Expected keyword '{}', found end of stream", this)
90 }
91 }
92 }
93 };
94}
95
96keyword!(ACCESS);
97keyword!(ADD);
98keyword!(AGGREGATE);
99keyword!(ALL);
100keyword!(ALLOW);
101keyword!(ALTER);
102keyword!(AND);
103keyword!(APPLY);
104keyword!(AS);
105keyword!(ASC);
106keyword!(ASCII);
107keyword!(AUTHORIZE);
108keyword!(BATCH);
109keyword!(BEGIN);
110keyword!(BIGINT);
111keyword!(BLOB);
112keyword!(BOOLEAN);
113keyword!(BY);
114keyword!(BYPASS);
115keyword!(CALLED);
116keyword!(CACHE);
117keyword!(CAST);
118keyword!(CLUSTERING);
119keyword!(COLUMNFAMILY);
120keyword!(COMPACT);
121keyword!(CONTAINS);
122keyword!(COUNT);
123keyword!(COUNTER);
124keyword!(CREATE);
125keyword!(CUSTOM);
126keyword!(DATACENTERS);
127keyword!(DATE);
128keyword!(DECIMAL);
129keyword!(DEFAULT);
130keyword!(DELETE);
131keyword!(DESC);
132keyword!(DESCRIBE);
133keyword!(DISTINCT);
134keyword!(DOUBLE);
135keyword!(DROP);
136keyword!(ENTRIES);
137keyword!(EXECUTE);
138keyword!(EXISTS);
139keyword!(FALSE);
140keyword!(FILTERING);
141keyword!(FINALFUNC);
142keyword!(FLOAT);
143keyword!(FROM);
144keyword!(FROZEN);
145keyword!(FULL);
146keyword!(FUNCTION);
147keyword!(FUNCTIONS);
148keyword!(GRANT);
149keyword!(GROUP);
150keyword!(IF);
151keyword!(IN);
152keyword!(INDEX);
153keyword!(INET);
154keyword!(INFINITY);
155keyword!(INITCOND);
156keyword!(INPUT);
157keyword!(INSERT);
158keyword!(INT);
159keyword!(INTO);
160keyword!(IS);
161keyword!(JSON);
162keyword!(KEY);
163keyword!(KEYS);
164keyword!(KEYSPACE);
165keyword!(KEYSPACES);
166keyword!(LANGUAGE);
167keyword!(LIKE);
168keyword!(LIMIT);
169keyword!(LIST);
170keyword!(LOGIN);
171keyword!(MAP);
172keyword!(MATERIALIZED);
173keyword!(MBEAN);
174keyword!(MBEANS);
175keyword!(MODIFY);
176keyword!(NAN);
177keyword!(NOLOGIN);
178keyword!(NORECURSIVE);
179keyword!(NOSUPERUSER);
180keyword!(NOT);
181keyword!(NULL);
182keyword!(OF);
183keyword!(ON);
184keyword!(OPTIONS);
185keyword!(OR);
186keyword!(ORDER);
187keyword!(PARTITION);
188keyword!(PASSWORD);
189keyword!(PER);
190keyword!(PERCENTILE);
191keyword!(PERMISSION);
192keyword!(PERMISSIONS);
193keyword!(PRIMARY);
194keyword!(RENAME);
195keyword!(REPLACE);
196keyword!(RETURNS);
197keyword!(REVOKE);
198keyword!(ROLE);
199keyword!(ROLES);
200keyword!(SCHEMA);
201keyword!(SELECT);
202keyword!(SET);
203keyword!(SFUNC);
204keyword!(SMALLINT);
205keyword!(STATIC);
206keyword!(STORAGE);
207keyword!(STYPE);
208keyword!(SUPERUSER);
209keyword!(TABLE);
210keyword!(TEXT);
211keyword!(TIME);
212keyword!(TIMEOUT);
213keyword!(TIMESTAMP);
214keyword!(TIMEUUID);
215keyword!(TINYINT);
216keyword!(TO);
217keyword!(TOKEN);
218keyword!(TRIGGER);
219keyword!(TRUNCATE);
220keyword!(TTL);
221keyword!(TUPLE);
222keyword!(TRUE);
223keyword!(TYPE);
224keyword!(UNLOGGED);
225keyword!(UNSET);
226keyword!(UPDATE);
227keyword!(USE);
228keyword!(USER);
229keyword!(USERS);
230keyword!(USING);
231keyword!(UUID);
232keyword!(VALUES);
233keyword!(VARCHAR);
234keyword!(VARINT);
235keyword!(VIEW);
236keyword!(WHERE);
237keyword!(WITH);
238keyword!(WRITETIME);
239
240impl Display for ReservedKeyword {
241 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
242 write!(
243 f,
244 "{}",
245 match self {
246 ReservedKeyword::ADD => "ADD",
247 ReservedKeyword::ALLOW => "ALLOW",
248 ReservedKeyword::ALTER => "ALTER",
249 ReservedKeyword::AND => "AND",
250 ReservedKeyword::APPLY => "APPLY",
251 ReservedKeyword::ASC => "ASC",
252 ReservedKeyword::AUTHORIZE => "AUTHORIZE",
253 ReservedKeyword::BATCH => "BATCH",
254 ReservedKeyword::BEGIN => "BEGIN",
255 ReservedKeyword::BY => "BY",
256 ReservedKeyword::COLUMNFAMILY => "COLUMNFAMILY",
257 ReservedKeyword::CREATE => "CREATE",
258 ReservedKeyword::DELETE => "DELETE",
259 ReservedKeyword::DESC => "DESC",
260 ReservedKeyword::DESCRIBE => "DESCRIBE",
261 ReservedKeyword::DROP => "DROP",
262 ReservedKeyword::ENTRIES => "ENTRIES",
263 ReservedKeyword::EXECUTE => "EXECUTE",
264 ReservedKeyword::FROM => "FROM",
265 ReservedKeyword::FULL => "FULL",
266 ReservedKeyword::GRANT => "GRANT",
267 ReservedKeyword::IF => "IF",
268 ReservedKeyword::IN => "IN",
269 ReservedKeyword::INDEX => "INDEX",
270 ReservedKeyword::INFINITY => "INFINITY",
271 ReservedKeyword::INSERT => "INSERT",
272 ReservedKeyword::INTO => "INTO",
273 ReservedKeyword::KEYSPACE => "KEYSPACE",
274 ReservedKeyword::LIMIT => "LIMIT",
275 ReservedKeyword::MODIFY => "MODIFY",
276 ReservedKeyword::NAN => "NAN",
277 ReservedKeyword::NORECURSIVE => "NORECURSIVE",
278 ReservedKeyword::NOT => "NOT",
279 ReservedKeyword::NULL => "NULL",
280 ReservedKeyword::OF => "OF",
281 ReservedKeyword::ON => "ON",
282 ReservedKeyword::OR => "OR",
283 ReservedKeyword::ORDER => "ORDER",
284 ReservedKeyword::PRIMARY => "PRIMARY",
285 ReservedKeyword::RENAME => "RENAME",
286 ReservedKeyword::REPLACE => "REPLACE",
287 ReservedKeyword::REVOKE => "REVOKE",
288 ReservedKeyword::SCHEMA => "SCHEMA",
289 ReservedKeyword::SELECT => "SELECT",
290 ReservedKeyword::SET => "SET",
291 ReservedKeyword::TABLE => "TABLE",
292 ReservedKeyword::TO => "TO",
293 ReservedKeyword::TRUNCATE => "TRUNCATE",
294 ReservedKeyword::UNLOGGED => "UNLOGGED",
295 ReservedKeyword::UPDATE => "UPDATE",
296 ReservedKeyword::USE => "USE",
297 ReservedKeyword::USING => "USING",
298 ReservedKeyword::VIEW => "VIEW",
299 ReservedKeyword::WHERE => "WHERE",
300 ReservedKeyword::WITH => "WITH",
301 }
302 )
303 }
304}
305
306impl FromStr for ReservedKeyword {
307 type Err = anyhow::Error;
308
309 fn from_str(s: &str) -> anyhow::Result<Self> {
310 Ok(match s.to_uppercase().as_str() {
311 "ADD" => ReservedKeyword::ADD,
312 "ALLOW" => ReservedKeyword::ALLOW,
313 "ALTER" => ReservedKeyword::ALTER,
314 "AND" => ReservedKeyword::AND,
315 "APPLY" => ReservedKeyword::APPLY,
316 "ASC" => ReservedKeyword::ASC,
317 "AUTHORIZE" => ReservedKeyword::AUTHORIZE,
318 "BATCH" => ReservedKeyword::BATCH,
319 "BEGIN" => ReservedKeyword::BEGIN,
320 "BY" => ReservedKeyword::BY,
321 "COLUMNFAMILY" => ReservedKeyword::COLUMNFAMILY,
322 "CREATE" => ReservedKeyword::CREATE,
323 "DELETE" => ReservedKeyword::DELETE,
324 "DESC" => ReservedKeyword::DESC,
325 "DESCRIBE" => ReservedKeyword::DESCRIBE,
326 "DROP" => ReservedKeyword::DROP,
327 "ENTRIES" => ReservedKeyword::ENTRIES,
328 "EXECUTE" => ReservedKeyword::EXECUTE,
329 "FROM" => ReservedKeyword::FROM,
330 "FULL" => ReservedKeyword::FULL,
331 "GRANT" => ReservedKeyword::GRANT,
332 "IF" => ReservedKeyword::IF,
333 "IN" => ReservedKeyword::IN,
334 "INDEX" => ReservedKeyword::INDEX,
335 "INFINITY" => ReservedKeyword::INFINITY,
336 "INSERT" => ReservedKeyword::INSERT,
337 "INTO" => ReservedKeyword::INTO,
338 "KEYSPACE" => ReservedKeyword::KEYSPACE,
339 "LIMIT" => ReservedKeyword::LIMIT,
340 "MODIFY" => ReservedKeyword::MODIFY,
341 "NAN" => ReservedKeyword::NAN,
342 "NORECURSIVE" => ReservedKeyword::NORECURSIVE,
343 "NOT" => ReservedKeyword::NOT,
344 "NULL" => ReservedKeyword::NULL,
345 "OF" => ReservedKeyword::OF,
346 "ON" => ReservedKeyword::ON,
347 "OR" => ReservedKeyword::OR,
348 "ORDER" => ReservedKeyword::ORDER,
349 "PRIMARY" => ReservedKeyword::PRIMARY,
350 "RENAME" => ReservedKeyword::RENAME,
351 "REPLACE" => ReservedKeyword::REPLACE,
352 "REVOKE" => ReservedKeyword::REVOKE,
353 "SCHEMA" => ReservedKeyword::SCHEMA,
354 "SELECT" => ReservedKeyword::SELECT,
355 "SET" => ReservedKeyword::SET,
356 "TABLE" => ReservedKeyword::TABLE,
357 "TO" => ReservedKeyword::TO,
358 "TRUNCATE" => ReservedKeyword::TRUNCATE,
359 "UNLOGGED" => ReservedKeyword::UNLOGGED,
360 "UPDATE" => ReservedKeyword::UPDATE,
361 "USE" => ReservedKeyword::USE,
362 "USING" => ReservedKeyword::USING,
363 "VIEW" => ReservedKeyword::VIEW,
364 "WHERE" => ReservedKeyword::WHERE,
365 "WITH" => ReservedKeyword::WITH,
366 _ => anyhow::bail!("Expected reserved keyword, found {}", s),
367 })
368 }
369}
370
371impl Parse for ReservedKeyword {
372 type Output = Self;
373 fn parse(s: &mut StatementStream<'_>) -> anyhow::Result<Self>
374 where
375 Self: Sized,
376 {
377 ReservedKeyword::from_str(&s.parse_from::<Alphanumeric>()?)
378 }
379}
380
381macro_rules! punctuation {
382 ($t:ident, $c:literal) => {
383 #[derive(ParseFromStr, Copy, Clone, Debug)]
384 pub struct $t;
385 impl Parse for $t {
386 type Output = Self;
387 fn parse(s: &mut StatementStream<'_>) -> anyhow::Result<Self::Output> {
388 let c = s.parse::<char>()?;
389 if c != $c {
390 anyhow::bail!("Expected '{}', found {}", $c, s.info_with_token(c.to_string()));
391 }
392 Ok($t)
393 }
394 }
395 };
396}
397
398punctuation!(Comma, ',');
399punctuation!(Semicolon, ';');
400punctuation!(LeftParen, '(');
401punctuation!(RightParen, ')');
402punctuation!(LeftBrace, '{');
403punctuation!(RightBrace, '}');
404punctuation!(LeftBracket, '[');
405punctuation!(RightBracket, ']');
406punctuation!(LeftAngle, '<');
407punctuation!(RightAngle, '>');
408punctuation!(Equals, '=');
409punctuation!(Plus, '+');
410punctuation!(Minus, '-');
411punctuation!(Dash, '-');
412punctuation!(Star, '*');
413punctuation!(Slash, '/');
414punctuation!(Percent, '%');
415punctuation!(Tilde, '~');
416punctuation!(Exclamation, '!');
417punctuation!(Question, '?');
418punctuation!(Colon, ':');
419punctuation!(Dot, '.');
420punctuation!(SingleQuote, '\'');
421punctuation!(DoubleQuote, '\"');