aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/yaml-cpp/src/singledocparser.cpp
diff options
context:
space:
mode:
authorAlexander Smirnov <alex@ydb.tech>2024-07-08 15:54:05 +0000
committerAlexander Smirnov <alex@ydb.tech>2024-07-08 15:54:05 +0000
commitfc7be18c76af2e700641f3598c4856baeef1428e (patch)
tree11dbca45eb321c3a4dd08b12152acc6ef5dd3fa9 /contrib/libs/yaml-cpp/src/singledocparser.cpp
parentec0e7ed6da6fb317741fd8468602949a1362eca5 (diff)
parentc92cb9d3a19331916f0c274d80e67f02a62caa9b (diff)
downloadydb-fc7be18c76af2e700641f3598c4856baeef1428e.tar.gz
Merge branch 'rightlib' into mergelibs-240708-1553
Diffstat (limited to 'contrib/libs/yaml-cpp/src/singledocparser.cpp')
-rw-r--r--contrib/libs/yaml-cpp/src/singledocparser.cpp63
1 files changed, 42 insertions, 21 deletions
diff --git a/contrib/libs/yaml-cpp/src/singledocparser.cpp b/contrib/libs/yaml-cpp/src/singledocparser.cpp
index a27c1c3b04..22913d198c 100644
--- a/contrib/libs/yaml-cpp/src/singledocparser.cpp
+++ b/contrib/libs/yaml-cpp/src/singledocparser.cpp
@@ -7,6 +7,7 @@
#include "singledocparser.h"
#include "tag.h"
#include "token.h"
+#include "yaml-cpp/depthguard.h"
#include "yaml-cpp/emitterstyle.h"
#include "yaml-cpp/eventhandler.h"
#include "yaml-cpp/exceptions.h" // IWYU pragma: keep
@@ -18,9 +19,10 @@ SingleDocParser::SingleDocParser(Scanner& scanner, const Directives& directives)
: m_scanner(scanner),
m_directives(directives),
m_pCollectionStack(new CollectionStack),
+ m_anchors{},
m_curAnchor(0) {}
-SingleDocParser::~SingleDocParser() {}
+SingleDocParser::~SingleDocParser() = default;
// HandleDocument
// . Handles the next document
@@ -46,6 +48,8 @@ void SingleDocParser::HandleDocument(EventHandler& eventHandler) {
}
void SingleDocParser::HandleNode(EventHandler& eventHandler) {
+ DepthGuard<500> depthguard(depth, m_scanner.mark(), ErrorMsg::BAD_FILE);
+
// an empty node *is* a possibility
if (m_scanner.empty()) {
eventHandler.OnNull(m_scanner.mark(), NullAnchor);
@@ -71,20 +75,31 @@ void SingleDocParser::HandleNode(EventHandler& eventHandler) {
}
std::string tag;
+ std::string anchor_name;
anchor_t anchor;
- ParseProperties(tag, anchor);
+ ParseProperties(tag, anchor, anchor_name);
- const Token& token = m_scanner.peek();
+ if (!anchor_name.empty())
+ eventHandler.OnAnchor(mark, anchor_name);
- if (token.type == Token::PLAIN_SCALAR && IsNullString(token.value)) {
+ // after parsing properties, an empty node is again a possibility
+ if (m_scanner.empty()) {
eventHandler.OnNull(mark, anchor);
- m_scanner.pop();
return;
}
+ const Token& token = m_scanner.peek();
+
// add non-specific tags
if (tag.empty())
tag = (token.type == Token::NON_PLAIN_SCALAR ? "!" : "?");
+
+ if (token.type == Token::PLAIN_SCALAR
+ && tag.compare("?") == 0 && IsNullString(token.value)) {
+ eventHandler.OnNull(mark, anchor);
+ m_scanner.pop();
+ return;
+ }
// now split based on what kind of node we should be
switch (token.type) {
@@ -152,7 +167,7 @@ void SingleDocParser::HandleBlockSequence(EventHandler& eventHandler) {
m_scanner.pop();
m_pCollectionStack->PushCollectionType(CollectionType::BlockSeq);
- while (1) {
+ while (true) {
if (m_scanner.empty())
throw ParserException(m_scanner.mark(), ErrorMsg::END_OF_SEQ);
@@ -166,10 +181,10 @@ void SingleDocParser::HandleBlockSequence(EventHandler& eventHandler) {
// check for null
if (!m_scanner.empty()) {
- const Token& token = m_scanner.peek();
- if (token.type == Token::BLOCK_ENTRY ||
- token.type == Token::BLOCK_SEQ_END) {
- eventHandler.OnNull(token.mark, NullAnchor);
+ const Token& nextToken = m_scanner.peek();
+ if (nextToken.type == Token::BLOCK_ENTRY ||
+ nextToken.type == Token::BLOCK_SEQ_END) {
+ eventHandler.OnNull(nextToken.mark, NullAnchor);
continue;
}
}
@@ -185,7 +200,7 @@ void SingleDocParser::HandleFlowSequence(EventHandler& eventHandler) {
m_scanner.pop();
m_pCollectionStack->PushCollectionType(CollectionType::FlowSeq);
- while (1) {
+ while (true) {
if (m_scanner.empty())
throw ParserException(m_scanner.mark(), ErrorMsg::END_OF_SEQ_FLOW);
@@ -238,7 +253,7 @@ void SingleDocParser::HandleBlockMap(EventHandler& eventHandler) {
m_scanner.pop();
m_pCollectionStack->PushCollectionType(CollectionType::BlockMap);
- while (1) {
+ while (true) {
if (m_scanner.empty())
throw ParserException(m_scanner.mark(), ErrorMsg::END_OF_MAP);
@@ -277,7 +292,7 @@ void SingleDocParser::HandleFlowMap(EventHandler& eventHandler) {
m_scanner.pop();
m_pCollectionStack->PushCollectionType(CollectionType::FlowMap);
- while (1) {
+ while (true) {
if (m_scanner.empty())
throw ParserException(m_scanner.mark(), ErrorMsg::END_OF_MAP_FLOW);
@@ -356,11 +371,13 @@ void SingleDocParser::HandleCompactMapWithNoKey(EventHandler& eventHandler) {
// ParseProperties
// . Grabs any tag or anchor tokens and deals with them.
-void SingleDocParser::ParseProperties(std::string& tag, anchor_t& anchor) {
+void SingleDocParser::ParseProperties(std::string& tag, anchor_t& anchor,
+ std::string& anchor_name) {
tag.clear();
+ anchor_name.clear();
anchor = NullAnchor;
- while (1) {
+ while (true) {
if (m_scanner.empty())
return;
@@ -369,7 +386,7 @@ void SingleDocParser::ParseProperties(std::string& tag, anchor_t& anchor) {
ParseTag(tag);
break;
case Token::ANCHOR:
- ParseAnchor(anchor);
+ ParseAnchor(anchor, anchor_name);
break;
default:
return;
@@ -387,11 +404,12 @@ void SingleDocParser::ParseTag(std::string& tag) {
m_scanner.pop();
}
-void SingleDocParser::ParseAnchor(anchor_t& anchor) {
+void SingleDocParser::ParseAnchor(anchor_t& anchor, std::string& anchor_name) {
Token& token = m_scanner.peek();
if (anchor)
throw ParserException(token.mark, ErrorMsg::MULTIPLE_ANCHORS);
+ anchor_name = token.value;
anchor = RegisterAnchor(token.value);
m_scanner.pop();
}
@@ -405,10 +423,13 @@ anchor_t SingleDocParser::RegisterAnchor(const std::string& name) {
anchor_t SingleDocParser::LookupAnchor(const Mark& mark,
const std::string& name) const {
- Anchors::const_iterator it = m_anchors.find(name);
- if (it == m_anchors.end())
- throw ParserException(mark, ErrorMsg::UNKNOWN_ANCHOR);
+ auto it = m_anchors.find(name);
+ if (it == m_anchors.end()) {
+ std::stringstream ss;
+ ss << ErrorMsg::UNKNOWN_ANCHOR << name;
+ throw ParserException(mark, ss.str());
+ }
return it->second;
}
-}
+} // namespace YAML