diff --git a/src/hammer.h b/src/hammer.h index 62b5676610415b0a03a9546cea7b829b3477d658..3d21c7b85ab709e5a392ec55f9b8f2e42897cb1f 100644 --- a/src/hammer.h +++ b/src/hammer.h @@ -224,6 +224,13 @@ const HParser* h_whitespace(const HParser* p); */ const HParser* h_action(const HParser* p, const HAction a); +/** + * Parse a single character in the given charset. + * + * Result token type: TT_UINT + */ +const HParser* h_in(const uint8_t *charset, size_t length); + /** * Parse a single character *NOT* in the given charset. * diff --git a/src/parsers/charset.c b/src/parsers/charset.c index 6420af501a2b0058731f1d5bd6c6678620378d46..b9642fccd0aef2394f64de70fed56e911a918ad9 100644 --- a/src/parsers/charset.c +++ b/src/parsers/charset.c @@ -28,16 +28,24 @@ const HParser* h_ch_range(const uint8_t lower, const uint8_t upper) { } -const HParser* h_not_in(const uint8_t *options, size_t count) { +const HParser* h_in_or_not(const uint8_t *options, size_t count, int val) { HParser *ret = g_new(HParser, 1); HCharset cs = new_charset(); for (size_t i = 0; i < 256; i++) - charset_set(cs, i, 1); + charset_set(cs, i, 1-val); for (size_t i = 0; i < count; i++) - charset_set(cs, options[i], 0); + charset_set(cs, options[i], val); ret->vtable = &charset_vt; ret->env = (void*)cs; return (const HParser*)ret; } +const HParser* h_in(const uint8_t *options, size_t count) { + return h_in_or_not(options, count, 1); +} + +const HParser* h_not_in(const uint8_t *options, size_t count) { + return h_in_or_not(options, count, 0); +} +