42sh/src/lexer/lexer.c

348 lines
8.1 KiB
C
Raw Normal View History

2026-01-08 16:32:48 +01:00
#include "lexer.h"
2026-01-12 21:31:15 +01:00
#include <ctype.h>
2026-01-08 16:32:48 +01:00
#include <stdbool.h>
2026-01-10 19:57:36 +01:00
#include <stddef.h>
#include <stdio.h>
2026-01-10 19:57:36 +01:00
#include <stdlib.h>
#include <string.h>
2026-01-08 16:32:48 +01:00
#include "io_backend/io_backend.h"
2026-01-12 21:31:15 +01:00
#include "utils/string_utils/string_utils.h"
2026-01-08 16:32:48 +01:00
static char *end_last_token;
static ssize_t remaining_chars;
static struct token *last_token;
static struct token *current_token;
2026-01-08 16:32:48 +01:00
/* @brief: sets the current_token to [tok].
* this function is called by token_peek().
*/
2026-01-17 10:43:23 +01:00
static void update_current_token(struct token *tok)
{
current_token = tok;
}
/* @brief: frees the last token and sets it to [tok].
* Also sets current_token to NULL.
* this function is called by token_pop().
*/
2026-01-17 10:43:23 +01:00
static void update_last_token(struct token *tok)
{
free_token(&last_token);
last_token = tok;
}
/* @brief: updates the current position in the stream.
* [stream] += [i]
2026-01-17 11:38:23 +01:00
* Also frees the last sent token, and sets it to current_token.
* Current token is then set to NULL.
* This function is called by token_pop().
2026-01-12 21:31:15 +01:00
*/
2026-01-17 11:38:23 +01:00
static void save_state(char *stream, ssize_t i)
2026-01-12 21:31:15 +01:00
{
remaining_chars -= i;
end_last_token = stream + i;
2026-01-17 11:38:23 +01:00
update_last_token(current_token);
update_current_token(NULL);
2026-01-12 21:31:15 +01:00
}
/* @return: true if a special character from the grammar was found,
* false otherwise.
*/
static bool is_special_char(char c)
{
2026-01-17 10:43:23 +01:00
if (c == EOF)
return true;
char special_chars[] = "\n'\"`;#|&\\$(){}<>*";
return strchr(special_chars, c) != NULL;
2026-01-12 21:31:15 +01:00
}
/* @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;
2026-01-17 10:43:23 +01:00
switch (begin[0])
{
2026-01-19 17:32:45 +01:00
case EOF:
tok->type = TOKEN_EOF;
break;
case ';':
tok->type = TOKEN_SEMICOLON;
break;
case '\n':
tok->type = TOKEN_NEWLINE;
break;
case '\'':
tok->type = TOKEN_QUOTE;
break;
case '"':
tok->type = TOKEN_DOUBLE_QUOTE;
break;
case '`':
tok->type = TOKEN_GRAVE;
break;
case '#':
tok->type = TOKEN_COMMENT;
break;
case '|':
tok->type = TOKEN_PIPE;
break;
case '&':
tok->type = TOKEN_AMPERSAND;
break;
case '\\':
tok->type = TOKEN_BACKSLASH;
break;
case '$':
tok->type = TOKEN_DOLLAR;
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_LESS;
break;
case '>':
tok->type = TOKEN_GREATER;
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)
2026-01-12 21:31:15 +01:00
{
if (tok->type != TOKEN_NULL || size == 0)
return;
if (strncmp(begin, "if", size) == 0)
{
tok->type = TOKEN_IF;
}
else if (strncmp(begin, "fi", size) == 0)
2026-01-12 21:31:15 +01:00
{
tok->type = TOKEN_FI;
2026-01-12 21:31:15 +01:00
}
else if (strncmp(begin, "then", size) == 0)
2026-01-12 21:31:15 +01:00
{
tok->type = TOKEN_THEN;
}
else if (strncmp(begin, "else", size) == 0)
{
tok->type = TOKEN_ELSE;
2026-01-12 21:31:15 +01:00
}
2026-01-14 19:58:59 +01:00
else if (strncmp(begin, "elif", size) == 0)
{
tok->type = TOKEN_ELIF;
}
2026-01-17 11:38:23 +01:00
// no keywords found.
if (tok->type == TOKEN_NULL)
return;
tok->data = calloc(size + 1, sizeof(char));
if (tok->data == NULL)
return;
strncpy(tok->data, begin, size);
2026-01-12 21:31:15 +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);
}
}
struct token *new_token(char *begin, ssize_t size)
2026-01-08 16:32:48 +01:00
{
struct token *tok = calloc(1, sizeof(struct token));
if (tok == NULL)
2026-01-08 16:32:48 +01:00
return NULL;
set_token_spechar(tok, begin, size);
set_token_keyword(tok, begin, size);
set_token_word(tok, begin, size);
return tok;
}
void free_token(struct token **tok)
{
if (tok == NULL || *tok == NULL)
return;
if ((*tok)->data != NULL)
free((*tok)->data);
free(*tok);
*tok = NULL;
2026-01-08 16:32:48 +01:00
}
char *stream_init(void)
{
char *stream;
if (last_token == NULL) // at the begining
2026-01-08 16:32:48 +01:00
{
remaining_chars = stream_read(&stream);
}
else
{
stream = end_last_token;
}
2026-01-12 21:41:13 +01:00
char *trimed_stream = trim_blank_left(stream);
2026-01-12 21:31:15 +01:00
remaining_chars -= trimed_stream - stream;
stream = trimed_stream;
2026-01-08 16:32:48 +01:00
return stream;
}
2026-01-19 17:32:45 +01:00
/*
* @brief: Updates the lexing_mode to LEXER_NORMAL
* if the SECOND quote is found at stream[i].
* Updates the lexing_mode to the corresponding quote type
* if the FIRST quote of any type is found.
*
* @return: true if an update was done. false otherwise.
*/
static bool update_lexing_mode(char *stream, ssize_t i,
enum lexing_mode *lexing_mode)
{
enum lexing_mode mode_before_update = *lexing_mode;
// FIRST quote
if (*lexing_mode == LEXER_NORMAL)
{
if (stream[i] == '"')
*lexing_mode = LEXER_DOUBLE_QUOTE;
if (stream[i] == '\'')
*lexing_mode = LEXER_QUOTE;
}
2026-01-19 18:19:35 +01:00
// SECOND quote
else
{
if (*lexing_mode == LEXER_QUOTE && stream[i] == '\'')
*lexing_mode = LEXER_NORMAL;
if (*lexing_mode == LEXER_DOUBLE_QUOTE && stream[i] == '"')
*lexing_mode = LEXER_NORMAL;
}
2026-01-19 17:32:45 +01:00
return *lexing_mode != mode_before_update;
}
struct token *peek_token(void)
2026-01-08 16:32:48 +01:00
{
// we already created the upcoming token during the previous call to peek()
if (current_token != NULL)
{
return current_token;
}
2026-01-08 16:32:48 +01:00
char *stream = stream_init();
ssize_t i = 0;
2026-01-19 17:32:45 +01:00
// Usefull to know if we are inside a quote or double quote
enum lexing_mode lexing_mode = LEXER_NORMAL;
2026-01-08 16:32:48 +01:00
while (i < remaining_chars)
{
2026-01-19 18:19:35 +01:00
// true if we didn't encounter a quote of any type at stream[i]
// AND we are not inside quotes
if (!update_lexing_mode(stream, i, &lexing_mode)
&& lexing_mode == LEXER_NORMAL)
2026-01-12 21:31:15 +01:00
{
2026-01-19 17:32:45 +01:00
if (is_special_char(stream[i]))
{
if (i == 0) // where we create spe_char token
i++;
break;
}
if (isblank(stream[i]))
{
break;
}
2026-01-12 21:31:15 +01:00
}
2026-01-08 16:32:48 +01:00
i++;
}
struct token *tok = new_token(stream, i);
update_current_token(tok);
return tok;
}
struct token *pop_token(void)
{
2026-01-17 11:38:23 +01:00
if (current_token != NULL && current_token->type == TOKEN_EOF)
{
2026-01-17 11:38:23 +01:00
// we reached end of input, frees all the token still allocated.
free_token(&last_token);
2026-01-17 11:38:23 +01:00
free_token(&current_token);
return NULL;
}
2026-01-13 17:56:30 +01:00
char *stream = stream_init();
ssize_t i = 0;
2026-01-19 17:32:45 +01:00
// Usefull to know if we are inside a quote or double quote
enum lexing_mode lexing_mode = LEXER_NORMAL;
2026-01-13 17:56:30 +01:00
while (i < remaining_chars)
{
2026-01-19 18:19:35 +01:00
// true if we didn't encounter a quote of any type at stream[i]
// AND we are not inside quotes
if (!update_lexing_mode(stream, i, &lexing_mode)
&& lexing_mode == LEXER_NORMAL)
2026-01-13 17:56:30 +01:00
{
2026-01-19 17:32:45 +01:00
if (is_special_char(stream[i]))
{
if (i == 0) // where we create spe_char token
i++;
break;
}
if (isblank(stream[i]))
{
break;
}
2026-01-13 17:56:30 +01:00
}
i++;
}
2026-01-17 11:38:23 +01:00
// just in case peek() was not called before poping.
// (this should never happen)
if (current_token == NULL)
{
current_token = new_token(stream, i);
}
save_state(stream, i);
2026-01-12 21:31:15 +01:00
2026-01-17 11:38:23 +01:00
return last_token;
2026-01-08 16:32:48 +01:00
}