blob: 723fbae2b8eec26a9f08c4aeb0de6f8e505dd09d [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át72749c62020-01-03 16:47:34 +010044 where.m_nodes.pop_back();
Václav Kubernát72749c62020-01-03 16:47:34 +010045 } else {
46 where.m_nodes.push_back(what);
47 }
48 };
49
50 if (m_curPath.type() == typeid(dataPath_)) {
51 pushNode(boost::get<dataPath_>(m_curPath), node);
52 } else {
53 pushNode(boost::get<schemaPath_>(m_curPath), dataNodeToSchemaNode(node));
54 }
55}
56
57void ParserContext::pushPathFragment(const schemaNode_& node)
58{
59 if (m_curPath.type() == typeid(dataPath_)) {
60 m_curPath = dataPathToSchemaPath(boost::get<dataPath_>(m_curPath));
61 }
62
63 boost::get<schemaPath_>(m_curPath).m_nodes.push_back(node);
Václav Kubernát72749c62020-01-03 16:47:34 +010064}
65
66void ParserContext::resetPath()
67{
68 m_curPath = m_curPathOrig;
Václav Kubernát72749c62020-01-03 16:47:34 +010069}