rapidyaml 0.16.0
parse and emit YAML, and do it fast
Loading...
Searching...
No Matches
YAML tags

Functions

void sample_tags ()
 deal with YAML type tags
void sample_tag_directives ()
 deal with YAML tag namespace directives

Detailed Description

Function Documentation

◆ sample_tags()

void sample_tags ( )

deal with YAML type tags

Definition at line 6083 of file quickstart.cpp.

6084{
6085 const std::string yaml = ""
6086 "--- !!map" "\n"
6087 "a: 0" "\n"
6088 "b: 1" "\n"
6089 "--- !map" "\n"
6090 "a: b" "\n"
6091 "--- !!seq" "\n"
6092 "- a" "\n"
6093 "- b" "\n"
6094 "--- !!str a b" "\n"
6095 "--- !!str 'a: b'" "\n"
6096 "---" "\n"
6097 "!!str a: b" "\n"
6098 "--- !!set" "\n"
6099 "? a" "\n"
6100 "? b" "\n"
6101 "--- !!set" "\n"
6102 "a:" "\n"
6103 "--- !!seq" "\n"
6104 "- !!int 0" "\n"
6105 "- !!str 1" "\n"
6106 "";
6108 const ryml::ConstNodeRef root = tree.rootref();
6109 CHECK(root.is_stream());
6110 CHECK(root.num_children() == 9);
6111 for(ryml::ConstNodeRef doc : root.children())
6112 CHECK(doc.is_doc());
6113 // tags are kept verbatim from the source:
6114 CHECK(root[0].has_val_tag());
6115 CHECK(root[0].val_tag() == "!!map"); // valid only if the node has a val tag
6116 CHECK(root[1].val_tag() == "!map");
6117 CHECK(root[2].val_tag() == "!!seq");
6118 CHECK(root[3].val_tag() == "!!str");
6119 CHECK(root[4].val_tag() == "!!str");
6120 CHECK(root[5]["a"].has_key_tag());
6121 CHECK(root[5]["a"].key_tag() == "!!str"); // valid only if the node has a key tag
6122 CHECK(root[6].val_tag() == "!!set");
6123 CHECK(root[7].val_tag() == "!!set");
6124 CHECK(root[8].val_tag() == "!!seq");
6125 CHECK(root[8][0].val_tag() == "!!int");
6126 CHECK(root[8][1].val_tag() == "!!str");
6127 // ryml also provides a complete toolbox to deal with tags.
6128 // there is an enumeration for the standard YAML tags:
6129 CHECK(ryml::to_tag("!map") == ryml::TAG_NONE);
6130 CHECK(ryml::to_tag("!!map") == ryml::TAG_MAP);
6131 CHECK(ryml::to_tag("!!seq") == ryml::TAG_SEQ);
6132 CHECK(ryml::to_tag("!!str") == ryml::TAG_STR);
6133 CHECK(ryml::to_tag("!!int") == ryml::TAG_INT);
6134 CHECK(ryml::to_tag("!!set") == ryml::TAG_SET);
6135 // given a tag enum, you can fetch the short tag string:
6137 CHECK(ryml::from_tag(ryml::TAG_MAP) == "!!map");
6138 CHECK(ryml::from_tag(ryml::TAG_SEQ) == "!!seq");
6139 CHECK(ryml::from_tag(ryml::TAG_STR) == "!!str");
6140 CHECK(ryml::from_tag(ryml::TAG_INT) == "!!int");
6141 CHECK(ryml::from_tag(ryml::TAG_SET) == "!!set");
6142 // you can also fetch the long tag string:
6144 CHECK(ryml::from_tag_long(ryml::TAG_MAP) == "<tag:yaml.org,2002:map>");
6145 CHECK(ryml::from_tag_long(ryml::TAG_SEQ) == "<tag:yaml.org,2002:seq>");
6146 CHECK(ryml::from_tag_long(ryml::TAG_STR) == "<tag:yaml.org,2002:str>");
6147 CHECK(ryml::from_tag_long(ryml::TAG_INT) == "<tag:yaml.org,2002:int>");
6148 CHECK(ryml::from_tag_long(ryml::TAG_SET) == "<tag:yaml.org,2002:set>");
6149 // and likewise:
6150 CHECK(ryml::to_tag("!map") == ryml::TAG_NONE);
6151 CHECK(ryml::to_tag("<tag:yaml.org,2002:map>") == ryml::TAG_MAP);
6152 CHECK(ryml::to_tag("<tag:yaml.org,2002:seq>") == ryml::TAG_SEQ);
6153 CHECK(ryml::to_tag("<tag:yaml.org,2002:str>") == ryml::TAG_STR);
6154 CHECK(ryml::to_tag("<tag:yaml.org,2002:int>") == ryml::TAG_INT);
6155 CHECK(ryml::to_tag("<tag:yaml.org,2002:set>") == ryml::TAG_SET);
6156 // to normalize a tag as much as possible, use normalize_tag():
6157 CHECK(ryml::normalize_tag("!!map") == "!!map");
6158 CHECK(ryml::normalize_tag("!<tag:yaml.org,2002:map>") == "!!map");
6159 CHECK(ryml::normalize_tag("<tag:yaml.org,2002:map>") == "!!map");
6160 CHECK(ryml::normalize_tag("tag:yaml.org,2002:map") == "!!map");
6161 CHECK(ryml::normalize_tag("!<!!map>") == "<!!map>");
6162 CHECK(ryml::normalize_tag("!map") == "!map");
6163 CHECK(ryml::normalize_tag("!my!foo") == "!my!foo");
6164 // and also for the long form:
6165 CHECK(ryml::normalize_tag_long("!!map") == "<tag:yaml.org,2002:map>");
6166 CHECK(ryml::normalize_tag_long("!<tag:yaml.org,2002:map>") == "<tag:yaml.org,2002:map>");
6167 CHECK(ryml::normalize_tag_long("<tag:yaml.org,2002:map>") == "<tag:yaml.org,2002:map>");
6168 CHECK(ryml::normalize_tag_long("tag:yaml.org,2002:map") == "<tag:yaml.org,2002:map>");
6169 CHECK(ryml::normalize_tag_long("!<!!map>") == "<!!map>");
6170 CHECK(ryml::normalize_tag_long("!map") == "!map");
6171 // The tree provides the following methods applying to every node
6172 // with a key and/or val tag:
6173 ryml::Tree normalized_tree = tree;
6174 normalized_tree.normalize_tags(); // normalize all tags in short form
6175 CHECK(ryml::emitrs_yaml<std::string>(normalized_tree) == ""
6176 "--- !!map" "\n"
6177 "a: 0" "\n"
6178 "b: 1" "\n"
6179 "--- !map" "\n"
6180 "a: b" "\n"
6181 "--- !!seq" "\n"
6182 "- a" "\n"
6183 "- b" "\n"
6184 "--- !!str a b" "\n"
6185 "--- !!str 'a: b'" "\n"
6186 "---" "\n"
6187 "!!str a: b" "\n"
6188 "--- !!set" "\n"
6189 "a: " "\n"
6190 "b: " "\n"
6191 "--- !!set" "\n"
6192 "a: " "\n"
6193 "--- !!seq" "\n"
6194 "- !!int 0" "\n"
6195 "- !!str 1" "\n"
6196 "");
6197 ryml::Tree normalized_tree_long = tree;
6198 normalized_tree_long.normalize_tags_long(); // normalize all tags in short form
6199 CHECK(ryml::emitrs_yaml<std::string>(normalized_tree_long) == ""
6200 "--- !<tag:yaml.org,2002:map>" "\n"
6201 "a: 0" "\n"
6202 "b: 1" "\n"
6203 "--- !map" "\n"
6204 "a: b" "\n"
6205 "--- !<tag:yaml.org,2002:seq>" "\n"
6206 "- a" "\n"
6207 "- b" "\n"
6208 "--- !<tag:yaml.org,2002:str> a b" "\n"
6209 "--- !<tag:yaml.org,2002:str> 'a: b'" "\n"
6210 "---" "\n"
6211 "!<tag:yaml.org,2002:str> a: b" "\n"
6212 "--- !<tag:yaml.org,2002:set>" "\n"
6213 "a: " "\n"
6214 "b: " "\n"
6215 "--- !<tag:yaml.org,2002:set>" "\n"
6216 "a: " "\n"
6217 "--- !<tag:yaml.org,2002:seq>" "\n"
6218 "- !<tag:yaml.org,2002:int> 0" "\n"
6219 "- !<tag:yaml.org,2002:str> 1" "\n"
6220 "");
6221}
Holds a pointer to an existing tree, and a node id.
Definition node.hpp:737
const_children_view children() const RYML_NOEXCEPT
get an iterable view over children
Definition node.hpp:987
NodeRef rootref()
Get the root as a NodeRef . Note that a non-const Tree implicitly converts to NodeRef.
Definition tree.cpp:56
void normalize_tags()
Definition tree.cpp:1571
void normalize_tags_long()
Definition tree.cpp:1578
substr emitrs_yaml(Tree const &t, id_type id, EmitOptions const &opts, CharOwningContainer *cont, bool append=false)
(1) emit+resize: emit YAML to the given std::string/std::vector<char>-like container,...
void parse_in_arena(Parser *parser, csubstr filename, csubstr yaml, Tree *tree, id_type node_id)
(1) parse YAML into an existing tree node. The filename will be used in any error messages arising du...
Definition parse.cpp:209
#define CHECK(predicate)
a testing assertion, used only in this quickstart
csubstr to_csubstr(const char(&s)[N]) noexcept
Definition substr.hpp:2380
csubstr from_tag_long(YamlTag_e tag)
Definition tag.cpp:130
csubstr normalize_tag_long(csubstr tag)
Definition tag.cpp:31
csubstr normalize_tag(csubstr tag)
Definition tag.cpp:19
csubstr from_tag(YamlTag_e tag)
Definition tag.cpp:170
YamlTag_e to_tag(csubstr tag)
Definition tag.cpp:68
@ TAG_SET
!
Definition tag.hpp:39
@ TAG_INT
!
Definition tag.hpp:45
@ TAG_SEQ
!
Definition tag.hpp:40
@ TAG_NONE
Definition tag.hpp:34
@ TAG_STR
!
Definition tag.hpp:48
@ TAG_MAP
!
Definition tag.hpp:36
bool is_stream() const RYML_NOEXCEPT
Forward to Tree::is_stream().
Definition node.hpp:204
id_type num_children() const RYML_NOEXCEPT
O(num_children).
Definition node.hpp:302

