more tests

This commit is contained in:
CK Tan 2021-03-07 17:28:41 -08:00
parent 937d88e009
commit dbc1ef26d6
29 changed files with 209 additions and 4 deletions

3
stdex/inlinetab1.toml Normal file
View file

@ -0,0 +1,3 @@
name = { first = "Tom", last = "Preston-Werner" }
point = { x = 1, y = 2 }
animal = { type.name = "pug" }

15
stdex/inlinetab1.toml.res Normal file
View file

@ -0,0 +1,15 @@
{
name = {
first = "Tom",
last = "Preston-Werner",
},
point = {
x = 1,
y = 2,
},
animal = {
type = {
name = "pug",
},
},
}

3
stdex/inlinetab2.toml Normal file
View file

@ -0,0 +1,3 @@
[product]
type = { name = "Nail" }
type.edible = false # INVALID

View file

@ -0,0 +1 @@
ERROR: line 3: cannot insert new entry into existing table

3
stdex/inlinetab3.toml Normal file
View file

@ -0,0 +1,3 @@
[product]
type.name = "Nail"
type = { edible = false } # INVALID

View file

@ -0,0 +1 @@
ERROR: line 3: key exists

7
stdex/tab01.toml Normal file
View file

@ -0,0 +1,7 @@
[table-1]
key1 = "some string"
key2 = 123
[table-2]
key1 = "another string"
key2 = 456

10
stdex/tab01.toml.res Normal file
View file

@ -0,0 +1,10 @@
{
table-1 = {
key1 = "some string",
key2 = 123,
},
table-2 = {
key1 = "another string",
key2 = 456,
},
}

2
stdex/tab02.toml Normal file
View file

@ -0,0 +1,2 @@
[dog."tater.man"]
type.name = "pug"

9
stdex/tab02.toml.res Normal file
View file

@ -0,0 +1,9 @@
{
dog = {
tater.man = {
type = {
name = "pug",
},
},
},
}

4
stdex/tab03.toml Normal file
View file

@ -0,0 +1,4 @@
[a.b.c] # this is best practice
[ d.e.f ] # same as [d.e.f]
[ g . h . i ] # same as [g.h.i]
[ j . "ʞ" . 'l' ] # same as [j."ʞ".'l']

26
stdex/tab03.toml.res Normal file
View file

@ -0,0 +1,26 @@
{
a = {
b = {
c = {
},
},
},
d = {
e = {
f = {
},
},
},
g = {
h = {
i = {
},
},
},
j = {
ʞ = {
l = {
},
},
},
}

6
stdex/tab04.toml Normal file
View file

@ -0,0 +1,6 @@
# [x] you
# [x.y] don't
# [x.y.z] need these
[x.y.z.w] # for this to work
[x] # defining a super-table afterward is ok

10
stdex/tab04.toml.res Normal file
View file

@ -0,0 +1,10 @@
{
x = {
y = {
z = {
w = {
},
},
},
},
}

7
stdex/tab05.toml Normal file
View file

@ -0,0 +1,7 @@
# DO NOT DO THIS
[fruit]
apple = "red"
[fruit]
orange = "orange"

1
stdex/tab05.toml.res Normal file
View file

@ -0,0 +1 @@
ERROR: line 6: key exists

7
stdex/tab06.toml Normal file
View file

@ -0,0 +1,7 @@
# DO NOT DO THIS EITHER
[fruit]
apple = "red"
[fruit.apple]
texture = "smooth"

1
stdex/tab06.toml.res Normal file
View file

@ -0,0 +1 @@
ERROR: line 6: key exists

4
stdex/tab07.toml Normal file
View file

@ -0,0 +1,4 @@
# VALID BUT DISCOURAGED
[fruit.apple]
[animal]
[fruit.orange]

10
stdex/tab07.toml.res Normal file
View file

@ -0,0 +1,10 @@
{
fruit = {
apple = {
},
orange = {
},
},
animal = {
},
}

8
stdex/tab08.toml Normal file
View file

@ -0,0 +1,8 @@
# Top-level table begins.
name = "Fido"
breed = "pug"
# Top-level table ends.
[owner]
name = "Regina Dogman"
member_since = 1999-08-04

8
stdex/tab08.toml.res Normal file
View file

@ -0,0 +1,8 @@
{
name = "Fido",
breed = "pug",
owner = {
name = "Regina Dogman",
member_since = 1999-08-04,
},
}

7
stdex/tab09.toml Normal file
View file

