blob: 7caa45cce7a5e378307986d17537de1c120c3dbf [file] [log] [blame]
Václav Kubernát0a2a2e82018-05-11 13:59:12 +02001/*
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#pragma once
10
Václav Kubernát24df80e2018-06-06 15:18:03 +020011#include "ast_commands.hpp"
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020012#include "ast_handlers.hpp"
13
Václav Kubernát60d6f292018-05-25 09:45:32 +020014
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020015x3::rule<keyValue_class, keyValue_> const keyValue = "keyValue";
Václav Kubernát89728d82018-09-13 16:28:28 +020016x3::rule<key_identifier_class, std::string> const key_identifier = "key_identifier";
Václav Kubernát744f57f2018-06-29 22:46:26 +020017x3::rule<node_identifier_class, std::string> const node_identifier = "node_identifier";
18x3::rule<module_identifier_class, std::string> const module_identifier = "module_identifier";
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020019x3::rule<listPrefix_class, std::string> const listPrefix = "listPrefix";
20x3::rule<listSuffix_class, std::vector<keyValue_>> const listSuffix = "listSuffix";
21x3::rule<listElement_class, listElement_> const listElement = "listElement";
Václav Kubernát2eaceb82018-10-08 19:56:30 +020022x3::rule<list_class, list_> const list = "list";
Václav Kubernát60d6f292018-05-25 09:45:32 +020023x3::rule<nodeup_class, nodeup_> const nodeup = "nodeup";
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020024x3::rule<container_class, container_> const container = "container";
Václav Kubernát07204242018-06-04 18:12:09 +020025x3::rule<leaf_class, leaf_> const leaf = "leaf";
Václav Kubernát744f57f2018-06-29 22:46:26 +020026x3::rule<module_class, module_> const module = "module";
Václav Kubernát2eaceb82018-10-08 19:56:30 +020027x3::rule<dataNode_class, dataNode_> const dataNode = "dataNode";
28x3::rule<schemaNode_class, schemaNode_> const schemaNode = "schemaNode";
Václav Kubernát37171a12018-08-31 17:01:48 +020029x3::rule<absoluteStart_class, Scope> const absoluteStart = "absoluteStart";
Václav Kubernát2eaceb82018-10-08 19:56:30 +020030x3::rule<schemaPath_class, schemaPath_> const schemaPath = "schemaPath";
Václav Kubernát5c75b252018-10-10 18:33:47 +020031x3::rule<dataNodeList_class, decltype(dataPath_::m_nodes)::value_type> const dataNodeList = "dataNodeList";
32x3::rule<dataNodesListEnd_class, decltype(dataPath_::m_nodes)> const dataNodesListEnd = "dataNodesListEnd";
33x3::rule<dataPathListEnd_class, dataPath_> const dataPathListEnd = "dataPathListEnd";
Václav Kubernát2eaceb82018-10-08 19:56:30 +020034x3::rule<dataPath_class, dataPath_> const dataPath = "dataPath";
35x3::rule<leaf_path_class, dataPath_> const leafPath = "leafPath";
Václav Kubernátebca2552018-06-08 19:06:02 +020036
37x3::rule<leaf_data_class, leaf_data_> const leaf_data = "leaf_data";
38x3::rule<leaf_data_enum_class, enum_> const leaf_data_enum = "leaf_data_enum";
39x3::rule<leaf_data_decimal_class, double> const leaf_data_decimal = "leaf_data_decimal";
40x3::rule<leaf_data_bool_class, bool> const leaf_data_bool = "leaf_data_bool";
41x3::rule<leaf_data_int_class, int32_t> const leaf_data_int = "leaf_data_int";
42x3::rule<leaf_data_uint_class, uint32_t> const leaf_data_uint = "leaf_data_uint";
43x3::rule<leaf_data_string_class, std::string> const leaf_data_string = "leaf_data_string";
44
Václav Kubernát6d791432018-10-25 16:00:35 +020045x3::rule<discard_class, discard_> const discard = "discard";
Václav Kubernát11afac72018-07-18 14:59:53 +020046x3::rule<ls_class, ls_> const ls = "ls";
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020047x3::rule<cd_class, cd_> const cd = "cd";
Václav Kubernát07204242018-06-04 18:12:09 +020048x3::rule<set_class, set_> const set = "set";
Václav Kubernátb6ff0b62018-08-30 16:14:53 +020049x3::rule<get_class, get_> const get = "get";
Václav Kubernátb61336d2018-05-28 17:35:03 +020050x3::rule<create_class, create_> const create = "create";
51x3::rule<delete_class, delete_> const delete_rule = "delete_rule";
Václav Kubernát812ee282018-08-30 17:10:03 +020052x3::rule<commit_class, commit_> const commit = "commit";
Václav Kubernátb61336d2018-05-28 17:35:03 +020053x3::rule<command_class, command_> const command = "command";
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020054
Václav Kubernát5c75b252018-10-10 18:33:47 +020055x3::rule<initializeContext_class, x3::unused_type> const initializeContext = "initializeContext";
56
Václav Kubernát41378452018-06-06 16:29:40 +020057#if __clang__
58#pragma GCC diagnostic push
59#pragma GCC diagnostic ignored "-Woverloaded-shift-op-parentheses"
60#endif
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020061
Václav Kubernát89728d82018-09-13 16:28:28 +020062auto const key_identifier_def =
63 lexeme[
64 ((alpha | char_("_")) >> *(alnum | char_("_") | char_("-") | char_(".")))
65 ];
66
67auto const quotedValue =
68 ('\'' > +(char_-'\'') > '\'') |
69 ('\"' > +(char_-'\"') > '\"');
70
71auto const number =
72 +x3::digit;
73
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020074auto const keyValue_def =
Václav Kubernát89728d82018-09-13 16:28:28 +020075 lexeme['[' > key_identifier > '=' > (quotedValue | number) > ']'];
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020076
Václav Kubernát744f57f2018-06-29 22:46:26 +020077auto const module_identifier_def =
78 lexeme[
79 ((alpha | char_("_")) >> *(alnum | char_("_") | char_("-") | char_(".")))
80 ];
81
82auto const node_identifier_def =
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020083 lexeme[
84 ((alpha | char_("_")) >> *(alnum | char_("_") | char_("-") | char_(".")))
85 ];
86
87auto const listPrefix_def =
Václav Kubernát89728d82018-09-13 16:28:28 +020088 node_identifier >> &char_('[');
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020089
Václav Kubernát7e4e82f2018-05-14 20:04:58 +020090// even though we don't allow no keys to be supplied, the star allows me to check which keys are missing
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020091auto const listSuffix_def =
Václav Kubernát89728d82018-09-13 16:28:28 +020092 *keyValue;
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020093
94auto const listElement_def =
95 listPrefix > listSuffix;
96
Václav Kubernát2eaceb82018-10-08 19:56:30 +020097auto const list_def =
Václav Kubernát5c75b252018-10-10 18:33:47 +020098 node_identifier >> !char_('[');
Václav Kubernát2eaceb82018-10-08 19:56:30 +020099
Václav Kubernát60d6f292018-05-25 09:45:32 +0200100auto const nodeup_def =
101 lit("..") > x3::attr(nodeup_());
102
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200103auto const container_def =
Václav Kubernát744f57f2018-06-29 22:46:26 +0200104 node_identifier;
105
106auto const module_def =
107 module_identifier >> x3::no_skip[':'] >> !x3::no_skip[space];
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200108
Václav Kubernát07204242018-06-04 18:12:09 +0200109auto const leaf_def =
Václav Kubernát744f57f2018-06-29 22:46:26 +0200110 node_identifier;
Václav Kubernát07204242018-06-04 18:12:09 +0200111
112// leaf cannot be in the middle of a path, however, I need the grammar's attribute to be a vector of variants
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200113auto const schemaNode_def =
114 -(module) >> x3::expect[container | list | nodeup | leaf];
115
116auto const dataNode_def =
Václav Kubernát5c75b252018-10-10 18:33:47 +0200117 -(module) >> (container | listElement | nodeup | leaf);
Václav Kubernát744f57f2018-06-29 22:46:26 +0200118
Václav Kubernát37171a12018-08-31 17:01:48 +0200119auto const absoluteStart_def =
120 x3::omit['/'] >> x3::attr(Scope::Absolute);
121
122// I have to insert an empty vector to the first alternative, otherwise they won't have the same attribute
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200123auto const dataPath_def =
124 absoluteStart >> x3::attr(decltype(dataPath_::m_nodes)()) >> x3::eoi |
125 -(absoluteStart) >> dataNode % '/';
126
Václav Kubernát5c75b252018-10-10 18:33:47 +0200127auto const dataNodeList_def =
128 -(module) >> list;
129
130// This intermediate rule is mandatory, because we need the first alternative
131// to be collapsed to a vector. If we didn't use the intermediate rule,
132// Spirit wouldn't know we want it to collapse.
133// https://github.com/boostorg/spirit/issues/408
134auto const dataNodesListEnd_def =
135 dataNode % '/' >> '/' >> dataNodeList |
136 initializeContext >> x3::attr(decltype(dataPath_::m_nodes)()) >> dataNodeList;
137
138auto const dataPathListEnd_def =
139 absoluteStart >> x3::attr(decltype(dataPath_::m_nodes)()) >> x3::eoi |
140 -(absoluteStart) >> dataNodesListEnd;
141
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200142auto const schemaPath_def =
143 absoluteStart >> x3::attr(decltype(schemaPath_::m_nodes)()) >> x3::eoi |
144 -(absoluteStart) >> schemaNode % '/';
Václav Kubernát07204242018-06-04 18:12:09 +0200145
Václav Kubernát0b0272f2018-06-13 14:13:08 +0200146auto const leafPath_def =
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200147 dataPath;
Václav Kubernát0b0272f2018-06-13 14:13:08 +0200148
Václav Kubernátebca2552018-06-08 19:06:02 +0200149auto const leaf_data_enum_def =
150 +char_;
151auto const leaf_data_decimal_def =
152 double_;
153
154struct bool_symbol_table : x3::symbols<bool> {
155 bool_symbol_table()
156 {
157 add
158 ("true", true)
159 ("false", false);
160 }
161} const bool_rule;
162
163auto const leaf_data_bool_def =
164 bool_rule;
165auto const leaf_data_int_def =
166 int_;
167auto const leaf_data_uint_def =
168 uint_;
169auto const leaf_data_string_def =
170 *char_;
171
172auto const leaf_data_def =
Václav Kubernát0b0272f2018-06-13 14:13:08 +0200173x3::expect[
Václav Kubernátebca2552018-06-08 19:06:02 +0200174 leaf_data_enum |
175 leaf_data_decimal |
176 leaf_data_bool |
177 leaf_data_int |
178 leaf_data_uint |
Václav Kubernát0b0272f2018-06-13 14:13:08 +0200179 leaf_data_string];
Václav Kubernát07204242018-06-04 18:12:09 +0200180
181auto const space_separator =
182 x3::omit[x3::no_skip[space]];
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200183
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200184struct ls_options_table : x3::symbols<LsOption> {
185 ls_options_table()
186 {
187 add
188 ("--recursive", LsOption::Recursive);
189 }
190} const ls_options;
191
Václav Kubernát5c75b252018-10-10 18:33:47 +0200192// A "nothing" parser, which is used to reset the context (when trying to parse different types of paths)
193auto const initializeContext_def =
194 x3::eps;
195
Václav Kubernát11afac72018-07-18 14:59:53 +0200196auto const ls_def =
Václav Kubernát5c75b252018-10-10 18:33:47 +0200197 lit("ls") >> *(space_separator >> ls_options) >> -(space_separator >> (dataPathListEnd | initializeContext >> dataPath));
Václav Kubernát11afac72018-07-18 14:59:53 +0200198
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200199auto const cd_def =
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200200 lit("cd") >> space_separator > dataPath;
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200201
Václav Kubernátb61336d2018-05-28 17:35:03 +0200202auto const create_def =
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200203 lit("create") >> space_separator > dataPath;
Václav Kubernátb61336d2018-05-28 17:35:03 +0200204
205auto const delete_rule_def =
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200206 lit("delete") >> space_separator > dataPath;
Václav Kubernát07204242018-06-04 18:12:09 +0200207
Václav Kubernátb6ff0b62018-08-30 16:14:53 +0200208auto const get_def =
Václav Kubernát5c75b252018-10-10 18:33:47 +0200209 lit("get") >> -(space_separator >> (dataPathListEnd | initializeContext >> dataPath));
Václav Kubernátb6ff0b62018-08-30 16:14:53 +0200210
Václav Kubernát07204242018-06-04 18:12:09 +0200211auto const set_def =
Václav Kubernát0b0272f2018-06-13 14:13:08 +0200212 lit("set") >> space_separator > leafPath > leaf_data;
Václav Kubernátb61336d2018-05-28 17:35:03 +0200213
Václav Kubernát812ee282018-08-30 17:10:03 +0200214auto const commit_def =
215 lit("commit") >> x3::attr(commit_());
216
Václav Kubernát6d791432018-10-25 16:00:35 +0200217auto const discard_def =
218 lit("discard") >> x3::attr(discard_());
219
Václav Kubernátb61336d2018-05-28 17:35:03 +0200220auto const command_def =
Václav Kubernát6d791432018-10-25 16:00:35 +0200221 x3::expect[cd | create | delete_rule | set | commit | get | ls | discard] >> x3::eoi;
Václav Kubernát41378452018-06-06 16:29:40 +0200222
223#if __clang__
224#pragma GCC diagnostic pop
225#endif
Václav Kubernátb61336d2018-05-28 17:35:03 +0200226
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200227BOOST_SPIRIT_DEFINE(keyValue)
Václav Kubernát89728d82018-09-13 16:28:28 +0200228BOOST_SPIRIT_DEFINE(key_identifier)
Václav Kubernát744f57f2018-06-29 22:46:26 +0200229BOOST_SPIRIT_DEFINE(node_identifier)
230BOOST_SPIRIT_DEFINE(module_identifier)
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200231BOOST_SPIRIT_DEFINE(listPrefix)
232BOOST_SPIRIT_DEFINE(listSuffix)
233BOOST_SPIRIT_DEFINE(listElement)
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200234BOOST_SPIRIT_DEFINE(list)
Václav Kubernát60d6f292018-05-25 09:45:32 +0200235BOOST_SPIRIT_DEFINE(nodeup)
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200236BOOST_SPIRIT_DEFINE(schemaNode)
237BOOST_SPIRIT_DEFINE(dataNode)
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200238BOOST_SPIRIT_DEFINE(container)
Václav Kubernát07204242018-06-04 18:12:09 +0200239BOOST_SPIRIT_DEFINE(leaf)
Václav Kubernát0b0272f2018-06-13 14:13:08 +0200240BOOST_SPIRIT_DEFINE(leafPath)
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200241BOOST_SPIRIT_DEFINE(schemaPath)
242BOOST_SPIRIT_DEFINE(dataPath)
Václav Kubernát5c75b252018-10-10 18:33:47 +0200243BOOST_SPIRIT_DEFINE(dataNodeList)
244BOOST_SPIRIT_DEFINE(dataNodesListEnd)
245BOOST_SPIRIT_DEFINE(dataPathListEnd)
Václav Kubernát37171a12018-08-31 17:01:48 +0200246BOOST_SPIRIT_DEFINE(absoluteStart)
Václav Kubernát744f57f2018-06-29 22:46:26 +0200247BOOST_SPIRIT_DEFINE(module)
Václav Kubernátebca2552018-06-08 19:06:02 +0200248BOOST_SPIRIT_DEFINE(leaf_data)
249BOOST_SPIRIT_DEFINE(leaf_data_enum)
250BOOST_SPIRIT_DEFINE(leaf_data_decimal)
251BOOST_SPIRIT_DEFINE(leaf_data_bool)
252BOOST_SPIRIT_DEFINE(leaf_data_int)
253BOOST_SPIRIT_DEFINE(leaf_data_uint)
254BOOST_SPIRIT_DEFINE(leaf_data_string)
Václav Kubernát5c75b252018-10-10 18:33:47 +0200255BOOST_SPIRIT_DEFINE(initializeContext)
Václav Kubernát07204242018-06-04 18:12:09 +0200256BOOST_SPIRIT_DEFINE(set)
Václav Kubernát812ee282018-08-30 17:10:03 +0200257BOOST_SPIRIT_DEFINE(commit)
Václav Kubernátb6ff0b62018-08-30 16:14:53 +0200258BOOST_SPIRIT_DEFINE(get)
Václav Kubernát11afac72018-07-18 14:59:53 +0200259BOOST_SPIRIT_DEFINE(ls)
Václav Kubernát6d791432018-10-25 16:00:35 +0200260BOOST_SPIRIT_DEFINE(discard)
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200261BOOST_SPIRIT_DEFINE(cd)
Václav Kubernátb61336d2018-05-28 17:35:03 +0200262BOOST_SPIRIT_DEFINE(create)
263BOOST_SPIRIT_DEFINE(delete_rule)
264BOOST_SPIRIT_DEFINE(command)