From e1ceef73192f0300ff9b10ba9a16475fbebeaa5f Mon Sep 17 00:00:00 2001 From: kartofen Date: Fri, 30 Aug 2024 17:01:28 +0300 Subject: proper repl, stylistic changes, removed trailing whitespace --- src/lexer.c | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) (limited to 'src/lexer.c') diff --git a/src/lexer.c b/src/lexer.c index b546fda..93c7f44 100644 --- a/src/lexer.c +++ b/src/lexer.c @@ -7,7 +7,7 @@ #define CH(lexer) (lexer)->str[(lexer)->str_idx] #define TOKEN_SEPARATOR_TABLE(X, l) \ - X((';' == CH(l)), CH(l) = '\0') \ + X((';' == CH(l)), lexer_clear_line(l)) \ X(('(' == CH(l)), on_separator(l, TOKEN_LP)) \ X((')' == CH(l)), on_separator(l, TOKEN_RP)) \ X(('\''== CH(l)), on_separator(l, TOKEN_QUOTE)) \ @@ -78,7 +78,7 @@ lexer_t lexer_create(FILE *fp) lexer->acc_idx = 0; memset(lexer->acc, 0, sizeof(lexer->acc)); memset(lexer->str, 0, sizeof(lexer->str)); - + lexer->token.type = TOKEN_NONE; return lexer; @@ -90,12 +90,18 @@ void lexer_destroy(lexer_t lexer) free(lexer); } +int lexer_clear_line(lexer_t lexer) +{ + lexer->str[lexer->str_idx] = '\0'; + return 0; +} + int lexer_token_next(lexer_t lexer, struct token *token) { if(lexer->acc_idx == 0 && lexer->acc[0] != '\0') { memset(lexer->acc, 0, sizeof(lexer->acc)); } - + while(lexer->token.type == TOKEN_NONE) { if(lexer->str[lexer->str_idx] == '\0') { @@ -103,10 +109,10 @@ int lexer_token_next(lexer_t lexer, struct token *token) lexer->str_idx = 0; lexer->line++; } - + TOKEN_SEPARATOR_TABLE(CALLBACK, lexer) TABLE_END; } - + *token = lexer->token; lexer->token.type = TOKEN_NONE; return 0; @@ -116,7 +122,7 @@ int token_value_string(struct token *token, size_t buf_sz, char *buf) { #define AS_STRING(ttype, ...) \ case ttype: return snprintf(buf, buf_sz, __VA_ARGS__); - + switch(token->type) { TOKEN_VALUE_STRING_TABLE(AS_STRING, token->value); } @@ -175,7 +181,7 @@ static int acc_add_char(lexer_t lexer, char ch) if(lexer->acc_idx >= LEN(lexer->acc) - 1) { return -ENAMETOOLONG; } - + lexer->acc[lexer->acc_idx++] = ch; lexer->str_idx++; @@ -183,10 +189,10 @@ static int acc_add_char(lexer_t lexer, char ch) } static int acc_empty(lexer_t lexer) -{ +{ TOKEN_VALUE_TABLE(CALLBACK_BLIND, lexer) TABLE_END; lexer->acc_idx = 0; - + return 0; } @@ -210,6 +216,6 @@ static int is_special(char *str, enum token_type *type) } else TOKEN_SPECIALS_TABLE(IS_SPECIAL) TABLE_END; - + return 0; } -- cgit v1.2.3