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