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 | 72749c6 | 2020-01-03 16:47:34 +0100 | [diff] [blame] | 10 | ParserContext::ParserContext(const Schema& schema, 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 | 72749c6 | 2020-01-03 16:47:34 +0100 | [diff] [blame] | 13 | , m_curPath(curDir) |
Václav Kubernát | 195eeea | 2018-05-18 13:52:36 +0200 | [diff] [blame] | 14 | { |
Václav Kubernát | 72749c6 | 2020-01-03 16:47:34 +0100 | [diff] [blame] | 15 | 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] | 16 | m_topLevelModulePresent = true; |
Václav Kubernát | 195eeea | 2018-05-18 13:52:36 +0200 | [diff] [blame] | 17 | } |
Václav Kubernát | 72749c6 | 2020-01-03 16:47:34 +0100 | [diff] [blame] | 18 | |
| 19 | void ParserContext::clearPath() |
| 20 | { |
| 21 | m_curPath = dataPath_{Scope::Absolute, {}}; |
| 22 | m_topLevelModulePresent = false; |
| 23 | } |
| 24 | |
| 25 | schemaPath_ 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 | |
| 34 | dataPath_ 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 | |
| 42 | void 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 | |
| 62 | void 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 | |
| 74 | void ParserContext::resetPath() |
| 75 | { |
| 76 | m_curPath = m_curPathOrig; |
| 77 | m_topLevelModulePresent = !currentDataPath().m_nodes.empty() && currentDataPath().m_nodes.at(0).m_prefix; |
| 78 | } |