rapidyaml 0.14.0
parse and emit YAML, and do it fast
Loading...
Searching...
No Matches
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
19namespace c4 {
20namespace yml {
21
22// instantiate the parser class
24
25
26namespace {
27void _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
38void 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
44void 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.
52void parse_in_place(Parser *parser, substr yaml, Tree *t, id_type node_id) { parse_in_place(parser, {}, yaml, t, node_id); }
53void 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()); }
54void 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()); }
55void 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()); }
56void parse_in_place(Parser *parser, substr yaml, NodeRef node ) { _RYML_CHECK_BASIC(!node.invalid()); parse_in_place(parser, {} , yaml, node.tree(), node.id()); }
57Tree 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; }
58Tree 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.
61void 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); }
62void 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); }
63void 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()); }
64void 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()); }
65void 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()); }
66void 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()); }
67Tree 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; }
68Tree 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.
72void parse_json_in_place(Parser *parser, substr json, Tree *t, id_type node_id) { parse_json_in_place(parser, {}, json, t, node_id); }
73void 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()); }
74void 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()); }
75void 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()); }
76void 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()); }
77Tree 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; }
78Tree 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.
81void 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); }
82void 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); }
83void 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()); }
84void 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()); }
85void 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()); }
86void 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()); }
87Tree 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; }
88Tree 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.
92void 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); }
93void 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); }
94void 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()); }
95void 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()); }
96void 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()); }
97void 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()); }
98Tree 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; }
99Tree 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.
102void 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); }
103void 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); }
104void 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()); }
105void 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()); }
106void 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()); }
107void 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()); }
108Tree 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; }
109Tree 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.
113void 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); }
114void 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); }
115void 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()); }
116void 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()); }
117void 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()); }
118void 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()); }
119Tree 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; }
120Tree 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.
123void 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); }
124void 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); }
125void 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()); }
126void 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()); }
127void 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()); }
128void 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()); }
129Tree 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; }
130Tree 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:967
Tree * tree() noexcept
Definition node.hpp:1089
id_type id() const noexcept
Definition node.hpp:1092
bool invalid() const noexcept
true if the object is not referring to any existing or seed node.
Definition node.hpp:1038
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.
id_type root_id() const
Get the id of the root node. The tree must not be empty.
Definition tree.hpp:337
void reserve(id_type node_capacity=RYML_DEFAULT_TREE_CAPACITY)
Definition tree.cpp:292
bool empty() const
Definition tree.hpp:284
Callbacks const & callbacks() const
Definition tree.hpp:290
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:932
#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
basic_substring< char > substr
a mutable string view
Definition substr.hpp:2356
basic_substring< const char > csubstr
an immutable string view
Definition substr.hpp:2357
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:249
(Undefined by default) Use shorter error message from checks/asserts: do not show the check condition...
Definition common.cpp:14
Node classes.
size_t len
the length of the substring
Definition substr.hpp:218
C * str
a restricted pointer to the first character of the substring
Definition substr.hpp:216
Options to give to the parser to control its behavior.
Definition common.hpp:355