219 lines
5.9 KiB
C
219 lines
5.9 KiB
C
#include "lexer.h"
|
|
|
|
#include <ctype.h>
|
|
#include <stdbool.h>
|
|
#include <stddef.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#include "../io_backend/io_backend.h"
|
|
#include "../utils/string_utils/string_utils.h"
|
|
#include "lexer_utils.h"
|
|
|
|
/* @brief: sets the ctx->current_token to [tok].
|
|
* this function is called by token_peek().
|
|
*/
|
|
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().
|
|
*/
|
|
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.
|
|
* Current token is then set to NULL.
|
|
* This function is called by token_pop().
|
|
*/
|
|
static void save_state(char *stream, ssize_t i, struct lexer_context *ctx)
|
|
{
|
|
ctx->remaining_chars -= i;
|
|
ctx->end_previous_token = stream + i;
|
|
|
|
update_previous_token(ctx->current_token, ctx);
|
|
update_current_token(NULL, ctx);
|
|
}
|
|
|
|
/*
|
|
* @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;
|
|
}
|
|
|
|
// 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;
|
|
}
|
|
|
|
return *lexing_mode != mode_before_update;
|
|
}
|
|
|
|
/* @brief: updates the flags only_digits and has_equal.
|
|
* according to the character at stream[i].
|
|
*/
|
|
static void update_flags(char *stream, ssize_t i, struct token_info *info)
|
|
{
|
|
if (stream[i] == '=' && !info->has_equal)
|
|
{
|
|
if (i == 0)
|
|
{
|
|
perror("Syntax error: word start with a '='");
|
|
return;
|
|
}
|
|
else
|
|
info->has_equal = true;
|
|
}
|
|
else if (!isdigit(stream[i]) && info->only_digits)
|
|
{
|
|
info->only_digits = false;
|
|
}
|
|
}
|
|
|
|
struct token *peek_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;
|
|
|
|
// 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)
|
|
{
|
|
// 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)
|
|
{
|
|
update_flags(stream, i, &info);
|
|
if (is_special_char(stream, i))
|
|
{
|
|
if (i == 0) // where we create spe_char token
|
|
i += len_op_sepchar(stream, i);
|
|
break;
|
|
}
|
|
if (isblank(stream[i]))
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
else if (stream[i] == EOF)
|
|
{
|
|
fprintf(stderr, "Lexing error: unmatched quote\n");
|
|
|
|
// error handling
|
|
return NULL;
|
|
}
|
|
i++;
|
|
}
|
|
|
|
struct token *tok = new_token(stream, i, &info);
|
|
// 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);
|
|
}
|
|
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)
|
|
{
|
|
// we reached end of input, frees all the token still allocated.
|
|
free_token(&ctx->previous_token);
|
|
free_token(&ctx->current_token);
|
|
return NULL;
|
|
}
|
|
|
|
while (i < ctx->remaining_chars)
|
|
{
|
|
// 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)
|
|
{
|
|
update_flags(stream, i, &info);
|
|
if (is_special_char(stream, i))
|
|
{
|
|
if (i == 0) // where we create spe_char token
|
|
i += len_op_sepchar(stream, i);
|
|
break;
|
|
}
|
|
if (isblank(stream[i]))
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
else if (stream[i] == EOF)
|
|
{
|
|
fprintf(stderr, "Lexing error: unmatched quote\n");
|
|
|
|
// error handling
|
|
return NULL;
|
|
}
|
|
i++;
|
|
}
|
|
|
|
// just in case peek() was not called before poping.
|
|
// (this should never happen)
|
|
if (ctx->current_token == NULL)
|
|
{
|
|
ctx->current_token = new_token(stream, i, &info);
|
|
}
|
|
save_state(stream, i, ctx);
|
|
|
|
return ctx->previous_token;
|
|
}
|