Václav Kubernát | d0ea9b2 | 2020-04-24 00:44:15 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 CESNET, https://photonics.cesnet.cz/ |
| 3 | * |
| 4 | * Written by Václav Kubernát <kubernat@cesnet.cz> |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #pragma once |
| 9 | |
| 10 | #include <boost/spirit/home/x3.hpp> |
| 11 | #include "ast_handlers.hpp" |
| 12 | #include "common_parsers.hpp" |
| 13 | #include "leaf_data.hpp" |
| 14 | |
| 15 | namespace x3 = boost::spirit::x3; |
| 16 | |
| 17 | x3::rule<dataPath_class, dataPath_> const dataPath = "dataPath"; |
| 18 | x3::rule<schemaPath_class, schemaPath_> const schemaPath = "schemaPath"; |
| 19 | x3::rule<dataNodeList_class, decltype(dataPath_::m_nodes)::value_type> const dataNodeList = "dataNodeList"; |
| 20 | x3::rule<dataNodesListEnd_class, decltype(dataPath_::m_nodes)> const dataNodesListEnd = "dataNodesListEnd"; |
| 21 | x3::rule<dataPathListEnd_class, dataPath_> const dataPathListEnd = "dataPathListEnd"; |
| 22 | x3::rule<leaf_path_class, dataPath_> const leafPath = "leafPath"; |
| 23 | x3::rule<presenceContainerPath_class, dataPath_> const presenceContainerPath = "presenceContainerPath"; |
| 24 | x3::rule<listInstancePath_class, dataPath_> const listInstancePath = "listInstancePath"; |
| 25 | x3::rule<initializePath_class, x3::unused_type> const initializePath = "initializePath"; |
| 26 | x3::rule<createPathSuggestions_class, x3::unused_type> const createPathSuggestions = "createPathSuggestions"; |
| 27 | x3::rule<trailingSlash_class, TrailingSlash> const trailingSlash = "trailingSlash"; |
| 28 | x3::rule<dataNode_class, dataNode_> const dataNode = "dataNode"; |
Václav Kubernát | d0ea9b2 | 2020-04-24 00:44:15 +0200 | [diff] [blame] | 29 | x3::rule<absoluteStart_class, Scope> const absoluteStart = "absoluteStart"; |
| 30 | x3::rule<keyValue_class, keyValue_> const keyValue = "keyValue"; |
| 31 | x3::rule<key_identifier_class, std::string> const key_identifier = "key_identifier"; |
| 32 | x3::rule<listPrefix_class, std::string> const listPrefix = "listPrefix"; |
| 33 | x3::rule<listSuffix_class, std::vector<keyValue_>> const listSuffix = "listSuffix"; |
| 34 | x3::rule<listElement_class, listElement_> const listElement = "listElement"; |
| 35 | x3::rule<list_class, list_> const list = "list"; |
| 36 | x3::rule<nodeup_class, nodeup_> const nodeup = "nodeup"; |
| 37 | x3::rule<container_class, container_> const container = "container"; |
| 38 | x3::rule<leaf_class, leaf_> const leaf = "leaf"; |
| 39 | x3::rule<createKeySuggestions_class, x3::unused_type> const createKeySuggestions = "createKeySuggestions"; |
| 40 | x3::rule<createValueSuggestions_class, x3::unused_type> const createValueSuggestions = "createValueSuggestions"; |
| 41 | x3::rule<suggestKeysEnd_class, x3::unused_type> const suggestKeysEnd = "suggestKeysEnd"; |
| 42 | |
| 43 | |
Václav Kubernát | 60a0ed5 | 2020-04-28 15:21:33 +0200 | [diff] [blame^] | 44 | struct schemaNode : x3::parser<schemaNode> { |
| 45 | using attribute_type = schemaNode_; |
| 46 | template <typename It, typename Ctx, typename RCtx, typename Attr> |
| 47 | bool parse(It& begin, It end, Ctx const& ctx, RCtx& rctx, Attr& attr) const |
| 48 | { |
| 49 | x3::symbols<schemaNode_> table(std::string{"schemaNode"}); // The constructor doesn't work with just the string literal |
| 50 | |
| 51 | ParserContext& parserContext = x3::get<parser_context_tag>(ctx); |
| 52 | parserContext.m_suggestions.clear(); |
| 53 | for (const auto& child : parserContext.m_schema.availableNodes(parserContext.currentSchemaPath(), Recursion::NonRecursive)) { |
| 54 | schemaNode_ out; |
| 55 | std::string parseString; |
| 56 | if (child.first) { |
| 57 | out.m_prefix = module_{*child.first}; |
| 58 | parseString = *child.first + ":"; |
| 59 | } |
| 60 | parseString += child.second; |
| 61 | switch (parserContext.m_schema.nodeType(parserContext.currentSchemaPath(), child)) { |
| 62 | case yang::NodeTypes::Container: |
| 63 | case yang::NodeTypes::PresenceContainer: |
| 64 | out.m_suffix = container_{child.second}; |
| 65 | parserContext.m_suggestions.emplace(Completion{parseString + "/"}); |
| 66 | break; |
| 67 | case yang::NodeTypes::Leaf: |
| 68 | out.m_suffix = leaf_{child.second}; |
| 69 | parserContext.m_suggestions.emplace(Completion{parseString + " "}); |
| 70 | break; |
| 71 | case yang::NodeTypes::List: |
| 72 | out.m_suffix = list_{child.second}; |
| 73 | parserContext.m_suggestions.emplace(Completion{parseString, "[", Completion::WhenToAdd::IfFullMatch}); |
| 74 | break; |
| 75 | case yang::NodeTypes::Action: |
| 76 | case yang::NodeTypes::AnyXml: |
| 77 | case yang::NodeTypes::LeafList: |
| 78 | case yang::NodeTypes::Notification: |
| 79 | case yang::NodeTypes::Rpc: |
| 80 | continue; |
| 81 | } |
| 82 | table.add(parseString, out); |
| 83 | table.add("..", attribute_type{nodeup_{}}); |
| 84 | if (!child.first) { |
| 85 | auto topLevelModule = parserContext.currentSchemaPath().m_nodes.begin()->m_prefix; |
| 86 | out.m_prefix = topLevelModule; |
| 87 | table.add(topLevelModule->m_name + ":" + parseString, out); |
| 88 | } |
| 89 | } |
| 90 | auto res = table.parse(begin, end, ctx, rctx, attr); |
| 91 | if (res) { |
| 92 | parserContext.pushPathFragment(attr); |
| 93 | } |
| 94 | return res; |
| 95 | } |
| 96 | } schemaNode; |
| 97 | |
Václav Kubernát | d0ea9b2 | 2020-04-24 00:44:15 +0200 | [diff] [blame] | 98 | #if __clang__ |
| 99 | #pragma GCC diagnostic push |
| 100 | #pragma GCC diagnostic ignored "-Woverloaded-shift-op-parentheses" |
| 101 | #endif |
| 102 | |
| 103 | auto const rest = |
| 104 | x3::omit[x3::no_skip[+(x3::char_ - '/' - space_separator)]]; |
| 105 | |
| 106 | auto const key_identifier_def = |
| 107 | x3::lexeme[ |
| 108 | ((x3::alpha | char_("_")) >> *(x3::alnum | char_("_") | char_("-") | char_("."))) |
| 109 | ]; |
| 110 | |
| 111 | auto const createKeySuggestions_def = |
| 112 | x3::eps; |
| 113 | |
| 114 | auto const createValueSuggestions_def = |
| 115 | x3::eps; |
| 116 | |
| 117 | auto const suggestKeysEnd_def = |
| 118 | x3::eps; |
| 119 | |
| 120 | auto const keyValue_def = |
| 121 | key_identifier > '=' > createValueSuggestions > leaf_data; |
| 122 | |
| 123 | auto const keyValueWrapper = |
| 124 | x3::lexeme['[' > createKeySuggestions > keyValue > suggestKeysEnd > ']']; |
| 125 | |
| 126 | auto const listPrefix_def = |
| 127 | node_identifier; |
| 128 | |
| 129 | // even though we don't allow no keys to be supplied, the star allows me to check which keys are missing |
| 130 | auto const listSuffix_def = |
| 131 | *keyValueWrapper; |
| 132 | |
| 133 | auto const listElement_def = |
| 134 | listPrefix >> &char_('[') > listSuffix; |
| 135 | |
| 136 | auto const list_def = |
| 137 | node_identifier >> !char_('['); |
| 138 | |
| 139 | auto const nodeup_def = |
| 140 | x3::lit("..") > x3::attr(nodeup_()); |
| 141 | |
| 142 | auto const container_def = |
| 143 | node_identifier; |
| 144 | |
| 145 | auto const leaf_def = |
| 146 | node_identifier; |
| 147 | |
| 148 | auto const absoluteStart_def = |
| 149 | x3::omit['/'] >> x3::attr(Scope::Absolute); |
| 150 | |
| 151 | auto const trailingSlash_def = |
| 152 | x3::omit['/'] >> x3::attr(TrailingSlash::Present); |
| 153 | |
| 154 | auto const createPathSuggestions_def = |
| 155 | x3::eps; |
| 156 | |
Václav Kubernát | d0ea9b2 | 2020-04-24 00:44:15 +0200 | [diff] [blame] | 157 | auto const dataNode_def = |
| 158 | createPathSuggestions >> -(module) >> (container | listElement | nodeup | leaf); |
| 159 | |
| 160 | // I have to insert an empty vector to the first alternative, otherwise they won't have the same attribute |
| 161 | auto const dataPath_def = initializePath >> absoluteStart >> createPathSuggestions >> x3::attr(decltype(dataPath_::m_nodes)()) >> x3::attr(TrailingSlash::NonPresent) >> x3::eoi | initializePath >> -(absoluteStart >> createPathSuggestions) >> dataNode % '/' >> (-(trailingSlash >> createPathSuggestions) >> -(completing >> rest) >> (&space_separator | x3::eoi)); |
| 162 | |
| 163 | auto const dataNodeList_def = |
| 164 | createPathSuggestions >> -(module) >> list; |
| 165 | |
| 166 | // This intermediate rule is mandatory, because we need the first alternative |
| 167 | // to be collapsed to a vector. If we didn't use the intermediate rule, |
| 168 | // Spirit wouldn't know we want it to collapse. |
| 169 | // https://github.com/boostorg/spirit/issues/408 |
| 170 | auto const dataNodesListEnd_def = |
| 171 | dataNode % '/' >> '/' >> dataNodeList >> -(&char_('/') >> createPathSuggestions) | |
| 172 | x3::attr(decltype(dataPath_::m_nodes)()) >> dataNodeList; |
| 173 | |
| 174 | auto const dataPathListEnd_def = initializePath >> absoluteStart >> createPathSuggestions >> x3::attr(decltype(dataPath_::m_nodes)()) >> x3::attr(TrailingSlash::NonPresent) >> x3::eoi | initializePath >> -(absoluteStart >> createPathSuggestions) >> dataNodesListEnd >> (-(trailingSlash >> createPathSuggestions) >> -(completing >> rest) >> (&space_separator | x3::eoi)); |
| 175 | |
| 176 | auto const schemaPath_def = initializePath >> absoluteStart >> createPathSuggestions >> x3::attr(decltype(schemaPath_::m_nodes)()) >> x3::attr(TrailingSlash::NonPresent) >> x3::eoi | initializePath >> -(absoluteStart >> createPathSuggestions) >> schemaNode % '/' >> (-(trailingSlash >> createPathSuggestions) >> -(completing >> rest) >> (&space_separator | x3::eoi)); |
| 177 | |
| 178 | auto const leafPath_def = |
| 179 | dataPath; |
| 180 | |
| 181 | auto const presenceContainerPath_def = |
| 182 | dataPath; |
| 183 | |
| 184 | auto const listInstancePath_def = |
| 185 | dataPath; |
| 186 | |
| 187 | // A "nothing" parser, which is used to indicate we tried to parse a path |
| 188 | auto const initializePath_def = |
| 189 | x3::eps; |
| 190 | |
| 191 | |
| 192 | |
| 193 | #if __clang__ |
| 194 | #pragma GCC diagnostic pop |
| 195 | #endif |
| 196 | |
| 197 | BOOST_SPIRIT_DEFINE(keyValue) |
| 198 | BOOST_SPIRIT_DEFINE(key_identifier) |
| 199 | BOOST_SPIRIT_DEFINE(listPrefix) |
| 200 | BOOST_SPIRIT_DEFINE(listSuffix) |
| 201 | BOOST_SPIRIT_DEFINE(listElement) |
| 202 | BOOST_SPIRIT_DEFINE(list) |
| 203 | BOOST_SPIRIT_DEFINE(nodeup) |
Václav Kubernát | d0ea9b2 | 2020-04-24 00:44:15 +0200 | [diff] [blame] | 204 | BOOST_SPIRIT_DEFINE(dataNode) |
| 205 | BOOST_SPIRIT_DEFINE(container) |
| 206 | BOOST_SPIRIT_DEFINE(leaf) |
| 207 | BOOST_SPIRIT_DEFINE(dataNodeList) |
| 208 | BOOST_SPIRIT_DEFINE(dataNodesListEnd) |
| 209 | BOOST_SPIRIT_DEFINE(leafPath) |
| 210 | BOOST_SPIRIT_DEFINE(presenceContainerPath) |
| 211 | BOOST_SPIRIT_DEFINE(listInstancePath) |
| 212 | BOOST_SPIRIT_DEFINE(schemaPath) |
| 213 | BOOST_SPIRIT_DEFINE(dataPath) |
| 214 | BOOST_SPIRIT_DEFINE(dataPathListEnd) |
| 215 | BOOST_SPIRIT_DEFINE(initializePath) |
| 216 | BOOST_SPIRIT_DEFINE(createKeySuggestions) |
| 217 | BOOST_SPIRIT_DEFINE(createPathSuggestions) |
| 218 | BOOST_SPIRIT_DEFINE(createValueSuggestions) |
| 219 | BOOST_SPIRIT_DEFINE(suggestKeysEnd) |
| 220 | BOOST_SPIRIT_DEFINE(absoluteStart) |
| 221 | BOOST_SPIRIT_DEFINE(trailingSlash) |