8#ifndef BOTAN_ALGORITHM_SPEC_H_
9#define BOTAN_ALGORITHM_SPEC_H_
11#include <botan/assert.h>
12#include <botan/types.h>
67 const char*
what() const noexcept final {
return m_msg; }
73 enum class Type : uint8_t {
98 std::string_view
to_string()
const {
return m_text; }
103 bool any_head()
const {
return m_head_count == 0; }
105 std::span<const std::string_view>
head_alternatives()
const {
return std::span(m_heads).first(m_head_count); }
107 std::span<const Element>
elements()
const {
return std::span(m_elements).first(m_element_count); }
112 constexpr void parse_pattern(std::string_view p);
113 constexpr void parse_elements(std::string_view p,
size_t& pos, uint8_t depth);
114 constexpr void parse_placeholder(std::string_view p,
size_t& pos, Element& e)
const;
116 static constexpr bool is_token_char(
char c) {
117 return c !=
'(' && c !=
')' && c !=
'{' && c !=
'}' && c !=
'[' && c !=
']' && c !=
',' && c !=
'|' &&
121 template <
typename Pred>
122 static constexpr std::string_view read_while(std::string_view p,
size_t& pos, Pred pred) {
123 const size_t start = pos;
124 while(pos < p.size() && pred(p[pos])) {
127 return p.substr(start, pos - start);
130 std::string_view m_text;
131 std::array<std::string_view, MaxHeadAlternatives> m_heads{};
133 size_t m_head_count = 0;
134 std::array<Element, MaxElements> m_elements{};
135 size_t m_element_count = 0;
161 explicit AlgorithmSpec(std::string_view spec, Syntax syntax = Syntax::Standard);
171 std::string_view
head()
const {
return m_head; }
175 std::span<const AlgorithmSpec>
args()
const {
return m_args; }
201 std::optional<AlgorithmMatch> match(
const AlgorithmPattern& pattern)
const&;
214 static AlgorithmSpec parse_name(std::string_view
text,
size_t& pos,
size_t depth,
bool mode_syntax);
216 std::string text_with_head(std::string_view
head)
const;
218 bool match_args(std::span<const AlgorithmPattern::Element> elems,
225 std::vector<AlgorithmSpec> m_args;
240 std::string_view
head()
const {
return m_head; }
245 bool has(std::string_view var)
const;
250 std::string_view str(std::string_view var)
const;
255 size_t integer(std::string_view var)
const;
265 std::span<const AlgorithmSpec> rest(std::string_view var)
const;
271 std::string_view var;
273 bool variadic =
false;
275 std::string_view
text;
278 std::span<const AlgorithmSpec>
rest;
283 Binding& declared(std::string_view var);
284 const Binding& bound(std::string_view var,
const char* accessor)
const;
286 std::string_view m_pattern;
287 std::string_view m_head;
288 std::array<Binding, AlgorithmPattern::MaxElements> m_bindings{};
289 size_t m_binding_count = 0;
293 return match(pattern).has_value();
299 parse_pattern(pattern);
302constexpr void AlgorithmPattern::parse_placeholder(std::string_view p,
size_t& pos, Element& e)
const {
303 const auto is_ident_char = [](
char c) {
304 return (c >=
'a' && c <=
'z') || (c >=
'A' && c <=
'Z') || (c >=
'0' && c <=
'9') || c ==
'_';
307 const auto is_canonical_decimal = [](std::string_view s) {
309 if(s.empty() || s.size() > 10) {
313 if(s.size() > 1 && s[0] ==
'0') {
316 for(
const char c : s) {
317 if(c <
'0' || c >
'9') {
327 e.placeholder =
true;
328 e.text = read_while(p, pos, is_ident_char);
333 if(pos < p.size() && p[pos] ==
':') {
335 const auto type = read_while(p, pos, is_ident_char);
338 }
else if(type ==
"int") {
340 }
else if(type ==
"str") {
347 if(pos < p.size() && p[pos] ==
'=') {
349 const size_t start = pos;
350 while(pos < p.size() && p[pos] !=
'}') {
353 e.default_value = p.substr(start, pos - start);
355 e.has_default =
true;
360 if(e.type ==
Type::Int && !is_canonical_decimal(e.default_value)) {
363 }
else if(pos < p.size() && p[pos] ==
'?') {
368 if(pos >= p.size() || p[pos] !=
'}') {
374constexpr void AlgorithmPattern::parse_elements(std::string_view p,
size_t& pos, uint8_t depth) {
376 bool seen_optional =
false;
377 bool seen_variadic =
false;
380 if(pos >= p.size()) {
395 parse_placeholder(p, pos, e);
396 if(p.substr(pos, 3) ==
"...") {
405 seen_variadic =
true;
407 }
else if(p[pos] ==
'[') {
409 e.text = read_while(p, pos, is_token_char);
413 if(pos >= p.size() || p[pos] !=
']') {
419 e.text = read_while(p, pos, is_token_char);
423 if(pos < p.size() && p[pos] ==
'(') {
427 if(
static_cast<size_t>(depth) + 1 >=
MaxElements) {
430 m_elements[m_element_count++] = e;
433 parse_elements(p, pos,
static_cast<uint8_t
>(depth + 1));
438 seen_optional =
true;
439 }
else if(seen_optional) {
444 m_elements[m_element_count++] = e;
447 if(pos >= p.size()) {
462constexpr void AlgorithmPattern::parse_pattern(std::string_view p) {
471 if(pos < p.size() && p[pos] ==
'|') {
479 const auto tok = read_while(p, pos, is_token_char);
483 m_heads[m_head_count++] = tok;
484 if(pos < p.size() && p[pos] ==
'|') {
497 parse_elements(p, pos, 0);
498 if(pos != p.size()) {
504 for(
size_t i = 0; i != m_element_count; ++i) {
505 if(!m_elements[i].placeholder) {
508 for(
size_t j = 0; j != i; ++j) {
509 if(m_elements[j].placeholder && m_elements[j].
text == m_elements[i].
text) {
#define BOTAN_ASSERT_NOMSG(expr)
std::span< const AlgorithmSpec > rest(std::string_view var) const
friend class AlgorithmSpec
size_t integer(std::string_view var) const
std::string_view head() const
InvalidAlgorithmPattern(const char *msg)
const char * what() const noexcept final
std::string_view to_string() const
std::span< const Element > elements() const
consteval AlgorithmPattern(const char(&pattern)[N])
static constexpr size_t MaxElements
static constexpr size_t MaxHeadAlternatives
std::span< const std::string_view > head_alternatives() const
std::span< const AlgorithmSpec > args() const
std::string canonical_form() const
std::optional< AlgorithmMatch > match(const AlgorithmPattern &pattern) const &&=delete
AlgorithmSpec with_head(std::string_view head) const
bool matches(const AlgorithmPattern &pattern) const
std::string_view to_string() const
std::optional< AlgorithmMatch > match(const AlgorithmPattern &pattern) const &
std::string_view head() const
AlgorithmSpec(std::string_view spec, Syntax syntax=Syntax::Standard)
std::string_view default_value