rapidyaml 0.15.2
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 5999 of file quickstart.cpp.

6000{
6001 const std::string yaml = ""
6002 "--- !!map" "\n"
6003 "a: 0" "\n"
6004 "b: 1" "\n"
6005 "--- !map" "\n"
6006 "a: b" "\n"
6007 "--- !!seq" "\n"
6008 "- a" "\n"
6009 "- b" "\n"
6010 "--- !!str a b" "\n"
6011 "--- !!str 'a: b'" "\n"
6012 "---" "\n"
6013 "!!str a: b" "\n"
6014 "--- !!set" "\n"
6015 "? a" "\n"
6016 "? b" "\n"
6017 "--- !!set" "\n"
6018 "a:" "\n"
6019 "--- !!seq" "\n"
6020 "- !!int 0" "\n"
6021 "- !!str 1" "\n"
6022 "";
6024 const ryml::ConstNodeRef root = tree.rootref();
6025 CHECK(root.is_stream());
6026 CHECK(root.num_children() == 9);
6027 for(ryml::ConstNodeRef doc : root.children())
6028 CHECK(doc.is_doc());
6029 // tags are kept verbatim from the source:
6030 CHECK(root[0].has_val_tag());
6031 CHECK(root[0].val_tag() == "!!map"); // valid only if the node has a val tag
6032 CHECK(root[1].val_tag() == "!map");
6033 CHECK(root[2].val_tag() == "!!seq");
6034 CHECK(root[3].val_tag() == "!!str");
6035 CHECK(root[4].val_tag() == "!!str");
6036 CHECK(root[5]["a"].has_key_tag());
6037 CHECK(root[5]["a"].key_tag() == "!!str"); // valid only if the node has a key tag
6038 CHECK(root[6].val_tag() == "!!set");
6039 CHECK(root[7].val_tag() == "!!set");
6040 CHECK(root[8].val_tag() == "!!seq");
6041 CHECK(root[8][0].val_tag() == "!!int");
6042 CHECK(root[8][1].val_tag() == "!!str");
6043 // ryml also provides a complete toolbox to deal with tags.
6044 // there is an enumeration for the standard YAML tags:
6045 CHECK(ryml::to_tag("!map") == ryml::TAG_NONE);
6046 CHECK(ryml::to_tag("!!map") == ryml::TAG_MAP);
6047 CHECK(ryml::to_tag("!!seq") == ryml::TAG_SEQ);
6048 CHECK(ryml::to_tag("!!str") == ryml::TAG_STR);
6049 CHECK(ryml::to_tag("!!int") == ryml::TAG_INT);
6050 CHECK(ryml::to_tag("!!set") == ryml::TAG_SET);
6051 // given a tag enum, you can fetch the short tag string:
6053 CHECK(ryml::from_tag(ryml::TAG_MAP) == "!!map");
6054 CHECK(ryml::from_tag(ryml::TAG_SEQ) == "!!seq");
6055 CHECK(ryml::from_tag(ryml::TAG_STR) == "!!str");
6056 CHECK(ryml::from_tag(ryml::TAG_INT) == "!!int");
6057 CHECK(ryml::from_tag(ryml::TAG_SET) == "!!set");
6058 // you can also fetch the long tag string:
6060 CHECK(ryml::from_tag_long(ryml::TAG_MAP) == "<tag:yaml.org,2002:map>");
6061 CHECK(ryml::from_tag_long(ryml::TAG_SEQ) == "<tag:yaml.org,2002:seq>");
6062 CHECK(ryml::from_tag_long(ryml::TAG_STR) == "<tag:yaml.org,2002:str>");
6063 CHECK(ryml::from_tag_long(ryml::TAG_INT) == "<tag:yaml.org,2002:int>");
6064 CHECK(ryml::from_tag_long(ryml::TAG_SET) == "<tag:yaml.org,2002:set>");
6065 // and likewise:
6066 CHECK(ryml::to_tag("!map") == ryml::TAG_NONE);
6067 CHECK(ryml::to_tag("<tag:yaml.org,2002:map>") == ryml::TAG_MAP);
6068 CHECK(ryml::to_tag("<tag:yaml.org,2002:seq>") == ryml::TAG_SEQ);
6069 CHECK(ryml::to_tag("<tag:yaml.org,2002:str>") == ryml::TAG_STR);
6070 CHECK(ryml::to_tag("<tag:yaml.org,2002:int>") == ryml::TAG_INT);
6071 CHECK(ryml::to_tag("<tag:yaml.org,2002:set>") == ryml::TAG_SET);
6072 // to normalize a tag as much as possible, use normalize_tag():
6073 CHECK(ryml::normalize_tag("!!map") == "!!map");
6074 CHECK(ryml::normalize_tag("!<tag:yaml.org,2002:map>") == "!!map");
6075 CHECK(ryml::normalize_tag("<tag:yaml.org,2002:map>") == "!!map");
6076 CHECK(ryml::normalize_tag("tag:yaml.org,2002:map") == "!!map");
6077 CHECK(ryml::normalize_tag("!<!!map>") == "<!!map>");
6078 CHECK(ryml::normalize_tag("!map") == "!map");
6079 CHECK(ryml::normalize_tag("!my!foo") == "!my!foo");
6080 // and also for the long form:
6081 CHECK(ryml::normalize_tag_long("!!map") == "<tag:yaml.org,2002:map>");
6082 CHECK(ryml::normalize_tag_long("!<tag:yaml.org,2002:map>") == "<tag:yaml.org,2002:map>");
6083 CHECK(ryml::normalize_tag_long("<tag:yaml.org,2002:map>") == "<tag:yaml.org,2002:map>");
6084 CHECK(ryml::normalize_tag_long("tag:yaml.org,2002:map") == "<tag:yaml.org,2002:map>");
6085 CHECK(ryml::normalize_tag_long("!<!!map>") == "<!!map>");
6086 CHECK(ryml::normalize_tag_long("!map") == "!map");
6087 // The tree provides the following methods applying to every node
6088 // with a key and/or val tag:
6089 ryml::Tree normalized_tree = tree;
6090 normalized_tree.normalize_tags(); // normalize all tags in short form
6091 CHECK(ryml::emitrs_yaml<std::string>(normalized_tree) == ""
6092 "--- !!map" "\n"
6093 "a: 0" "\n"
6094 "b: 1" "\n"
6095 "--- !map" "\n"
6096 "a: b" "\n"
6097 "--- !!seq" "\n"
6098 "- a" "\n"
6099 "- b" "\n"
6100 "--- !!str a b" "\n"
6101 "--- !!str 'a: b'" "\n"
6102 "---" "\n"
6103 "!!str a: b" "\n"
6104 "--- !!set" "\n"
6105 "a: " "\n"
6106 "b: " "\n"
6107 "--- !!set" "\n"
6108 "a: " "\n"
6109 "--- !!seq" "\n"
6110 "- !!int 0" "\n"
6111 "- !!str 1" "\n"
6112 "");
6113 ryml::Tree normalized_tree_long = tree;
6114 normalized_tree_long.normalize_tags_long(); // normalize all tags in short form
6115 CHECK(ryml::emitrs_yaml<std::string>(normalized_tree_long) == ""
6116 "--- !<tag:yaml.org,2002:map>" "\n"
6117 "a: 0" "\n"
6118 "b: 1" "\n"
6119 "--- !map" "\n"
6120 "a: b" "\n"
6121 "--- !<tag:yaml.org,2002:seq>" "\n"
6122 "- a" "\n"
6123 "- b" "\n"
6124 "--- !<tag:yaml.org,2002:str> a b" "\n"
6125 "--- !<tag:yaml.org,2002:str> 'a: b'" "\n"
6126 "---" "\n"
6127 "!<tag:yaml.org,2002:str> a: b" "\n"
6128 "--- !<tag:yaml.org,2002:set>" "\n"
6129 "a: " "\n"
6130 "b: " "\n"
6131 "--- !<tag:yaml.org,2002:set>" "\n"
6132 "a: " "\n"
6133 "--- !<tag:yaml.org,2002:seq>" "\n"
6134 "- !<tag:yaml.org,2002:int> 0" "\n"
6135 "- !<tag:yaml.org,2002:str> 1" "\n"
6136 "");
6137}
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 6142 of file quickstart.cpp.

