nodebuilder.cpp
Go to the documentation of this file.
1 #include "nodebuilder.h"
2 #include "yaml-cpp-pm/mark.h"
3 #include "yaml-cpp-pm/node.h"
4 #include <cassert>
5 
6 namespace YAML_PM
7 {
8  NodeBuilder::NodeBuilder(Node& root): m_root(root), m_initializedRoot(false), m_finished(false)
9  {
10  m_root.Clear();
11  m_anchors.push_back(0); // since the anchors start at 1
12  }
13 
15  {
16  }
17 
19  {
20  }
21 
23  {
24  assert(m_finished);
25  }
26 
27  void NodeBuilder::OnNull(const Mark& mark, anchor_t anchor)
28  {
29  Node& node = Push(anchor);
30  node.Init(NodeType::Null, mark, "");
31  Pop();
32  }
33 
34  void NodeBuilder::OnAlias(const Mark& /*mark*/, anchor_t anchor)
35  {
36  Node& node = *m_anchors[anchor];
37  Insert(node);
38  node.MarkAsAliased();
39  }
40 
41  void NodeBuilder::OnScalar(const Mark& mark, const std::string& tag, anchor_t anchor, const std::string& value)
42  {
43  Node& node = Push(anchor);
44  node.Init(NodeType::Scalar, mark, tag);
45  node.SetScalarData(value);
46  Pop();
47  }
48 
49  void NodeBuilder::OnSequenceStart(const Mark& mark, const std::string& tag, anchor_t anchor)
50  {
51  Node& node = Push(anchor);
52  node.Init(NodeType::Sequence, mark, tag);
53  }
54 
56  {
57  Pop();
58  }
59 
60  void NodeBuilder::OnMapStart(const Mark& mark, const std::string& tag, anchor_t anchor)
61  {
62  Node& node = Push(anchor);
63  node.Init(NodeType::Map, mark, tag);
64  m_didPushKey.push(false);
65  }
66 
68  {
69  m_didPushKey.pop();
70  Pop();
71  }
72 
74  {
75  Node& node = Push();
76  RegisterAnchor(anchor, node);
77  return node;
78  }
79 
81  {
82  if(!m_initializedRoot) {
83  m_initializedRoot = true;
84  return m_root;
85  }
86 
87  Node& node = m_root.CreateNode();
88  m_stack.push(&node);
89  return node;
90  }
91 
93  {
94  return m_stack.empty() ? m_root : *m_stack.top();
95  }
96 
98  {
99  assert(!m_finished);
100  if(m_stack.empty()) {
101  m_finished = true;
102  return;
103  }
104 
105  Node& node = *m_stack.top();
106  m_stack.pop();
107  Insert(node);
108  }
109 
111  {
112  Node& curTop = Top();
113  switch(curTop.Type()) {
114  case NodeType::Null:
115  case NodeType::Scalar:
116  assert(false);
117  break;
118  case NodeType::Sequence:
119  curTop.Append(node);
120  break;
121  case NodeType::Map:
122  assert(!m_didPushKey.empty());
123  if(m_didPushKey.top()) {
124  assert(!m_pendingKeys.empty());
125 
126  Node& key = *m_pendingKeys.top();
127  m_pendingKeys.pop();
128  curTop.Insert(key, node);
129  m_didPushKey.top() = false;
130  } else {
131  m_pendingKeys.push(&node);
132  m_didPushKey.top() = true;
133  }
134  break;
135  }
136  }
137 
139  {
140  if(anchor) {
141  assert(anchor == m_anchors.size());
142  m_anchors.push_back(&node);
143  }
144  }
145 }
virtual void OnDocumentEnd()
Definition: nodebuilder.cpp:22
virtual void OnMapEnd()
Definition: nodebuilder.cpp:67
void Clear()
Definition: node.cpp:34
std::size_t anchor_t
Definition: anchor.h:12
virtual void OnAlias(const Mark &mark, anchor_t anchor)
Definition: nodebuilder.cpp:34
::std::string string
Definition: gtest.h:1979
Node & CreateNode()
Definition: node.cpp:49
virtual void OnSequenceStart(const Mark &mark, const std::string &tag, anchor_t anchor)
Definition: nodebuilder.cpp:49
virtual void OnNull(const Mark &mark, anchor_t anchor)
Definition: nodebuilder.cpp:27
void SetScalarData(const std::string &data)
Definition: node.cpp:121
virtual void OnDocumentStart(const Mark &mark)
Definition: nodebuilder.cpp:18
void Insert(Node &key, Node &value)
Definition: node.cpp:133
virtual void OnSequenceEnd()
Definition: nodebuilder.cpp:55
NodeBuilder(Node &root)
Definition: nodebuilder.cpp:8
void RegisterAnchor(anchor_t anchor, Node &node)
NodeType::value Type() const
Definition: node.h:47
virtual void OnScalar(const Mark &mark, const std::string &tag, anchor_t anchor, const std::string &value)
Definition: nodebuilder.cpp:41
virtual void OnMapStart(const Mark &mark, const std::string &tag, anchor_t anchor)
Definition: nodebuilder.cpp:60
std::stack< Node * > m_stack
Definition: nodebuilder.h:51
void MarkAsAliased()
Definition: node.cpp:116
void Append(Node &node)
Definition: node.cpp:127
std::stack< Node * > m_pendingKeys
Definition: nodebuilder.h:52
void Init(NodeType::value type, const Mark &mark, const std::string &tag)
Definition: node.cpp:108
std::stack< bool > m_didPushKey
Definition: nodebuilder.h:53
void Insert(Node &node)


libpointmatcher
Author(s):
autogenerated on Sat May 27 2023 02:38:02