feat+fix: make compiler STRONGAAA
- FEAT: add source code length field in `fzl_reader_t` struct - FIX: check name, lpair (lparen) and rpair (rparen) when lexing call - FIX: check string literal termination - FIX: increment position even on empty string to prevent infinite lexing loop - FIX: add string terminator in `fzl_mktoken` when allocating space for string value - FIX: add check for errors in recursevly called `walk` - FIX: change source code holder from stack-stored `char[8192]` to dynamically allocated with actual file size holder - FIX: correct constant hasing util to hash actual string value instead of link
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
#include <compiler/fzl_reader.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
@@ -16,6 +17,7 @@ fzl_reader_t *fzl_mkreader(char *source)
|
||||
reader->first = first_node;
|
||||
reader->last = first_node;
|
||||
reader->source = source;
|
||||
reader->size = strlen(source);
|
||||
reader->position = 0;
|
||||
reader->at_end = true;
|
||||
|
||||
@@ -60,18 +62,27 @@ fzl_token_t *parse_string_literal(fzl_reader_t *reader)
|
||||
return NULL;
|
||||
|
||||
char open_symbol = reader->source[lpos];
|
||||
char close_symbol = '\0';
|
||||
|
||||
do
|
||||
{
|
||||
lpos++;
|
||||
size++;
|
||||
} while (reader->source[lpos] != open_symbol);
|
||||
|
||||
if (reader->source[lpos] == open_symbol)
|
||||
close_symbol = open_symbol;
|
||||
|
||||
} while (reader->source[lpos] != open_symbol && lpos < reader->size);
|
||||
size++;
|
||||
|
||||
if (close_symbol != open_symbol)
|
||||
return NULL;
|
||||
|
||||
if (size == 2)
|
||||
{
|
||||
fzl_token_t *tok = fzl_mktoken(FZL_TOKTYPE_STR_LITERAL, reader->position,
|
||||
reader->position + 1, "", 0);
|
||||
reader->position += 2;
|
||||
return tok;
|
||||
}
|
||||
|
||||
@@ -88,8 +99,7 @@ fzl_token_t *parse_string_literal(fzl_reader_t *reader)
|
||||
|
||||
int fzl_read(fzl_reader_t *reader)
|
||||
{
|
||||
const size_t len = strlen(reader->source);
|
||||
if (reader->position >= len)
|
||||
if (reader->position >= reader->size)
|
||||
return 0;
|
||||
|
||||
do
|
||||
@@ -139,8 +149,7 @@ int fzl_read(fzl_reader_t *reader)
|
||||
linepos++;
|
||||
}
|
||||
|
||||
sprintf(last_err, "error parsing token (position %ld of line %ld)", line,
|
||||
linepos);
|
||||
sprintf(last_err, "cannot parse token (%ld:%ld)", line, linepos);
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -149,7 +158,7 @@ int fzl_read(fzl_reader_t *reader)
|
||||
|
||||
reader->last->next = tok_node;
|
||||
reader->last = tok_node;
|
||||
} while (reader->position < len);
|
||||
} while (reader->position < reader->size);
|
||||
|
||||
reader->at_end = true;
|
||||
return 0;
|
||||
|
||||
Reference in New Issue
Block a user