6143{
6144 const std::string yaml = ""
6145 "%TAG !m! !my-" "\n"
6146 "--- # Bulb here" "\n"
6147 "!m!light fluorescent" "\n"
6148 "..." "\n"
6149 "%TAG !m! !meta-" "\n"
6150 "--- # Color here" "\n"
6151 "!m!light green" "\n"
6152 "";
6153 // tags are not resolved by default:
6156 "%TAG !m! !my-" "\n"
6157 "--- !m!light fluorescent" "\n"
6158 "..." "\n"
6159 "%TAG !m! !meta-" "\n"
6160 "--- !m!light green" "\n"
6161 "");
6162 // Use Tree::resolve_tags() to accomplish this in an existing
6163 // tree:
6164 ryml::TagCache tag_cache; // reduces memory requirements by reusing resolved tags
6165 tree.resolve_tags(tag_cache);
6167 "%TAG !m! !my-" "\n"
6168 "--- !<!my-light> fluorescent" "\n"
6169 "..." "\n"
6170 "%TAG !m! !meta-" "\n"
6171 "--- !<!meta-light> green" "\n"
6172 "");
6173 // You can also Use ParserOptions to force resolution of tags
6174 // while parsing:
6176 ryml::Tree resolved_tree = ryml::parse_in_arena(ryml::to_csubstr(yaml), opts);
6177 CHECK(ryml::emitrs_yaml<std::string>(resolved_tree) == ""
6178 "%TAG !m! !my-" "\n"
6179 "--- !<!my-light> fluorescent" "\n"
6180 "..." "\n"
6181 "%TAG !m! !meta-" "\n"
6182 "--- !<!meta-light> green" "\n"
6183 "");
6184 // see also tree.normalize_tags()
6185 // see also tree.normalize_tags_long()
6186}
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().