mirror of
https://github.com/matrix-construct/construct
synced 2024-06-28 14:58:20 +02:00
ircd::json: Add suite for type optimized type query; refactor grammars.
This commit is contained in:
parent
b137f59c5e
commit
31dfedbcd7
|
@ -23,11 +23,23 @@ namespace ircd::json
|
|||
struct iov;
|
||||
using members = std::initializer_list<const member>;
|
||||
|
||||
/// strict_t overloads scan the whole string to determine both the type
|
||||
/// and validity of the string. For large strings this may involve a lot
|
||||
/// of memory operations; if the validity is known good it is better to
|
||||
/// not use the strict overload.
|
||||
IRCD_OVERLOAD(strict)
|
||||
|
||||
// Determine the type
|
||||
enum type type(const string_view &);
|
||||
enum type type(const string_view &, std::nothrow_t);
|
||||
enum type type(const string_view &, strict_t);
|
||||
enum type type(const string_view &, strict_t, std::nothrow_t);
|
||||
|
||||
// Query if type
|
||||
bool type(const string_view &, const enum type &, strict_t);
|
||||
bool type(const string_view &, const enum type &);
|
||||
|
||||
// Utils
|
||||
string_view reflect(const enum type &);
|
||||
|
||||
extern const string_view literal_null;
|
||||
|
|
74
ircd/json.cc
74
ircd/json.cc
|
@ -4193,35 +4193,60 @@ ircd::json::serialized(const string_view &v)
|
|||
|
||||
namespace ircd::json
|
||||
{
|
||||
using type_rule = parser::rule<enum json::type>;
|
||||
[[gnu::visibility("internal")]]
|
||||
extern const parser::rule<>
|
||||
type_parse_is[5],
|
||||
type_parse_is_strict[5];
|
||||
|
||||
[[gnu::visibility("internal")]]
|
||||
extern const parser::rule<enum json::type>;
|
||||
extern const parser::rule<enum json::type>
|
||||
type_parse,
|
||||
type_parse_strict,
|
||||
type_parse_parse,
|
||||
type_parse_strict_parse;
|
||||
}
|
||||
|
||||
//TODO: XXX array designated initializers
|
||||
decltype(ircd::json::type_parse_is)
|
||||
ircd::json::type_parse_is
|
||||
{
|
||||
{ -parser.ws >> parser.quote },
|
||||
{ -parser.ws >> parser.object_begin },
|
||||
{ -parser.ws >> parser.array_begin },
|
||||
{ -parser.ws >> parser.number >> eoi },
|
||||
{ -parser.ws >> parser.literal >> eoi },
|
||||
};
|
||||
|
||||
//TODO: XXX array designated initializers
|
||||
decltype(ircd::json::type_parse_is_strict)
|
||||
ircd::json::type_parse_is_strict
|
||||
{
|
||||
{ -parser.ws >> &parser.quote >> parser.string },
|
||||
{ -parser.ws >> &parser.object_begin >> parser.object(0) },
|
||||
{ -parser.ws >> &parser.array_begin >> parser.array(0) },
|
||||
{ -parser.ws >> parser.number >> eoi },
|
||||
{ -parser.ws >> parser.literal >> eoi },
|
||||
};
|
||||
|
||||
decltype(ircd::json::type_parse)
|
||||
ircd::json::type_parse
|
||||
{
|
||||
(omit[parser.quote] >> attr(json::STRING)) |
|
||||
(omit[parse.object_begin] >> attr(json::OBJECT)) |
|
||||
(omit[parse.array_begin] >> attr(json::ARRAY)) |
|
||||
(omit[parse.number >> eoi] >> attr(json::NUMBER)) |
|
||||
(omit[parser.literal >> eoi] >> attr(json::LITERAL))
|
||||
(omit[type_parse_is[json::STRING]] >> attr(json::STRING)) |
|
||||
(omit[type_parse_is[json::OBJECT]] >> attr(json::OBJECT)) |
|
||||
(omit[type_parse_is[json::ARRAY]] >> attr(json::ARRAY)) |
|
||||
(omit[type_parse_is[json::NUMBER]] >> attr(json::NUMBER)) |
|
||||
(omit[type_parse_is[json::LITERAL]] >> attr(json::LITERAL))
|
||||
,"type check"
|
||||
};
|
||||
|
||||
decltype(ircd::json::type_parse_strict)
|
||||
ircd::json::type_parse_strict
|
||||
{
|
||||
(omit[&parser.quote >> parser.string] >> attr(json::STRING)) |
|
||||
(omit[&parser.object_begin >> parser.object(0)] >> attr(json::OBJECT)) |
|
||||
(omit[&parser.array_begin >> parser.array(0)] >> attr(json::ARRAY)) |
|
||||
(omit[parser.number] >> attr(json::NUMBER)) |
|
||||
(omit[parserliteral] >> attr(json::LITERAL))
|
||||
(omit[type_parse_is_strict[json::STRING]] >> attr(json::STRING)) |
|
||||
(omit[type_parse_is_strict[json::OBJECT]] >> attr(json::OBJECT)) |
|
||||
(omit[type_parse_is_strict[json::ARRAY]] >> attr(json::ARRAY)) |
|
||||
(omit[type_parse_is_strict[json::NUMBER]] >> attr(json::NUMBER)) |
|
||||
(omit[type_parse_is_strict[json::LITERAL]] >> attr(json::LITERAL))
|
||||
,"type check strict"
|
||||
};
|
||||
|
||||
|
@ -4239,6 +4264,31 @@ ircd::json::type_parse_strict_parse
|
|||
,"type check strict"
|
||||
};
|
||||
|
||||
bool
|
||||
ircd::json::type(const string_view &buf,
|
||||
const enum type &type)
|
||||
{
|
||||
const bool ret
|
||||
{
|
||||
qi::parse(begin(buf), end(buf), type_parse_is[type])
|
||||
};
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool
|
||||
ircd::json::type(const string_view &buf,
|
||||
const enum type &type,
|
||||
strict_t)
|
||||
{
|
||||
const bool ret
|
||||
{
|
||||
qi::parse(begin(buf), end(buf), type_parse_is_strict[type])
|
||||
};
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
enum ircd::json::type
|
||||
ircd::json::type(const string_view &buf,
|
||||
strict_t)
|
||||
|
|
Loading…
Reference in a new issue