@ -0,0 +1,7 @@
fruit.apple.color = "red"
# Defines a table named fruit
# Defines a table named fruit.apple
fruit.apple.taste.sweet = true
# Defines a table named fruit.apple.taste
# fruit and fruit.apple were already created

10
stdex/tab09.toml.res Normal file
View file

@ -0,0 +1,10 @@
{
fruit = {
apple = {
color = "red",
taste = {
sweet = true,
},
},
},
}

5
stdex/tab10.toml Normal file
View file

@ -0,0 +1,5 @@
[fruit]
apple.color = "red"
apple.taste.sweet = true
[fruit.apple] # INVALID

1
stdex/tab10.toml.res Normal file
View file

@ -0,0 +1 @@
ERROR: line 5: key exists

9
stdex/tab11.toml Normal file
View file

@ -0,0 +1,9 @@
[fruit]
apple.color = "red"
apple.taste.sweet = true
# [fruit.apple] # INVALID
# [fruit.apple.taste] # INVALID
[fruit.apple.texture] # you can add sub-tables
smooth = true

13
stdex/tab11.toml.res Normal file
View file

@ -0,0 +1,13 @@
{
fruit = {
apple = {
color = "red",
taste = {
sweet = true,
},
texture = {
smooth = true,
},
},
},
}

20
toml.c
View file

@ -290,6 +290,7 @@ struct toml_array_t {
struct toml_table_t { struct toml_table_t {
const char* key; /* key to this table */ const char* key; /* key to this table */
bool implicit; /* table was created implicitly */ bool implicit; /* table was created implicitly */
bool readonly; /* no more modification allowed */
/* key-values in the table */ /* key-values in the table */
int nkval; int nkval;
@ -391,6 +392,12 @@ static int e_keyexists(context_t* ctx, int lineno)
return -1; return -1;
} }
static int e_forbid(context_t* ctx, int lineno, const char* msg)
{
snprintf(ctx->errbuf, ctx->errbufsz, "line %d: %s", lineno, msg);
return -1;
}
static void* expand(void* p, int sz, int newsz) static void* expand(void* p, int sz, int newsz)
{ {
void* s = MALLOC(newsz); void* s = MALLOC(newsz);
@ -926,7 +933,7 @@ static inline int eat_token(context_t* ctx, tokentype_t typ, int isdotspecial, c
/* We are at '{ ... }'. /* We are at '{ ... }'.
* Parse the table. * Parse the table.
*/ */
static int parse_table(context_t* ctx, toml_table_t* tab) static int parse_inline_table(context_t* ctx, toml_table_t* tab)
{ {
if (eat_token(ctx, LBRACE, 1, FLINE)) if (eat_token(ctx, LBRACE, 1, FLINE))
return -1; return -1;
@ -959,6 +966,9 @@ static int parse_table(context_t* ctx, toml_table_t* tab)
if (eat_token(ctx, RBRACE, 1, FLINE)) if (eat_token(ctx, RBRACE, 1, FLINE))
return -1; return -1;
tab->readonly = 1;
return 0; return 0;
} }
@ -1045,7 +1055,7 @@ static int parse_array(context_t* ctx, toml_array_t* arr)
toml_table_t* subtab = create_table_in_array(ctx, arr); toml_table_t* subtab = create_table_in_array(ctx, arr);
if (!subtab) return -1; if (!subtab) return -1;
if (parse_table(ctx, subtab)) return -1; if (parse_inline_table(ctx, subtab)) return -1;
break; break;
} }
@ -1075,6 +1085,10 @@ static int parse_array(context_t* ctx, toml_array_t* arr)
*/ */
static int parse_keyval(context_t* ctx, toml_table_t* tab) static int parse_keyval(context_t* ctx, toml_table_t* tab)
{ {
if (tab->readonly) {
return e_forbid(ctx, ctx->tok.lineno, "cannot insert new entry into existing table");
}
token_t key = ctx->tok; token_t key = ctx->tok;
if (eat_token(ctx, STRING, 1, FLINE)) return -1; if (eat_token(ctx, STRING, 1, FLINE)) return -1;
@ -1135,7 +1149,7 @@ static int parse_keyval(context_t* ctx, toml_table_t* tab)
{ /* key = { table } */ { /* key = { table } */
toml_table_t* nxttab = create_keytable_in_table(ctx, tab, key); toml_table_t* nxttab = create_keytable_in_table(ctx, tab, key);
if (!nxttab) return -1; if (!nxttab) return -1;
if (parse_table(ctx, nxttab)) return -1; if (parse_inline_table(ctx, nxttab)) return -1;
return 0; return 0;
} }