blob: 2bf7be45576acea65c401789f414924962002ba5 [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átd6fd2492018-11-19 15:11:16 +010031x3::rule<trailingSlash_class, TrailingSlash> const trailingSlash = "trailingSlash";
Václav Kubernát5c75b252018-10-10 18:33:47 +020032x3::rule<dataNodeList_class, decltype(dataPath_::m_nodes)::value_type> const dataNodeList = "dataNodeList";
33x3::rule<dataNodesListEnd_class, decltype(dataPath_::m_nodes)> const dataNodesListEnd = "dataNodesListEnd";
34x3::rule<dataPathListEnd_class, dataPath_> const dataPathListEnd = "dataPathListEnd";
Václav Kubernát2eaceb82018-10-08 19:56:30 +020035x3::rule<dataPath_class, dataPath_> const dataPath = "dataPath";
36x3::rule<leaf_path_class, dataPath_> const leafPath = "leafPath";
Václav Kubernátebca2552018-06-08 19:06:02 +020037
38x3::rule<leaf_data_class, leaf_data_> const leaf_data = "leaf_data";
39x3::rule<leaf_data_enum_class, enum_> const leaf_data_enum = "leaf_data_enum";
40x3::rule<leaf_data_decimal_class, double> const leaf_data_decimal = "leaf_data_decimal";
41x3::rule<leaf_data_bool_class, bool> const leaf_data_bool = "leaf_data_bool";
42x3::rule<leaf_data_int_class, int32_t> const leaf_data_int = "leaf_data_int";
43x3::rule<leaf_data_uint_class, uint32_t> const leaf_data_uint = "leaf_data_uint";
44x3::rule<leaf_data_string_class, std::string> const leaf_data_string = "leaf_data_string";
45
Václav Kubernát6d791432018-10-25 16:00:35 +020046x3::rule<discard_class, discard_> const discard = "discard";
Václav Kubernát11afac72018-07-18 14:59:53 +020047x3::rule<ls_class, ls_> const ls = "ls";
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020048x3::rule<cd_class, cd_> const cd = "cd";
Václav Kubernát07204242018-06-04 18:12:09 +020049x3::rule<set_class, set_> const set = "set";
Václav Kubernátb6ff0b62018-08-30 16:14:53 +020050x3::rule<get_class, get_> const get = "get";
Václav Kubernátb61336d2018-05-28 17:35:03 +020051x3::rule<create_class, create_> const create = "create";
52x3::rule<delete_class, delete_> const delete_rule = "delete_rule";
Václav Kubernát812ee282018-08-30 17:10:03 +020053x3::rule<commit_class, commit_> const commit = "commit";
Václav Kubernátb61336d2018-05-28 17:35:03 +020054x3::rule<command_class, command_> const command = "command";
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020055
Václav Kubernát4108e0d2018-10-29 13:32:22 +010056x3::rule<initializePath_class, x3::unused_type> const initializePath = "initializePath";
57x3::rule<createPathSuggestions_class, x3::unused_type> const createPathSuggestions = "createPathSuggestions";
Václav Kubernát329c6c32019-02-06 16:41:53 +010058x3::rule<createKeySuggestions_class, x3::unused_type> const createKeySuggestions = "createKeySuggestions";
59x3::rule<suggestKeysEnd_class, x3::unused_type> const suggestKeysEnd = "suggestKeysEnd";
Václav Kubernát5c75b252018-10-10 18:33:47 +020060
Václav Kubernát41378452018-06-06 16:29:40 +020061#if __clang__
62#pragma GCC diagnostic push
63#pragma GCC diagnostic ignored "-Woverloaded-shift-op-parentheses"
64#endif
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020065
Václav Kubernát89728d82018-09-13 16:28:28 +020066auto const key_identifier_def =
67 lexeme[
68 ((alpha | char_("_")) >> *(alnum | char_("_") | char_("-") | char_(".")))
69 ];
70
71auto const quotedValue =
72 ('\'' > +(char_-'\'') > '\'') |
73 ('\"' > +(char_-'\"') > '\"');
74
75auto const number =
76 +x3::digit;
77
Václav Kubernát329c6c32019-02-06 16:41:53 +010078auto const createKeySuggestions_def =
79 x3::eps;
80
81auto const suggestKeysEnd_def =
82 x3::eps;
83
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020084auto const keyValue_def =
Václav Kubernát329c6c32019-02-06 16:41:53 +010085 key_identifier > '=' > (quotedValue | number);
86
87auto const keyValueWrapper =
88 lexeme['[' > createKeySuggestions > keyValue > suggestKeysEnd > ']'];
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020089
Václav Kubernát744f57f2018-06-29 22:46:26 +020090auto const module_identifier_def =
91 lexeme[
92 ((alpha | char_("_")) >> *(alnum | char_("_") | char_("-") | char_(".")))
93 ];
94
95auto const node_identifier_def =
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020096 lexeme[
97 ((alpha | char_("_")) >> *(alnum | char_("_") | char_("-") | char_(".")))
98 ];
99
100auto const listPrefix_def =
Václav Kubernát89728d82018-09-13 16:28:28 +0200101 node_identifier >> &char_('[');
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200102
Václav Kubernát7e4e82f2018-05-14 20:04:58 +0200103// 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 +0200104auto const listSuffix_def =
Václav Kubernát329c6c32019-02-06 16:41:53 +0100105 *keyValueWrapper;
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200106
107auto const listElement_def =
108 listPrefix > listSuffix;
109
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200110auto const list_def =
Václav Kubernát5c75b252018-10-10 18:33:47 +0200111 node_identifier >> !char_('[');
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200112
Václav Kubernát60d6f292018-05-25 09:45:32 +0200113auto const nodeup_def =
114 lit("..") > x3::attr(nodeup_());
115
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200116auto const container_def =
Václav Kubernát744f57f2018-06-29 22:46:26 +0200117 node_identifier;
118
119auto const module_def =
120 module_identifier >> x3::no_skip[':'] >> !x3::no_skip[space];
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200121
Václav Kubernát07204242018-06-04 18:12:09 +0200122auto const leaf_def =
Václav Kubernát744f57f2018-06-29 22:46:26 +0200123 node_identifier;
Václav Kubernát07204242018-06-04 18:12:09 +0200124
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100125auto const createPathSuggestions_def =
126 x3::eps;
127
Václav Kubernát07204242018-06-04 18:12:09 +0200128// 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 +0200129auto const schemaNode_def =
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100130 createPathSuggestions >> -(module) >> (container | list | nodeup | leaf);
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200131
132auto const dataNode_def =
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100133 createPathSuggestions >> -(module) >> (container | listElement | nodeup | leaf);
Václav Kubernát744f57f2018-06-29 22:46:26 +0200134
Václav Kubernát37171a12018-08-31 17:01:48 +0200135auto const absoluteStart_def =
136 x3::omit['/'] >> x3::attr(Scope::Absolute);
137
Václav Kubernátd6fd2492018-11-19 15:11:16 +0100138auto const trailingSlash_def =
139 x3::omit['/'] >> x3::attr(TrailingSlash::Present);
140
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100141auto const space_separator =
142 x3::omit[x3::no_skip[space]];
143
Václav Kubernát37171a12018-08-31 17:01:48 +0200144// 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 +0200145auto const dataPath_def =
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100146 initializePath >> absoluteStart >> createPathSuggestions >> x3::attr(decltype(dataPath_::m_nodes)()) >> x3::attr(TrailingSlash::NonPresent) >> x3::eoi |
147 initializePath >> -(absoluteStart >> createPathSuggestions) >> dataNode % '/' >> (trailingSlash >> createPathSuggestions | (&space_separator | x3::eoi));
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200148
Václav Kubernát5c75b252018-10-10 18:33:47 +0200149auto const dataNodeList_def =
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100150 -(module) >> createPathSuggestions >> list;
Václav Kubernát5c75b252018-10-10 18:33:47 +0200151
152// This intermediate rule is mandatory, because we need the first alternative
153// to be collapsed to a vector. If we didn't use the intermediate rule,
154// Spirit wouldn't know we want it to collapse.
155// https://github.com/boostorg/spirit/issues/408
156auto const dataNodesListEnd_def =
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100157 initializePath >> dataNode % '/' >> '/' >> dataNodeList >> -(&char_('/') >> createPathSuggestions) |
158 initializePath >> x3::attr(decltype(dataPath_::m_nodes)()) >> dataNodeList;
Václav Kubernát5c75b252018-10-10 18:33:47 +0200159
160auto const dataPathListEnd_def =
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100161 initializePath >> absoluteStart >> createPathSuggestions >> x3::attr(decltype(dataPath_::m_nodes)()) >> x3::attr(TrailingSlash::NonPresent) >> x3::eoi |
162 initializePath >> -(absoluteStart >> createPathSuggestions) >> dataNodesListEnd >> (trailingSlash >> createPathSuggestions | (&space_separator | x3::eoi));
Václav Kubernát5c75b252018-10-10 18:33:47 +0200163
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200164auto const schemaPath_def =
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100165 initializePath >> absoluteStart >> createPathSuggestions >> x3::attr(decltype(schemaPath_::m_nodes)()) >> x3::attr(TrailingSlash::NonPresent) >> x3::eoi |
166 initializePath >> -(absoluteStart >> createPathSuggestions) >> schemaNode % '/' >> (trailingSlash >> createPathSuggestions | (&space_separator | x3::eoi));
Václav Kubernát07204242018-06-04 18:12:09 +0200167
Václav Kubernát0b0272f2018-06-13 14:13:08 +0200168auto const leafPath_def =
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200169 dataPath;
Václav Kubernát0b0272f2018-06-13 14:13:08 +0200170
Václav Kubernátebca2552018-06-08 19:06:02 +0200171auto const leaf_data_enum_def =
172 +char_;
173auto const leaf_data_decimal_def =
174 double_;
175
176struct bool_symbol_table : x3::symbols<bool> {
177 bool_symbol_table()
178 {
179 add
180 ("true", true)
181 ("false", false);
182 }
183} const bool_rule;
184
185auto const leaf_data_bool_def =
186 bool_rule;
187auto const leaf_data_int_def =
188 int_;
189auto const leaf_data_uint_def =
190 uint_;
191auto const leaf_data_string_def =
192 *char_;
193
194auto const leaf_data_def =
Václav Kubernát0b0272f2018-06-13 14:13:08 +0200195x3::expect[
Václav Kubernátebca2552018-06-08 19:06:02 +0200196 leaf_data_enum |
197 leaf_data_decimal |
198 leaf_data_bool |
199 leaf_data_int |
200 leaf_data_uint |
Václav Kubernát0b0272f2018-06-13 14:13:08 +0200201 leaf_data_string];
Václav Kubernát07204242018-06-04 18:12:09 +0200202
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200203struct ls_options_table : x3::symbols<LsOption> {
204 ls_options_table()
205 {
206 add
207 ("--recursive", LsOption::Recursive);
208 }
209} const ls_options;
210
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100211// A "nothing" parser, which is used to indicate we tried to parse a path
212auto const initializePath_def =
Václav Kubernát5c75b252018-10-10 18:33:47 +0200213 x3::eps;
214
Václav Kubernát11afac72018-07-18 14:59:53 +0200215auto const ls_def =
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100216 lit("ls") >> *(space_separator >> ls_options) >> -(space_separator >> (dataPathListEnd | dataPath | schemaPath));
Václav Kubernát11afac72018-07-18 14:59:53 +0200217
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200218auto const cd_def =
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200219 lit("cd") >> space_separator > dataPath;
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200220
Václav Kubernátb61336d2018-05-28 17:35:03 +0200221auto const create_def =
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200222 lit("create") >> space_separator > dataPath;
Václav Kubernátb61336d2018-05-28 17:35:03 +0200223
224auto const delete_rule_def =
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200225 lit("delete") >> space_separator > dataPath;
Václav Kubernát07204242018-06-04 18:12:09 +0200226
Václav Kubernátb6ff0b62018-08-30 16:14:53 +0200227auto const get_def =
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100228 lit("get") >> -(space_separator >> (dataPathListEnd | dataPath));
Václav Kubernátb6ff0b62018-08-30 16:14:53 +0200229
Václav Kubernát07204242018-06-04 18:12:09 +0200230auto const set_def =
Václav Kubernát0b0272f2018-06-13 14:13:08 +0200231 lit("set") >> space_separator > leafPath > leaf_data;
Václav Kubernátb61336d2018-05-28 17:35:03 +0200232
Václav Kubernát812ee282018-08-30 17:10:03 +0200233auto const commit_def =
234 lit("commit") >> x3::attr(commit_());
235
Václav Kubernát6d791432018-10-25 16:00:35 +0200236auto const discard_def =
237 lit("discard") >> x3::attr(discard_());
238
Václav Kubernátb61336d2018-05-28 17:35:03 +0200239auto const command_def =
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100240 x3::expect[cd | create | delete_rule | set | commit | get | ls | discard];
Václav Kubernát41378452018-06-06 16:29:40 +0200241
242#if __clang__
243#pragma GCC diagnostic pop
244#endif
Václav Kubernátb61336d2018-05-28 17:35:03 +0200245
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200246BOOST_SPIRIT_DEFINE(keyValue)
Václav Kubernát89728d82018-09-13 16:28:28 +0200247BOOST_SPIRIT_DEFINE(key_identifier)
Václav Kubernát744f57f2018-06-29 22:46:26 +0200248BOOST_SPIRIT_DEFINE(node_identifier)
249BOOST_SPIRIT_DEFINE(module_identifier)
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200250BOOST_SPIRIT_DEFINE(listPrefix)
251BOOST_SPIRIT_DEFINE(listSuffix)
252BOOST_SPIRIT_DEFINE(listElement)
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200253BOOST_SPIRIT_DEFINE(list)
Václav Kubernát60d6f292018-05-25 09:45:32 +0200254BOOST_SPIRIT_DEFINE(nodeup)
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200255BOOST_SPIRIT_DEFINE(schemaNode)
256BOOST_SPIRIT_DEFINE(dataNode)
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200257BOOST_SPIRIT_DEFINE(container)
Václav Kubernát07204242018-06-04 18:12:09 +0200258BOOST_SPIRIT_DEFINE(leaf)
Václav Kubernát0b0272f2018-06-13 14:13:08 +0200259BOOST_SPIRIT_DEFINE(leafPath)
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200260BOOST_SPIRIT_DEFINE(schemaPath)
261BOOST_SPIRIT_DEFINE(dataPath)
Václav Kubernát5c75b252018-10-10 18:33:47 +0200262BOOST_SPIRIT_DEFINE(dataNodeList)
263BOOST_SPIRIT_DEFINE(dataNodesListEnd)
264BOOST_SPIRIT_DEFINE(dataPathListEnd)
Václav Kubernát37171a12018-08-31 17:01:48 +0200265BOOST_SPIRIT_DEFINE(absoluteStart)
Václav Kubernátd6fd2492018-11-19 15:11:16 +0100266BOOST_SPIRIT_DEFINE(trailingSlash)
Václav Kubernát744f57f2018-06-29 22:46:26 +0200267BOOST_SPIRIT_DEFINE(module)
Václav Kubernátebca2552018-06-08 19:06:02 +0200268BOOST_SPIRIT_DEFINE(leaf_data)
269BOOST_SPIRIT_DEFINE(leaf_data_enum)
270BOOST_SPIRIT_DEFINE(leaf_data_decimal)
271BOOST_SPIRIT_DEFINE(leaf_data_bool)
272BOOST_SPIRIT_DEFINE(leaf_data_int)
273BOOST_SPIRIT_DEFINE(leaf_data_uint)
274BOOST_SPIRIT_DEFINE(leaf_data_string)
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100275BOOST_SPIRIT_DEFINE(initializePath)
Václav Kubernát07204242018-06-04 18:12:09 +0200276BOOST_SPIRIT_DEFINE(set)
Václav Kubernát812ee282018-08-30 17:10:03 +0200277BOOST_SPIRIT_DEFINE(commit)
Václav Kubernátb6ff0b62018-08-30 16:14:53 +0200278BOOST_SPIRIT_DEFINE(get)
Václav Kubernát11afac72018-07-18 14:59:53 +0200279BOOST_SPIRIT_DEFINE(ls)
Václav Kubernát6d791432018-10-25 16:00:35 +0200280BOOST_SPIRIT_DEFINE(discard)
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200281BOOST_SPIRIT_DEFINE(cd)
Václav Kubernátb61336d2018-05-28 17:35:03 +0200282BOOST_SPIRIT_DEFINE(create)
283BOOST_SPIRIT_DEFINE(delete_rule)
284BOOST_SPIRIT_DEFINE(command)
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100285BOOST_SPIRIT_DEFINE(createPathSuggestions)
Václav Kubernát329c6c32019-02-06 16:41:53 +0100286BOOST_SPIRIT_DEFINE(createKeySuggestions)
287BOOST_SPIRIT_DEFINE(suggestKeysEnd)