xale-db 1.0
minimal SQL engine, written in c++
Loading...
Searching...
No Matches
Token.h
Go to the documentation of this file.
1#ifndef QUERY_TOKEN_H
2#define QUERY_TOKEN_H
3
4#include <string>
5#include <unordered_set>
6
7namespace Xale::Query
8{
26
30 struct Token
31 {
33 std::string lexeme;
34 size_t position;
35 };
36
37 /* --------------------------------------------------
38 * SQL (Basic) Keywords declaration
39 * --------------------------------------------------
40 */
41
42#define DECLARE_TOKENS(name, ...) \
43 const std::unordered_set<std::string> name({__VA_ARGS__})
44
45 // Data definition keywords
46 DECLARE_TOKENS(sql_definition_kw,
47 "CREATE",
48 "ALTER",
49 "DROP",
50 "LIST"
51 );
52
53 // Data manipulation keywords
54 DECLARE_TOKENS(sql_manipulation_kw,
55 "SELECT",
56 "INSERT",
57 "UPDATE",
58 "DELETE"
59 );
60
61 // Query keywords
62 DECLARE_TOKENS(sql_query_kw,
63 "FROM",
64 "WHERE"
65 );
66
67 // Join keywords
68 DECLARE_TOKENS(sql_join_kw,
69 "JOIN",
70 "LEFT",
71 "RIGHT"
72 );
73
74 // Logical keywords
75 DECLARE_TOKENS(sql_logical_kw,
76 "AND",
77 "OR",
78 "NOT"
79 );
80
81 // Operators
82 DECLARE_TOKENS(sql_operators,
83 "*",
84 ",",
85 "(",
86 ")",
87 "=",
88 "!=",
89 "<",
90 ">",
91 "<=",
92 ">="
93 );
94
95 // Special token
96 DECLARE_TOKENS(sql_end_query,
97 ";"
98 );
99
100 // For debug purpose
105 inline const std::string to_string(TokenType type)
106 {
107 switch (type)
108 {
109 case TokenType::DefinitionKeyword: return "DefinitionKeyword";
110 case TokenType::ManipulationKeyword: return "ManipulationKeyword";
111 case TokenType::QueryKeyword: return "QueryKeyword";
112 case TokenType::JoinKeyword: return "JoinKeyword";
113 case TokenType::LogicalKeyword: return "LogicalKeyword";
114 case TokenType::Operator: return "Operator";
115 case TokenType::Identifier: return "Identifier";
116 case TokenType::StringLiteral: return "StringLiteral";
117 case TokenType::NumericLiteral: return "NumericLiteral";
118 case TokenType::EndOfInput: return "EndOfInput";
119 case TokenType::Unknown: return "Unknown";
120 }
121 return "Invalid";
122 }
123}
124
125#endif // QUERY_TOKEN_H
#define DECLARE_TOKENS(name,...)
Definition Token.h:42
Definition BasicParser.h:16
@ Unknown
Definition Statement.h:23
@ Identifier
Definition Statement.h:31
@ StringLiteral
Definition Statement.h:32
@ NumericLiteral
Definition Statement.h:33
TokenType
Types of possible tokens.
Definition Token.h:13
@ LogicalKeyword
Definition Token.h:18
@ JoinKeyword
Definition Token.h:17
@ Identifier
Definition Token.h:20
@ EndOfInput
Definition Token.h:23
@ StringLiteral
Definition Token.h:21
@ Unknown
Definition Token.h:24
@ ManipulationKeyword
Definition Token.h:15
@ NumericLiteral
Definition Token.h:22
@ QueryKeyword
Definition Token.h:16
@ DefinitionKeyword
Definition Token.h:14
@ Operator
Definition Token.h:19
const std::string to_string(TokenType type)
Convert TokenType to string.
Definition Token.h:105
Token struct defined by type, value and pos.
Definition Token.h:31
TokenType type
Definition Token.h:32
size_t position
Definition Token.h:34
std::string lexeme
Definition Token.h:33