Spicy
util.h
1 // Copyright (c) 2020-2021 by the Zeek Project. See LICENSE for details.
2 
3 #pragma once
4 
5 #include <optional>
6 #include <string>
7 #include <tuple>
8 
9 #include <hilti/rt/types/integer.h>
10 #include <hilti/rt/types/vector.h>
11 
12 #include <spicy/rt/parser.h>
13 
14 namespace hilti::rt {
15 class Bytes;
16 
17 namespace type_info {
18 class Struct;
19 class Value;
20 }; // namespace type_info
21 } // namespace hilti::rt
22 
23 namespace spicy::rt {
24 
26 extern std::string version();
27 
29 extern std::string bytes_to_hexstring(const hilti::rt::Bytes& value);
30 
32 extern const hilti::rt::Vector<
33  std::optional<std::tuple<hilti::rt::integer::safe<uint64_t>, std::optional<hilti::rt::integer::safe<uint64_t>>>>>*
34 get_offsets_for_unit(const hilti::rt::type_info::Struct& struct_, const hilti::rt::type_info::Value& value);
35 
37 template<typename U>
38 inline void confirm(U& p) {
39  // If we are not in trial mode `confirm` is a no-op.
40  if ( p.__error ) {
41  p.__error.reset();
42 
43  // TODO(bbannier): For consistence we would ideally bracket the hook
44  // invocation with calls to `ParserBuilder::beforeHook` and
45  // `afterHook`, but this is not possible since we have no direct access
46  // to the parser state here.
47  p.__on_0x25_confirmed();
48  }
49 }
50 
52 template<typename U>
53 inline void reject(U& p) {
54  // Only invoke hook if we were actually in trial mode.
55  if ( const auto& error = p.__error ) {
56  // TODO(bbannier): For consistence we would ideally bracket the hook
57  // invocation with calls to `ParserBuilder::beforeHook` and
58  // `afterHook`, but this is not possible since we have no direct access
59  // to the parser state here.
60  p.__on_0x25_rejected();
61 
62  throw *error;
63  }
64  else
65  throw spicy::rt::ParseError("unit rejected outside of trial mode");
66 }
67 
68 } // namespace spicy::rt
Definition: any.h:7
Definition: parser.h:292
Definition: bytes.h:158
Definition: type-info.h:82
Definition: vector.h:251
std::string version()
Definition: util.cc:22
Definition: type-info.h:881