ref: split reader into separate files
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
#include <compiler/fzl_reader.h>
|
||||
#include <compiler/fzl_token.h>
|
||||
#include <compiler/internals/reader.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
// Try parse string literal
|
||||
fzl_token_t *rd_string_literal(fzl_reader_t *reader)
|
||||
{
|
||||
size_t lpos = reader->position;
|
||||
size_t size = 0;
|
||||
|
||||
if (reader->source[lpos] != '\'' && reader->source[lpos] != '"')
|
||||
return NULL;
|
||||
|
||||
char open_symbol = reader->source[lpos];
|
||||
char close_symbol = '\0';
|
||||
|
||||
do
|
||||
{
|
||||
lpos++;
|
||||
size++;
|
||||
|
||||
if (reader->source[lpos] == open_symbol)
|
||||
close_symbol = open_symbol;
|
||||
|
||||
} while (reader->source[lpos] != open_symbol && lpos < reader->size);
|
||||
size++;
|
||||
|
||||
fzl_file_loc l;
|
||||
bool ok = fzl_linked_getpos(reader->linked, reader->position, &l);
|
||||
if (!ok)
|
||||
{
|
||||
snprintf(last_err, sizeof(last_err), "failed to locate line:col");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (close_symbol != open_symbol)
|
||||
{
|
||||
snprintf(last_err, sizeof(last_err), "'%s': unterminated string at %ld:%ld",
|
||||
l.filename, l.line, l.col);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (size == 2)
|
||||
{
|
||||
fzl_token_t *tok =
|
||||
fzl_mktoken(FZL_TOKTYPE_STR_LITERAL, l.line, l.col, "", 0, l.filename);
|
||||
move(reader, 2);
|
||||
return tok;
|
||||
}
|
||||
|
||||
char *value = malloc(size - 2);
|
||||
memcpy(value, reader->source + reader->position + 1, size - 2);
|
||||
fzl_token_t *tok = fzl_mktoken(FZL_TOKTYPE_STR_LITERAL, l.line, l.col, value,
|
||||
size - 2, l.filename);
|
||||
free(value);
|
||||
move(reader, size);
|
||||
|
||||
return tok;
|
||||
}
|
||||
Reference in New Issue
Block a user