blob: af98208ea0f12a25182d202868276374df661fdb [file] [log] [blame]
Václav Kubernátd0ea9b22020-04-24 00:44:15 +02001/*
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
15namespace x3 = boost::spirit::x3;
16
17x3::rule<dataPath_class, dataPath_> const dataPath = "dataPath";
18x3::rule<schemaPath_class, schemaPath_> const schemaPath = "schemaPath";
19x3::rule<dataNodeList_class, decltype(dataPath_::m_nodes)::value_type> const dataNodeList = "dataNodeList";
20x3::rule<dataNodesListEnd_class, decltype(dataPath_::m_nodes)> const dataNodesListEnd = "dataNodesListEnd";
21x3::rule<dataPathListEnd_class, dataPath_> const dataPathListEnd = "dataPathListEnd";
22x3::rule<leaf_path_class, dataPath_> const leafPath = "leafPath";
23x3::rule<presenceContainerPath_class, dataPath_> const presenceContainerPath = "presenceContainerPath";
24x3::rule<listInstancePath_class, dataPath_> const listInstancePath = "listInstancePath";
25x3::rule<initializePath_class, x3::unused_type> const initializePath = "initializePath";
26x3::rule<createPathSuggestions_class, x3::unused_type> const createPathSuggestions = "createPathSuggestions";
27x3::rule<trailingSlash_class, TrailingSlash> const trailingSlash = "trailingSlash";
28x3::rule<dataNode_class, dataNode_> const dataNode = "dataNode";
29x3::rule<schemaNode_class, schemaNode_> const schemaNode = "schemaNode";
30x3::rule<absoluteStart_class, Scope> const absoluteStart = "absoluteStart";
31x3::rule<keyValue_class, keyValue_> const keyValue = "keyValue";
32x3::rule<key_identifier_class, std::string> const key_identifier = "key_identifier";
33x3::rule<listPrefix_class, std::string> const listPrefix = "listPrefix";
34x3::rule<listSuffix_class, std::vector<keyValue_>> const listSuffix = "listSuffix";
35x3::rule<listElement_class, listElement_> const listElement = "listElement";
36x3::rule<list_class, list_> const list = "list";
37x3::rule<nodeup_class, nodeup_> const nodeup = "nodeup";
38x3::rule<container_class, container_> const container = "container";
39x3::rule<leaf_class, leaf_> const leaf = "leaf";
40x3::rule<createKeySuggestions_class, x3::unused_type> const createKeySuggestions = "createKeySuggestions";
41x3::rule<createValueSuggestions_class, x3::unused_type> const createValueSuggestions = "createValueSuggestions";
42x3::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
50auto const rest =
51 x3::omit[x3::no_skip[+(x3::char_ - '/' - space_separator)]];
52
53auto const key_identifier_def =
54 x3::lexeme[
55 ((x3::alpha | char_("_")) >> *(x3::alnum | char_("_") | char_("-") | char_(".")))
56 ];
57
58auto const createKeySuggestions_def =
59 x3::eps;
60
61auto const createValueSuggestions_def =
62 x3::eps;
63
64auto const suggestKeysEnd_def =
65 x3::eps;
66
67auto const keyValue_def =
68 key_identifier > '=' > createValueSuggestions > leaf_data;
69
70auto const keyValueWrapper =
71 x3::lexeme['[' > createKeySuggestions > keyValue > suggestKeysEnd > ']'];
72
73auto 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
77auto const listSuffix_def =
78 *keyValueWrapper;
79
80auto const listElement_def =
81 listPrefix >> &char_('[') > listSuffix;
82
83auto const list_def =
84 node_identifier >> !char_('[');
85
86auto const nodeup_def =
87 x3::lit("..") > x3::attr(nodeup_());
88
89auto const container_def =
90 node_identifier;
91
92auto const leaf_def =
93 node_identifier;
94
95auto const absoluteStart_def =
96 x3::omit['/'] >> x3::attr(Scope::Absolute);
97
98auto const trailingSlash_def =
99 x3::omit['/'] >> x3::attr(TrailingSlash::Present);
100
101auto 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
105auto const schemaNode_def =
106 createPathSuggestions >> -(module) >> (container | list | nodeup | leaf);
107
108auto 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
112auto 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
114auto 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
121auto const dataNodesListEnd_def =
122 dataNode % '/' >> '/' >> dataNodeList >> -(&char_('/') >> createPathSuggestions) |
123 x3::attr(decltype(dataPath_::m_nodes)()) >> dataNodeList;
124
125auto 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
127auto 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
129auto const leafPath_def =
130 dataPath;
131
132auto const presenceContainerPath_def =
133 dataPath;
134
135auto const listInstancePath_def =
136 dataPath;
137
138// A "nothing" parser, which is used to indicate we tried to parse a path
139auto const initializePath_def =
140 x3::eps;
141
142
143
144#if __clang__
145#pragma GCC diagnostic pop
146#endif
147
148BOOST_SPIRIT_DEFINE(keyValue)
149BOOST_SPIRIT_DEFINE(key_identifier)
150BOOST_SPIRIT_DEFINE(listPrefix)
151BOOST_SPIRIT_DEFINE(listSuffix)
152BOOST_SPIRIT_DEFINE(listElement)
153BOOST_SPIRIT_DEFINE(list)
154BOOST_SPIRIT_DEFINE(nodeup)
155BOOST_SPIRIT_DEFINE(schemaNode)
156BOOST_SPIRIT_DEFINE(dataNode)
157BOOST_SPIRIT_DEFINE(container)
158BOOST_SPIRIT_DEFINE(leaf)
159BOOST_SPIRIT_DEFINE(dataNodeList)
160BOOST_SPIRIT_DEFINE(dataNodesListEnd)
161BOOST_SPIRIT_DEFINE(leafPath)
162BOOST_SPIRIT_DEFINE(presenceContainerPath)
163BOOST_SPIRIT_DEFINE(listInstancePath)
164BOOST_SPIRIT_DEFINE(schemaPath)
165BOOST_SPIRIT_DEFINE(dataPath)
166BOOST_SPIRIT_DEFINE(dataPathListEnd)
167BOOST_SPIRIT_DEFINE(initializePath)
168BOOST_SPIRIT_DEFINE(createKeySuggestions)
169BOOST_SPIRIT_DEFINE(createPathSuggestions)
170BOOST_SPIRIT_DEFINE(createValueSuggestions)
171BOOST_SPIRIT_DEFINE(suggestKeysEnd)
172BOOST_SPIRIT_DEFINE(absoluteStart)
173BOOST_SPIRIT_DEFINE(trailingSlash)