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