diff options
author | Johnny Richard <johnny@johnnyrichard.com> | 2023-04-16 03:47:03 +0200 |
---|---|---|
committer | Johnny Richard <johnny@johnnyrichard.com> | 2023-04-16 03:47:03 +0200 |
commit | eab9986ba884921329fb8f9a1bd9a394cd240aaa (patch) | |
tree | 4e4fd94c567cfaad34a31fbf75f2ee38b42c41a4 | |
parent | d951985665bf3e0248286a30c67c28f505eb27c9 (diff) |
lexer: Extract lexer_define_literal_token_props function
This is an attempt of reducing code duplication.
Signed-off-by: Johnny Richard <johnny@johnnyrichard.com>
-rw-r--r-- | src/lexer.c | 94 |
1 files changed, 41 insertions, 53 deletions
diff --git a/src/lexer.c b/src/lexer.c index 9be96a8..00a186a 100644 --- a/src/lexer.c +++ b/src/lexer.c @@ -35,6 +35,16 @@ lexer_init(lexer_t *lexer, char *filepath) lexer_load_file_contents(lexer); } +static void +lexer_define_literal_token_props(lexer_t *lexer, token_t *token, token_kind_t kind) +{ + token->kind = kind; + token->value = string_view_new(lexer->src, 1); + token->filepath = lexer->filepath; + token->row = lexer->row; + token->col = lexer->cur - lexer->bol; +} + void lexer_next_token(lexer_t *lexer, token_t *token) { @@ -76,64 +86,42 @@ lexer_next_token(lexer_t *lexer, token_t *token) return; } - if (lexer_is_not_eof(lexer) && lexer_current_char(lexer) == '(') { - token->kind = TOKEN_OPAREN; - token->value = string_view_new(lexer->src, 1); - token->filepath = lexer->filepath; - token->row = lexer->row; - token->col = lexer->cur - lexer->bol; - lexer_drop_char(lexer); - return; - } + if (lexer_is_not_eof(lexer)) { + if (lexer_current_char(lexer) == '(') { + lexer_define_literal_token_props(lexer, token, TOKEN_OPAREN); + lexer_drop_char(lexer); + return; + } - if (lexer_is_not_eof(lexer) && lexer_current_char(lexer) == ')') { - token->kind = TOKEN_CPAREN; - token->value = string_view_new(lexer->src, 1); - token->filepath = lexer->filepath; - token->row = lexer->row; - token->col = lexer->cur - lexer->bol; - lexer_drop_char(lexer); - return; - } + if (lexer_current_char(lexer) == ')') { + lexer_define_literal_token_props(lexer, token, TOKEN_CPAREN); + lexer_drop_char(lexer); + return; + } - if (lexer_is_not_eof(lexer) && lexer_current_char(lexer) == ':') { - token->kind = TOKEN_COLON; - token->value = string_view_new(lexer->src, 1); - token->filepath = lexer->filepath; - token->row = lexer->row; - token->col = lexer->cur - lexer->bol; - lexer_drop_char(lexer); - return; - } + if (lexer_current_char(lexer) == ':') { + lexer_define_literal_token_props(lexer, token, TOKEN_COLON); + lexer_drop_char(lexer); + return; + } - if (lexer_is_not_eof(lexer) && lexer_current_char(lexer) == ';') { - token->kind = TOKEN_SEMICOLON; - token->value = string_view_new(lexer->src, 1); - token->filepath = lexer->filepath; - token->row = lexer->row; - token->col = lexer->cur - lexer->bol; - lexer_drop_char(lexer); - return; - } + if (lexer_current_char(lexer) == ';') { + lexer_define_literal_token_props(lexer, token, TOKEN_SEMICOLON); + lexer_drop_char(lexer); + return; + } - if (lexer_is_not_eof(lexer) && lexer_current_char(lexer) == '{') { - token->kind = TOKEN_OCURLY; - token->value = string_view_new(lexer->src, 1); - token->filepath = lexer->filepath; - token->row = lexer->row; - token->col = lexer->cur - lexer->bol; - lexer_drop_char(lexer); - return; - } + if (lexer_current_char(lexer) == '{') { + lexer_define_literal_token_props(lexer, token, TOKEN_OCURLY); + lexer_drop_char(lexer); + return; + } - if (lexer_is_not_eof(lexer) && lexer_current_char(lexer) == '}') { - token->kind = TOKEN_CCURLY; - token->value = string_view_new(lexer->src, 1); - token->filepath = lexer->filepath; - token->row = lexer->row; - token->col = lexer->cur - lexer->bol; - lexer_drop_char(lexer); - return; + if (lexer_current_char(lexer) == '}') { + lexer_define_literal_token_props(lexer, token, TOKEN_CCURLY); + lexer_drop_char(lexer); + return; + } } token->kind = TOKEN_EOF; |