Skip to content

Commit 2f0a7e6

Browse files
authored
Add lexer_error to DRY up the error handling in the Lexer (#22)
* Add `lexer_error` to DRY up the error handling
1 parent e5171a5 commit 2f0a7e6

File tree

1 file changed

+21
-21
lines changed

1 file changed

+21
-21
lines changed

src/lexer.c

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include <ctype.h>
77
#include <stdio.h>
8+
#include <stdnoreturn.h>
89
#include <string.h>
910

1011
char* lexer_state_to_string(lexer_T* lexer) {
@@ -41,6 +42,16 @@ lexer_T* lexer_init(char* source) {
4142
return lexer;
4243
}
4344

45+
noreturn void lexer_error(lexer_T* lexer, const char* message) {
46+
fprintf(stderr,
47+
"Lexer Error [character '%c', line %d, col %d]: %s\n",
48+
lexer->current_character,
49+
lexer->current_line,
50+
lexer->current_column,
51+
message);
52+
exit(1);
53+
}
54+
4455
char lexer_peek(lexer_T* lexer, int offset) {
4556
return lexer->source[MIN(lexer->current_position + offset, lexer->source_length)];
4657
}
@@ -212,8 +223,7 @@ token_T* lexer_handle_data_state(lexer_T* lexer) {
212223
}
213224

214225
// TODO: handle this case
215-
printf("lexer_handle_data_state: '%c'\n", lexer->current_character);
216-
exit(1);
226+
lexer_error(lexer, "Unexpected character in lexer_handle_data_state");
217227
}
218228

219229
default: {
@@ -233,8 +243,7 @@ token_T* lexer_handle_data_state(lexer_T* lexer) {
233243
return token_init("%>", TOKEN_ERB_END, lexer);
234244
}
235245

236-
printf("lexer_handle_html_attributes_state in '%%': '%c'\n", lexer_peek(lexer, 1));
237-
exit(1);
246+
lexer_error(lexer, "Unexpected character in lexer_handle_html_attributes_state");
238247
} break;
239248

240249
default: {
@@ -282,8 +291,7 @@ token_T* lexer_handle_html_attributes_state(lexer_T* lexer) {
282291
}
283292

284293
// TODO: handle this case
285-
printf("lexer_handle_html_attributes_state in '/': '%c'\n", lexer_peek(lexer, 1));
286-
exit(1);
294+
lexer_error(lexer, "Unexpected character in lexer_handle_html_attributes_state");
287295
} break;
288296

289297
default: {
@@ -312,8 +320,7 @@ token_T* lexer_handle_tag_name_state(lexer_T* lexer) {
312320
} break;
313321

314322
default: {
315-
printf("lexer_handle_tag_name_state ELSE: '%c'\n", lexer->current_character);
316-
exit(1);
323+
lexer_error(lexer, "Unexpected character in lexer_handle_tag_name_state");
317324
}
318325
}
319326
}
@@ -347,8 +354,7 @@ token_T* lexer_handle_html_attribute_name_state(lexer_T* lexer) {
347354
}
348355

349356
// TODO: handle this case
350-
printf("lexer_handle_html_attribute_name_state in '/': '%c'\n", lexer_peek(lexer, 1));
351-
exit(1);
357+
lexer_error(lexer, "Unexpected character in lexer_handle_html_attribute_name_state");
352358
} break;
353359

354360
case '>': {
@@ -358,8 +364,7 @@ token_T* lexer_handle_html_attribute_name_state(lexer_T* lexer) {
358364
} break;
359365

360366
default: {
361-
printf("lexer_handle_html_attribute_name_state: '%c'\n", lexer->current_character);
362-
exit(1);
367+
lexer_error(lexer, "Unexpected character in lexer_handle_html_attribute_name_state");
363368
}
364369
}
365370
}
@@ -415,16 +420,15 @@ token_T* lexer_handle_html_attribute_value_state(lexer_T* lexer) {
415420
}
416421

417422
default: {
418-
printf("lexer_handle_html_attribute_value_state: '%c'\n", lexer->current_character);
419-
exit(1);
423+
lexer_error(lexer, "Unexpected character in lexer_handle_html_attribute_value_state");
420424
}
421425
}
422426
}
423427

424428
token_T* lexer_handle_html_tag_open_state(lexer_T* lexer) {
425429
switch (lexer->current_character) {
426430
case ' ': {
427-
exit(1);
431+
lexer_error(lexer, "Unexpected character in lexer_handle_html_tag_open_state");
428432
} break;
429433

430434
default: {
@@ -463,15 +467,11 @@ token_T* lexer_handle_html_comment_close_state(lexer_T* lexer) {
463467
}
464468

465469
// TODO: handle this case
466-
printf("lexer_handle_html_comment_close_state in '-': '%c' and '%c'\n",
467-
lexer_peek(lexer, 1),
468-
lexer_peek(lexer, 2));
469-
exit(1);
470+
lexer_error(lexer, "Unexpected character in lexer_handle_html_comment_close_state");
470471
}
471472

472473
default: {
473-
printf("lexer_handle_html_comment_close_state: '%c'\n", lexer->current_character);
474-
exit(1);
474+
lexer_error(lexer, "Unexpected character in lexer_handle_html_comment_close_state");
475475
}
476476
}
477477
}

0 commit comments

Comments
 (0)