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"; |
| 29 | x3::rule<schemaNode_class, schemaNode_> const schemaNode = "schemaNode"; |
| 30 | x3::rule<absoluteStart_class, Scope> const absoluteStart = "absoluteStart"; |
| 31 | x3::rule<keyValue_class, keyValue_> const keyValue = "keyValue"; |
| 32 | x3::rule<key_identifier_class, std::string> const key_identifier = "key_identifier"; |
| 33 | x3::rule<listPrefix_class, std::string> const listPrefix = "listPrefix"; |
| 34 | x3::rule<listSuffix_class, std::vector<keyValue_>> const listSuffix = "listSuffix"; |
| 35 | x3::rule<listElement_class, listElement_> const listElement = "listElement"; |
| 36 | x3::rule<list_class, list_> const list = "list"; |
| 37 | x3::rule<nodeup_class, nodeup_> const nodeup = "nodeup"; |
| 38 | x3::rule<container_class, container_> const container = "container"; |
| 39 | x3::rule<leaf_class, leaf_> const leaf = "leaf"; |
| 40 | x3::rule<createKeySuggestions_class, x3::unused_type> const createKeySuggestions = "createKeySuggestions"; |
| 41 | x3::rule<createValueSuggestions_class, x3::unused_type> const createValueSuggestions = "createValueSuggestions"; |
| 42 | x3::rule<suggestKeysEnd_class, x3::unused_type> const suggestKeysEnd = "suggestKeysEnd"; |
| 43 | |
| 44 | |
| 45 | #if __clang__ |
| 46 | #pragma GCC diagnostic push |
| 47 | #pragma GCC diagnostic ignored "-Woverloaded-shift-op-parentheses" |
| 48 | #endif |
| 49 | |
| 50 | auto const rest = |
| 51 | x3::omit[x3::no_skip[+(x3::char_ - '/' - space_separator)]]; |
| 52 | |
| 53 | auto const key_identifier_def = |
| 54 | x3::lexeme[ |
| 55 | ((x3::alpha | char_("_")) >> *(x3::alnum | char_("_") | char_("-") | char_("."))) |
| 56 | ]; |
| 57 | |
| 58 | auto const createKeySuggestions_def = |
| 59 | x3::eps; |
| 60 | |
| 61 | auto const createValueSuggestions_def = |
| 62 | x3::eps; |
| 63 | |
| 64 | auto const suggestKeysEnd_def = |
| 65 | x3::eps; |
| 66 | |
| 67 | auto const keyValue_def = |
| 68 | key_identifier > '=' > createValueSuggestions > leaf_data; |
| 69 | |
| 70 | auto const keyValueWrapper = |
| 71 | x3::lexeme['[' > createKeySuggestions > keyValue > suggestKeysEnd > ']']; |
| 72 | |
| 73 | auto const listPrefix_def = |
| 74 | node_identifier; |
| 75 | |
| 76 | // even though we don't allow no keys to be supplied, the star allows me to check which keys are missing |
| 77 | auto const listSuffix_def = |
| 78 | *keyValueWrapper; |
| 79 | |
| 80 | auto const listElement_def = |
| 81 | listPrefix >> &char_('[') > listSuffix; |
| 82 | |
| 83 | auto const list_def = |
| 84 | node_identifier >> !char_('['); |
| 85 | |
| 86 | auto const nodeup_def = |
| 87 | x3::lit("..") > x3::attr(nodeup_()); |
| 88 | |
| 89 | auto const container_def = |
| 90 | node_identifier; |
| 91 | |
| 92 | auto const leaf_def = |
| 93 | node_identifier; |
| 94 | |
| 95 | auto const absoluteStart_def = |
| 96 | x3::omit['/'] >> x3::attr(Scope::Absolute); |
| 97 | |
| 98 | auto const trailingSlash_def = |
| 99 | x3::omit['/'] >> x3::attr(TrailingSlash::Present); |
| 100 | |
| 101 | auto const createPathSuggestions_def = |
| 102 | x3::eps; |
| 103 | |
| 104 | // leaf cannot be in the middle of a path, however, I need the grammar's attribute to be a vector of variants |
| 105 | auto const schemaNode_def = |
| 106 | createPathSuggestions >> -(module) >> (container | list | nodeup | leaf); |
| 107 | |
| 108 | auto const dataNode_def = |
| 109 | createPathSuggestions >> -(module) >> (container | listElement | nodeup | leaf); |
| 110 | |
| 111 | // I have to insert an empty vector to the first alternative, otherwise they won't have the same attribute |
| 112 | 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)); |
| 113 | |
| 114 | auto const dataNodeList_def = |
| 115 | createPathSuggestions >> -(module) >> list; |
| 116 | |
| 117 | // This intermediate rule is mandatory, because we need the first alternative |
| 118 | // to be collapsed to a vector. If we didn't use the intermediate rule, |
| 119 | // Spirit wouldn't know we want it to collapse. |
| 120 | // https://github.com/boostorg/spirit/issues/408 |
| 121 | auto const dataNodesListEnd_def = |
| 122 | dataNode % '/' >> '/' >> dataNodeList >> -(&char_('/') >> createPathSuggestions) | |
| 123 | x3::attr(decltype(dataPath_::m_nodes)()) >> dataNodeList; |
| 124 | |
| 125 | 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)); |
| 126 | |
| 127 | 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)); |
| 128 | |
| 129 | auto const leafPath_def = |
| 130 | dataPath; |
| 131 | |
| 132 | auto const presenceContainerPath_def = |
| 133 | dataPath; |
| 134 | |
| 135 | auto const listInstancePath_def = |
| 136 | dataPath; |
| 137 | |
| 138 | // A "nothing" parser, which is used to indicate we tried to parse a path |
| 139 | auto const initializePath_def = |
| 140 | x3::eps; |
| 141 | |
| 142 | |
| 143 | |
| 144 | #if __clang__ |
| 145 | #pragma GCC diagnostic pop |
| 146 | #endif |
| 147 | |
| 148 | BOOST_SPIRIT_DEFINE(keyValue) |
| 149 | BOOST_SPIRIT_DEFINE(key_identifier) |
| 150 | BOOST_SPIRIT_DEFINE(listPrefix) |
| 151 | BOOST_SPIRIT_DEFINE(listSuffix) |
| 152 | BOOST_SPIRIT_DEFINE(listElement) |
| 153 | BOOST_SPIRIT_DEFINE(list) |
| 154 | BOOST_SPIRIT_DEFINE(nodeup) |
| 155 | BOOST_SPIRIT_DEFINE(schemaNode) |
| 156 | BOOST_SPIRIT_DEFINE(dataNode) |
| 157 | BOOST_SPIRIT_DEFINE(container) |
| 158 | BOOST_SPIRIT_DEFINE(leaf) |
| 159 | BOOST_SPIRIT_DEFINE(dataNodeList) |
| 160 | BOOST_SPIRIT_DEFINE(dataNodesListEnd) |
| 161 | BOOST_SPIRIT_DEFINE(leafPath) |
| 162 | BOOST_SPIRIT_DEFINE(presenceContainerPath) |
| 163 | BOOST_SPIRIT_DEFINE(listInstancePath) |
| 164 | BOOST_SPIRIT_DEFINE(schemaPath) |
| 165 | BOOST_SPIRIT_DEFINE(dataPath) |
| 166 | BOOST_SPIRIT_DEFINE(dataPathListEnd) |
| 167 | BOOST_SPIRIT_DEFINE(initializePath) |
| 168 | BOOST_SPIRIT_DEFINE(createKeySuggestions) |
| 169 | BOOST_SPIRIT_DEFINE(createPathSuggestions) |
| 170 | BOOST_SPIRIT_DEFINE(createValueSuggestions) |
| 171 | BOOST_SPIRIT_DEFINE(suggestKeysEnd) |
| 172 | BOOST_SPIRIT_DEFINE(absoluteStart) |
| 173 | BOOST_SPIRIT_DEFINE(trailingSlash) |