Circus 0.0.1-alpha
C++ Serialization Framework
Loading...
Searching...
No Matches
token.hpp
1#pragma once
2#include <iostream>
3#include <variant>
4
5#include "visitor.hpp"
6
7namespace circus {
8
17struct tokens__ {
18 // https://stackoverflow.com/questions/1801363/c-c-any-way-to-get-reflective-enums
20#define TOKEN_DEFs \
21 TOKEN_DEF(TK_QUOTE_DOUBLE, '\"') \
22 TOKEN_DEF(TK_PAREN_L, '(') \
23 TOKEN_DEF(TK_PAREN_R, ')') \
24 TOKEN_DEF(TK_COMMA, ',') \
25 TOKEN_DEF(TK_COLON, ':') \
26 TOKEN_DEF(TK_BRACE_L, '[') \
27 TOKEN_DEF(TK_BRACE_R, ']') \
28 TOKEN_DEF(TK_DOLLA, '$') \
29 TOKEN_DEF(TK_CURL_L, '{') \
30 TOKEN_DEF(TK_CURL_R, '}') \
31 TOKEN_DEF(TK_QUOTE_SINGLE, '\'') \
32 TOKEN_DEF(TK_STAR, '*') \
33 TOKEN_DEF(TK_SLASH, '/') \
34 TOKEN_DEF(TK_EOF, '\0') \
35 TOKEN_DEF(TK_SPACE, ' ') \
36 TOKEN_DEF(TK_NEWLINE, '\n') \
37 TOKEN_DEF(TK_LITERAL_INT, 0xFE) \
38 TOKEN_DEF(TK_LITERAL_FLOAT, 0xFD) \
39 TOKEN_DEF(TK_IDENTIFIER, 0xFC) \
40 TOKEN_DEF(TK_LITERAL_STRING, 0xFB) \
41 TOKEN_DEF(TK_LITERAL_DOUBLE, 0xFA) \
42 TOKEN_DEF(TK_UNKNOWN, 0xFF)
43
45#define TOKEN_DEF(NAME, VALUE) NAME = VALUE,
46 enum TYPE : unsigned char {
47 TOKEN_DEFs
48 };
49#undef TOKEN_DEF
50
52 using literal_variant_t = std::variant<char, std::string, int, float, double>;
53
55 using location_t = std::pair<std::size_t, std::size_t>;
56
58 TYPE _token_type;
59
66 constexpr static inline const char *to_string(TYPE type) noexcept {
67 switch (type) {
68#define TOKEN_DEF(NAME, VALUE) \
69 case NAME: \
70 return #NAME;
71 TOKEN_DEFs
72#undef TOKEN_DEF
73 default : return "UNKNOWN_TYPE";
74 }
75 }
76
83 constexpr static inline const char *to_stringized(TYPE type) noexcept {
84 switch (type) {
85#define TOKEN_DEF(NAME, VALUE) \
86 case NAME: \
87 return #VALUE;
88 TOKEN_DEFs
89#undef TOKEN_DEF
90 default : return "UNKNOWN_LITERAL";
91 }
92 }
93
100 constexpr static inline std::string to_literal(TYPE type) noexcept {
101 const char *lit = to_stringized(type);
102 std::string ret{lit};
103 return ret.substr(1, ret.size() - 2);
104 }
105
106 public:
108 std::string embedded;
109
111 literal_variant_t literal;
112
114 location_t location;
115
119 void print_token() const noexcept {
120 std::cout << "TOKEN TYPE ID (" << to_literal(_token_type) << ")"
121 << " [" << to_string(_token_type) << "]\n";
122 std::cout << "location (row, col) < " << location.first << " , " << location.second << " > ";
123 std::visit(internal::visitor{
124 [](char c) { std::cout << "[CHAR] " << c << std::endl; },
125 [](std::string s) { std::cout << "[STRING] " << s << std::endl; },
126 [](int i) { std::cout << "[INT] " << i << std::endl; },
127 [](float f) { std::cout << "[FLOAT] " << f << std::endl; },
128 [](double d) { std::cout << "[DOUBLE] " << d << std::endl; },
129 },
130 literal);
131 };
132
141 tokens__(TYPE type, std::string embedded, literal_variant_t lit, location_t loc)
142 : _token_type(type), embedded(embedded), literal(lit), location(loc) {};
143};
144
145}; // namespace circus
tokens__(TYPE type, std::string embedded, literal_variant_t lit, location_t loc)
Constructor to create a token instance.
Definition token.hpp:141