-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.cpp
More file actions
153 lines (124 loc) · 4.26 KB
/
parser.cpp
File metadata and controls
153 lines (124 loc) · 4.26 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include "parser.h"
#include <iostream>
#include <stdexcept>
void expectChar(const std::string& input, const size_t& pos, char expected) {
if (pos >= input.size() || input[pos] != expected) {
throw std::runtime_error(
"Expected '" + std::string(1, expected) +
"' at position " + std::to_string(pos)
);
}
}
bool isInteger(const std::string& s) {
if (s.empty()) return false;
size_t i = 0;
if (s[0] == '-' || s[0] == '+') {
if (s.size() == 1) return false;
i = 1;
}
for (; i < s.size(); i++) {
if (!std::isdigit(s[i])) return false;
}
return true;
}
static std::string readLine(const std::string& input, size_t& pos) {
const size_t startIndex = pos;
while (pos < input.size()) {
if (input[pos] == '\r' && pos + 1 < input.size() && input[pos + 1] == '\n') {
std::string line = input.substr(startIndex, pos - startIndex);
pos += 2;
return line;
}
pos++;
}
throw IncompleteMessageException();
}
RespValue parseValue(const std::string& input, size_t& pos) {
char type = input.at(pos);
switch (type) {
case '+':
return parseSimpleString(input, pos);
case '-':
return parseError(input, pos);
case ':':
return parseInteger(input, pos);
case '$':
return parseBulkString(input, pos);
case '*':
return parseArray(input, pos);
default:
throw std::runtime_error("Invalid RESP type prefix.");
}
}
RespValue parseSimpleString(const std::string& input, size_t& pos) {
expectChar(input, pos, '+');
pos++;
std::string lineContent = readLine(input, pos);
return RespValue(RespType::SIMPLE_STRING, lineContent);
}
RespValue parseError(const std::string& input, size_t& pos) {
expectChar(input, pos, '-');
pos++;
std::string lineContent = readLine(input, pos);
return RespValue(RespType::ERR, lineContent);
}
RespValue parseInteger(const std::string& input, size_t& pos) {
expectChar(input, pos, ':');
pos++;
std::string lineContent = readLine(input, pos);
if (!isInteger(lineContent)) {
throw std::runtime_error("Invalid RESP integer at position " + std::to_string(pos));
}
long long value = std::stoll(lineContent);
return RespValue(RespType::INTEGER, value);
}
RespValue parseBulkString(const std::string& input, size_t& pos) {
expectChar(input, pos, '$');
pos++;
std::string lengthStr = readLine(input, pos);
long long length;
try {
length = std::stoll(lengthStr);
} catch (const std::exception&) {
throw std::runtime_error("Invalid bulk string length at position " + std::to_string(pos));
}
if (length == -1) {
return RespValue(RespType::BULK_STRING, std::optional<std::string>{});
}
if (length < 0) {
throw std::runtime_error("Invalid bulk string length (negative) at position " + std::to_string(pos));
}
if (pos + length + 2 > input.size()) {
// Bulk string too short
throw IncompleteMessageException();
}
std::string content = input.substr(pos, length);
pos += length;
if (input[pos] != '\r' || input[pos + 1] != '\n') {
throw std::runtime_error("Bulk string trailing CRLF missing");
}
pos += 2;
return RespValue(RespType::BULK_STRING, std::optional<std::string>{content});
}
RespValue parseArray(const std::string& input, size_t& pos) {
expectChar(input, pos, '*');
pos++;
std::string numStr = readLine(input, pos);
long long numElements;
try {
numElements = std::stoll(numStr);
} catch (const std::exception&) {
throw std::runtime_error("Invalid array length at position " + std::to_string(pos));
}
if (numElements == -1) {
return RespValue(RespType::ARRAY, std::optional<std::vector<RespValue>>{});
}
if (numElements < 0) {
throw std::runtime_error("Invalid array length (negative) at position " + std::to_string(pos));
}
std::vector<RespValue> elements;
for (long long i = 0; i < numElements; ++i) {
elements.push_back(parseValue(input, pos));
}
return RespValue(RespType::ARRAY, std::optional<std::vector<RespValue>>{elements});
}