3 #ifndef C4_YML_NODE_HPP_
6 #ifndef C4_YML_PARSE_ENGINE_HPP_
9 #ifndef C4_YML_PARSE_ENGINE_DEF_HPP_
12 #ifndef C4_YML_EVENT_HANDLER_TREE_HPP_
24 template class RYML_EXPORT ParseEngine<EventHandlerTree>;
29 void check_(Tree *tree)
32 RYML_ERR_BASIC_("null tree");
33 if C4_UNLIKELY(tree->empty())
36 void check_(NodeRef &node)
39 if C4_UNLIKELY(node.id() ==
NONE)
40 RYML_ERR_VISIT_CB_(node.tree()->m_callbacks, node.tree(), node.id(),
"invalid node");
45 if C4_UNLIKELY(!parser)
46 RYML_ERR_BASIC_("null parser");
47 if C4_UNLIKELY(!parser->m_evt_handler)
49 RYML_ERR_BASIC_("null
handler");
51 void check_(
Parser *parser, Tree *tree)
53 if C4_UNLIKELY(!parser && !tree)
55 RYML_ERR_BASIC_(
"null parser and tree");
57 else if C4_UNLIKELY(!parser)
59 RYML_ERR_BASIC_CB_(tree->callbacks(),
"null parser");
61 else if C4_UNLIKELY(!tree)
63 if C4_UNLIKELY(!parser->m_evt_handler)
64 RYML_ERR_BASIC_("null tree and handler");
66 RYML_ERR_BASIC_CB_(parser->callbacks(), "null tree");
68 if C4_UNLIKELY(!parser->m_evt_handler)
70 RYML_ERR_BASIC_(
"null handler");
72 if C4_UNLIKELY(tree->empty())
77 void check_(
Parser *parser, NodeRef &node)
79 check_(parser, node.tree());
82 void checksrc_(Tree *tree,
csubstr src)
84 if C4_UNLIKELY(src.len && !src.str)
85 RYML_ERR_BASIC_CB_(tree->callbacks(), "null source buffer");
90 return tree->copy_to_arena(src);
96 return cpsrc_(tree, src);
101 return cpsrc_(node.tree(), src);
105 check_(parser, tree);
106 return cpsrc_(tree, src);
110 check_(parser, node);
111 return cpsrc_(node.tree(), src);
119 TmpParser(ParserOptions
const& opts={})
124 TmpParser(Tree* tree, ParserOptions
const& opts={})
129 TmpParser(NodeRef &node, ParserOptions
const& opts={})
130 :
handler(node.tree()->callbacks())
136 C4_ALWAYS_INLINE
void reset_handler_(
Parser *parser, Tree *tree,
id_type node_id)
138 RYML_ASSERT_BASIC_(parser);
139 RYML_ASSERT_BASIC_(parser->m_evt_handler);
140 RYML_ASSERT_BASIC_(tree);
141 if C4_UNLIKELY(node_id ==
NONE || node_id >= tree->capacity())
142 RYML_ERR_VISIT_CB_(tree->m_callbacks, tree, node_id,
"invalid node");
143 parser->m_evt_handler->reset(tree, node_id);
144 RYML_ASSERT_BASIC_(parser->m_evt_handler->m_tree == tree);
149 reset_handler_(parser, tree, node_id);
150 checksrc_(tree, yaml);
151 parser->parse_in_place_ev(filename, yaml);
156 reset_handler_(parser, tree, node_id);
157 checksrc_(tree, json);
158 parser->parse_json_in_place_ev(filename, json);
209 void parse_in_arena(
Parser *parser,
csubstr filename,
csubstr yaml,
Tree *tree,
id_type node_id) {
substr src = checkcp_(parser, tree, yaml); parse_yaml_(parser, filename, src, tree, node_id); }
219 void parse_in_arena(
csubstr filename,
csubstr yaml,
Tree *tree,
id_type node_id,
ParserOptions const& opts) {
substr src = checkcp_(tree, yaml); TmpParser tmp(tree, opts); parse_yaml_(&tmp.parser, filename, src, tree, node_id); }
231 void parse_json_in_arena(
Parser *parser,
csubstr filename,
csubstr json,
Tree *tree,
id_type node_id) {
substr src = checkcp_(parser, tree, json); parse_json_(parser, filename, src, tree, node_id); }
241 void parse_json_in_arena(
csubstr filename,
csubstr json,
Tree *tree,
id_type node_id,
ParserOptions const& opts) {
substr src = checkcp_(tree, json); TmpParser tmp(tree, opts); parse_json_(&tmp.parser, filename, src, tree, node_id); }
257 for(
size_t i = 0; i < src.
len; ++i)
259 const char c = src.
str[i];
260 num_nodes += (c ==
'\n') || (c ==
',') || (c ==
'[') || (c ==
'{');
void parse_json_in_arena(Parser *parser, csubstr filename, csubstr json, Tree *tree, id_type node_id)
(1) parse JSON into an existing tree node. The filename will be used in any error messages arising du...
C * str
a restricted pointer to the first character of the substring
ParseEngine< EventHandlerTree > Parser
This is the main ryml parser, where the parser events are handled to create a ryml tree (see Event Ha...
Tree const * tree() const noexcept
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...
id_type root_id() const
Get the id of the root node. The tree must not be empty. The tree can be empty only when constructed ...
A reference to a node in an existing yaml tree, offering a more convenient API than the index-based A...
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...
Options to give to the ParseEngine to control its behavior.
id_type id() const noexcept
basic_substring< char > substr
a mutable string view
basic_substring< const char > csubstr
an immutable string view
EventHandler handler_type
size_t len
the length of the substring
id_type estimate_tree_capacity(csubstr src)
Quickly inspect the source to estimate the number of nodes the resulting tree is likely to have...
This is the main driver of parsing logic: it scans the YAML or JSON source for tokens, and emits the appropriate sequence of parsing events to its event handler.
Callbacks const & get_callbacks()
get the global callbacks
Callbacks const & callbacks() const
Get the current callbacks in the parser.
void parse_in_place(Parser *parser, csubstr filename, substr yaml, Tree *tree, id_type node_id)
(1) parse YAML into an existing tree node.
void parse_json_in_place(Parser *parser, csubstr filename, substr json, Tree *tree, id_type node_id)
(1) parse JSON into an existing tree node. The filename will be used in any error messages arising du...