parser 1.1.3
UTF-8 CEL expression compilation and checked library extensions
Loading...
Searching...
No Matches
lookup.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2#ifndef CEL_PARSER_LOOKUP_HPP
3#define CEL_PARSER_LOOKUP_HPP
4
5#include <array>
6#include <cel/parser/diagnostic.hpp>
7#include <cel/runtime.hpp>
8#include <functional>
9#include <memory>
10#include <optional>
11#include <span>
12#include <string>
13#include <tuple>
14#include <type_traits>
15#include <typeindex>
16#include <unordered_map>
17#include <utility>
18#include <vector>
19
23
24namespace cel {
27
28namespace parser_detail {
29enum class lookup_selector;
30enum class scope_selection;
31struct lookup_access;
32} // namespace parser_detail
42 std::function<bool(std::string_view, dyn_segment&, std::size_t, source_span)>;
48using operation_emitter = std::function<void(dyn_segment&, source_span)>;
49
51namespace parser_detail {
53struct name_hash {
54 using is_transparent = void;
55
57 std::size_t operator()(std::string_view name) const noexcept {
58 return std::hash<std::string_view>{}(name);
59 }
60};
61} // namespace parser_detail
62
81class op_lookup {
82 friend struct parser_detail::lookup_access;
84 struct operation {
85 std::vector<value_type> inputs;
86 value_type result;
88 };
90 struct function_binding {
91 value_type marker;
93 };
95 template <class T>
96 using names = std::unordered_map<std::string, T, parser_detail::name_hash, std::equal_to<>>;
97 names<value_type> types_;
98 names<std::vector<operation>> operations_;
99 names<std::vector<operation>> structural_;
100 names<std::vector<operation>> builtins_;
101 names<function_binding> functions_;
102 std::unordered_map<std::type_index, std::string> marker_names_;
103 std::vector<scope_callback> libraries_;
104 std::vector<scope_callback> scopes_;
105
109 static void insert(names<std::vector<operation>>& destination,
110 std::string_view name,
111 std::span<const value_type> inputs,
112 value_type result,
113 operation_emitter emit);
117 static bool emit_match(const names<std::vector<operation>>& registrations,
118 std::string_view name,
119 dyn_segment& builder,
120 std::size_t arity,
121 source_span span);
124 bool scope_match(std::string_view name,
125 dyn_segment& builder,
126 std::size_t arity,
127 source_span span,
128 parser_detail::scope_selection selection) const;
131 bool direct_match(std::string_view name,
132 dyn_segment& builder,
133 std::size_t arity,
134 source_span span,
135 parser_detail::scope_selection selection) const;
137 void install_builtins();
139 template <class R, class... A, class F>
140 void builtin(std::string_view name, F&& callback) {
141 const std::array<value_type, sizeof...(A)> inputs{native_type<A>()...};
142 insert(builtins_, name, inputs, native_type<R>(),
143 [callback = std::forward<F>(callback)](dyn_segment& builder, source_span) {
144 builder.template op<R, A...>(callback);
145 });
146 }
149 void insert_type(std::string_view name, value_type type);
155 void insert_record_type(std::string_view name, value_type type);
160 void insert_function(std::string_view name, value_type marker, operation_emitter emit);
161
167 void push_scope(scope_callback callback);
172 void pop_scope();
176 std::vector<scope_callback> isolate_scopes();
181 void restore_scopes(std::vector<scope_callback> saved);
196 void apply(std::string_view name,
197 dyn_segment& builder,
198 std::size_t arity,
199 source_span span,
200 parser_detail::lookup_selector selector,
201 parser_detail::scope_selection selection) const;
207 void apply_cast(std::string_view target, dyn_segment& builder, source_span span) const;
208
209public:
213 std::optional<value_type> find_type(std::string_view name) const;
219 op_lookup(const op_lookup&) = delete;
221 op_lookup& operator=(const op_lookup&) = delete;
223 op_lookup(op_lookup&&) noexcept = default;
226 op_lookup& operator=(op_lookup&&) noexcept = default;
227 // [knowledge] Keep registries noncopyable: shared mutable callback captures are not
228 // independent value copies, even when their std::function wrappers can be copied.
233 template <class T>
234 void register_type(std::string_view name) {
235 if constexpr (!detail::native_leaf<T>) {
236 throw std::invalid_argument("named leaf registration requires an opaque leaf");
237 } else {
238 auto type = value_type::leaf<T>();
239 if (!type.compatible_with(native_type<T>()))
240 throw std::invalid_argument("named leaf registration requires an opaque leaf");
241 insert_type(name, std::move(type));
242 }
243 }
244
252 template <class T>
253 void register_record_type(std::string_view name) {
254 auto descriptor = record_adapter<T>::type();
255 if (descriptor.kind() != type_kind::record || descriptor.record_identity().empty())
256 throw std::invalid_argument("record adapter type must be a nominal record");
257 insert_record_type(name, std::move(descriptor));
258 }
259
267 void register_record(std::string_view name, std::span<const record_field> fields);
276 void register_operation(std::string_view name,
277 std::span<const value_type> inputs,
278 value_type result,
279 operation_emitter emit);
284 void register_structural_op(std::string_view name,
285 std::span<const value_type> inputs,
286 value_type result,
287 operation_emitter emit);
298 template <class R, class... A, class F>
299 requires(!std::is_void_v<R> && requires(dyn_segment & b, F&& callback) {
300 b.template op<R, A...>(std::forward<F>(callback));
301 }) void register_op(std::string_view name, F&& callback) {
302 const std::array<value_type, sizeof...(A)> inputs{native_type<A>()...};
303 auto owned = std::make_shared<const std::decay_t<F>>(std::forward<F>(callback));
304 register_operation(name, inputs, native_type<R>(),
305 [owned = std::move(owned)](dyn_segment& builder, source_span) {
306 builder.template op<R, A...>([owned](A&&... arguments) -> R {
307 return std::invoke(*owned, std::move(arguments)...);
308 });
309 });
310 }
311
319 template <class M>
320 void register_function(std::string_view name, M&& marker) {
321 using marker_type = std::decay_t<M>;
322 if constexpr (!detail::native_leaf<marker_type>) {
323 throw std::invalid_argument("function markers require an opaque leaf");
324 } else {
325 const auto type = value_type::leaf<marker_type>();
326 auto owned = value::from<marker_type>(std::forward<M>(marker));
327 if (!type.compatible_with(owned.type()))
328 throw std::invalid_argument("function markers require an opaque leaf");
329 insert_function(name, type,
330 [owned = std::move(owned)](dyn_segment& builder, source_span) {
331 builder.just(owned);
332 });
333 }
334 }
335
342};
343
345} // namespace cel
346#endif
op_lookup(op_lookup &&) noexcept=default
Transfers owned registration and scope state.
void register_type(std::string_view name)
Registers an opaque trivially-copyable leaf under an owned nonempty name. Throws std::invalid_argumen...
Definition lookup.hpp:234
void register_record_type(std::string_view name)
Registers the adapter's nominal schema under a CEL source name. The source name is independent of the...
Definition lookup.hpp:253
op_lookup & operator=(const op_lookup &)=delete
Disallows copy assignment of this registry.
std::optional< value_type > find_type(std::string_view name) const
Returns a retained descriptor for extension signatures, or nullopt for an unknown name....
void push_library_scope(scope_callback callback)
Owns a persistent LIFO library scope visible in independent closure bodies. Precondition: no transien...
void register_record(std::string_view name, std::span< const record_field > fields)
Registers an owned complete nominal record schema, never a guessed native layout. Throws invalid_argu...
op_lookup(const op_lookup &)=delete
Disallows copying this registry.
void register_structural_op(std::string_view name, std::span< const value_type > inputs, value_type result, operation_emitter emit)
Registers a lower-priority structural signature with complete recursive identities....
void register_op(std::string_view name, F &&callback)
Registers an owned const-invocable callback with exactly the declared result type....
Definition lookup.hpp:301
void register_function(std::string_view name, M &&marker)
Binds an ordinary name to its first owned trivially-copyable marker. Repeated installers may use anot...
Definition lookup.hpp:320
op_lookup()
Creates only core operators, casts, and the persistent full-range constructor. Standard-library funct...
void register_operation(std::string_view name, std::span< const value_type > inputs, value_type result, operation_emitter emit)
Registers one exact complete operation; a return type never distinguishes overloads....
std::function< void(dyn_segment &, source_span)> operation_emitter
Owns a checked emitter whose successful effect consumes its declared inputs and adds one result....
Definition lookup.hpp:48
std::function< bool(std::string_view, dyn_segment &, std::size_t, source_span)> scope_callback
Owns a copyable resolver called with a borrowed name, builder, operand count, and source span....
Definition lookup.hpp:41
Provides checked CEL compilation, extension libraries, and owned results.
Definition compiler.hpp:10
Identifies a half-open UTF-8 byte interval; begin never exceeds end. Example: source_span{0,...
Definition diagnostic.hpp:15