2026-01-07 20:18:11 +01:00
|
|
|
#ifndef LEXER_H
|
|
|
|
|
#define LEXER_H
|
|
|
|
|
|
2026-01-10 19:28:59 +01:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
2026-01-13 19:44:56 +01:00
|
|
|
enum token_type
|
|
|
|
|
{
|
|
|
|
|
TOKEN_EOF,
|
|
|
|
|
TOKEN_WORD,
|
|
|
|
|
TOKEN_NEWLINE,
|
|
|
|
|
TOKEN_QUOTE,
|
|
|
|
|
TOKEN_SEMICOLON,
|
|
|
|
|
TOKEN_IF,
|
|
|
|
|
TOKEN_THEN,
|
|
|
|
|
TOKEN_ELSE,
|
|
|
|
|
TOKEN_FI
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct token
|
|
|
|
|
{
|
|
|
|
|
enum token_type type;
|
|
|
|
|
char* data;
|
|
|
|
|
};
|
|
|
|
|
|
2026-01-13 17:45:15 +01:00
|
|
|
/*
|
|
|
|
|
* @brief: returns the next (newly allocated) token without consuming it.
|
2026-01-13 19:44:56 +01:00
|
|
|
* if end of input is reached, returns a token of type TOKEN_EOF.
|
2026-01-13 17:45:15 +01:00
|
|
|
*
|
|
|
|
|
*/
|
2026-01-13 19:44:56 +01:00
|
|
|
struct token *peek_token(void);
|
2026-01-13 17:45:15 +01:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* @brief: returns the next (newly allocated) token and consumes it.
|
2026-01-13 19:44:56 +01:00
|
|
|
* if end of input is reached, returns a token of type TOKEN_EOF.
|
2026-01-08 16:32:48 +01:00
|
|
|
*
|
|
|
|
|
*/
|
2026-01-13 19:44:56 +01:00
|
|
|
struct token *pop_token(void);
|
2026-01-08 16:32:48 +01:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* @warning: NOT IMPLEMENTED.
|
|
|
|
|
*
|
|
|
|
|
* @note: maybe usefull for subshells.
|
|
|
|
|
*/
|
|
|
|
|
|
2026-01-13 19:44:56 +01:00
|
|
|
struct token *get_token_str(void);
|
2026-01-08 16:32:48 +01:00
|
|
|
|
|
|
|
|
/*
|
2026-01-13 19:44:56 +01:00
|
|
|
* @brief: return a newly allocated token, with the corresponding type.
|
|
|
|
|
* The data contains [size] char, starting from [begin].
|
|
|
|
|
*
|
|
|
|
|
* @return: NULL on error, a token otherwise.
|
2026-01-08 16:32:48 +01:00
|
|
|
*
|
2026-01-13 19:44:56 +01:00
|
|
|
*/
|
|
|
|
|
struct token *new_token(char *begin, ssize_t size);
|
|
|
|
|
|
|
|
|
|
/* @brief: frees the token given in argument
|
2026-01-08 16:32:48 +01:00
|
|
|
*
|
|
|
|
|
*/
|
2026-01-13 19:44:56 +01:00
|
|
|
void free_token(struct token* tok);
|
2026-01-08 16:32:48 +01:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* @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.
|
2026-01-12 21:31:15 +01:00
|
|
|
* Also trims left blanks before returning.
|
2026-01-08 16:32:48 +01:00
|
|
|
*
|
|
|
|
|
* @return: char* stream from which we tokenise.
|
|
|
|
|
*/
|
|
|
|
|
char *stream_init(void);
|
|
|
|
|
|
2026-01-07 20:18:11 +01:00
|
|
|
#endif /* ! LEXER_H */
|