blob: 688394efa0a460d53ba949509cbdd5f3742bc76b [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át60d6f292018-05-25 09:45:32 +020022x3::rule<nodeup_class, nodeup_> const nodeup = "nodeup";
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020023x3::rule<container_class, container_> const container = "container";
Václav Kubernát07204242018-06-04 18:12:09 +020024x3::rule<leaf_class, leaf_> const leaf = "leaf";
Václav Kubernát744f57f2018-06-29 22:46:26 +020025x3::rule<module_class, module_> const module = "module";
26x3::rule<node_class, node_> const node = "node";
Václav Kubernát37171a12018-08-31 17:01:48 +020027x3::rule<absoluteStart_class, Scope> const absoluteStart = "absoluteStart";
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020028x3::rule<path_class, path_> const path = "path";
Václav Kubernát0b0272f2018-06-13 14:13:08 +020029x3::rule<leaf_path_class, path_> const leafPath = "leafPath";
Václav Kubernátebca2552018-06-08 19:06:02 +020030
31x3::rule<leaf_data_class, leaf_data_> const leaf_data = "leaf_data";
32x3::rule<leaf_data_enum_class, enum_> const leaf_data_enum = "leaf_data_enum";
33x3::rule<leaf_data_decimal_class, double> const leaf_data_decimal = "leaf_data_decimal";
34x3::rule<leaf_data_bool_class, bool> const leaf_data_bool = "leaf_data_bool";
35x3::rule<leaf_data_int_class, int32_t> const leaf_data_int = "leaf_data_int";
36x3::rule<leaf_data_uint_class, uint32_t> const leaf_data_uint = "leaf_data_uint";
37x3::rule<leaf_data_string_class, std::string> const leaf_data_string = "leaf_data_string";
38
Václav Kubernát11afac72018-07-18 14:59:53 +020039x3::rule<ls_class, ls_> const ls = "ls";
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020040x3::rule<cd_class, cd_> const cd = "cd";
Václav Kubernát07204242018-06-04 18:12:09 +020041x3::rule<set_class, set_> const set = "set";
Václav Kubernátb6ff0b62018-08-30 16:14:53 +020042x3::rule<get_class, get_> const get = "get";
Václav Kubernátb61336d2018-05-28 17:35:03 +020043x3::rule<create_class, create_> const create = "create";
44x3::rule<delete_class, delete_> const delete_rule = "delete_rule";
Václav Kubernát812ee282018-08-30 17:10:03 +020045x3::rule<commit_class, commit_> const commit = "commit";
Václav Kubernátb61336d2018-05-28 17:35:03 +020046x3::rule<command_class, command_> const command = "command";
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020047
Václav Kubernát41378452018-06-06 16:29:40 +020048#if __clang__
49#pragma GCC diagnostic push
50#pragma GCC diagnostic ignored "-Woverloaded-shift-op-parentheses"
51#endif
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020052
Václav Kubernát89728d82018-09-13 16:28:28 +020053auto const key_identifier_def =
54 lexeme[
55 ((alpha | char_("_")) >> *(alnum | char_("_") | char_("-") | char_(".")))
56 ];
57
58auto const quotedValue =
59 ('\'' > +(char_-'\'') > '\'') |
60 ('\"' > +(char_-'\"') > '\"');
61
62auto const number =
63 +x3::digit;
64
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020065auto const keyValue_def =
Václav Kubernát89728d82018-09-13 16:28:28 +020066 lexeme['[' > key_identifier > '=' > (quotedValue | number) > ']'];
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020067
Václav Kubernát744f57f2018-06-29 22:46:26 +020068auto const module_identifier_def =
69 lexeme[
70 ((alpha | char_("_")) >> *(alnum | char_("_") | char_("-") | char_(".")))
71 ];
72
73auto const node_identifier_def =
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020074 lexeme[
75 ((alpha | char_("_")) >> *(alnum | char_("_") | char_("-") | char_(".")))
76 ];
77
78auto const listPrefix_def =
Václav Kubernát89728d82018-09-13 16:28:28 +020079 node_identifier >> &char_('[');
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020080
Václav Kubernát7e4e82f2018-05-14 20:04:58 +020081// 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 +020082auto const listSuffix_def =
Václav Kubernát89728d82018-09-13 16:28:28 +020083 *keyValue;
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020084
85auto const listElement_def =
86 listPrefix > listSuffix;
87
Václav Kubernát60d6f292018-05-25 09:45:32 +020088auto const nodeup_def =
89 lit("..") > x3::attr(nodeup_());
90
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020091auto const container_def =
Václav Kubernát744f57f2018-06-29 22:46:26 +020092 node_identifier;
93
94auto const module_def =
95 module_identifier >> x3::no_skip[':'] >> !x3::no_skip[space];
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020096
Václav Kubernát07204242018-06-04 18:12:09 +020097auto const leaf_def =
Václav Kubernát744f57f2018-06-29 22:46:26 +020098 node_identifier;
Václav Kubernát07204242018-06-04 18:12:09 +020099
100// 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át744f57f2018-06-29 22:46:26 +0200101auto const node_def =
102 -(module) >> x3::expect[container | listElement | nodeup | leaf];
103
Václav Kubernát37171a12018-08-31 17:01:48 +0200104auto const absoluteStart_def =
105 x3::omit['/'] >> x3::attr(Scope::Absolute);
106
107// I have to insert an empty vector to the first alternative, otherwise they won't have the same attribute
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200108auto const path_def =
Václav Kubernát37171a12018-08-31 17:01:48 +0200109 absoluteStart >> x3::attr(decltype(path_::m_nodes)()) >> x3::eoi |
110 -(absoluteStart) >> node % '/';
Václav Kubernát07204242018-06-04 18:12:09 +0200111
Václav Kubernát0b0272f2018-06-13 14:13:08 +0200112auto const leafPath_def =
113 path;
114
Václav Kubernátebca2552018-06-08 19:06:02 +0200115auto const leaf_data_enum_def =
116 +char_;
117auto const leaf_data_decimal_def =
118 double_;
119
120struct bool_symbol_table : x3::symbols<bool> {
121 bool_symbol_table()
122 {
123 add
124 ("true", true)
125 ("false", false);
126 }
127} const bool_rule;
128
129auto const leaf_data_bool_def =
130 bool_rule;
131auto const leaf_data_int_def =
132 int_;
133auto const leaf_data_uint_def =
134 uint_;
135auto const leaf_data_string_def =
136 *char_;
137
138auto const leaf_data_def =
Václav Kubernát0b0272f2018-06-13 14:13:08 +0200139x3::expect[
Václav Kubernátebca2552018-06-08 19:06:02 +0200140 leaf_data_enum |
141 leaf_data_decimal |
142 leaf_data_bool |
143 leaf_data_int |
144 leaf_data_uint |
Václav Kubernát0b0272f2018-06-13 14:13:08 +0200145 leaf_data_string];
Václav Kubernát07204242018-06-04 18:12:09 +0200146
147auto const space_separator =
148 x3::omit[x3::no_skip[space]];
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200149
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200150struct ls_options_table : x3::symbols<LsOption> {
151 ls_options_table()
152 {
153 add
154 ("--recursive", LsOption::Recursive);
155 }
156} const ls_options;
157
Václav Kubernát11afac72018-07-18 14:59:53 +0200158auto const ls_def =
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200159 lit("ls") >> *(space_separator >> ls_options) >> -(space_separator >> path);
Václav Kubernát11afac72018-07-18 14:59:53 +0200160
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200161auto const cd_def =
Václav Kubernát41378452018-06-06 16:29:40 +0200162 lit("cd") >> space_separator > path;
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200163
Václav Kubernátb61336d2018-05-28 17:35:03 +0200164auto const create_def =
Václav Kubernát41378452018-06-06 16:29:40 +0200165 lit("create") >> space_separator > path;
Václav Kubernátb61336d2018-05-28 17:35:03 +0200166
167auto const delete_rule_def =
Václav Kubernát41378452018-06-06 16:29:40 +0200168 lit("delete") >> space_separator > path;
Václav Kubernát07204242018-06-04 18:12:09 +0200169
Václav Kubernátb6ff0b62018-08-30 16:14:53 +0200170auto const get_def =
171 lit("get") >> -path;
172
Václav Kubernát07204242018-06-04 18:12:09 +0200173auto const set_def =
Václav Kubernát0b0272f2018-06-13 14:13:08 +0200174 lit("set") >> space_separator > leafPath > leaf_data;
Václav Kubernátb61336d2018-05-28 17:35:03 +0200175
Václav Kubernát812ee282018-08-30 17:10:03 +0200176auto const commit_def =
177 lit("commit") >> x3::attr(commit_());
178
Václav Kubernátb61336d2018-05-28 17:35:03 +0200179auto const command_def =
Václav Kubernátb6ff0b62018-08-30 16:14:53 +0200180 x3::expect[cd | create | delete_rule | set | commit | get | ls] >> x3::eoi;
Václav Kubernát41378452018-06-06 16:29:40 +0200181
182#if __clang__
183#pragma GCC diagnostic pop
184#endif
Václav Kubernátb61336d2018-05-28 17:35:03 +0200185
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200186BOOST_SPIRIT_DEFINE(keyValue)
Václav Kubernát89728d82018-09-13 16:28:28 +0200187BOOST_SPIRIT_DEFINE(key_identifier)
Václav Kubernát744f57f2018-06-29 22:46:26 +0200188BOOST_SPIRIT_DEFINE(node_identifier)
189BOOST_SPIRIT_DEFINE(module_identifier)
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200190BOOST_SPIRIT_DEFINE(listPrefix)
191BOOST_SPIRIT_DEFINE(listSuffix)
192BOOST_SPIRIT_DEFINE(listElement)
Václav Kubernát60d6f292018-05-25 09:45:32 +0200193BOOST_SPIRIT_DEFINE(nodeup)
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200194BOOST_SPIRIT_DEFINE(container)
Václav Kubernát07204242018-06-04 18:12:09 +0200195BOOST_SPIRIT_DEFINE(leaf)
Václav Kubernát0b0272f2018-06-13 14:13:08 +0200196BOOST_SPIRIT_DEFINE(leafPath)
Václav Kubernát744f57f2018-06-29 22:46:26 +0200197BOOST_SPIRIT_DEFINE(node)
Václav Kubernát37171a12018-08-31 17:01:48 +0200198BOOST_SPIRIT_DEFINE(absoluteStart)
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200199BOOST_SPIRIT_DEFINE(path)
Václav Kubernát744f57f2018-06-29 22:46:26 +0200200BOOST_SPIRIT_DEFINE(module)
Václav Kubernátebca2552018-06-08 19:06:02 +0200201BOOST_SPIRIT_DEFINE(leaf_data)
202BOOST_SPIRIT_DEFINE(leaf_data_enum)
203BOOST_SPIRIT_DEFINE(leaf_data_decimal)
204BOOST_SPIRIT_DEFINE(leaf_data_bool)
205BOOST_SPIRIT_DEFINE(leaf_data_int)
206BOOST_SPIRIT_DEFINE(leaf_data_uint)
207BOOST_SPIRIT_DEFINE(leaf_data_string)
Václav Kubernát07204242018-06-04 18:12:09 +0200208BOOST_SPIRIT_DEFINE(set)
Václav Kubernát812ee282018-08-30 17:10:03 +0200209BOOST_SPIRIT_DEFINE(commit)
Václav Kubernátb6ff0b62018-08-30 16:14:53 +0200210BOOST_SPIRIT_DEFINE(get)
Václav Kubernát11afac72018-07-18 14:59:53 +0200211BOOST_SPIRIT_DEFINE(ls)
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200212BOOST_SPIRIT_DEFINE(cd)
Václav Kubernátb61336d2018-05-28 17:35:03 +0200213BOOST_SPIRIT_DEFINE(create)
214BOOST_SPIRIT_DEFINE(delete_rule)
215BOOST_SPIRIT_DEFINE(command)