From 453a8ab0da0b9b29e95d8efc638bb25aca61a64d Mon Sep 17 00:00:00 2001 From: Matteo Flebus Date: Thu, 8 Jan 2026 16:32:48 +0100 Subject: [PATCH] feat(lexer): usefull functions --- src/lexer/lexer.c | 67 +++++++++++++++++++++++++++++++++++++++++++++++ src/lexer/lexer.h | 36 +++++++++++++++++++++++++ 2 files changed, 103 insertions(+) diff --git a/src/lexer/lexer.c b/src/lexer/lexer.c index e69de29..5b03c18 100644 --- a/src/lexer/lexer.c +++ b/src/lexer/lexer.c @@ -0,0 +1,67 @@ +#include "lexer.h" + +#include + +#include "io_backend/io_backend.h" + +static char *end_last_token; +static ssize_t remaining_chars; + +char *new_token(char *begin, size_t size) +{ + char *res = calloc(size + 1, sizeof(char)); + if (res == NULL) + return NULL; + strncpy(res, begin, size); + return res; +} + +char *stream_init(void) +{ + char *stream; + + if (remaining_chars == 0) + { + remaining_chars = stream_read(&stream); + } + else + { + stream = end_last_token; + } + + return stream; +} + +char *get_token(void) +{ + char *stream = stream_init(); + + bool inquotes = false; + ssize_t i = 0; + + while (i < remaining_chars) + { + switch (stream[i]) + { + case '\'': + inquotes = !inquotes; + break; + + case ' ' | '\n' | '\t': + if (inquotes) + break; + else + { + // token creation + // skip blank char + // exit from loop + char *token = new_token(stream, i); + } + default: + break; + } + i++; + } + + remaining_chars -= i; +} diff --git a/src/lexer/lexer.h b/src/lexer/lexer.h index e69de29..d9a484c 100644 --- a/src/lexer/lexer.h +++ b/src/lexer/lexer.h @@ -0,0 +1,36 @@ +#ifndef LEXER_H +#define LEXER_H + +/* @return: char*, the next token + * + */ +char *get_token(void); + +/* + * @warning: NOT IMPLEMENTED. + * + * @note: maybe usefull for subshells. + */ + +char *get_token_str(void); + +/* + * @brief: return a newly allocated token. + * This token contains [size] chars, starting from [begin]. + * + * @return: NULL on error, null-terminated char* otherwise. + * + */ +char *new_token(char *begin, ssize_t size); + +/* + * @brief: checks if the stream used for the last token creation is empty. + * If it is, it calls stream_read() from IO_backend, + * and sets [remaing_chars]. + * If not, it starts from the end of the last token. + * + * @return: char* stream from which we tokenise. + */ +char *stream_init(void); + +#endif /* LEXER_H */