2026-01-08 16:32:48 +01:00
|
|
|
#include "lexer.h"
|
|
|
|
|
|
|
|
|
|
#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"
|
|
|
|
|
|
|
|
|
|
static char *end_last_token;
|
|
|
|
|
static ssize_t remaining_chars;
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return stream;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char *get_token(void)
|
|
|
|
|
{
|
|
|
|
|
char *stream = stream_init();
|
|
|
|
|
|
|
|
|
|
bool inquotes = false;
|
|
|
|
|
ssize_t i = 0;
|
|
|
|
|
|
|
|
|
|
while (i < remaining_chars)
|
|
|
|
|
{
|
|
|
|
|
switch (stream[i])
|
|
|
|
|
{
|
|
|
|
|
case '\'':
|
|
|
|
|
inquotes = !inquotes;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case ' ' | '\n' | '\t':
|
|
|
|
|
if (inquotes)
|
|
|
|
|
break;
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// token creation
|
|
|
|
|
// skip blank char
|
|
|
|
|
// exit from loop
|
|
|
|
|
char *token = new_token(stream, i);
|
2026-01-10 19:57:36 +01:00
|
|
|
return token;
|
2026-01-08 16:32:48 +01:00
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
i++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
remaining_chars -= i;
|
2026-01-10 19:57:36 +01:00
|
|
|
return NULL;
|
2026-01-08 16:32:48 +01:00
|
|
|
}
|