2026-01-21 16:19:10 +01:00
|
|
|
#ifndef LEXER_UTILS_H
|
|
|
|
|
#define LEXER_UTILS_H
|
|
|
|
|
|
2026-01-23 15:56:21 +01:00
|
|
|
#include <stdbool.h>
|
2026-01-21 16:19:10 +01:00
|
|
|
#include <stddef.h>
|
2026-01-21 18:43:17 +01:00
|
|
|
#include <sys/types.h>
|
2026-01-21 16:19:10 +01:00
|
|
|
|
|
|
|
|
struct lexer_context
|
|
|
|
|
{
|
|
|
|
|
char *end_previous_token;
|
|
|
|
|
ssize_t remaining_chars;
|
|
|
|
|
|
2026-01-23 15:56:21 +01:00
|
|
|
// usefull to detect IO numbers.
|
|
|
|
|
// tells us if we only lexed digits in current token.
|
|
|
|
|
bool only_digits;
|
|
|
|
|
|
2026-01-21 16:19:10 +01:00
|
|
|
struct token *previous_token;
|
|
|
|
|
struct token *current_token;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/* @brief: frees all fields of ctx and sets ctx to NULL.
|
|
|
|
|
*/
|
|
|
|
|
void destroy_lexer_context(struct lexer_context **ctx);
|
|
|
|
|
|
|
|
|
|
enum lexing_mode
|
|
|
|
|
{
|
|
|
|
|
LEXER_NORMAL,
|
|
|
|
|
LEXER_QUOTE,
|
|
|
|
|
LEXER_DOUBLE_QUOTE
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
enum token_type
|
|
|
|
|
{
|
|
|
|
|
// Special characters
|
|
|
|
|
TOKEN_NULL = 0,
|
|
|
|
|
TOKEN_EOF,
|
|
|
|
|
TOKEN_WORD,
|
|
|
|
|
TOKEN_NEWLINE,
|
|
|
|
|
|
|
|
|
|
// WARNING: quote and double quote should never be used inside a token.
|
|
|
|
|
TOKEN_QUOTE,
|
|
|
|
|
TOKEN_DOUBLE_QUOTE,
|
|
|
|
|
|
|
|
|
|
TOKEN_GRAVE,
|
|
|
|
|
TOKEN_SEMICOLON,
|
|
|
|
|
TOKEN_COMMENT,
|
2026-01-23 15:56:21 +01:00
|
|
|
TOKEN_STAR,
|
2026-01-21 16:19:10 +01:00
|
|
|
TOKEN_BACKSLASH,
|
|
|
|
|
TOKEN_DOLLAR,
|
|
|
|
|
TOKEN_LEFT_PAREN,
|
|
|
|
|
TOKEN_RIGHT_PAREN,
|
|
|
|
|
TOKEN_LEFT_BRACKET,
|
|
|
|
|
TOKEN_RIGHT_BRACKET,
|
2026-01-23 15:56:21 +01:00
|
|
|
|
|
|
|
|
// redirections
|
2026-01-23 19:34:47 +01:00
|
|
|
TOKEN_REDIR_LEFT,
|
|
|
|
|
TOKEN_REDIR_RIGHT,
|
|
|
|
|
TOKEN_REDIR_LEFT_RIGHT,
|
|
|
|
|
TOKEN_REDIR_DOUBLE_RIGHT,
|
|
|
|
|
TOKEN_REDIR_LEFT_AMP,
|
|
|
|
|
TOKEN_REDIR_RIGHT_AMP,
|
|
|
|
|
TOKEN_REDIR_RIGHT_PIPE,
|
|
|
|
|
|
2026-01-23 15:56:21 +01:00
|
|
|
TOKEN_IONUMBER,
|
2026-01-23 19:34:47 +01:00
|
|
|
TOKEN_PIPE,
|
2026-01-21 16:19:10 +01:00
|
|
|
|
|
|
|
|
// Keywords
|
|
|
|
|
TOKEN_IF,
|
|
|
|
|
TOKEN_THEN,
|
|
|
|
|
TOKEN_ELSE,
|
|
|
|
|
TOKEN_FI,
|
2026-01-22 19:57:49 +01:00
|
|
|
TOKEN_ELIF,
|
|
|
|
|
TOKEN_AND,
|
|
|
|
|
TOKEN_OR
|
2026-01-21 16:19:10 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct token
|
|
|
|
|
{
|
|
|
|
|
enum token_type type;
|
|
|
|
|
char *data;
|
|
|
|
|
};
|
|
|
|
|
|
2026-01-23 19:34:47 +01:00
|
|
|
/* @return: true if a special character from the grammar was found,
|
|
|
|
|
* false otherwise.
|
|
|
|
|
*/
|
|
|
|
|
bool is_special_char(char c);
|
|
|
|
|
|
|
|
|
|
/* @brief: return a newly allocated token, with the corresponding type.
|
2026-01-21 16:19:10 +01:00
|
|
|
* The data contains [size] char, starting from [begin].
|
|
|
|
|
*
|
|
|
|
|
* @return: NULL on error, a token otherwise.
|
|
|
|
|
*/
|
2026-01-23 15:56:21 +01:00
|
|
|
struct token *new_token(char *begin, ssize_t size, bool only_digits);
|
2026-01-21 16:19:10 +01:00
|
|
|
|
|
|
|
|
/* @brief: frees the token given in argument
|
|
|
|
|
*/
|
|
|
|
|
void free_token(struct token **tok);
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* @brief: checks if the stream used for the last token creation is empty.
|
|
|
|
|
* If it is, it calls stream_read() from IO_backend,
|
2026-01-21 18:43:17 +01:00
|
|
|
* and sets [remaining_chars].
|
2026-01-21 16:19:10 +01:00
|
|
|
* If not, it starts from the end of the last token.
|
|
|
|
|
* Also trims left blanks before returning.
|
|
|
|
|
*
|
|
|
|
|
* @return: char* stream from which we tokenise.
|
|
|
|
|
*/
|
2026-01-21 18:43:17 +01:00
|
|
|
void stream_init(struct lexer_context *ctx);
|
|
|
|
|
|
2026-01-23 15:56:21 +01:00
|
|
|
/* @brief: finds the next '\n' or EOF character,
|
2026-01-23 19:34:47 +01:00
|
|
|
* starting at [ctx->end_previous_token],
|
|
|
|
|
* and updates the stream and remaining_chars accordingly.
|
|
|
|
|
*
|
|
|
|
|
* @note: Daft Punk. bang.
|
2026-01-23 15:56:21 +01:00
|
|
|
*/
|
|
|
|
|
void go_end_of_line(struct lexer_context *ctx);
|
|
|
|
|
|
2026-01-23 19:34:47 +01:00
|
|
|
/* @brief: this function is called when we found a special character
|
|
|
|
|
* in the stream. This can either be an operator (ig '>>' or '<&' etc),
|
|
|
|
|
* or a special char (ig '\' or '#' etc).
|
|
|
|
|
* @return: the length of the operator/special char found (can be 1, 2 or 3).
|
|
|
|
|
* -1 on error.
|
|
|
|
|
*/
|
|
|
|
|
ssize_t len_op_sepchar(char *stream, ssize_t i);
|
|
|
|
|
|
|
|
|
|
/* @brief: drops the current stream and asks IOB for a new one
|
2026-01-21 18:43:17 +01:00
|
|
|
*/
|
|
|
|
|
void get_next_stream(struct lexer_context *ctx);
|
2026-01-21 16:19:10 +01:00
|
|
|
|
|
|
|
|
#endif /* LEXER_UTILS_H */
|