tlibtoml/src/toml_parse/toml_parse_inline_table.c

101 lines
3.0 KiB
C

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "toml_internal.h"
TomlValue toml_parse_inline_table(TomlParser* self)
{
TomlValue table_value = TomlValue_new_table();
while (self->ptr < self->end) {
char ch = *self->ptr;
while (self->ptr < self->end && (ch == ' ' || ch == '\t')) {
toml_move_next(self);
ch = *self->ptr;
}
str key;
if (isalnum(ch) || ch == '_') {
key = toml_parse_bare_key(self);
if (key.data == NULL)
goto error;
} else if (ch == '\"') {
toml_move_next(self);
key = toml_parse_basic_string(self);
if (key.data == NULL)
goto error;
} else if (ch == '\'') {
toml_move_next(self);
key = toml_parse_literal_string(self);
if (key.data == NULL)
goto error;
} else if (ch == '}') {
break;
} else {
Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_SYNTAX, "%s:%d:%d: unexpected token",
self->filename, self->lineno, self->colno);
goto error;
}
ch = *self->ptr;
while (self->ptr < self->end && (ch == ' ' || ch == '\t')) {
toml_move_next(self);
ch = *self->ptr;
}
if (self->ptr == self->end) {
Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_SYNTAX, "%s:%d:%d: unterminated key value pair",
self->filename, self->lineno, self->colno);
goto error;
}
if (ch != '=') {
Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_SYNTAX, "%s:%d:%d: unexpected token",
self->filename, self->lineno, self->colno);
goto error;
}
toml_move_next(self);
ch = *self->ptr;
while (self->ptr < self->end && (ch == ' ' || ch == '\t')) {
toml_move_next(self);
ch = *self->ptr;
}
if (self->ptr == self->end) {
Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_SYNTAX, "%s:%d:%d: unterminated key value pair",
self->filename, self->lineno, self->colno);
goto error;
}
TomlValue value = toml_parse_value(self);
if (value.type == TLIBTOML_INVALID_TYPE)
goto error;
TomlTable_set(table_value.value.table, key, value);
str_destroy(key);
while (self->ptr < self->end && (*self->ptr == ' ' ||* self->ptr == '\t')) {
toml_move_next(self);
}
if (*self->ptr == ',') {
toml_move_next(self);
} else if (*self->ptr == '}') {
toml_move_next(self);
break;
}
}
goto end;
error:
TomlValue_destroy(&table_value);
//TODO: error handling
assert(false && "TODO error handling");
end:
return table_value;
}