42sh/src/lexer/lexer_utils.c

341 lines
8.3 KiB
C
Raw Normal View History

2026-01-21 16:19:10 +01:00
#include "lexer_utils.h"
#include <stdlib.h>
#include <string.h>
#include "../io_backend/io_backend.h"
#include "../utils/string_utils/string_utils.h"
/* @brief: if a special character is found at [begin],
* [tok->token_type] is set accordingly
*/
static void set_token_spechar(struct token *tok, char *begin, ssize_t size)
{
if (size != 1)
return;
switch (begin[0])
{
case EOF:
tok->type = TOKEN_EOF;
break;
case ';':
tok->type = TOKEN_SEMICOLON;
break;
case '\n':
tok->type = TOKEN_NEWLINE;
break;
case '`':
tok->type = TOKEN_GRAVE;
break;
case '#':
tok->type = TOKEN_COMMENT;
break;
case '\\':
tok->type = TOKEN_BACKSLASH;
break;
case '(':
tok->type = TOKEN_LEFT_PAREN;
break;
case ')':
tok->type = TOKEN_RIGHT_PAREN;
break;
case '{':
tok->type = TOKEN_LEFT_BRACKET;
break;
case '}':
tok->type = TOKEN_RIGHT_BRACKET;
break;
case '*':
tok->type = TOKEN_STAR;
break;
default:
break;
}
}
/* @brief: if a keyword is found at [begin],
* [tok->token_type] is set accordingly
*/
static void set_token_keyword(struct token *tok, char *begin, ssize_t size)
{
if (tok->type != TOKEN_NULL || size == 0)
return;
2026-01-27 16:44:53 +01:00
if (strncmp(begin, "!", size) == 0 && size == 1)
tok->type = TOKEN_NEGATION;
else if (strncmp(begin, "if", size) == 0 && size == 2)
2026-01-21 16:19:10 +01:00
tok->type = TOKEN_IF;
else if (strncmp(begin, "fi", size) == 0 && size == 2)
2026-01-21 16:19:10 +01:00
tok->type = TOKEN_FI;
else if (strncmp(begin, "then", size) == 0 && size == 4)
2026-01-21 16:19:10 +01:00
tok->type = TOKEN_THEN;
else if (strncmp(begin, "else", size) == 0 && size == 4)
2026-01-21 16:19:10 +01:00
tok->type = TOKEN_ELSE;
else if (strncmp(begin, "elif", size) == 0 && size == 4)
2026-01-21 16:19:10 +01:00
tok->type = TOKEN_ELIF;
2026-01-30 19:13:27 +01:00
else if (strncmp(begin, "export", size) == 0 && size == 6)
tok->type = TOKEN_ELIF;
2026-01-21 16:19:10 +01:00
// no keywords found.
if (tok->type == TOKEN_NULL)
return;
tok->data = calloc(size + 1, sizeof(char));
if (tok->data == NULL)
2026-01-27 16:44:53 +01:00
{
perror("could not allocate memory in lexer");
2026-01-21 16:19:10 +01:00
return;
2026-01-27 16:44:53 +01:00
}
2026-01-21 16:19:10 +01:00
strncpy(tok->data, begin, size);
}
2026-01-23 19:34:47 +01:00
/* @brief: if an operator is found at [begin],
* [tok->token_type] is set accordingly
*/
static void set_token_operator(struct token *tok, char *begin, ssize_t size)
{
if (tok->type != TOKEN_NULL)
return;
if (strncmp(begin, "&&", size) == 0 && size == 2)
2026-01-30 17:47:51 +01:00
{
tok->type = TOKEN_AND;
2026-01-30 17:47:51 +01:00
}
else if (strncmp(begin, "||", size) == 0 && size == 2)
2026-01-30 17:47:51 +01:00
{
tok->type = TOKEN_OR;
2026-01-30 17:47:51 +01:00
}
2026-01-30 17:47:51 +01:00
else if (strncmp(begin, ">", size) == 0 && size == 1)
2026-01-23 19:34:47 +01:00
{
tok->type = TOKEN_REDIR_RIGHT;
}
2026-01-30 17:47:51 +01:00
else if (strncmp(begin, "<", size) == 0 && size == 1)
2026-01-23 19:34:47 +01:00
{
tok->type = TOKEN_REDIR_LEFT;
}
2026-01-30 17:47:51 +01:00
else if (strncmp(begin, ">>", size) == 0 && size == 2)
2026-01-23 19:34:47 +01:00
{
tok->type = TOKEN_REDIR_DOUBLE_RIGHT;
}
2026-01-30 17:47:51 +01:00
else if (strncmp(begin, ">&", size) == 0 && size == 2)
2026-01-23 19:34:47 +01:00
{
tok->type = TOKEN_REDIR_RIGHT_AMP;
}
2026-01-30 17:47:51 +01:00
else if (strncmp(begin, ">|", size) == 0 && size == 2)
2026-01-23 19:34:47 +01:00
{
tok->type = TOKEN_REDIR_RIGHT_PIPE;
}
2026-01-30 17:47:51 +01:00
else if (strncmp(begin, "<&", size) == 0 && size == 2)
2026-01-23 19:34:47 +01:00
{
tok->type = TOKEN_REDIR_LEFT_AMP;
}
2026-01-30 17:47:51 +01:00
else if (strncmp(begin, "<>", size) == 0 && size == 2)
2026-01-23 19:34:47 +01:00
{
tok->type = TOKEN_REDIR_LEFT_RIGHT;
}
2026-01-30 17:47:51 +01:00
else if (strncmp(begin, "|", size) == 0 && size == 1)
2026-01-23 19:34:47 +01:00
{
tok->type = TOKEN_PIPE;
}
}
2026-01-21 16:19:10 +01:00
/* @brief: if token_type has not yet been set, then it is a TOKEN_WORD
* Also allocates the data and fills it.
*/
static void set_token_word(struct token *tok, char *begin, ssize_t size)
{
if (tok->type == TOKEN_NULL && size != 0)
{
tok->type = TOKEN_WORD;
tok->data = calloc(size + 1, sizeof(char));
if (tok->data == NULL)
return;
strncpy(tok->data, begin, size);
}
}
/* @brief: Sets the token to an assignment_word
* Also allocates the data and fills it.
*/
static void set_token_assignment(struct token *tok, char *begin, ssize_t size)
{
if (tok->type == TOKEN_NULL && size != 0)
{
tok->type = TOKEN_ASSIGNMENT_WORD;
tok->data = calloc(size + 1, sizeof(char));
if (tok->data == NULL)
return;
strncpy(tok->data, begin, size);
}
}
/* @brief: Sets the token to an IO number
* Also allocates the data and fills it.
*/
static void set_token_ION(struct token *tok, char *begin, ssize_t size)
{
if (tok->type == TOKEN_NULL && size != 0)
{
tok->type = TOKEN_IONUMBER;
tok->data = calloc(size + 1, sizeof(char));
if (tok->data == NULL)
return;
strncpy(tok->data, begin, size);
}
}
2026-01-23 19:34:47 +01:00
/* @brief: check if [c] is a delimiter for end of line.
* @return: true if [c] == '\n' or EOF. false otherwise.
*/
static bool is_end_of_line(char c)
{
return c == EOF || c == '\n';
}
// === Functions
bool is_token_redir(struct token *token)
{
switch (token->type)
{
case TOKEN_REDIR_LEFT:
case TOKEN_REDIR_RIGHT:
case TOKEN_REDIR_LEFT_RIGHT:
case TOKEN_REDIR_DOUBLE_RIGHT:
case TOKEN_REDIR_LEFT_AMP:
case TOKEN_REDIR_RIGHT_AMP:
case TOKEN_REDIR_RIGHT_PIPE:
return true;
default:
return false;
}
}
bool is_special_char(char *stream, ssize_t i)
2026-01-23 19:34:47 +01:00
{
char c = stream[i];
2026-01-23 19:34:47 +01:00
if (c == EOF)
return true;
if (i > 0 && c == '#' && stream[i - 1] == '$')
return false; // the edge case of $#
if (i > 0 && stream[i - 1] == '\\')
return false; // TODO handle backslash better
// this doesnt work, ex : echo \\#comment
// (need to count the previous consequtive backslashes)
2026-01-23 19:34:47 +01:00
char special_chars[] = "\n'\"`;#|&(){}<>*";
2026-01-23 19:34:47 +01:00
return strchr(special_chars, c) != NULL;
}
struct token *new_token(char *begin, ssize_t size, struct token_info *info)
2026-01-21 16:19:10 +01:00
{
struct token *tok = calloc(1, sizeof(struct token));
if (tok == NULL)
return NULL;
if (info->only_digits)
set_token_ION(tok, begin, size);
else if (info->has_equal)
set_token_assignment(tok, begin, size);
else
{
set_token_keyword(tok, begin, size);
set_token_operator(tok, begin, size);
set_token_spechar(tok, begin, size);
set_token_word(tok, begin, size);
}
2026-01-21 16:19:10 +01:00
return tok;
}
void destroy_lexer_context(struct lexer_context **ctx)
{
if (ctx == NULL || *ctx == NULL)
return;
if ((*ctx)->previous_token != NULL)
free((*ctx)->previous_token);
if ((*ctx)->current_token != NULL)
free((*ctx)->current_token);
free(*ctx);
*ctx = NULL;
}
void free_token(struct token **tok)
{
if (tok == NULL || *tok == NULL)
return;
if ((*tok)->data != NULL)
free((*tok)->data);
free(*tok);
*tok = NULL;
}
void stream_init(struct lexer_context *ctx)
2026-01-21 16:19:10 +01:00
{
char *stream;
if (ctx->remaining_chars == 0) // at the begining
2026-01-21 16:19:10 +01:00
{
ctx->remaining_chars = stream_read(&stream);
}
else
{
stream = ctx->end_previous_token;
}
char *trimed_stream = trim_blank_left(stream);
ctx->remaining_chars -= trimed_stream - stream;
ctx->end_previous_token = trimed_stream;
}
2026-01-23 19:34:47 +01:00
ssize_t len_op_sepchar(char *stream, ssize_t i)
{
if (!is_special_char(stream, i))
2026-01-23 19:34:47 +01:00
return -1; // should never happen
// OR
if (stream[i] == '|' && stream[i + 1] == '|')
return 2;
// AND
if (stream[i] == '|' && stream[i + 1] == '|')
return 2;
// special chars
2026-01-23 19:34:47 +01:00
if (stream[i] != '>' && stream[i] != '<')
return 1;
2026-01-23 19:34:47 +01:00
// REDIRS
2026-01-23 19:34:47 +01:00
if (stream[i] == '<')
{
if (stream[i + 1] == '&' || stream[i + 1] == '>')
return 2; // <&, <>
}
else if (stream[i + 1] == '>' || stream[i + 1] == '|'
|| stream[i + 1] == '&')
return 2; // >>, >&, >|
return 1; // >, <
}
void go_end_of_line(struct lexer_context *ctx)
{
if (ctx == NULL || ctx->end_previous_token == NULL)
return;
ssize_t i = 0;
2026-01-23 19:34:47 +01:00
while (!is_end_of_line(ctx->end_previous_token[i]))
{
i++;
}
ctx->end_previous_token += i;
ctx->remaining_chars -= i;
}
void get_next_stream(struct lexer_context *ctx)
{
ctx->remaining_chars = 0;
stream_init(ctx);
2026-01-21 16:19:10 +01:00
}