Václav Kubernát | 195eeea | 2018-05-18 13:52:36 +0200 | [diff] [blame] | 1 | /* |
| 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át | 43908fb | 2020-01-02 19:05:51 +0100 | [diff] [blame^] | 10 | ParserContext::ParserContext(const Schema& schema, const std::shared_ptr<const DataQuery> dataQuery, const dataPath_& curDir) |
Václav Kubernát | 48fc383 | 2018-05-28 14:21:22 +0200 | [diff] [blame] | 11 | : m_schema(schema) |
Václav Kubernát | 5c75b25 | 2018-10-10 18:33:47 +0200 | [diff] [blame] | 12 | , m_curPathOrig(curDir) |
Václav Kubernát | 43908fb | 2020-01-02 19:05:51 +0100 | [diff] [blame^] | 13 | , m_dataquery(dataQuery) |
Václav Kubernát | 72749c6 | 2020-01-03 16:47:34 +0100 | [diff] [blame] | 14 | , m_curPath(curDir) |
Václav Kubernát | 195eeea | 2018-05-18 13:52:36 +0200 | [diff] [blame] | 15 | { |
Václav Kubernát | 72749c6 | 2020-01-03 16:47:34 +0100 | [diff] [blame] | 16 | if (!currentDataPath().m_nodes.empty() && currentDataPath().m_nodes.at(0).m_prefix) |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 17 | m_topLevelModulePresent = true; |
Václav Kubernát | 195eeea | 2018-05-18 13:52:36 +0200 | [diff] [blame] | 18 | } |
Václav Kubernát | 72749c6 | 2020-01-03 16:47:34 +0100 | [diff] [blame] | 19 | |
| 20 | void ParserContext::clearPath() |
| 21 | { |
| 22 | m_curPath = dataPath_{Scope::Absolute, {}}; |
| 23 | m_topLevelModulePresent = false; |
| 24 | } |
| 25 | |
| 26 | schemaPath_ 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 | |
| 35 | dataPath_ 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 | |
| 43 | void 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 | |
| 63 | void 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 | |
| 75 | void ParserContext::resetPath() |
| 76 | { |
| 77 | m_curPath = m_curPathOrig; |
| 78 | m_topLevelModulePresent = !currentDataPath().m_nodes.empty() && currentDataPath().m_nodes.at(0).m_prefix; |
| 79 | } |