42sh/src/lexer/lexer.c

220 lines
5.9 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"
#include "../utils/string_utils/string_utils.h"
#include "lexer_utils.h"
2026-01-08 16:32:48 +01:00
/* @brief: sets the ctx->current_token to [tok].
* this function is called by token_peek().
*/
2026-01-20 19:29:24 +01:00
static void update_current_token(struct token *tok, struct lexer_context *ctx)
{
ctx->current_token = tok;
}
/* @brief: frees the last token and sets it to [tok].
* Also sets ctx->current_token to NULL.
* this function is called by token_pop().
*/
2026-01-20 19:29:24 +01:00
static void update_previous_token(struct token *tok, struct lexer_context *ctx)
{
free_token(&ctx->previous_token);
ctx->previous_token = tok;
}
/* @brief: updates the current position in the stream.
* [stream] += [i]
* Also frees the last sent token, and sets it to ctx->current_token.
2026-01-17 11:38:23 +01:00
* Current token is then set to NULL.
* This function is called by token_pop().
2026-01-12 21:31:15 +01:00
*/
static void save_state(char *stream, ssize_t i, struct lexer_context *ctx)
2026-01-12 21:31:15 +01:00
{
ctx->remaining_chars -= i;
2026-01-20 19:40:00 +01:00
ctx->end_previous_token = stream + i;
2026-01-20 20:32:59 +01:00
update_previous_token(ctx->current_token, ctx);
update_current_token(NULL, ctx);
2026-01-12 21:31:15 +01:00
}
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;
}
/* @brief: updates the flags only_digits and has_equal.
2026-01-29 11:18:56 +01:00
* according to the character at stream[i].
*/
static void update_flags(char *stream, ssize_t i, struct token_info *info)
2026-01-29 11:18:56 +01:00
{
if (stream[i] == '=' && !info->has_equal)
2026-01-29 11:18:56 +01:00
{
if (i == 0)
{
perror("Syntax error: word start with a '='");
return;
}
else
info->has_equal = true;
2026-01-29 11:18:56 +01:00
}
else if (!isdigit(stream[i]) && info->only_digits)
2026-01-29 11:18:56 +01:00
{
info->only_digits = false;
2026-01-29 11:18:56 +01:00
}
}
struct token *peek_token(struct lexer_context *ctx)
2026-01-08 16:32:48 +01:00
{
stream_init(ctx);
// Usefull to know if we are inside a quote or double quote
enum lexing_mode lexing_mode = LEXER_NORMAL;
struct token_info info = {true, 0};
char *stream = ctx->end_previous_token;
ssize_t i = 0;
// we already created the upcoming token during the previous call to peek()
if (ctx->current_token != NULL)
{
return ctx->current_token;
}
while (i < ctx->remaining_chars)
2026-01-08 16:32:48 +01:00
{
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
{
update_flags(stream, i, &info);
if (is_special_char(stream, i))
2026-01-19 17:32:45 +01:00
{
if (i == 0) // where we create spe_char token
i += len_op_sepchar(stream, i);
2026-01-19 17:32:45 +01:00
break;
}
if (isblank(stream[i]))
{
break;
}
2026-01-12 21:31:15 +01:00
}
else if (stream[i] == EOF)
{
fprintf(stderr, "Lexing error: unmatched quote\n");
// error handling
return NULL;
}
2026-01-08 16:32:48 +01:00
i++;
}
struct token *tok = new_token(stream, i, &info);
2026-01-21 16:19:10 +01:00
// if token is comment, we don't want it
if (tok->type == TOKEN_COMMENT)
{
// Find next newline or EOF.
go_end_of_line(ctx);
free_token(&tok);
tok = peek_token(ctx);
2026-01-21 16:19:10 +01:00
}
2026-01-20 20:32:59 +01:00
update_current_token(tok, ctx);
return tok;
}
struct token *pop_token(struct lexer_context *ctx)
{
stream_init(ctx);
// Usefull to know if we are inside a quote or double quote
enum lexing_mode lexing_mode = LEXER_NORMAL;
struct token_info info = {true, 0};
char *stream = ctx->end_previous_token;
ssize_t i = 0;
if (ctx->current_token != NULL && ctx->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(&ctx->previous_token);
free_token(&ctx->current_token);
return NULL;
}
2026-01-19 17:32:45 +01:00
while (i < ctx->remaining_chars)
2026-01-13 17:56:30 +01:00
{
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
{
update_flags(stream, i, &info);
if (is_special_char(stream, i))
2026-01-19 17:32:45 +01:00
{
if (i == 0) // where we create spe_char token
2026-01-23 19:34:47 +01:00
i += len_op_sepchar(stream, i);
2026-01-19 17:32:45 +01:00
break;
}
if (isblank(stream[i]))
{
break;
}
2026-01-13 17:56:30 +01:00
}
else if (stream[i] == EOF)
{
fprintf(stderr, "Lexing error: unmatched quote\n");
// error handling
return NULL;
}
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 (ctx->current_token == NULL)
2026-01-17 11:38:23 +01:00
{
ctx->current_token = new_token(stream, i, &info);
2026-01-17 11:38:23 +01:00
}
2026-01-20 20:32:59 +01:00
save_state(stream, i, ctx);
2026-01-12 21:31:15 +01:00
return ctx->previous_token;
2026-01-08 16:32:48 +01:00
}