Skip to content

Commit 1e1cb4f

Browse files
committed
Add lexer_error to DRY up the error handling
1 parent e5171a5 commit 1e1cb4f

File tree

1 file changed

+16
-21
lines changed

1 file changed

+16
-21
lines changed

src/lexer.c

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <ctype.h>
77
#include <stdio.h>
88
#include <string.h>
9+
#include <stdnoreturn.h>
910

1011
char* lexer_state_to_string(lexer_T* lexer) {
1112
switch (lexer->state) {
@@ -41,6 +42,11 @@ 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, "Lexer Error [character '%c', line %d, col %d]: %s\n", lexer->current_character, lexer->current_line, lexer->current_column, message);
47+
exit(1);
48+
}
49+
4450
char lexer_peek(lexer_T* lexer, int offset) {
4551
return lexer->source[MIN(lexer->current_position + offset, lexer->source_length)];
4652
}
@@ -212,8 +218,7 @@ token_T* lexer_handle_data_state(lexer_T* lexer) {
212218
}
213219

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

219224
default: {
@@ -233,8 +238,7 @@ token_T* lexer_handle_data_state(lexer_T* lexer) {
233238
return token_init("%>", TOKEN_ERB_END, lexer);
234239
}
235240

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

240244
default: {
@@ -282,8 +286,7 @@ token_T* lexer_handle_html_attributes_state(lexer_T* lexer) {
282286
}
283287

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

289292
default: {
@@ -312,8 +315,7 @@ token_T* lexer_handle_tag_name_state(lexer_T* lexer) {
312315
} break;
313316

314317
default: {
315-
printf("lexer_handle_tag_name_state ELSE: '%c'\n", lexer->current_character);
316-
exit(1);
318+
lexer_error(lexer, "Unexpected character in lexer_handle_tag_name_state");
317319
}
318320
}
319321
}
@@ -347,8 +349,7 @@ token_T* lexer_handle_html_attribute_name_state(lexer_T* lexer) {
347349
}
348350

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

354355
case '>': {
@@ -358,8 +359,7 @@ token_T* lexer_handle_html_attribute_name_state(lexer_T* lexer) {
358359
} break;
359360

360361
default: {
361-
printf("lexer_handle_html_attribute_name_state: '%c'\n", lexer->current_character);
362-
exit(1);
362+
lexer_error(lexer, "Unexpected character in lexer_handle_html_attribute_name_state");
363363
}
364364
}
365365
}
@@ -415,16 +415,15 @@ token_T* lexer_handle_html_attribute_value_state(lexer_T* lexer) {
415415
}
416416

417417
default: {
418-
printf("lexer_handle_html_attribute_value_state: '%c'\n", lexer->current_character);
419-
exit(1);
418+
lexer_error(lexer, "Unexpected character in lexer_handle_html_attribute_value_state");
420419
}
421420
}
422421
}
423422

424423
token_T* lexer_handle_html_tag_open_state(lexer_T* lexer) {
425424
switch (lexer->current_character) {
426425
case ' ': {
427-
exit(1);
426+
lexer_error(lexer, "Unexpected character in lexer_handle_html_tag_open_state");
428427
} break;
429428

430429
default: {
@@ -463,15 +462,11 @@ token_T* lexer_handle_html_comment_close_state(lexer_T* lexer) {
463462
}
464463

465464
// 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);
465+
lexer_error(lexer, "Unexpected character in lexer_handle_html_comment_close_state");
470466
}
471467

472468
default: {
473-
printf("lexer_handle_html_comment_close_state: '%c'\n", lexer->current_character);
474-
exit(1);
469+
lexer_error(lexer, "Unexpected character in lexer_handle_html_comment_close_state");
475470
}
476471
}
477472
}

0 commit comments

Comments
 (0)