Merge branch 'cktan_master'

This commit is contained in:
chinky 2020-05-10 09:27:25 +08:00
commit 4c7f351ed8
2 changed files with 1339 additions and 1348 deletions

91
toml.c
View File

@ -382,18 +382,18 @@ static int e_bad_key_error(context_t* ctx, int lineno)
}
/*
static int e_noimpl(context_t* ctx, const char* feature)
{
static int e_noimpl(context_t* ctx, const char* feature)
{
snprintf(ctx->errbuf, ctx->errbufsz, "not implemented: %s", feature);
longjmp(ctx->jmp, 1);
return -1;
}
}
*/
static int e_key_exists_error(context_t* ctx, int lineno, const char* key)
static int e_key_exists_error(context_t* ctx, int lineno)
{
snprintf(ctx->errbuf, ctx->errbufsz,
"line %d: key %s exists", lineno, key);
"line %d: key exists", lineno);
longjmp(ctx->jmp, 1);
return -1;
}
@ -691,7 +691,7 @@ static toml_keyval_t* create_keyval_in_table(context_t* ctx, toml_table_t* tab,
toml_keyval_t* dest = 0;
if (key_kind(tab, newkey)) {
xfree(newkey);
e_key_exists_error(ctx, keytok.lineno, newkey);
e_key_exists_error(ctx, keytok.lineno);
return 0; /* not reached */
}
@ -738,7 +738,7 @@ static toml_table_t* create_keytable_in_table(context_t* ctx, toml_table_t* tab,
dest->implicit = 0;
return dest;
}
e_key_exists_error(ctx, keytok.lineno, newkey);
e_key_exists_error(ctx, keytok.lineno);
return 0; /* not reached */
}
@ -780,7 +780,7 @@ static toml_array_t* create_keyarray_in_table(context_t* ctx,
/* if key exists: error out */
if (key_kind(tab, newkey)) {
xfree(newkey); /* don't need this anymore */
e_key_exists_error(ctx, keytok.lineno, newkey);
e_key_exists_error(ctx, keytok.lineno);
return 0; /* not reached */
}
@ -850,7 +850,15 @@ static toml_table_t* create_table_in_array(context_t* ctx,
}
#define SKIP_NEWLINES(ctx, isdotspecial) while (ctx->tok.tok == NEWLINE) next_token(ctx, isdotspecial)
static void skip_newlines(context_t* ctx, int isdotspecial)
{
while (ctx->tok.tok == NEWLINE) {
next_token(ctx, isdotspecial);
if (ctx->tok.eof) break;
}
}
#define EAT_TOKEN(ctx, typ, isdotspecial) \
if ((ctx)->tok.tok != typ) e_internal_error(ctx, FLINE); else next_token(ctx, isdotspecial)
@ -923,7 +931,7 @@ static void parse_array(context_t* ctx, toml_array_t* arr)
EAT_TOKEN(ctx, LBRACKET, 0);
for (;;) {
SKIP_NEWLINES(ctx, 0);
skip_newlines(ctx, 0);
/* until ] */
if (ctx->tok.tok == RBRACKET) break;
@ -1002,7 +1010,7 @@ static void parse_array(context_t* ctx, toml_array_t* arr)
return; /* not reached */
}
SKIP_NEWLINES(ctx, 0);
skip_newlines(ctx, 0);
/* on comma, continue to scan for next element */
if (ctx->tok.tok == COMMA) {
@ -1013,7 +1021,7 @@ static void parse_array(context_t* ctx, toml_array_t* arr)
}
if (ctx->tok.tok != RBRACKET) {
e_syntax_error(ctx, ctx->tok.lineno, "syntax error");
e_syntax_error(ctx, ctx->tok.lineno, "expect a right bracket");
return; /* not reached */
}
@ -1186,7 +1194,7 @@ static void walk_tabpath(context_t* ctx)
break;
case 'v':
e_key_exists_error(ctx, ctx->tpath.tok[i].lineno, key);
e_key_exists_error(ctx, ctx->tpath.tok[i].lineno);
return; /* not reached */
default:
@ -2089,27 +2097,30 @@ int toml_rtod(const char* src, double* ret_)
int toml_rtos(const char* src, char** ret)
{
char dummy_errbuf[1];
int multiline = 0;
const char* sp;
const char* sq;
if (!src) return -1;
if (*src != '\'' && *src != '"') return -1;
*ret = 0;
if (!src) return -1;
int qchar = src[0];
int srclen = strlen(src);
if (*src == '\'') {
if (0 == strncmp(src, "'''", 3)) {
if (! (qchar == '\'' || qchar == '"')) {
return -1;
}
// triple quotes?
if (qchar == src[1] && qchar == src[2]) {
multiline = 1;
sp = src + 3;
sq = src + srclen - 3;
/* last 3 chars in src must be ''' */
if (! (sp <= sq && 0 == strcmp(sq, "'''")))
/* last 3 chars in src must be qchar */
if (! (sp <= sq && sq[0] == qchar && sq[1] == qchar && sq[2] == qchar))
return -1;
/* skip first new line right after ''' */
if (*sp == '\n')
/* skip new line immediate after qchar */
if (sp[0] == '\n')
sp++;
else if (sp[0] == '\r' && sp[1] == '\n')
sp += 2;
@ -2117,40 +2128,20 @@ int toml_rtos(const char* src, char** ret)
} else {
sp = src + 1;
sq = src + srclen - 1;
/* last char in src must be ' */
if (! (sp <= sq && *sq == '\''))
/* last char in src must be qchar */
if (! (sp <= sq && *sq == qchar))
return -1;
/* copy from sp to p */
*ret = STRNDUP(sp, sq - sp);
}
if (qchar == '\'') {
*ret = norm_lit_str(sp, sq - sp,
multiline,
dummy_errbuf, sizeof(dummy_errbuf));
return *ret ? 0 : -1;
}
if (0 == strncmp(src, "\"\"\"", 3)) {
multiline = 1;
sp = src + 3;
sq = src + srclen - 3;
if (! (sp <= sq && 0 == strcmp(sq, "\"\"\"")))
return -1;
/* skip first new line right after """ */
if (*sp == '\n')
sp++;
else if (sp[0] == '\r' && sp[1] == '\n')
sp += 2;
0, 0);
} else {
sp = src + 1;
sq = src + srclen - 1;
if (! (sp <= sq && *sq == '"'))
return -1;
}
*ret = norm_basic_str(sp, sq - sp,
multiline,
dummy_errbuf, sizeof(dummy_errbuf));
0, 0);
}
return *ret ? 0 : -1;
}