blob: 26e6fa32b0d2f7b930e617da95495380a93df560 [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át72749c62020-01-03 16:47:34 +010016 if (!currentDataPath().m_nodes.empty() && currentDataPath().m_nodes.at(0).m_prefix)
Václav Kubernát744f57f2018-06-29 22:46:26 +020017 m_topLevelModulePresent = true;
Václav Kubernát195eeea2018-05-18 13:52:36 +020018}
Václav Kubernát72749c62020-01-03 16:47:34 +010019
20void ParserContext::clearPath()
21{
22 m_curPath = dataPath_{Scope::Absolute, {}};
23 m_topLevelModulePresent = false;
24}
25
26schemaPath_ ParserContext::currentSchemaPath()
27{
28 if (m_curPath.type() == typeid(dataPath_)) {
29 return dataPathToSchemaPath(boost::get<dataPath_>(m_curPath));
30 } else {
31 return boost::get<schemaPath_>(m_curPath);
32 }
33}
34
35dataPath_ ParserContext::currentDataPath()
36{
37 if (m_curPath.type() != typeid(dataPath_)) {
38 throw std::runtime_error("Tried getting a dataPath_ from ParserContext when only schemaPath_ was available.");
39 }
40 return boost::get<dataPath_>(m_curPath);
41}
42
43void ParserContext::pushPathFragment(const dataNode_& node)
44{
45 auto pushNode = [this] (auto& where, const auto& what) {
46 if (what.m_suffix.type() == typeid(nodeup_)) {
47 where.m_nodes.pop_back();
48 if (where.m_nodes.empty()) {
49 m_topLevelModulePresent = false;
50 }
51 } else {
52 where.m_nodes.push_back(what);
53 }
54 };
55
56 if (m_curPath.type() == typeid(dataPath_)) {
57 pushNode(boost::get<dataPath_>(m_curPath), node);
58 } else {
59 pushNode(boost::get<schemaPath_>(m_curPath), dataNodeToSchemaNode(node));
60 }
61}
62
63void ParserContext::pushPathFragment(const schemaNode_& node)
64{
65 if (m_curPath.type() == typeid(dataPath_)) {
66 m_curPath = dataPathToSchemaPath(boost::get<dataPath_>(m_curPath));
67 }
68
69 boost::get<schemaPath_>(m_curPath).m_nodes.push_back(node);
70 if (currentSchemaPath().m_nodes.empty()) {
71 m_topLevelModulePresent = false;
72 }
73}
74
75void ParserContext::resetPath()
76{
77 m_curPath = m_curPathOrig;
78 m_topLevelModulePresent = !currentDataPath().m_nodes.empty() && currentDataPath().m_nodes.at(0).m_prefix;
79}