From dbc1ef26d603e98dbdbd39cc93d80cf60534b2ce Mon Sep 17 00:00:00 2001 From: CK Tan Date: Sun, 7 Mar 2021 17:28:41 -0800 Subject: [PATCH] more tests --- stdex/inlinetab1.toml | 3 +++ stdex/inlinetab1.toml.res | 15 +++++++++++++++ stdex/inlinetab2.toml | 3 +++ stdex/inlinetab2.toml.res | 1 + stdex/inlinetab3.toml | 3 +++ stdex/inlinetab3.toml.res | 1 + stdex/tab01.toml | 7 +++++++ stdex/tab01.toml.res | 10 ++++++++++ stdex/tab02.toml | 2 ++ stdex/tab02.toml.res | 9 +++++++++ stdex/tab03.toml | 4 ++++ stdex/tab03.toml.res | 26 ++++++++++++++++++++++++++ stdex/tab04.toml | 6 ++++++ stdex/tab04.toml.res | 10 ++++++++++ stdex/tab05.toml | 7 +++++++ stdex/tab05.toml.res | 1 + stdex/tab06.toml | 7 +++++++ stdex/tab06.toml.res | 1 + stdex/tab07.toml | 4 ++++ stdex/tab07.toml.res | 10 ++++++++++ stdex/tab08.toml | 8 ++++++++ stdex/tab08.toml.res | 8 ++++++++ stdex/tab09.toml | 7 +++++++ stdex/tab09.toml.res | 10 ++++++++++ stdex/tab10.toml | 5 +++++ stdex/tab10.toml.res | 1 + stdex/tab11.toml | 9 +++++++++ stdex/tab11.toml.res | 13 +++++++++++++ toml.c | 22 ++++++++++++++++++---- 29 files changed, 209 insertions(+), 4 deletions(-) create mode 100644 stdex/inlinetab1.toml create mode 100644 stdex/inlinetab1.toml.res create mode 100644 stdex/inlinetab2.toml create mode 100644 stdex/inlinetab2.toml.res create mode 100644 stdex/inlinetab3.toml create mode 100644 stdex/inlinetab3.toml.res create mode 100644 stdex/tab01.toml create mode 100644 stdex/tab01.toml.res create mode 100644 stdex/tab02.toml create mode 100644 stdex/tab02.toml.res create mode 100644 stdex/tab03.toml create mode 100644 stdex/tab03.toml.res create mode 100644 stdex/tab04.toml create mode 100644 stdex/tab04.toml.res create mode 100644 stdex/tab05.toml create mode 100644 stdex/tab05.toml.res create mode 100644 stdex/tab06.toml create mode 100644 stdex/tab06.toml.res create mode 100644 stdex/tab07.toml create mode 100644 stdex/tab07.toml.res create mode 100644 stdex/tab08.toml create mode 100644 stdex/tab08.toml.res create mode 100644 stdex/tab09.toml create mode 100644 stdex/tab09.toml.res create mode 100644 stdex/tab10.toml create mode 100644 stdex/tab10.toml.res create mode 100644 stdex/tab11.toml create mode 100644 stdex/tab11.toml.res diff --git a/stdex/inlinetab1.toml b/stdex/inlinetab1.toml new file mode 100644 index 0000000..26062b5 --- /dev/null +++ b/stdex/inlinetab1.toml @@ -0,0 +1,3 @@ +name = { first = "Tom", last = "Preston-Werner" } +point = { x = 1, y = 2 } +animal = { type.name = "pug" } diff --git a/stdex/inlinetab1.toml.res b/stdex/inlinetab1.toml.res new file mode 100644 index 0000000..7553b02 --- /dev/null +++ b/stdex/inlinetab1.toml.res @@ -0,0 +1,15 @@ +{ + name = { + first = "Tom", + last = "Preston-Werner", + }, + point = { + x = 1, + y = 2, + }, + animal = { + type = { + name = "pug", + }, + }, +} diff --git a/stdex/inlinetab2.toml b/stdex/inlinetab2.toml new file mode 100644 index 0000000..7689e14 --- /dev/null +++ b/stdex/inlinetab2.toml @@ -0,0 +1,3 @@ +[product] +type = { name = "Nail" } +type.edible = false # INVALID diff --git a/stdex/inlinetab2.toml.res b/stdex/inlinetab2.toml.res new file mode 100644 index 0000000..9684a3d --- /dev/null +++ b/stdex/inlinetab2.toml.res @@ -0,0 +1 @@ +ERROR: line 3: cannot insert new entry into existing table diff --git a/stdex/inlinetab3.toml b/stdex/inlinetab3.toml new file mode 100644 index 0000000..d6937eb --- /dev/null +++ b/stdex/inlinetab3.toml @@ -0,0 +1,3 @@ +[product] +type.name = "Nail" +type = { edible = false } # INVALID diff --git a/stdex/inlinetab3.toml.res b/stdex/inlinetab3.toml.res new file mode 100644 index 0000000..0a6a07c --- /dev/null +++ b/stdex/inlinetab3.toml.res @@ -0,0 +1 @@ +ERROR: line 3: key exists diff --git a/stdex/tab01.toml b/stdex/tab01.toml new file mode 100644 index 0000000..6b4884c --- /dev/null +++ b/stdex/tab01.toml @@ -0,0 +1,7 @@ +[table-1] +key1 = "some string" +key2 = 123 + +[table-2] +key1 = "another string" +key2 = 456 diff --git a/stdex/tab01.toml.res b/stdex/tab01.toml.res new file mode 100644 index 0000000..0418849 --- /dev/null +++ b/stdex/tab01.toml.res @@ -0,0 +1,10 @@ +{ + table-1 = { + key1 = "some string", + key2 = 123, + }, + table-2 = { + key1 = "another string", + key2 = 456, + }, +} diff --git a/stdex/tab02.toml b/stdex/tab02.toml new file mode 100644 index 0000000..32cbe79 --- /dev/null +++ b/stdex/tab02.toml @@ -0,0 +1,2 @@ +[dog."tater.man"] +type.name = "pug" diff --git a/stdex/tab02.toml.res b/stdex/tab02.toml.res new file mode 100644 index 0000000..82cfae1 --- /dev/null +++ b/stdex/tab02.toml.res @@ -0,0 +1,9 @@ +{ + dog = { + tater.man = { + type = { + name = "pug", + }, + }, + }, +} diff --git a/stdex/tab03.toml b/stdex/tab03.toml new file mode 100644 index 0000000..0b10db3 --- /dev/null +++ b/stdex/tab03.toml @@ -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'] diff --git a/stdex/tab03.toml.res b/stdex/tab03.toml.res new file mode 100644 index 0000000..983d6cb --- /dev/null +++ b/stdex/tab03.toml.res @@ -0,0 +1,26 @@ +{ + a = { + b = { + c = { + }, + }, + }, + d = { + e = { + f = { + }, + }, + }, + g = { + h = { + i = { + }, + }, + }, + j = { + ʞ = { + l = { + }, + }, + }, +} diff --git a/stdex/tab04.toml b/stdex/tab04.toml new file mode 100644 index 0000000..256e6c0 --- /dev/null +++ b/stdex/tab04.toml @@ -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 diff --git a/stdex/tab04.toml.res b/stdex/tab04.toml.res new file mode 100644 index 0000000..718d94a --- /dev/null +++ b/stdex/tab04.toml.res @@ -0,0 +1,10 @@ +{ + x = { + y = { + z = { + w = { + }, + }, + }, + }, +} diff --git a/stdex/tab05.toml b/stdex/tab05.toml new file mode 100644 index 0000000..0704ba7 --- /dev/null +++ b/stdex/tab05.toml @@ -0,0 +1,7 @@ +# DO NOT DO THIS + +[fruit] +apple = "red" + +[fruit] +orange = "orange" diff --git a/stdex/tab05.toml.res b/stdex/tab05.toml.res new file mode 100644 index 0000000..b57e247 --- /dev/null +++ b/stdex/tab05.toml.res @@ -0,0 +1 @@ +ERROR: line 6: key exists diff --git a/stdex/tab06.toml b/stdex/tab06.toml new file mode 100644 index 0000000..e58bf8e --- /dev/null +++ b/stdex/tab06.toml @@ -0,0 +1,7 @@ +# DO NOT DO THIS EITHER + +[fruit] +apple = "red" + +[fruit.apple] +texture = "smooth" diff --git a/stdex/tab06.toml.res b/stdex/tab06.toml.res new file mode 100644 index 0000000..b57e247 --- /dev/null +++ b/stdex/tab06.toml.res @@ -0,0 +1 @@ +ERROR: line 6: key exists diff --git a/stdex/tab07.toml b/stdex/tab07.toml new file mode 100644 index 0000000..03fe3a3 --- /dev/null +++ b/stdex/tab07.toml @@ -0,0 +1,4 @@ +# VALID BUT DISCOURAGED +[fruit.apple] +[animal] +[fruit.orange] diff --git a/stdex/tab07.toml.res b/stdex/tab07.toml.res new file mode 100644 index 0000000..239ced9 --- /dev/null +++ b/stdex/tab07.toml.res @@ -0,0 +1,10 @@ +{ + fruit = { + apple = { + }, + orange = { + }, + }, + animal = { + }, +} diff --git a/stdex/tab08.toml b/stdex/tab08.toml new file mode 100644 index 0000000..c57a229 --- /dev/null +++ b/stdex/tab08.toml @@ -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 diff --git a/stdex/tab08.toml.res b/stdex/tab08.toml.res new file mode 100644 index 0000000..64371ae --- /dev/null +++ b/stdex/tab08.toml.res @@ -0,0 +1,8 @@ +{ + name = "Fido", + breed = "pug", + owner = { + name = "Regina Dogman", + member_since = 1999-08-04, + }, +} diff --git a/stdex/tab09.toml b/stdex/tab09.toml new file mode 100644 index 0000000..d5a2c11 --- /dev/null +++ b/stdex/tab09.toml @@ -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 diff --git a/stdex/tab09.toml.res b/stdex/tab09.toml.res new file mode 100644 index 0000000..9346469 --- /dev/null +++ b/stdex/tab09.toml.res @@ -0,0 +1,10 @@ +{ + fruit = { + apple = { + color = "red", + taste = { + sweet = true, + }, + }, + }, +} diff --git a/stdex/tab10.toml b/stdex/tab10.toml new file mode 100644 index 0000000..9eac5f6 --- /dev/null +++ b/stdex/tab10.toml @@ -0,0 +1,5 @@ +[fruit] +apple.color = "red" +apple.taste.sweet = true + +[fruit.apple] # INVALID diff --git a/stdex/tab10.toml.res b/stdex/tab10.toml.res new file mode 100644 index 0000000..eb9b163 --- /dev/null +++ b/stdex/tab10.toml.res @@ -0,0 +1 @@ +ERROR: line 5: key exists diff --git a/stdex/tab11.toml b/stdex/tab11.toml new file mode 100644 index 0000000..ae2d65b --- /dev/null +++ b/stdex/tab11.toml @@ -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 diff --git a/stdex/tab11.toml.res b/stdex/tab11.toml.res new file mode 100644 index 0000000..914481b --- /dev/null +++ b/stdex/tab11.toml.res @@ -0,0 +1,13 @@ +{ + fruit = { + apple = { + color = "red", + taste = { + sweet = true, + }, + texture = { + smooth = true, + }, + }, + }, +} diff --git a/toml.c b/toml.c index df93354..e89bade 100644 --- a/toml.c +++ b/toml.c @@ -288,8 +288,9 @@ struct toml_array_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 readonly; /* no more modification allowed */ /* key-values in the table */ int nkval; @@ -391,6 +392,12 @@ static int e_keyexists(context_t* ctx, int lineno) 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) { 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 '{ ... }'. * 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)) return -1; @@ -959,6 +966,9 @@ static int parse_table(context_t* ctx, toml_table_t* tab) if (eat_token(ctx, RBRACE, 1, FLINE)) return -1; + + tab->readonly = 1; + 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); if (!subtab) return -1; - if (parse_table(ctx, subtab)) return -1; + if (parse_inline_table(ctx, subtab)) return -1; 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) { + if (tab->readonly) { + return e_forbid(ctx, ctx->tok.lineno, "cannot insert new entry into existing table"); + } + token_t key = ctx->tok; 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 } */ toml_table_t* nxttab = create_keytable_in_table(ctx, tab, key); if (!nxttab) return -1; - if (parse_table(ctx, nxttab)) return -1; + if (parse_inline_table(ctx, nxttab)) return -1; return 0; }