8#include <botan/internal/algorithm_spec.h>
10#include <botan/assert.h>
11#include <botan/exceptn.h>
12#include <botan/internal/fmt.h>
13#include <botan/internal/parsing.h>
19constexpr size_t MaxNestingDepth = 16;
21bool is_spec_separator(
char c,
bool slash_separates) {
22 return c ==
'(' || c ==
')' || c ==
',' || (slash_separates && c ==
'/');
28AlgorithmSpec AlgorithmSpec::parse_name(std::string_view
text,
size_t& pos,
size_t depth,
bool mode_syntax) {
29 if(depth > MaxNestingDepth) {
30 throw Invalid_Algorithm_Name(
text);
34 const bool slash_separates = mode_syntax && (depth == 0);
35 const size_t start = pos;
37 while(pos <
text.size() && !is_spec_separator(
text[pos], slash_separates)) {
42 node.m_head = std::string(
text.substr(start, pos - start));
44 if(node.m_head.empty()) {
45 throw Invalid_Algorithm_Name(
text);
48 if(pos <
text.size() &&
text[pos] ==
'(') {
51 node.m_args.push_back(parse_name(
text, pos, depth + 1, mode_syntax));
52 }
while(pos <
text.size() &&
text[pos] ==
',');
54 if(pos >=
text.size() ||
text[pos] !=
')') {
55 throw Invalid_Algorithm_Name(
text);
60 node.m_text = std::string(
text.substr(start, pos - start));
72 if(spec.find_first_of(mode_syntax ?
"(),/" :
"(),") == std::string_view::npos) {
73 m_text = std::string(spec);
74 m_head = std::string(spec);
79 std::vector<AlgorithmSpec> parts;
80 parts.push_back(parse_name(spec, pos, 0, mode_syntax));
82 while(mode_syntax && pos < spec.size() && spec[pos] ==
'/') {
84 parts.push_back(parse_name(spec, pos, 0, mode_syntax));
87 if(pos != spec.size()) {
91 if(parts.size() == 1) {
92 m_head = std::move(parts[0].m_head);
93 m_args = std::move(parts[0].m_args);
97 m_head = std::move(mode.m_head);
98 m_args.push_back(std::move(parts[0]));
99 for(
auto& mode_arg : mode.m_args) {
100 m_args.push_back(std::move(mode_arg));
102 for(
size_t i = 2; i < parts.size(); ++i) {
103 m_args.push_back(std::move(parts[i]));
107 m_text = std::string(spec);
111 if(i >= m_args.size()) {
117std::string AlgorithmSpec::text_with_head(std::string_view head)
const {
119 if(!m_args.empty()) {
121 for(
size_t i = 0; i != m_args.size(); ++i) {
125 text += m_args[i].m_text;
135 if(!pattern.any_head()) {
136 bool head_matches =
false;
137 for(
const auto alt : pattern.head_alternatives()) {
151 if(!match_args(pattern.elements(), idx, 0, m)) {
158bool AlgorithmSpec::match_args(std::span<const AlgorithmPattern::Element> elems,
166 while(idx < elems.size() && elems[idx].depth == depth) {
167 const auto& e = elems[idx];
170 if(arg_idx >= m_args.size()) {
176 if(e.placeholder && e.has_default) {
177 auto& b = m.declared(e.text);
179 b.text = e.default_value;
180 if(e.type == Type::Int) {
182 const auto v =
parse_sz(e.default_value,
true);
194 auto& b = m.declared(e.text);
196 b.rest = std::span(m_args).subspan(arg_idx);
197 arg_idx = m_args.size();
202 auto& b = m.declared(e.text);
204 if(e.type == Type::Int) {
205 if(!
arg.m_args.empty()) {
218 }
else if(e.optional) {
220 if(
arg.m_head != e.text || !
arg.m_args.empty()) {
224 if(
arg.m_head != e.text) {
228 if(idx < elems.size() && elems[idx].depth == depth + 1) {
229 if(!
arg.match_args(elems, idx, depth + 1, m)) {
232 }
else if(!
arg.m_args.empty()) {
240 return arg_idx == m_args.size();
244 m_pattern(pattern.
to_string()), m_head(spec.head()) {
245 for(
const auto& e : pattern.elements()) {
250 b.variadic = e.variadic;
251 m_bindings[m_binding_count++] = b;
256AlgorithmMatch::Binding& AlgorithmMatch::declared(std::string_view var) {
257 for(
size_t i = 0; i != m_binding_count; ++i) {
258 if(m_bindings[i].var == var) {
259 return m_bindings[i];
262 throw Internal_Error(
fmt(
"AlgorithmMatch: no variable '{}' in pattern '{}'", var, m_pattern));
265const AlgorithmMatch::Binding& AlgorithmMatch::bound(std::string_view var,
const char* accessor)
const {
266 for(
size_t i = 0; i != m_binding_count; ++i) {
267 if(m_bindings[i].var == var) {
268 if(!m_bindings[i].bound) {
270 fmt(
"AlgorithmMatch::{}: variable '{}' in pattern '{}' was not bound; check has() first",
275 return m_bindings[i];
278 throw Internal_Error(
fmt(
"AlgorithmMatch::{}: no variable '{}' in pattern '{}'", accessor, var, m_pattern));
282 for(
size_t i = 0; i != m_binding_count; ++i) {
283 if(m_bindings[i].var == var) {
284 return m_bindings[i].bound;
287 throw Internal_Error(
fmt(
"AlgorithmMatch::has: no variable '{}' in pattern '{}'", var, m_pattern));
291 const auto& b = bound(var,
"str");
293 throw Internal_Error(
fmt(
"AlgorithmMatch::str: variable '{}' in pattern '{}' is variadic", var, m_pattern));
299 const auto& b = bound(var,
"integer");
301 throw Internal_Error(
fmt(
"AlgorithmMatch::integer: variable '{}' in pattern '{}' is not an int", var, m_pattern));
307 const auto& b = bound(var,
"name");
309 throw Internal_Error(
fmt(
"AlgorithmMatch::name: variable '{}' in pattern '{}' is not a name", var, m_pattern));
315 const auto& b = bound(var,
"rest");
317 throw Internal_Error(
fmt(
"AlgorithmMatch::rest: variable '{}' in pattern '{}' is not variadic", var, m_pattern));
#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
bool has(std::string_view var) const
std::string_view str(std::string_view var) const
const AlgorithmSpec & name(std::string_view var) const
const AlgorithmSpec & arg(size_t i) 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 fmt(std::string_view format, const T &... args)
std::optional< size_t > parse_sz(std::string_view input, bool require_canonical)
std::string to_string(ErrorType type)
Convert an ErrorType to string.