42sh/src/lexer/lexer.h

94 lines
2.1 KiB
C
Raw Normal View History

#ifndef LEXER_H
#define LEXER_H
2026-01-10 19:28:59 +01:00
#include <sys/types.h>
enum token_type
{
2026-01-17 10:43:23 +01:00
// Special characters
TOKEN_NULL = 0,
TOKEN_EOF,
TOKEN_WORD,
TOKEN_NEWLINE,
TOKEN_QUOTE,
2026-01-17 10:43:23 +01:00
TOKEN_DOUBLE_QUOTE,
TOKEN_GRAVE,
TOKEN_SEMICOLON,
2026-01-17 10:43:23 +01:00
TOKEN_COMMENT,
TOKEN_PIPE,
TOKEN_AMPERSAND,
TOKEN_BACKSLASH,
TOKEN_DOLLAR,
TOKEN_LEFT_PAREN,
TOKEN_RIGHT_PAREN,
TOKEN_LEFT_BRACKET,
TOKEN_RIGHT_BRACKET,
TOKEN_LESS,
TOKEN_GREATER,
TOKEN_STAR,
// Keywords
TOKEN_IF,
TOKEN_THEN,
TOKEN_ELSE,
2026-01-14 19:58:59 +01:00
TOKEN_FI,
TOKEN_ELIF
};
struct token
{
enum token_type type;
char *data;
};
/*
* @brief: returns the next (newly allocated) token without consuming it.
* if end of input is reached, enters in EOF looping node,
* returning only the same token of type TOKEN_EOF.
*/
struct token *peek_token(void);
/*
* @brief: returns the next (newly allocated) token and consumes it.
* if end of input is reached, returns a token of type TOKEN_EOF.
* It also frees the last token created if there was one.
2026-01-17 10:43:23 +01:00
*
* @warning: if the last returned token was a token EOF, it frees it
* and returns NULL. This means that after peeking a token EOF
* in the parser, there must be EXACTLY ONE call to pop_token().
2026-01-08 16:32:48 +01:00
*
*/
struct token *pop_token(void);
2026-01-08 16:32:48 +01:00
2026-01-17 10:43:23 +01:00
/* @note: maybe usefull for subshells.
2026-01-08 16:32:48 +01:00
*
2026-01-17 10:43:23 +01:00
* @warning: NOT IMPLEMENTED.
2026-01-08 16:32:48 +01:00
*/
struct token *get_token_str(void);
2026-01-08 16:32:48 +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.
*/
struct token *new_token(char *begin, ssize_t size);
/* @brief: frees the token given in argument
2026-01-08 16:32:48 +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);
#endif /* ! LEXER_H */