rapidyaml  0.12.1
parse and emit YAML, and do it fast
tag.hpp
Go to the documentation of this file.
1 #ifndef _C4_YML_TAG_HPP_
2 #define _C4_YML_TAG_HPP_
3 
4 #ifndef _C4_YML_COMMON_HPP_
5 #include <c4/yml/common.hpp>
6 #endif
7 #ifndef _C4_YML_DETAIL_STACK_HPP_
8 #include <c4/yml/detail/stack.hpp>
9 #endif
10 
11 namespace c4 {
12 namespace yml {
13 
14 class Tree;
15 
16 /** @addtogroup doc_tag_utils
17  *
18  * @{
19  */
20 
21 
22 #ifndef RYML_MAX_TAG_DIRECTIVES
23 /** the maximum number of tag directives in a Tree */
24 #define RYML_MAX_TAG_DIRECTIVES 4
25 #endif
26 
27 /** the integral type necessary to cover all the bits marking node tags */
28 using tag_bits = uint16_t;
29 
30 /** a bit mask for marking tags for types */
31 typedef enum : tag_bits { // NOLINT
32  TAG_NONE = 0,
33  // container types
34  TAG_MAP = 1, /**< !!map Unordered set of key: value pairs without duplicates. @see https://yaml.org/type/map.html */
35  TAG_OMAP = 2, /**< !!omap Ordered sequence of key: value pairs without duplicates. @see https://yaml.org/type/omap.html */
36  TAG_PAIRS = 3, /**< !!pairs Ordered sequence of key: value pairs allowing duplicates. @see https://yaml.org/type/pairs.html */
37  TAG_SET = 4, /**< !!set Unordered set of non-equal values. @see https://yaml.org/type/set.html */
38  TAG_SEQ = 5, /**< !!seq Sequence of arbitrary values. @see https://yaml.org/type/seq.html */
39  // scalar types
40  TAG_BINARY = 6, /**< !!binary A sequence of zero or more octets (8 bit values). @see https://yaml.org/type/binary.html */
41  TAG_BOOL = 7, /**< !!bool Mathematical Booleans. @see https://yaml.org/type/bool.html */
42  TAG_FLOAT = 8, /**< !!float Floating-point approximation to real numbers. https://yaml.org/type/float.html */
43  TAG_INT = 9, /**< !!float Mathematical integers. https://yaml.org/type/int.html */
44  TAG_MERGE = 10, /**< !!merge Specify one or more mapping to be merged with the current one. https://yaml.org/type/merge.html */
45  TAG_NULL = 11, /**< !!null Devoid of value. https://yaml.org/type/null.html */
46  TAG_STR = 12, /**< !!str A sequence of zero or more Unicode characters. https://yaml.org/type/str.html */
47  TAG_TIMESTAMP = 13, /**< !!timestamp A point in time https://yaml.org/type/timestamp.html */
48  TAG_VALUE = 14, /**< !!value Specify the default value of a mapping https://yaml.org/type/value.html */
49  TAG_YAML = 15, /**< !!yaml Specify the default value of a mapping https://yaml.org/type/yaml.html */
50 } YamlTag_e;
51 
52 RYML_EXPORT YamlTag_e to_tag(csubstr tag);
53 RYML_EXPORT csubstr from_tag(YamlTag_e tag);
55 RYML_EXPORT csubstr normalize_tag(csubstr tag);
56 RYML_EXPORT csubstr normalize_tag_long(csubstr tag);
57 RYML_EXPORT csubstr normalize_tag_long(csubstr tag, substr output);
58 
59 /** is a tag of the form `!handle!tag`? */
60 RYML_EXPORT bool is_custom_tag(csubstr tag);
61 RYML_EXPORT bool is_valid_tag_handle(csubstr handle);
62 
63 
64 //-----------------------------------------------------------------------------
65 
66 /** Accelerator structure to reduce memory requirements by enabling
67  * reuse of resolved tags. */
69 {
70  struct Entry
71  {
72  csubstr tag;
73  csubstr resolved;
75  };
76  using Entries = detail::stack<Entry>;
78  struct LookupResult
79  {
80  csubstr resolved;
82  operator bool() const noexcept { return resolved.len > 0; }
83  };
84 
85 public:
86 
87  TagCache() noexcept : m_entries() {}
88  LookupResult find(csubstr tag, id_type doc_id, id_type linear_threshold=Entries::sso_size) const noexcept;
89  void add(csubstr tag, csubstr resolved, id_type doc_id, const_iterator pos) RYML_NOEXCEPT;
90 
91  void clear() noexcept { m_entries.clear(); }
92 
93 public:
94 
95  /** @cond dev */
96  Entries m_entries;
97  /** @endcond */
98 
99 };
100 
101 
102 //-----------------------------------------------------------------------------
103 
105 {
106  /** Eg <pre>!e!</pre> in <pre>%TAG !e! tag:example.com,2000:app/</pre> */
107  csubstr handle;
108  /** Eg <pre>tag:example.com,2000:app/</pre> in <pre>%TAG !e! tag:example.com,2000:app/</pre> */
109  csubstr prefix;
110  /** ID of the target document */
112 };
113 
115 {
116  TagDirective const* C4_RESTRICT b;
117  TagDirective const* C4_RESTRICT e;
118  C4_ALWAYS_INLINE TagDirective const* begin() const noexcept { return b; }
119  C4_ALWAYS_INLINE TagDirective const* end() const noexcept { return e; }
120  id_type size() const noexcept { return static_cast<id_type>(e - b); }
121 };
122 
124 {
126  bool redefines_qmrk() const noexcept;
127  TagDirective const* add(csubstr handle, csubstr prefix, id_type doc_id) noexcept;
128  void clear() noexcept;
129  id_type size() const noexcept;
130  TagDirective const* lookup(csubstr tag, id_type id) const noexcept;
131  TagDirective * begin() noexcept { return m_directives; };
132  TagDirective * end() noexcept { return m_directives + size(); };
133  TagDirective const* begin() const noexcept { return m_directives; };
134  TagDirective const* end() const noexcept { return m_directives + size(); };
135  TagDirectiveRange directives() const noexcept { return TagDirectiveRange{m_directives, m_directives + size()}; }
136  TagDirectiveRange lookup_range(id_type doc_id) const noexcept;
137  /** @note the str member of the return value may be null, meaning
138  * that the buffer was not enough to fit the transformed tag.
139  *
140  * @note the return value may actually be not a substring of the
141  * input buffer. */
142  csubstr resolve(substr buf, size_t *bufsz, csubstr tag, id_type doc_id, Location const& ymlloc, Callbacks const& callbacks, bool with_brackets=true) const;
143 };
144 
145 /** returns the length of the transformed tag, or 0 to signal that the
146  * tag is local and cannot be resolved */
147 RYML_EXPORT size_t transform_tag(substr output, csubstr handle, csubstr prefix, csubstr tag,
148  Callbacks const& callbacks, Location const& ymlloc={},
149  bool with_brackets=true);
150 
151 /** @} */
152 
153 } // namespace yml
154 } // namespace c4
155 
156 #endif /* _C4_YML_TAG_HPP_ */
Common utilities and infrastructure used by ryml.
#define RYML_NOEXCEPT
Conditionally expands to noexcept when RYML_USE_ASSERT is 0 and is empty otherwise.
Definition: common.hpp:192
#define RYML_EXPORT
Definition: export.hpp:15
csubstr from_tag_long(YamlTag_e tag)
Definition: tag.cpp:130
bool is_valid_tag_handle(csubstr handle)
Definition: tag.cpp:210
bool is_custom_tag(csubstr tag)
is a tag of the form !handle!tag?
Definition: tag.cpp:9
csubstr normalize_tag_long(csubstr tag)
Definition: tag.cpp:31
YamlTag_e
a bit mask for marking tags for types
Definition: tag.hpp:31
size_t transform_tag(substr output, csubstr handle, csubstr prefix, csubstr tag, Callbacks const &callbacks, Location const &ymlloc, bool with_brackets)
returns the length of the transformed tag, or 0 to signal that the tag is local and cannot be resolve...
Definition: tag.cpp:287
csubstr normalize_tag(csubstr tag)
Definition: tag.cpp:19
uint16_t tag_bits
the integral type necessary to cover all the bits marking node tags
Definition: tag.hpp:28
csubstr from_tag(YamlTag_e tag)
Definition: tag.cpp:170
YamlTag_e to_tag(csubstr tag)
Definition: tag.cpp:68
#define RYML_MAX_TAG_DIRECTIVES
the maximum number of tag directives in a Tree
Definition: tag.hpp:24
@ TAG_SET
!!set Unordered set of non-equal values.
Definition: tag.hpp:37
@ TAG_MERGE
!!merge Specify one or more mapping to be merged with the current one.
Definition: tag.hpp:44
@ TAG_INT
!!float Mathematical integers.
Definition: tag.hpp:43
@ TAG_SEQ
!!seq Sequence of arbitrary values.
Definition: tag.hpp:38
@ TAG_NULL
!!null Devoid of value.
Definition: tag.hpp:45
@ TAG_YAML
!!yaml Specify the default value of a mapping https://yaml.org/type/yaml.html
Definition: tag.hpp:49
@ TAG_TIMESTAMP
!!timestamp A point in time https://yaml.org/type/timestamp.html
Definition: tag.hpp:47
@ TAG_NONE
Definition: tag.hpp:32
@ TAG_STR
!!str A sequence of zero or more Unicode characters.
Definition: tag.hpp:46
@ TAG_BOOL
!!bool Mathematical Booleans.
Definition: tag.hpp:41
@ TAG_MAP
!!map Unordered set of key: value pairs without duplicates.
Definition: tag.hpp:34
@ TAG_BINARY
!!binary A sequence of zero or more octets (8 bit values).
Definition: tag.hpp:40
@ TAG_PAIRS
!!pairs Ordered sequence of key: value pairs allowing duplicates.
Definition: tag.hpp:36
@ TAG_VALUE
!!value Specify the default value of a mapping https://yaml.org/type/value.html
Definition: tag.hpp:48
@ TAG_OMAP
!!omap Ordered sequence of key: value pairs without duplicates.
Definition: tag.hpp:35
@ TAG_FLOAT
!!float Floating-point approximation to real numbers.
Definition: tag.hpp:42
RYML_ID_TYPE id_type
The type of a node id in the YAML tree; to override the default type, define the macro RYML_ID_TYPE t...
Definition: common.hpp:244
(Undefined by default) Use shorter error message from checks/asserts: do not show the check condition...
Definition: common.cpp:14
A c-style callbacks class to customize behavior on errors or allocation.
Definition: common.hpp:538
holds a source or yaml file position, for example when an error is detected; See also location_format...
Definition: common.hpp:283
Accelerator structure to reduce memory requirements by enabling reuse of resolved tags.
Definition: tag.hpp:69
detail::stack< Entry > Entries
Definition: tag.hpp:76
id_type const_iterator
Definition: tag.hpp:77
TagCache() noexcept
Definition: tag.hpp:87
void clear() noexcept
Definition: tag.hpp:91
id_type size() const noexcept
Definition: tag.hpp:120
TagDirective const * b
Definition: tag.hpp:116
TagDirective const * begin() const noexcept
Definition: tag.hpp:118
TagDirective const * end() const noexcept
Definition: tag.hpp:119
TagDirective const * e
Definition: tag.hpp:117
csubstr handle
Eg.
Definition: tag.hpp:107
csubstr prefix
Eg.
Definition: tag.hpp:109
id_type doc_id
ID of the target document.
Definition: tag.hpp:111
bool redefines_qmrk() const noexcept
TagDirective const * begin() const noexcept
Definition: tag.hpp:133
TagDirective * end() noexcept
Definition: tag.hpp:132
TagDirectiveRange directives() const noexcept
Definition: tag.hpp:135
TagDirective const * end() const noexcept
Definition: tag.hpp:134