blob: d1ebe06b58c508e9e3e7b732027ec083bba7b8e2 [file] [log] [blame]
Václav Kubernát195eeea2018-05-18 13:52:36 +02001/*
2 * Copyright (C) 2018 CESNET, https://photonics.cesnet.cz/
3 * Copyright (C) 2018 FIT CVUT, https://fit.cvut.cz/
4 *
5 * Written by Václav Kubernát <kubervac@fit.cvut.cz>
6 *
7*/
8
9#include "parser_context.hpp"
Václav Kubernát43908fb2020-01-02 19:05:51 +010010ParserContext::ParserContext(const Schema& schema, const std::shared_ptr<const DataQuery> dataQuery, const dataPath_& curDir)
Václav Kubernát48fc3832018-05-28 14:21:22 +020011 : m_schema(schema)
Václav Kubernát5c75b252018-10-10 18:33:47 +020012 , m_curPathOrig(curDir)
Václav Kubernát43908fb2020-01-02 19:05:51 +010013 , m_dataquery(dataQuery)
Václav Kubernát72749c62020-01-03 16:47:34 +010014 , m_curPath(curDir)
Václav Kubernát195eeea2018-05-18 13:52:36 +020015{
Václav Kubernát195eeea2018-05-18 13:52:36 +020016}
Václav Kubernát72749c62020-01-03 16:47:34 +010017
18void ParserContext::clearPath()
19{
20 m_curPath = dataPath_{Scope::Absolute, {}};
Václav Kubernát72749c62020-01-03 16:47:34 +010021}
22
23schemaPath_ ParserContext::currentSchemaPath()
24{
25 if (m_curPath.type() == typeid(dataPath_)) {
26 return dataPathToSchemaPath(boost::get<dataPath_>(m_curPath));
27 } else {
28 return boost::get<schemaPath_>(m_curPath);
29 }
30}
31
32dataPath_ ParserContext::currentDataPath()
33{
34 if (m_curPath.type() != typeid(dataPath_)) {
35 throw std::runtime_error("Tried getting a dataPath_ from ParserContext when only schemaPath_ was available.");
36 }
37 return boost::get<dataPath_>(m_curPath);
38}
39
40void ParserContext::pushPathFragment(const dataNode_& node)
41{
Václav Kubernátf3e377b2020-05-28 23:26:38 +020042 auto pushNode = [] (auto& where, const auto& what) {
Václav Kubernátb5ca1542020-05-27 01:03:54 +020043 if (std::holds_alternative<nodeup_>(what.m_suffix)) {
Václav Kubernátc2bfe492020-06-15 13:19:07 +020044 if (!where.m_nodes.empty()) { // Allow going up, when already at root
45 where.m_nodes.pop_back();
46 }
Václav Kubernát72749c62020-01-03 16:47:34 +010047 } else {
48 where.m_nodes.push_back(what);
49 }
50 };
51
52 if (m_curPath.type() == typeid(dataPath_)) {
53 pushNode(boost::get<dataPath_>(m_curPath), node);
54 } else {
55 pushNode(boost::get<schemaPath_>(m_curPath), dataNodeToSchemaNode(node));
56 }
57}
58
59void ParserContext::pushPathFragment(const schemaNode_& node)
60{
61 if (m_curPath.type() == typeid(dataPath_)) {
62 m_curPath = dataPathToSchemaPath(boost::get<dataPath_>(m_curPath));
63 }
64
65 boost::get<schemaPath_>(m_curPath).m_nodes.push_back(node);
Václav Kubernát72749c62020-01-03 16:47:34 +010066}
67
68void ParserContext::resetPath()
69{
70 m_curPath = m_curPathOrig;
Václav Kubernát72749c62020-01-03 16:47:34 +010071}