#include "lexer.h" #include #include #include #include #include #include #include "io_backend/io_backend.h" #include "utils/string_utils/string_utils.h" static char *end_last_token; static ssize_t remaining_chars; static bool at_beginning = true; /* @brief: saves state for the next call the the lexer. * */ static void save_state(char *stream, ssize_t i) { remaining_chars -= i; end_last_token = stream + i; at_beginning = false; return; } /* @return: true if a special character from the grammar was found, * false otherwise. * */ static bool is_special_char(char c) { return c == '\'' || c == '\n' || c == ';' || c == EOF; } /* @brief: if a special character is found at [begin], * [tok->token_type] is set accordingly * */ static void set_token_spechar(struct token *tok, char *begin, ssize_t size) { if (size != 1) return; if (begin[0] == EOF) { tok->type = TOKEN_EOF; remaining_chars = 0; } else if (begin[0] == ';') { tok->type = TOKEN_NEWLINE; } else if (begin[0] == '\'') { tok->type = TOKEN_QUOTE; } else if (begin[0] == ';') { tok->type = TOKEN_SEMICOLON; } } /* @brief: if a keyword is found at [begin], * [tok->token_type] is set accordingly * */ static void set_token_keyword(struct token *tok, char *begin, ssize_t size) { if (tok->type != TOKEN_NULL) return; if (strncmp(begin, "if", size) == 0) { tok->type = TOKEN_IF; } else if (strncmp(begin, "fi", size) == 0) { tok->type = TOKEN_FI; } else if (strncmp(begin, "then", size) == 0) { tok->type = TOKEN_THEN; } else if (strncmp(begin, "else", size) == 0) { tok->type = TOKEN_ELSE; } else if (strncmp(begin, "elif", size) == 0) { tok->type = TOKEN_ELIF; } tok->data = calloc(size + 1, sizeof(char)); if (tok->data == NULL) return; strncpy(tok->data, begin, size); } /* @brief: if token_type has not yet been set, then it is a TOKEN_WORD * Also allocates the data and fills it. */ static void set_token_word(struct token *tok, char *begin, ssize_t size) { if (tok->type == TOKEN_NULL) { tok->type = TOKEN_WORD; tok->data = calloc(size + 1, sizeof(char)); if (tok->data == NULL) return; strncpy(tok->data, begin, size); } } struct token *new_token(char *begin, ssize_t size) { struct token *tok = calloc(1, sizeof(struct token)); if (tok == NULL) return NULL; set_token_spechar(tok, begin, size); set_token_keyword(tok, begin, size); set_token_word(tok, begin, size); return tok; } void free_token(struct token *tok) { if (tok == NULL) return; if (tok->data != NULL) free(tok->data); free(tok); } char *stream_init(void) { char *stream; if (at_beginning) { remaining_chars = stream_read(&stream); // at_beginning = true; } else { stream = end_last_token; } char *trimed_stream = trim_blank_left(stream); remaining_chars -= trimed_stream - stream; stream = trimed_stream; return stream; } struct token *peek_token(void) { char *stream = stream_init(); ssize_t i = 0; while (i < remaining_chars) { if (is_special_char(stream[i])) { if (i == 0) // where we create spe_char token i++; break; } if (isblank(stream[i])) { break; } i++; } return new_token(stream, i); } struct token *pop_token(void) { char *stream = stream_init(); ssize_t i = 0; while (i < remaining_chars) { if (is_special_char(stream[i])) { if (i == 0) // where we create spe_char token i++; break; } if (isblank(stream[i])) { break; } i++; } save_state(stream, i); return new_token(stream, i); }