-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathtoken.h
101 lines (91 loc) · 2.59 KB
/
token.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#pragma once
#include <any>
#include <cstdint>
#include <string>
#include "error/error.h"
namespace miniplc0 {
enum TokenType {
NULL_TOKEN,
UNSIGNED_INTEGER,
IDENTIFIER,
BEGIN,
END,
VAR,
CONST,
PRINT,
PLUS_SIGN,
MINUS_SIGN,
MULTIPLICATION_SIGN,
DIVISION_SIGN,
EQUAL_SIGN,
SEMICOLON,
LEFT_BRACKET,
RIGHT_BRACKET
};
class Token final {
private:
using uint64_t = std::uint64_t;
using int32_t = std::int32_t;
public:
friend void swap(Token &lhs, Token &rhs);
public:
Token(TokenType type, std::any value, uint64_t start_line,
uint64_t start_column, uint64_t end_line, uint64_t end_column)
: _type(type),
_value(std::move(value)),
_start_pos(start_line, start_column),
_end_pos(end_line, end_column) {}
Token(TokenType type, std::any value, std::pair<uint64_t, uint64_t> start,
std::pair<uint64_t, uint64_t> end)
: Token(type, value, start.first, start.second, end.first, end.second) {}
Token(const Token &t) {
_type = t._type;
_value = t._value;
_start_pos = t._start_pos;
_end_pos = t._end_pos;
}
Token(Token &&t) : Token(TokenType::NULL_TOKEN, nullptr, 0, 0, 0, 0) {
swap(*this, t);
}
Token &operator=(Token t) {
swap(*this, t);
return *this;
}
bool operator==(const Token &rhs) const {
return _type == rhs._type && GetValueString() == rhs.GetValueString() &&
_start_pos == rhs._start_pos && _end_pos == rhs._end_pos;
}
TokenType GetType() const { return _type; };
std::any GetValue() const { return _value; };
std::pair<uint64_t, uint64_t> GetStartPos() const { return _start_pos; }
std::pair<uint64_t, uint64_t> GetEndPos() const { return _end_pos; }
std::string GetValueString() const {
try {
return std::any_cast<std::string>(_value);
} catch (const std::bad_any_cast &) {
}
try {
return std::string(1, std::any_cast<char>(_value));
} catch (const std::bad_any_cast &) {
}
try {
return std::to_string(std::any_cast<int32_t>(_value));
} catch (const std::bad_any_cast &) {
DieAndPrint("No suitable cast for token value.");
}
return "Invalid";
}
private:
TokenType _type;
std::any _value;
std::pair<uint64_t, uint64_t> _start_pos;
std::pair<uint64_t, uint64_t> _end_pos;
};
inline void swap(Token &lhs, Token &rhs) {
using std::swap;
swap(lhs._type, rhs._type);
swap(lhs._value, rhs._value);
swap(lhs._start_pos, rhs._start_pos);
swap(lhs._end_pos, rhs._end_pos);
}
} // namespace miniplc0