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>
|
2026-01-13 22:07:40 +01:00
|
|
|
#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;
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
2026-01-13 22:07:40 +01:00
|
|
|
return c == '\'' || c == '\n' || c == ';' || c == EOF;
|
2026-01-12 21:31:15 +01:00
|
|
|
}
|
|
|
|
|
|
2026-01-13 22:00:02 +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-13 22:07:40 +01:00
|
|
|
if (begin[0] == EOF)
|
2026-01-13 22:00:02 +01:00
|
|
|
{
|
|
|
|
|
tok->type = TOKEN_EOF;
|
|
|
|
|
}
|
|
|
|
|
else if (begin[0] == ';')
|
|
|
|
|
{
|
|
|
|
|
tok->type = TOKEN_NEWLINE;
|
|
|
|
|
}
|
|
|
|
|
else if (begin[0] == '\'')
|
|
|
|
|
{
|
|
|
|
|
tok->type = TOKEN_QUOTE;
|
|
|
|
|
}
|
|
|
|
|
else if (begin[0] == ';')
|
|
|
|
|
{
|
|
|
|
|
tok->type = TOKEN_SEMICOLON;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* @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
|
|
|
{
|
2026-01-13 22:00:02 +01:00
|
|
|
if (tok->type != TOKEN_NULL)
|
|
|
|
|
return;
|
2026-01-13 19:44:56 +01:00
|
|
|
if (strncmp(begin, "if", size) == 0)
|
|
|
|
|
{
|
|
|
|
|
tok->type = TOKEN_IF;
|
|
|
|
|
}
|
2026-01-13 22:00:02 +01:00
|
|
|
else if (strncmp(begin, "fi", size) == 0)
|
2026-01-12 21:31:15 +01:00
|
|
|
{
|
2026-01-13 19:44:56 +01:00
|
|
|
tok->type = TOKEN_FI;
|
2026-01-12 21:31:15 +01:00
|
|
|
}
|
2026-01-13 22:00:02 +01:00
|
|
|
else if (strncmp(begin, "then", size) == 0)
|
2026-01-12 21:31:15 +01:00
|
|
|
{
|
2026-01-13 19:44:56 +01:00
|
|
|
tok->type = TOKEN_THEN;
|
|
|
|
|
}
|
2026-01-13 22:00:02 +01:00
|
|
|
else if (strncmp(begin, "else", size) == 0)
|
2026-01-13 19:44:56 +01:00
|
|
|
{
|
|
|
|
|
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-14 18:48:12 +01:00
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2026-01-13 22:00:02 +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)
|
|
|
|
|
{
|
2026-01-13 22:07:40 +01:00
|
|
|
if (tok->type == TOKEN_NULL)
|
2026-01-13 22:00:02 +01:00
|
|
|
{
|
2026-01-13 22:07:40 +01:00
|
|
|
tok->data = calloc(size + 1, sizeof(char));
|
|
|
|
|
if (tok->data == NULL)
|
|
|
|
|
return;
|
|
|
|
|
strncpy(tok->data, begin, size);
|
2026-01-13 22:00:02 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-13 19:44:56 +01:00
|
|
|
struct token *new_token(char *begin, ssize_t size)
|
2026-01-08 16:32:48 +01:00
|
|
|
{
|
2026-01-13 22:00:02 +01:00
|
|
|
struct token *tok = calloc(1, sizeof(struct token));
|
|
|
|
|
if (tok == NULL)
|
2026-01-08 16:32:48 +01:00
|
|
|
return NULL;
|
2026-01-13 19:44:56 +01:00
|
|
|
|
2026-01-13 22:00:02 +01:00
|
|
|
set_token_spechar(tok, begin, size);
|
|
|
|
|
set_token_keyword(tok, begin, size);
|
|
|
|
|
set_token_word(tok, begin, size);
|
2026-01-13 19:44:56 +01:00
|
|
|
|
2026-01-13 22:00:02 +01:00
|
|
|
return tok;
|
|
|
|
|
}
|
2026-01-13 19:44:56 +01:00
|
|
|
|
2026-01-13 22:00:02 +01:00
|
|
|
void free_token(struct token *tok)
|
|
|
|
|
{
|
|
|
|
|
if (tok == NULL)
|
|
|
|
|
return;
|
|
|
|
|
if (tok->data != NULL)
|
|
|
|
|
free(tok->data);
|
|
|
|
|
free(tok);
|
2026-01-08 16:32:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 19:44:56 +01:00
|
|
|
struct token *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;
|
|
|
|
|
}
|
2026-01-08 16:32:48 +01:00
|
|
|
i++;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-13 17:45:15 +01:00
|
|
|
return new_token(stream, i);
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-13 19:44:56 +01:00
|
|
|
struct token *pop_token(void)
|
2026-01-13 17:45:15 +01:00
|
|
|
{
|
2026-01-13 17:56:30 +01:00
|
|
|
char *stream = stream_init();
|
|
|
|
|
|
|
|
|
|
ssize_t i = 0;
|
|
|
|
|
|
|
|
|
|
while (i < remaining_chars)
|
|
|
|
|
{
|
|
|
|
|
if (is_special_char(stream[i]))
|
|
|
|
|
{
|
|
|
|
|
if (i == 0) // where we create spe_char token
|
|
|
|
|
i++;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (isblank(stream[i]))
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
i++;
|
|
|
|
|
}
|
2026-01-13 17:45:15 +01:00
|
|
|
|
2026-01-12 21:31:15 +01:00
|
|
|
save_state(stream, i);
|
|
|
|
|
|
2026-01-13 17:56:30 +01:00
|
|
|
return new_token(stream, i);
|
2026-01-08 16:32:48 +01:00
|
|
|
}
|