-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathirc_message_parser.c
359 lines (274 loc) · 10.1 KB
/
irc_message_parser.c
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "irc_token.h"
#include "irc_lexer.h"
#include "irc_message_parser.h"
#include "irc_spec.h"
#include "irc_message.h"
#include "irc_logger.h"
enum irc_parser_state {
IRC_PARSER_STATE_PREFIX = 0,
IRC_PARSER_STATE_COMMAND,
IRC_PARSER_STATE_PARAM,
IRC_PARSER_STATE_DONE
};
enum irc_command_parser_substate {
IRC_PARSER_SUBSTATE_NONE = 0,
IRC_PARSER_SUBSTATE_CODE,
IRC_PARSER_SUBSTATE_NAME
};
struct irc_message_parser {
struct irc_lexer *lexer;
struct irc_token *current_token;
enum irc_parser_state state;
};
typedef void (*handle_parser_state_t)(struct irc_message_parser *parser, struct irc_message *message);
static inline struct irc_message *__allocate_irc_message();
static void __eat_token(struct irc_message_parser *parser, enum irc_token_type expected_token_type);
static inline void __force_advance(struct irc_message_parser *parser);
static void __handle_prefix(struct irc_message_parser *parser, struct irc_message *message);
static void __handle_command(struct irc_message_parser *parser, struct irc_message *message);
static void __handle_parameter(struct irc_message_parser *parser, struct irc_message *message);
static void __expecting_error(struct irc_message_parser *parser,
enum irc_token_type expected,
enum irc_token_type got);
static handle_parser_state_t parser_handlers[] = {
&__handle_prefix,
&__handle_command,
&__handle_parameter
};
struct irc_message_parser *allocate_irc_message_parser(struct irc_lexer *lexer) {
struct irc_message_parser *parser;
union irc_token_value token_value;
if ((parser = (struct irc_message_parser *) malloc(sizeof(struct irc_message_parser))) == NULL)
return NULL;
parser->lexer = lexer;
token_value.character = '\0';
parser->current_token = allocate_irc_token(IRC_TOKEN_EOF, token_value);
parser->state = IRC_PARSER_STATE_PREFIX;
return parser;
}
void deallocate_irc_message_parser(struct irc_message_parser *parser) {
deallocate_irc_token(parser->current_token);
deallocate_irc_lexer(parser->lexer);
free(parser);
}
bool irc_message_parser_can_parse(struct irc_message_parser *parser) {
return !irc_token_is_token_type(parser->current_token, IRC_TOKEN_EOF);
}
struct irc_message *irc_message_parser_parse(struct irc_message_parser *parser) {
struct irc_message *msg = __allocate_irc_message();
if (msg == NULL)
return NULL;
if (irc_token_is_token_type(parser->current_token, IRC_TOKEN_EOF)) {
free(msg);
__eat_token(parser, IRC_TOKEN_EOF);
return NULL;
}
parser->state = IRC_PARSER_STATE_PREFIX;
while (parser->state != IRC_PARSER_STATE_DONE) {
parser_handlers[parser->state](parser, msg);
}
return msg;
}
void deallocate_irc_message(struct irc_message *msg) {
uint8_t i;
if (msg->prefix != NULL) {
free(msg->prefix->value);
free(msg->prefix);
}
if (msg->command != NULL) {
free(msg->command->command.value);
for (i = 0; i < msg->command->parameter_count; i++) {
free(msg->command->parameters[i]->value);
free(msg->command->parameters[i]);
}
free(msg->command);
}
free(msg);
}
static inline struct irc_message *__allocate_irc_message() {
struct irc_message *msg;
if ((msg = (struct irc_message *) malloc(sizeof(struct irc_message))) == NULL) {
return NULL;
}
msg->prefix = NULL;
msg->command = NULL;
return msg;
}
// @todo: Make a variadic function
static void __eat_token(struct irc_message_parser *parser, enum irc_token_type expected_token_type) {
enum irc_token_type current_token_type;
current_token_type = irc_token_get_token_type(parser->current_token);
if (irc_token_is_token_type(parser->current_token, expected_token_type)) {
__force_advance(parser);
return;
}
__expecting_error(parser, expected_token_type, current_token_type);
exit(EXIT_FAILURE);
}
static void __handle_prefix(struct irc_message_parser *parser, struct irc_message *message) {
size_t prefix_len;
char *current_line = NULL;
struct irc_prefix *prefix;
if (!irc_token_is_token_type(parser->current_token, IRC_TOKEN_COLON)) {
parser->state = IRC_PARSER_STATE_COMMAND;
return;
}
__eat_token(parser, IRC_TOKEN_COLON);
prefix_len = 0;
while (true) {
if (irc_token_is_token_type(parser->current_token, IRC_TOKEN_SPACE)) {
__eat_token(parser, IRC_TOKEN_SPACE);
break;
}
prefix_len++;
__force_advance(parser);
}
prefix = (struct irc_prefix *) malloc(sizeof(struct irc_prefix));
if (prefix == NULL) {
log_error("Parser: Unable to allocate prefix.\n");
exit(EXIT_FAILURE);
}
prefix->length = prefix_len;
prefix->value = (char *) calloc(prefix->length + 1, sizeof(char));
current_line = irc_lexer_get_current_line(parser->lexer);
if (prefix->value == NULL || current_line == NULL) {
goto done;
}
strncpy(prefix->value, current_line + 1, prefix_len);
message->prefix = prefix;
done:
free(current_line);
parser->state = IRC_PARSER_STATE_COMMAND;
}
static void __handle_command(struct irc_message_parser *parser, struct irc_message *message) {
size_t cmd_length = 0;
int cmd_code = 0;
int cmd_parser_substate = IRC_PARSER_SUBSTATE_NONE;
struct irc_command *command;
if ((command = (struct irc_command *) malloc(sizeof(struct irc_command))) == NULL) {
log_error("Parser: Unable to allocate irc command.\n");
exit(EXIT_FAILURE);
}
command->parameter_count = 0;
command->datetime_created = time(NULL);
memset(command->parameters, '\0', sizeof(struct irc_command_parameter *) * IRC_SPEC_MAX_COMMAND_PARAMETER_COUNT);
while (true) {
if (irc_token_is_token_type(parser->current_token, IRC_TOKEN_SPACE)) {
__eat_token(parser, IRC_TOKEN_SPACE);
break;
}
if (irc_token_is_token_type(parser->current_token, IRC_TOKEN_EOL)) {
__eat_token(parser, IRC_TOKEN_EOL);
break;
}
if (cmd_parser_substate == IRC_PARSER_SUBSTATE_NONE) {
if (irc_token_is_token_type(parser->current_token, IRC_TOKEN_DIGIT))
cmd_parser_substate = IRC_PARSER_SUBSTATE_CODE;
else
cmd_parser_substate = IRC_PARSER_SUBSTATE_NAME;
}
cmd_length++;
if (cmd_parser_substate == IRC_PARSER_SUBSTATE_CODE) {
cmd_code = cmd_code * 10 + irc_token_get_token_value(parser->current_token).integer;
__eat_token(parser, IRC_TOKEN_DIGIT);
}
if (cmd_parser_substate == IRC_PARSER_SUBSTATE_NAME) {
__eat_token(parser, IRC_TOKEN_LETTER);
}
}
if (cmd_parser_substate == IRC_PARSER_SUBSTATE_NONE) {
free(command);
goto advance_state;
}
if (cmd_parser_substate == IRC_PARSER_SUBSTATE_CODE) {
if (cmd_length != 3) {
log_error("Parser: Command must be 3 digits.\n");
exit(EXIT_FAILURE);
}
}
command->command.length = cmd_length;
command->command.value = (char *) calloc(cmd_length + 1, sizeof(char));
// todo: If calloc fails, do something
char *line = irc_lexer_get_current_line(parser->lexer);
size_t prefix_len = (message->prefix == NULL) ? 0 : message->prefix->length + 2;
strncpy(command->command.value, line + prefix_len, cmd_length);
free(line);
message->command = command;
advance_state:
parser->state = IRC_PARSER_STATE_PARAM;
}
static void __handle_parameter(struct irc_message_parser *parser, struct irc_message *message) {
bool kill = false;
bool notcolon = true;
size_t param_character_counter = 0;
struct irc_command_parameter *param = NULL;
char *param_string = NULL;
char *line = irc_lexer_get_current_line(parser->lexer);
size_t line_offset;
line_offset = irc_lexer_get_current_column(parser->lexer);
while (true) {
if (kill) {
break;
}
if (irc_token_is_token_type(parser->current_token, IRC_TOKEN_EOL) ||
irc_token_is_token_type(parser->current_token, IRC_TOKEN_NONE)) {
break;
}
if (message->command->parameter_count > IRC_SPEC_MAX_COMMAND_PARAMETER_COUNT) {
log_error("Parser: Parameter count exceeded.\n");
exit(EXIT_FAILURE);
break;
}
param_character_counter = 0;
while (true) {
if (irc_token_is_token_type(parser->current_token, IRC_TOKEN_COLON) && notcolon) {
notcolon = false;
line_offset++;
__eat_token(parser, IRC_TOKEN_COLON);
continue;
}
if ((irc_token_is_token_type(parser->current_token, IRC_TOKEN_SPACE) && notcolon) ||
irc_token_is_token_type(parser->current_token, IRC_TOKEN_EOL)) {
param = (struct irc_command_parameter *) malloc(sizeof(struct irc_command_parameter));
param_string = (char *) calloc(param_character_counter + 1, sizeof(char));
line_offset = (line_offset + message->command->parameter_count);
strncpy(param_string, line + line_offset - param_character_counter - 1, param_character_counter);
param->length = param_character_counter;
param->value = param_string;
message->command->parameters[message->command->parameter_count] = param;
message->command->parameter_count++;
if (irc_token_is_token_type(parser->current_token, IRC_TOKEN_EOL)) {
kill = true;
__eat_token(parser, IRC_TOKEN_EOL);
break;
} else {
__eat_token(parser, IRC_TOKEN_SPACE);
}
break;
}
param_character_counter++;
line_offset++;
__force_advance(parser);
}
}
free(line);
parser->state = IRC_PARSER_STATE_DONE;
}
static inline void __force_advance(struct irc_message_parser *parser) {
deallocate_irc_token(parser->current_token);
parser->current_token = irc_lexer_get_next_token(parser->lexer);
}
void __expecting_error(struct irc_message_parser *parser,
enum irc_token_type expected,
enum irc_token_type got) {
struct irc_lexer *lex = parser->lexer;
char *line = irc_lexer_get_current_line(lex);
line[irc_lexer_get_current_line_length(lex) - 2] = '\0';
line[irc_lexer_get_current_line_length(lex) - 1] = '\0';
log_error("Parse: There was an error parsing input. Expecting token %i but got %i instead in the line \"%s\"\n",
expected, got, line);
free(line);
}