summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJo-Philipp Wich <jo@mein.io>2020-10-05 12:32:42 +0200
committerJo-Philipp Wich <jo@mein.io>2020-10-05 13:03:08 +0200
commitb1391241a116d794a091c97bbb63b485f6514fb7 (patch)
tree8a95edba3ea58aacbb0db0a76f8131b387660544
parent03b9ad307bd4313adc974982fc008f4c58337c11 (diff)
ast: add helpers to set error token bits
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
-rw-r--r--ast.h13
-rw-r--r--lib.c4
-rw-r--r--parser.y2
3 files changed, 16 insertions, 3 deletions
diff --git a/ast.h b/ast.h
index 67330fb..365d4c0 100644
--- a/ast.h
+++ b/ast.h
@@ -140,6 +140,19 @@ static inline bool ut_is_type(struct json_object *val, int type) {
return (tag && tag->type == type);
};
+
+#define UT_ET_DIV (sizeof(s->error.info.tokens[0]) * 8)
+#define UT_ET_TYPE typeof(s->error.info.tokens[0])
+
+static inline void ut_set_error_token(struct ut_state *s, int tokennr) {
+ s->error.info.tokens[tokennr / UT_ET_DIV] |= ((UT_ET_TYPE)1 << (tokennr % UT_ET_DIV));
+}
+
+static inline bool ut_is_error_token(struct ut_state *s, int tokennr) {
+ return (s->error.info.tokens[tokennr / UT_ET_DIV] & ((UT_ET_TYPE)1 << (tokennr % UT_ET_DIV)));
+}
+
+
uint32_t ut_new_op(struct ut_state *s, int type, struct json_object *val, ...);
uint32_t ut_wrap_op(struct ut_state *s, uint32_t parent, ...);
uint32_t ut_append_op(struct ut_state *s, uint32_t a, uint32_t b);
diff --git a/lib.c b/lib.c
index 603deb8..c46745e 100644
--- a/lib.c
+++ b/lib.c
@@ -177,11 +177,11 @@ ut_format_error(struct ut_state *state, const char *expr)
sprintf_append(&msg, &msglen, "Syntax error: Unexpected token\n");
for (i = 0, max_i = 0; i < sizeof(state->error.info.tokens) * 8; i++)
- if ((state->error.info.tokens[i / 64] & ((unsigned)1 << (i % 64))) && tokennames[i])
+ if (ut_is_error_token(state, i) && tokennames[i])
max_i = i;
for (i = 0; i < sizeof(state->error.info.tokens) * 8; i++) {
- if ((state->error.info.tokens[i / 64] & ((unsigned)1 << (i % 64))) && tokennames[i]) {
+ if (ut_is_error_token(state, i) && tokennames[i]) {
if (first) {
sprintf_append(&msg, &msglen, "Expecting %s", tokennames[i]);
first = false;
diff --git a/parser.y b/parser.y
index aac728a..11bf3b6 100644
--- a/parser.y
+++ b/parser.y
@@ -90,7 +90,7 @@ ut_no_empty_obj(struct ut_state *s, uint32_t off)
for (i = 0; i < sizeof(tokennames) / sizeof(tokennames[0]); i++)
if (yy_find_shift_action(yypParser, (YYCODETYPE)i) < YYNSTATE + YYNRULE)
- s->error.info.tokens[i / 64] |= ((unsigned)1 << (i % 64));
+ ut_set_error_token(s, i);
}