ref: split reader into separate files
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
#include <compiler/fzl_reader.h>
|
||||
#include <compiler/internals/reader.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
// Parse string format
|
||||
fzl_token_t *rd_fmtstr(fzl_reader_t *reader)
|
||||
{
|
||||
size_t p = reader->position;
|
||||
char *src = reader->source;
|
||||
|
||||
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 (src[p] == '{')
|
||||
{
|
||||
move(reader, 1);
|
||||
fzl_token_t *id = rd_name(reader);
|
||||
|
||||
if (id == NULL || src[reader->position] != '}')
|
||||
{
|
||||
snprintf(last_err, sizeof(last_err),
|
||||
"'%s':%zu:%zu => failed to parse format string", l.filename,
|
||||
l.line, l.col);
|
||||
fzl_freetoken(id);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
move(reader, 1);
|
||||
return id;
|
||||
}
|
||||
|
||||
if (src[p] == '"')
|
||||
{
|
||||
move(reader, 1);
|
||||
|
||||
return fzl_mktoken(FZL_TOKTYPE_STR_FORMAT_END, l.line, l.col, NULL, 0,
|
||||
l.filename);
|
||||
}
|
||||
|
||||
size_t size = 0;
|
||||
size_t lpos = p;
|
||||
|
||||
do
|
||||
{
|
||||
if (src[lpos + 1] == '"')
|
||||
break;
|
||||
|
||||
size++;
|
||||
lpos++;
|
||||
} while (lpos < reader->size && reader->source[lpos] != '{' &&
|
||||
reader->source[lpos] != '"');
|
||||
|
||||
ok = fzl_linked_getpos(reader->linked, reader->position, &l);
|
||||
if (!ok)
|
||||
{
|
||||
snprintf(last_err, sizeof(last_err), "failed to locate line:col");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char *value = malloc(size);
|
||||
memcpy(value, reader->source + reader->position, size);
|
||||
fzl_token_t *tok = fzl_mktoken(FZL_TOKTYPE_STR_LITERAL, l.line, l.col, value,
|
||||
size, l.filename);
|
||||
free(value);
|
||||
move(reader, size);
|
||||
|
||||
return tok;
|
||||
}
|
||||
Reference in New Issue
Block a user