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 <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;
|
|
|
|
|
|
2026-01-12 21:31:15 +01:00
|
|
|
/* @brief: saves state for the next call the the lexer.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
static void save_state(char *stream, ssize_t i)
|
|
|
|
|
{
|
|
|
|
|
remaining_chars -= i;
|
|
|
|
|
end_last_token = stream + i;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* @return: true if a special character from the grammar was found,
|
|
|
|
|
* false otherwise.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
static bool is_special_char(char c)
|
|
|
|
|
{
|
|
|
|
|
return c == '\'' || c == '\n' || c == ';';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* @return: true if a keyword from the grammar was found, false otherwise.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
static bool is_keyword(char *stream, ssize_t i)
|
|
|
|
|
{
|
|
|
|
|
if (i == 2)
|
|
|
|
|
{
|
|
|
|
|
return strcmp(stream, "if") == 0 || strcmp(stream, "fi") == 0;
|
|
|
|
|
}
|
|
|
|
|
if (i == 4)
|
|
|
|
|
{
|
|
|
|
|
return strcmp(stream, "then") || strcmp(stream, "else")
|
|
|
|
|
|| strcmp(stream, "elif");
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-10 19:57:36 +01:00
|
|
|
char *new_token(char *begin, ssize_t size)
|
2026-01-08 16:32:48 +01:00
|
|
|
{
|
|
|
|
|
char *res = calloc(size + 1, sizeof(char));
|
|
|
|
|
if (res == NULL)
|
|
|
|
|
return NULL;
|
|
|
|
|
strncpy(res, begin, size);
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char *stream_init(void)
|
|
|
|
|
{
|
|
|
|
|
char *stream;
|
|
|
|
|
|
|
|
|
|
if (remaining_chars == 0)
|
|
|
|
|
{
|
|
|
|
|
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-13 17:45:15 +01:00
|
|
|
char *peek_token(void)
|
2026-01-08 16:32:48 +01:00
|
|
|
{
|
|
|
|
|
char *stream = stream_init();
|
|
|
|
|
|
|
|
|
|
ssize_t i = 0;
|
|
|
|
|
|
|
|
|
|
while (i < remaining_chars)
|
|
|
|
|
{
|
2026-01-12 21:31:15 +01:00
|
|
|
if (is_special_char(stream[i]))
|
2026-01-08 16:32:48 +01:00
|
|
|
{
|
2026-01-12 21:31:15 +01:00
|
|
|
if (i == 0) // where we create spe_char token
|
|
|
|
|
i++;
|
2026-01-08 16:32:48 +01:00
|
|
|
break;
|
2026-01-12 21:31:15 +01:00
|
|
|
}
|
|
|
|
|
if (isblank(stream[i]))
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
else if (is_keyword(stream, i))
|
|
|
|
|
{
|
|
|
|
|
i++;
|
2026-01-08 16:32:48 +01:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
i++;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-13 17:45:15 +01:00
|
|
|
return new_token(stream, i);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char *pop_token(void)
|
|
|
|
|
{
|
|
|
|
|
char *token = peek_token();
|
|
|
|
|
|
2026-01-12 21:31:15 +01:00
|
|
|
save_state(stream, i);
|
|
|
|
|
|
2026-01-13 17:45:15 +01:00
|
|
|
return token;
|
2026-01-08 16:32:48 +01:00
|
|
|
}
|