93 lines
2.1 KiB
C
93 lines
2.1 KiB
C
#ifndef LEXER_H
|
|
#define LEXER_H
|
|
|
|
#include <sys/types.h>
|
|
|
|
enum token_type
|
|
{
|
|
// Special characters
|
|
TOKEN_NULL = 0,
|
|
TOKEN_EOF,
|
|
TOKEN_WORD,
|
|
TOKEN_NEWLINE,
|
|
TOKEN_QUOTE,
|
|
TOKEN_DOUBLE_QUOTE,
|
|
TOKEN_GRAVE,
|
|
TOKEN_SEMICOLON,
|
|
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,
|
|
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.
|
|
*
|
|
* @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().
|
|
*
|
|
*/
|
|
struct token *pop_token(void);
|
|
|
|
/* @note: maybe usefull for subshells.
|
|
*
|
|
* @warning: NOT IMPLEMENTED.
|
|
*/
|
|
|
|
struct token *get_token_str(void);
|
|
|
|
/*
|
|
* @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
|
|
*/
|
|
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,
|
|
* and sets [remaing_chars].
|
|
* If not, it starts from the end of the last token.
|
|
* Also trims left blanks before returning.
|
|
*
|
|
* @return: char* stream from which we tokenise.
|
|
*/
|
|
char *stream_init(void);
|
|
|
|
#endif /* ! LEXER_H */
|