Referenced by main().

◆ sample_tag_directives()

void sample_tag_directives ( )

deal with YAML tag namespace directives

Definition at line 6226 of file quickstart.cpp.

6227{
6228 const std::string yaml = ""
6229 "%TAG !m! !my-" "\n"
6230 "--- # Bulb here" "\n"
6231 "!m!light fluorescent" "\n"
6232 "..." "\n"
6233 "%TAG !m! !meta-" "\n"
6234 "--- # Color here" "\n"
6235 "!m!light green" "\n"
6236 "";
6237 // tags are not resolved by default:
6240 "%TAG !m! !my-" "\n"
6241 "--- !m!light fluorescent" "\n"
6242 "..." "\n"
6243 "%TAG !m! !meta-" "\n"
6244 "--- !m!light green" "\n"
6245 "");
6246 // Use Tree::resolve_tags() to accomplish this in an existing
6247 // tree:
6248 ryml::TagCache tag_cache; // reduces memory requirements by reusing resolved tags
6249 tree.resolve_tags(tag_cache);
6251 "%TAG !m! !my-" "\n"
6252 "--- !<!my-light> fluorescent" "\n"
6253 "..." "\n"
6254 "%TAG !m! !meta-" "\n"
6255 "--- !<!meta-light> green" "\n"
6256 "");
6257 // You can also Use ParserOptions to force resolution of tags
6258 // while parsing:
6260 ryml::Tree resolved_tree = ryml::parse_in_arena(ryml::to_csubstr(yaml), opts);
6261 CHECK(ryml::emitrs_yaml<std::string>(resolved_tree) == ""
6262 "%TAG !m! !my-" "\n"
6263 "--- !<!my-light> fluorescent" "\n"
6264 "..." "\n"
6265 "%TAG !m! !meta-" "\n"
6266 "--- !<!meta-light> green" "\n"
6267 "");
6268 // see also tree.normalize_tags()
6269 // see also tree.normalize_tags_long()
6270}
void resolve_tags(TagCache &cache, bool all=true)
Resolve tags in the tree such as "!!str" -> "<tag:yaml.org,2002:str>", "!foo" -> "<!...
Definition tree.cpp:1553
Options to give to the ParseEngine to control its behavior.
ParserOptions & resolve_tags(bool enabled) noexcept
enable/disable resolution of YAML tags during parsing.
Accelerator structure to reduce memory requirements by enabling reuse of resolved tags.
Definition tag.hpp:71

Referenced by main().