rapidyaml  0.13.0
parse and emit YAML, and do it fast
parse.cpp
Go to the documentation of this file.
1 #include "c4/yml/parse.hpp"
2 
3 #ifndef _C4_YML_NODE_HPP_
4 #include "c4/yml/node.hpp"
5 #endif
6 #ifndef _C4_YML_PARSE_ENGINE_HPP_
8 #endif
9 #ifndef _C4_YML_PARSE_ENGINE_DEF_HPP_
11 #endif
12 #ifndef _C4_YML_EVENT_HANDLER_TREE_HPP_
14 #endif
15 
16 
17 //-----------------------------------------------------------------------------
18 
19 namespace c4 {
20 namespace yml {
21 
22 // instantiate the parser class
23 template class RYML_EXPORT ParseEngine<EventHandlerTree>;
24 
25 
26 namespace {
27 void _reset_tree_handler(Parser *parser, Tree *t, id_type node_id)
28 {
29  _RYML_ASSERT_BASIC(parser);
30  _RYML_ASSERT_BASIC(t);
31  if(!parser->m_evt_handler)
32  _RYML_ERR_BASIC_(t->m_callbacks, "event handler is not set");
33  parser->m_evt_handler->reset(t, node_id);
34  _RYML_ASSERT_BASIC(parser->m_evt_handler->m_tree == t);
35 }
36 } // namespace
37 
38 void parse_in_place(Parser *parser, csubstr filename, substr yaml, Tree *t, id_type node_id)
39 {
40  _reset_tree_handler(parser, t, node_id);
41  parser->parse_in_place_ev(filename, yaml);
42 }
43 
44 void parse_json_in_place(Parser *parser, csubstr filename, substr json, Tree *t, id_type node_id)
45 {
46  _reset_tree_handler(parser, t, node_id);
47  parser->parse_json_in_place_ev(filename, json);
48 }
49 
50 
51 // this is vertically aligned to highlight the parameter differences.
52 void parse_in_place(Parser *parser, substr yaml, Tree *t, id_type node_id) { parse_in_place(parser, {}, yaml, t, node_id); }
53 void parse_in_place(Parser *parser, csubstr filename, substr yaml, Tree *t ) { _RYML_CHECK_BASIC(t); if(t->empty()) { t->reserve(); } parse_in_place(parser, filename, yaml, t, t->root_id()); }
54 void parse_in_place(Parser *parser, substr yaml, Tree *t ) { _RYML_CHECK_BASIC(t); if(t->empty()) { t->reserve(); } parse_in_place(parser, {} , yaml, t, t->root_id()); }
55 void parse_in_place(Parser *parser, csubstr filename, substr yaml, NodeRef node ) { _RYML_CHECK_BASIC(!node.invalid()); parse_in_place(parser, filename, yaml, node.tree(), node.id()); }
56 void parse_in_place(Parser *parser, substr yaml, NodeRef node ) { _RYML_CHECK_BASIC(!node.invalid()); parse_in_place(parser, {} , yaml, node.tree(), node.id()); }
57 Tree parse_in_place(Parser *parser, csubstr filename, substr yaml ) { _RYML_CHECK_BASIC(parser); _RYML_CHECK_BASIC(parser->m_evt_handler); Tree tree(parser->callbacks()); parse_in_place(parser, filename, yaml, &tree, tree.root_id()); return tree; }
58 Tree parse_in_place(Parser *parser, substr yaml ) { _RYML_CHECK_BASIC(parser); _RYML_CHECK_BASIC(parser->m_evt_handler); Tree tree(parser->callbacks()); parse_in_place(parser, {} , yaml, &tree, tree.root_id()); return tree; }
59 
60 // this is vertically aligned to highlight the parameter differences.
61 void parse_in_place(csubstr filename, substr yaml, Tree *t, id_type node_id, ParserOptions const& opts) { _RYML_CHECK_BASIC(t); Parser::handler_type event_handler(t->callbacks()); Parser parser(&event_handler, opts); parse_in_place(&parser, filename, yaml, t, node_id); }
62 void parse_in_place( substr yaml, Tree *t, id_type node_id, ParserOptions const& opts) { _RYML_CHECK_BASIC(t); Parser::handler_type event_handler(t->callbacks()); Parser parser(&event_handler, opts); parse_in_place(&parser, {} , yaml, t, node_id); }
63 void parse_in_place(csubstr filename, substr yaml, Tree *t , ParserOptions const& opts) { _RYML_CHECK_BASIC(t); Parser::handler_type event_handler(t->callbacks()); Parser parser(&event_handler, opts); if(t->empty()) { t->reserve(); } parse_in_place(&parser, filename, yaml, t, t->root_id()); }
64 void parse_in_place( substr yaml, Tree *t , ParserOptions const& opts) { _RYML_CHECK_BASIC(t); Parser::handler_type event_handler(t->callbacks()); Parser parser(&event_handler, opts); if(t->empty()) { t->reserve(); } parse_in_place(&parser, {} , yaml, t, t->root_id()); }
65 void parse_in_place(csubstr filename, substr yaml, NodeRef node , ParserOptions const& opts) { _RYML_CHECK_BASIC(!node.invalid()); Parser::handler_type event_handler(node.tree()->callbacks()); Parser parser(&event_handler, opts); parse_in_place(&parser, filename, yaml, node.tree(), node.id()); }
66 void parse_in_place( substr yaml, NodeRef node , ParserOptions const& opts) { _RYML_CHECK_BASIC(!node.invalid()); Parser::handler_type event_handler(node.tree()->callbacks()); Parser parser(&event_handler, opts); parse_in_place(&parser, {} , yaml, node.tree(), node.id()); }
67 Tree parse_in_place(csubstr filename, substr yaml , ParserOptions const& opts) { Parser::handler_type event_handler; Parser parser(&event_handler, opts); Tree tree(parser.callbacks()); parse_in_place(&parser, filename, yaml, &tree, tree.root_id()); return tree; }
68 Tree parse_in_place( substr yaml , ParserOptions const& opts) { Parser::handler_type event_handler; Parser parser(&event_handler, opts); Tree tree(parser.callbacks()); parse_in_place(&parser, {} , yaml, &tree, tree.root_id()); return tree; }
69 
70 
71 // this is vertically aligned to highlight the parameter differences.
72 void parse_json_in_place(Parser *parser, substr json, Tree *t, id_type node_id) { parse_json_in_place(parser, {}, json, t, node_id); }
73 void parse_json_in_place(Parser *parser, csubstr filename, substr json, Tree *t ) { _RYML_CHECK_BASIC(t); if(t->empty()) { t->reserve(); } parse_json_in_place(parser, filename, json, t, t->root_id()); }
74 void parse_json_in_place(Parser *parser, substr json, Tree *t ) { _RYML_CHECK_BASIC(t); if(t->empty()) { t->reserve(); } parse_json_in_place(parser, {} , json, t, t->root_id()); }
75 void parse_json_in_place(Parser *parser, csubstr filename, substr json, NodeRef node ) { _RYML_CHECK_BASIC(!node.invalid()); parse_json_in_place(parser, filename, json, node.tree(), node.id()); }
76 void parse_json_in_place(Parser *parser, substr json, NodeRef node ) { _RYML_CHECK_BASIC(!node.invalid()); parse_json_in_place(parser, {} , json, node.tree(), node.id()); }
77 Tree parse_json_in_place(Parser *parser, csubstr filename, substr json ) { _RYML_CHECK_BASIC(parser); _RYML_CHECK_BASIC(parser->m_evt_handler); Tree tree(parser->callbacks()); parse_json_in_place(parser, filename, json, &tree, tree.root_id()); return tree; }
78 Tree parse_json_in_place(Parser *parser, substr json ) { _RYML_CHECK_BASIC(parser); _RYML_CHECK_BASIC(parser->m_evt_handler); Tree tree(parser->callbacks()); parse_json_in_place(parser, {} , json, &tree, tree.root_id()); return tree; }
79 
80 // this is vertically aligned to highlight the parameter differences.
81 void parse_json_in_place(csubstr filename, substr json, Tree *t, id_type node_id, ParserOptions const& opts) { _RYML_CHECK_BASIC(t); Parser::handler_type event_handler(t->callbacks()); Parser parser(&event_handler, opts); parse_json_in_place(&parser, filename, json, t, node_id); }
82 void parse_json_in_place( substr json, Tree *t, id_type node_id, ParserOptions const& opts) { _RYML_CHECK_BASIC(t); Parser::handler_type event_handler(t->callbacks()); Parser parser(&event_handler, opts); parse_json_in_place(&parser, {} , json, t, node_id); }
83 void parse_json_in_place(csubstr filename, substr json, Tree *t , ParserOptions const& opts) { _RYML_CHECK_BASIC(t); Parser::handler_type event_handler(t->callbacks()); Parser parser(&event_handler, opts); if(t->empty()) { t->reserve(); } parse_json_in_place(&parser, filename, json, t, t->root_id()); }
84 void parse_json_in_place( substr json, Tree *t , ParserOptions const& opts) { _RYML_CHECK_BASIC(t); Parser::handler_type event_handler(t->callbacks()); Parser parser(&event_handler, opts); if(t->empty()) { t->reserve(); } parse_json_in_place(&parser, {} , json, t, t->root_id()); }
85 void parse_json_in_place(csubstr filename, substr json, NodeRef node , ParserOptions const& opts) { _RYML_CHECK_BASIC(!node.invalid()); Parser::handler_type event_handler(node.tree()->callbacks()); Parser parser(&event_handler, opts); parse_json_in_place(&parser, filename, json, node.tree(), node.id()); }
86 void parse_json_in_place( substr json, NodeRef node , ParserOptions const& opts) { _RYML_CHECK_BASIC(!node.invalid()); Parser::handler_type event_handler(node.tree()->callbacks()); Parser parser(&event_handler, opts); parse_json_in_place(&parser, {} , json, node.tree(), node.id()); }
87 Tree parse_json_in_place(csubstr filename, substr json , ParserOptions const& opts) { Parser::handler_type event_handler; Parser parser(&event_handler, opts); Tree tree(parser.callbacks()); parse_json_in_place(&parser, filename, json, &tree, tree.root_id()); return tree; }
88 Tree parse_json_in_place( substr json , ParserOptions const& opts) { Parser::handler_type event_handler; Parser parser(&event_handler, opts); Tree tree(parser.callbacks()); parse_json_in_place(&parser, {} , json, &tree, tree.root_id()); return tree; }
89 
90 
91 // this is vertically aligned to highlight the parameter differences.
92 void parse_in_arena(Parser *parser, csubstr filename, csubstr yaml, Tree *t, id_type node_id) { _RYML_CHECK_BASIC(t); substr src = t->copy_to_arena(yaml); parse_in_place(parser, filename, src, t, node_id); }
93 void parse_in_arena(Parser *parser, csubstr yaml, Tree *t, id_type node_id) { _RYML_CHECK_BASIC(t); substr src = t->copy_to_arena(yaml); parse_in_place(parser, {} , src, t, node_id); }
94 void parse_in_arena(Parser *parser, csubstr filename, csubstr yaml, Tree *t ) { _RYML_CHECK_BASIC(t); substr src = t->copy_to_arena(yaml); if(t->empty()) { t->reserve(); } parse_in_place(parser, filename, src, t, t->root_id()); }
95 void parse_in_arena(Parser *parser, csubstr yaml, Tree *t ) { _RYML_CHECK_BASIC(t); substr src = t->copy_to_arena(yaml); if(t->empty()) { t->reserve(); } parse_in_place(parser, {} , src, t, t->root_id()); }
96 void parse_in_arena(Parser *parser, csubstr filename, csubstr yaml, NodeRef node ) { _RYML_CHECK_BASIC(!node.invalid()); substr src = node.tree()->copy_to_arena(yaml); parse_in_place(parser, filename, src, node.tree(), node.id()); }
97 void parse_in_arena(Parser *parser, csubstr yaml, NodeRef node ) { _RYML_CHECK_BASIC(!node.invalid()); substr src = node.tree()->copy_to_arena(yaml); parse_in_place(parser, {} , src, node.tree(), node.id()); }
98 Tree parse_in_arena(Parser *parser, csubstr filename, csubstr yaml ) { _RYML_CHECK_BASIC(parser); _RYML_CHECK_BASIC(parser->m_evt_handler); Tree tree(parser->callbacks()); substr src = tree.copy_to_arena(yaml); parse_in_place(parser, filename, src, &tree, tree.root_id()); return tree; }
99 Tree parse_in_arena(Parser *parser, csubstr yaml ) { _RYML_CHECK_BASIC(parser); _RYML_CHECK_BASIC(parser->m_evt_handler); Tree tree(parser->callbacks()); substr src = tree.copy_to_arena(yaml); parse_in_place(parser, {} , src, &tree, tree.root_id()); return tree; }
100 
101 // this is vertically aligned to highlight the parameter differences.
102 void parse_in_arena(csubstr filename, csubstr yaml, Tree *t, id_type node_id, ParserOptions const& opts) { _RYML_CHECK_BASIC(t); Parser::handler_type event_handler(t->callbacks()); Parser parser(&event_handler, opts); substr src = t->copy_to_arena(yaml); parse_in_place(&parser, filename, src, t, node_id); }
103 void parse_in_arena( csubstr yaml, Tree *t, id_type node_id, ParserOptions const& opts) { _RYML_CHECK_BASIC(t); Parser::handler_type event_handler(t->callbacks()); Parser parser(&event_handler, opts); substr src = t->copy_to_arena(yaml); parse_in_place(&parser, {} , src, t, node_id); }
104 void parse_in_arena(csubstr filename, csubstr yaml, Tree *t , ParserOptions const& opts) { _RYML_CHECK_BASIC(t); Parser::handler_type event_handler(t->callbacks()); Parser parser(&event_handler, opts); substr src = t->copy_to_arena(yaml); if(t->empty()) { t->reserve(); } parse_in_place(&parser, filename, src, t, t->root_id()); }
105 void parse_in_arena( csubstr yaml, Tree *t , ParserOptions const& opts) { _RYML_CHECK_BASIC(t); Parser::handler_type event_handler(t->callbacks()); Parser parser(&event_handler, opts); substr src = t->copy_to_arena(yaml); if(t->empty()) { t->reserve(); } parse_in_place(&parser, {} , src, t, t->root_id()); }
106 void parse_in_arena(csubstr filename, csubstr yaml, NodeRef node , ParserOptions const& opts) { _RYML_CHECK_BASIC(!node.invalid()); Parser::handler_type event_handler(node.tree()->callbacks()); Parser parser(&event_handler, opts); substr src = node.tree()->copy_to_arena(yaml); parse_in_place(&parser, filename, src, node.tree(), node.id()); }
107 void parse_in_arena( csubstr yaml, NodeRef node , ParserOptions const& opts) { _RYML_CHECK_BASIC(!node.invalid()); Parser::handler_type event_handler(node.tree()->callbacks()); Parser parser(&event_handler, opts); substr src = node.tree()->copy_to_arena(yaml); parse_in_place(&parser, {} , src, node.tree(), node.id()); }
108 Tree parse_in_arena(csubstr filename, csubstr yaml , ParserOptions const& opts) { Parser::handler_type event_handler; Parser parser(&event_handler, opts); Tree tree(parser.callbacks()); substr src = tree.copy_to_arena(yaml); parse_in_place(&parser, filename, src, &tree, tree.root_id()); return tree; }
109 Tree parse_in_arena( csubstr yaml , ParserOptions const& opts) { Parser::handler_type event_handler; Parser parser(&event_handler, opts); Tree tree(parser.callbacks()); substr src = tree.copy_to_arena(yaml); parse_in_place(&parser, {} , src, &tree, tree.root_id()); return tree; }
110 
111 
112 // this is vertically aligned to highlight the parameter differences.
113 void parse_json_in_arena(Parser *parser, csubstr filename, csubstr json, Tree *t, id_type node_id) { _RYML_CHECK_BASIC(t); substr src = t->copy_to_arena(json); parse_json_in_place(parser, filename, src, t, node_id); }
114 void parse_json_in_arena(Parser *parser, csubstr json, Tree *t, id_type node_id) { _RYML_CHECK_BASIC(t); substr src = t->copy_to_arena(json); parse_json_in_place(parser, {} , src, t, node_id); }
115 void parse_json_in_arena(Parser *parser, csubstr filename, csubstr json, Tree *t ) { _RYML_CHECK_BASIC(t); substr src = t->copy_to_arena(json); if(t->empty()) { t->reserve(); } parse_json_in_place(parser, filename, src, t, t->root_id()); }
116 void parse_json_in_arena(Parser *parser, csubstr json, Tree *t ) { _RYML_CHECK_BASIC(t); substr src = t->copy_to_arena(json); if(t->empty()) { t->reserve(); } parse_json_in_place(parser, {} , src, t, t->root_id()); }
117 void parse_json_in_arena(Parser *parser, csubstr filename, csubstr json, NodeRef node ) { _RYML_CHECK_BASIC(!node.invalid()); substr src = node.tree()->copy_to_arena(json); parse_json_in_place(parser, filename, src, node.tree(), node.id()); }
118 void parse_json_in_arena(Parser *parser, csubstr json, NodeRef node ) { _RYML_CHECK_BASIC(!node.invalid()); substr src = node.tree()->copy_to_arena(json); parse_json_in_place(parser, {} , src, node.tree(), node.id()); }
119 Tree parse_json_in_arena(Parser *parser, csubstr filename, csubstr json ) { _RYML_CHECK_BASIC(parser); _RYML_CHECK_BASIC(parser->m_evt_handler); Tree tree(parser->callbacks()); substr src = tree.copy_to_arena(json); parse_json_in_place(parser, filename, src, &tree, tree.root_id()); return tree; }
120 Tree parse_json_in_arena(Parser *parser, csubstr json ) { _RYML_CHECK_BASIC(parser); _RYML_CHECK_BASIC(parser->m_evt_handler); Tree tree(parser->callbacks()); substr src = tree.copy_to_arena(json); parse_json_in_place(parser, {} , src, &tree, tree.root_id()); return tree; }
121 
122 // this is vertically aligned to highlight the parameter differences.
123 void parse_json_in_arena(csubstr filename, csubstr json, Tree *t, id_type node_id, ParserOptions const& opts) { _RYML_CHECK_BASIC(t); Parser::handler_type event_handler(t->callbacks()); Parser parser(&event_handler, opts); substr src = t->copy_to_arena(json); parse_json_in_place(&parser, filename, src, t, node_id); }
124 void parse_json_in_arena( csubstr json, Tree *t, id_type node_id, ParserOptions const& opts) { _RYML_CHECK_BASIC(t); Parser::handler_type event_handler(t->callbacks()); Parser parser(&event_handler, opts); substr src = t->copy_to_arena(json); parse_json_in_place(&parser, {} , src, t, node_id); }
125 void parse_json_in_arena(csubstr filename, csubstr json, Tree *t , ParserOptions const& opts) { _RYML_CHECK_BASIC(t); Parser::handler_type event_handler(t->callbacks()); Parser parser(&event_handler, opts); substr src = t->copy_to_arena(json); if(t->empty()) { t->reserve(); } parse_json_in_place(&parser, filename, src, t, t->root_id()); }
126 void parse_json_in_arena( csubstr json, Tree *t , ParserOptions const& opts) { _RYML_CHECK_BASIC(t); Parser::handler_type event_handler(t->callbacks()); Parser parser(&event_handler, opts); substr src = t->copy_to_arena(json); if(t->empty()) { t->reserve(); } parse_json_in_place(&parser, {} , src, t, t->root_id()); }
127 void parse_json_in_arena(csubstr filename, csubstr json, NodeRef node , ParserOptions const& opts) { _RYML_CHECK_BASIC(!node.invalid()); Parser::handler_type event_handler(node.tree()->callbacks()); Parser parser(&event_handler, opts); substr src = node.tree()->copy_to_arena(json); parse_json_in_place(&parser, filename, src, node.tree(), node.id()); }
128 void parse_json_in_arena( csubstr json, NodeRef node , ParserOptions const& opts) { _RYML_CHECK_BASIC(!node.invalid()); Parser::handler_type event_handler(node.tree()->callbacks()); Parser parser(&event_handler, opts); substr src = node.tree()->copy_to_arena(json); parse_json_in_place(&parser, {} , src, node.tree(), node.id()); }
129 Tree parse_json_in_arena(csubstr filename, csubstr json , ParserOptions const& opts) { Parser::handler_type event_handler; Parser parser(&event_handler, opts); Tree tree(parser.callbacks()); substr src = tree.copy_to_arena(json); parse_json_in_place(&parser, filename, src, &tree, tree.root_id()); return tree; }
130 Tree parse_json_in_arena( csubstr json , ParserOptions const& opts) { Parser::handler_type event_handler; Parser parser(&event_handler, opts); Tree tree(parser.callbacks()); substr src = tree.copy_to_arena(json); parse_json_in_place(&parser, {} , src, &tree, tree.root_id()); return tree; }
131 
132 
133 //-----------------------------------------------------------------------------
134 
136 {
137  id_type num_nodes = 1; // root
138  for(size_t i = 0; i < src.len; ++i)
139  {
140  const char c = src.str[i];
141  num_nodes += (c == '\n') || (c == ',') || (c == '[') || (c == '{');
142  }
143  return num_nodes;
144 }
145 
146 } // namespace yml
147 } // namespace c4
A reference to a node in an existing yaml tree, offering a more convenient API than the index-based A...
Definition: node.hpp:996
id_type id() const noexcept
Definition: node.hpp:1121
bool invalid() const noexcept
true if the object is not referring to any existing or seed node.
Definition: node.hpp:1067
Tree * tree() noexcept
Definition: node.hpp:1118
This is the main driver of parsing logic: it scans the YAML or JSON source for tokens,...
void parse_json_in_place_ev(csubstr filename, substr src)
parse JSON in place, emitting events to the current handler
void parse_in_place_ev(csubstr filename, substr src)
parse YAML in place, emitting events to the current handler
Callbacks const & callbacks() const
Get the current callbacks in the parser.
EventHandler handler_type
id_type root_id() const
Get the id of the root node. The tree must not be empty.
Definition: tree.hpp:335
void reserve(id_type node_capacity=RYML_DEFAULT_TREE_CAPACITY)
Definition: tree.cpp:292
Callbacks const & callbacks() const
Definition: tree.hpp:288
bool empty() const
Definition: tree.hpp:282
substr copy_to_arena(csubstr s)
copy the given string to the tree's arena, growing the arena by the required size.
Definition: tree.hpp:930
#define RYML_EXPORT
Definition: export.hpp:18
void parse_in_arena(Parser *parser, csubstr filename, csubstr yaml, Tree *t, 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:92
void parse_json_in_arena(Parser *parser, csubstr filename, csubstr json, Tree *t, id_type node_id)
(1) parse JSON into an existing tree node. The filename will be used in any error messages arising du...
Definition: parse.cpp:113
void parse_json_in_place(Parser *parser, csubstr filename, substr json, Tree *t, id_type node_id)
(1) parse JSON into an existing tree node. The filename will be used in any error messages arising du...
Definition: parse.cpp:44
void parse_in_place(Parser *parser, csubstr filename, substr yaml, Tree *t, id_type node_id)
(1) parse YAML into an existing tree node.
Definition: parse.cpp:38
ParseEngine< EventHandlerTree > Parser
This is the main ryml parser, where the parser events are handled to create a ryml tree.
Definition: fwd.hpp:19
id_type estimate_tree_capacity(csubstr src)
Quickly inspect the source to estimate the number of nodes the resulting tree is likely to have.
Definition: parse.cpp:135
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
Node classes.
Options to give to the parser to control its behavior.
Definition: common.hpp:350