From 6818b362a776f0cc5a6068ed119dc2ebcbc5a9cc Mon Sep 17 00:00:00 2001 From: Michael Smith Date: Thu, 24 Feb 2022 00:47:05 +0000 Subject: Fix some old KV parser issues - Implement conditionals in the lexer and reject or ignore them in callbacks. This will allow something to use them later if needed. - Make error handling less stupid (return a bool instead of using the state struct). --- src/kv.h | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) (limited to 'src/kv.h') diff --git a/src/kv.h b/src/kv.h index 4ed459b..44dc896 100644 --- a/src/kv.h +++ b/src/kv.h @@ -34,8 +34,8 @@ */ struct kv_parser { ushort line, col; /* the current line and column in the text */ - schar state; /* internal, shouldn't usually be touched directly */ - bool incomment; /* internal */ + char state : 7; /* internal, shouldn't usually be touched directly */ + bool incomment : 1; /* internal */ ushort nestlvl; /* internal */ const char *errmsg; /* the error message, *IF* parsing just failed */ @@ -46,8 +46,6 @@ struct kv_parser { char tokbuf[KV_TOKEN_MAX]; }; -#define KV_PARSER_ERROR -1 - /* * These are the tokens that can be received by a kv_parser_cb (below). * The x-macro and string descriptions are given to allow for easy debug @@ -61,6 +59,8 @@ struct kv_parser { X(KV_IDENT_QUOTED, "quoted-ident") \ X(KV_VAL, "value") \ X(KV_VAL_QUOTED, "quoted-value") \ + X(KV_COND_PREFIX, "cond-prefix") \ + X(KV_COND_SUFFIX, "cond-suffix") \ X(KV_NEST_START, "object-start") \ X(KV_NEST_END, "object-end") @@ -76,20 +76,21 @@ typedef void (*kv_parser_cb)(enum kv_token type, const char *p, uint len, * read in from a file. * * The lexer is reentrant and can be fed arbitrarily sized blocks of data at a - * time. The function may return early in the event of an error; you must check - * if parser->state == KV_PARSER_ERROR between calls! Continuing to try parsing - * after an error is undefined. + * time. The function may return early in the event of an error; a return value + * of false indicates thaat this has happened, otherwise true is returned. + * + * In the event of an error, the errmsg, line and col fields of the parser + * struct can be used for diagnostics. */ -// FIXME: revise API usage so errors aren't passed through "state" value -void kv_parser_feed(struct kv_parser *this, const char *in, uint sz, +bool kv_parser_feed(struct kv_parser *this, const char *in, uint sz, kv_parser_cb cb, void *ctxt); /* - * This indicates that parsing is done; if the state is midway through a token - * this will be converted into an error state which can be checked in the same - * way as noted above. + * This indicates that parsing is done; if this is called at an unexpected time, + * a parsing error will result; this is indicated in the return value as with + * kv_parser_feed. */ -void kv_parser_done(struct kv_parser *this); +bool kv_parser_done(struct kv_parser *this); #endif -- cgit v1.2.3