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{
27
31 struct Token
32 {
34 std::string lexeme;
35 size_t position;
36 };
37
38 /* --------------------------------------------------
39 * SQL (Basic) Keywords declaration
40 * --------------------------------------------------
41 */
42
43#define DECLARE_TOKENS(name, ...) \
44 const std::unordered_set<std::string> name({__VA_ARGS__})
45
46 // Data definition keywords
47 DECLARE_TOKENS(sql_definition_kw,
48 "CREATE",
49 "ALTER",
50 "DROP",
51 "LIST"
52 );
53
54 // Data manipulation keywords
55 DECLARE_TOKENS(sql_manipulation_kw,
56 "SELECT",
57 "INSERT",
58 "UPDATE",
59 "DELETE"
60 );
61
62 // Query keywords
63 DECLARE_TOKENS(sql_query_kw,
64 "FROM",
65 "WHERE",
66 "ON"
67 );
68
69 // Join keywords
70 DECLARE_TOKENS(sql_join_kw,
71 "JOIN",
72 "LEFT",
73 "RIGHT"
74 );
75
76 // Logical keywords
77 DECLARE_TOKENS(sql_logical_kw,
78 "AND",
79 "OR",
80 "NOT"
81 );
82
83 // Operators
84 DECLARE_TOKENS(sql_operators,
85 "*",
86 ",",
87 "(",
88 ")",
89 "=",
90 "!=",
91 "<",
92 ">",
93 "<=",
94 ">="
95 );
96
97 // Special token
98 DECLARE_TOKENS(sql_end_query,
99 ";"
100 );
101
102 // For debug purpose
107 inline const std::string to_string(TokenType type)
108 {
109 switch (type)
110 {
111 case TokenType::DefinitionKeyword: return "DefinitionKeyword";
112 case TokenType::ManipulationKeyword: return "ManipulationKeyword";
113 case TokenType::QueryKeyword: return "QueryKeyword";
114 case TokenType::JoinKeyword: return "JoinKeyword";
115 case TokenType::LogicalKeyword: return "LogicalKeyword";
116 case TokenType::Operator: return "Operator";
117 case TokenType::Identifier: return "Identifier";
118 case TokenType::StringLiteral: return "StringLiteral";
119 case TokenType::NumericLiteral: return "NumericLiteral";
120 case TokenType::EndOfInput: return "EndOfInput";
121 case TokenType::Semicolon: return "Semicolon";
122 case TokenType::Unknown: return "Unknown";
123 }
124 return "Invalid";
125 }
126}
127
128#endif // QUERY_TOKEN_H
#define DECLARE_TOKENS(name,...)
Definition Token.h:43
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:25
@ Semicolon
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:107
Token struct defined by type, value and pos.
Definition Token.h:32
TokenType type
Definition Token.h:33
size_t position
Definition Token.h:35
std::string lexeme
Definition Token.h:34