Skip to content

Commit fee10e4

Browse files
author
BirdeeHub
committed
refactor(errors): 1 better error message, plus TODO: comments
1 parent ce728aa commit fee10e4

5 files changed

Lines changed: 23 additions & 9 deletions

File tree

README.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ Basic benchmarking shows promising results:
3232

3333
## Installation
3434

35-
Better installation instructions are on the roadmap.
36-
3735
### Using Make
3836

3937
```bash
@@ -55,8 +53,8 @@ luarocks install tomlua
5553

5654
### Using Nix
5755

58-
* Flake or flakeless support.
59-
* Overlay and packages available for Lua versions 5.1+.
56+
* Flake or flakeless support, both methods return according to the [flake outputs schema](https://wiki.nixos.org/wiki/Flakes).
57+
* Overlay and packages available for Lua versions 5.1+, as well as a neovim plugin.
6058
* Dev shell included for building via `make`.
6159
* Not yet on nixpkgs
6260

src/decode.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,14 @@ static inline bool set_kv(lua_State *L, int keys_len, int value_idx) {
3636
lua_pushvalue(L, -2); // copy so we can continue with it after rawset
3737
lua_rawset(L, parent_idx); // t[key] = new table
3838
} else if (vtype != LUA_TTABLE) {
39+
// TODO: print keys
3940
return set_tmlerr(new_tmlerr(L, DECODE_DEFINED_IDX), false, 18, "key is not a table");
4041
}
4142
lua_remove(L, parent_idx);
4243
// NOTE: We need to check if it was defined inline, because if it was defined by key in the same heading we can redefine but inline we can't
4344
lua_pushvalue(L, -1);
4445
lua_rawget(L, DECODE_DEFINED_IDX);
46+
// TODO: print keys
4547
if (lua_tointeger(L, -1) == -2) return set_tmlerr(new_tmlerr(L, DECODE_DEFINED_IDX), false, 36, "Keys may not redefine inline tables!");
4648
lua_pop(L, 1);
4749
lua_pushvalue(L, -1);
@@ -52,6 +54,7 @@ static inline bool set_kv(lua_State *L, int keys_len, int value_idx) {
5254
lua_pushvalue(L, -2);
5355
lua_rawget(L, -2);
5456
if (!lua_isnil(L, -1)) {
57+
// TODO: print keys
5558
return set_tmlerr(new_tmlerr(L, DECODE_DEFINED_IDX), false, 20, "key already defined!");
5659
}
5760
lua_pop(L, 1);
@@ -81,6 +84,7 @@ static inline bool heading_nav(lua_State *L, int keys_len, bool array_type) {
8184
lua_pushvalue(L, -2);
8285
lua_rawset(L, parent_idx); // t[key] = new table
8386
} else if (vtype != LUA_TTABLE) {
87+
// TODO: print keys
8488
return set_tmlerr(new_tmlerr(L, DECODE_DEFINED_IDX), false, 33, "cannot navigate through non-table");
8589
}
8690
lua_remove(L, parent_idx); // remove parent table, keep child on top
@@ -102,6 +106,7 @@ static inline bool heading_nav(lua_State *L, int keys_len, bool array_type) {
102106
lua_rawseti(L, parent_idx, len);
103107
lua_remove(L, parent_idx); // remove parent table, keep child on top
104108
} else if (len != 0) {
109+
// TODO: print keys
105110
return set_tmlerr(new_tmlerr(L, DECODE_DEFINED_IDX), false, 22, "table already defined!");
106111
} else {
107112
lua_pushvalue(L, -1);
@@ -112,6 +117,7 @@ static inline bool heading_nav(lua_State *L, int keys_len, bool array_type) {
112117
lua_rawgeti(L, -1, len);
113118
lua_remove(L, -2);
114119
if (!lua_istable(L, -1))
120+
// TODO: print keys
115121
return set_tmlerr(new_tmlerr(L, DECODE_DEFINED_IDX), false, 33, "cannot navigate through non-table");
116122
}
117123
}
@@ -163,11 +169,13 @@ int tomlua_decode(lua_State *L) {
163169
int keys_len = parse_keys(L, &src, &scratch, int_keys, DECODE_DEFINED_IDX);
164170
if (!keys_len) goto fail;
165171
if (!iter_starts_with(&src, "]]", 2)) {
172+
// TODO: print keys
166173
set_tmlerr(new_tmlerr(L, DECODE_DEFINED_IDX), false, 30, "table heading must end with ]]");
167174
goto fail;
168175
}
169176
iter_skip_n(&src, 2); // consume ]]
170177
if (!consume_whitespace_to_line(&src)) {
178+
// TODO: print keys
171179
set_tmlerr(new_tmlerr(L, DECODE_DEFINED_IDX), false, 56, "array [[headers]] must have a new line before new values");
172180
goto fail;
173181
}
@@ -178,11 +186,13 @@ int tomlua_decode(lua_State *L) {
178186
int keys_len = parse_keys(L, &src, &scratch, int_keys, DECODE_DEFINED_IDX);
179187
if (!keys_len) goto fail;
180188
if (iter_peek(&src).v != ']') {
189+
// TODO: print keys
181190
set_tmlerr(new_tmlerr(L, DECODE_DEFINED_IDX), false, 29, "table heading must end with ]");
182191
goto fail;
183192
}
184193
iter_skip(&src); // consume ]
185194
if (!consume_whitespace_to_line(&src)) {
195+
// TODO: print keys
186196
set_tmlerr(new_tmlerr(L, DECODE_DEFINED_IDX), false, 54, "table [headers] must have a new line before new values");
187197
goto fail;
188198
}
@@ -193,11 +203,13 @@ int tomlua_decode(lua_State *L) {
193203
int keys_len = parse_keys(L, &src, &scratch, int_keys, DECODE_DEFINED_IDX);
194204
if (!keys_len) goto fail;
195205
if (iter_peek(&src).v != '=') {
206+
// TODO: print keys
196207
set_tmlerr(new_tmlerr(L, DECODE_DEFINED_IDX), false, 35, "keys for assignment must end with =");
197208
goto fail;
198209
}
199210
iter_skip(&src); // consume =
200211
if (consume_whitespace_to_line(&src)) {
212+
// TODO: print keys
201213
set_tmlerr(new_tmlerr(L, DECODE_DEFINED_IDX), false, 76, "the value in key = value expressions must begin on the same line as the key!");
202214
goto fail;
203215
}
@@ -221,8 +233,9 @@ int tomlua_decode(lua_State *L) {
221233

222234
fail:
223235
lua_settop(L, DECODE_DEFINED_IDX);
224-
tmlerr_push_ctx_from_iter(get_err_val(L, DECODE_DEFINED_IDX), 7, &src);
225236
free_str_buf(&scratch);
237+
// TODO: figure out why this does not add context when pos == len
238+
tmlerr_push_ctx_from_iter(get_err_val(L, DECODE_DEFINED_IDX), 7, &src);
226239
lua_pushnil(L);
227240
push_tmlerr_string(L, get_err_val(L, DECODE_DEFINED_IDX));
228241
return 2;

src/decode_inline_value.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ static inline bool set_kv(lua_State *L, int keys_len, int value_idx, int erridx)
4646
lua_pushvalue(L, -2);
4747
lua_rawget(L, -2);
4848
if (!lua_isnil(L, -1)) {
49+
// TODO: print keys
4950
return set_tmlerr(new_tmlerr(L, erridx), false, 20, "key already defined!");
5051
}
5152
lua_pop(L, 1);

src/decode_str.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ static inline uint32_t hex_to_codepoint(const char src[], int len, int erridx) {
2626

2727
static inline bool push_unicode(lua_State *L, str_buf *dst, char src[], int len, int erridx) {
2828
for (size_t i = 0; i < len; i++) {
29-
if (!is_hex_char(src[i])) return set_tmlerr(new_tmlerr(L, erridx), false, 33, "unexpected unicode specifier char");
29+
if (!is_hex_char(src[i])) {
30+
tmlerr_push_fmt(new_tmlerr(L, erridx), "Unexpected unicode specifier character '%c'", src[i]);
31+
return false;
32+
}
3033
}
3134
uint32_t cp = hex_to_codepoint(src, len, erridx);
3235
if (!buf_push_utf8(dst, cp)) return set_tmlerr(new_tmlerr(L, erridx), false, 3, "OOM");

tests/scratch.lua

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,8 @@ print(tomlua.encode(val))
184184
do
185185
local errtoml = [=[
186186
[product]
187-
type = { name = "Nail" }
188-
type.edible = false
189-
]=]
187+
type.bleh =
188+
]=]
190189

191190
local d, e = tomlua.decode(errtoml)
192191
print(tomlua.encode(d), e)

0 commit comments

Comments
 (0)