blob: 5a3db0f481910a32cb0aaa7a5b89a9cb261f9c68 [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át744f57f2018-06-29 22:46:26 +020016x3::rule<node_identifier_class, std::string> const node_identifier = "node_identifier";
17x3::rule<module_identifier_class, std::string> const module_identifier = "module_identifier";
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020018x3::rule<listPrefix_class, std::string> const listPrefix = "listPrefix";
19x3::rule<listSuffix_class, std::vector<keyValue_>> const listSuffix = "listSuffix";
20x3::rule<listElement_class, listElement_> const listElement = "listElement";
Václav Kubernát60d6f292018-05-25 09:45:32 +020021x3::rule<nodeup_class, nodeup_> const nodeup = "nodeup";
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020022x3::rule<container_class, container_> const container = "container";
Václav Kubernát07204242018-06-04 18:12:09 +020023x3::rule<leaf_class, leaf_> const leaf = "leaf";
Václav Kubernát744f57f2018-06-29 22:46:26 +020024x3::rule<module_class, module_> const module = "module";
25x3::rule<node_class, node_> const node = "node";
Václav Kubernát37171a12018-08-31 17:01:48 +020026x3::rule<absoluteStart_class, Scope> const absoluteStart = "absoluteStart";
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020027x3::rule<path_class, path_> const path = "path";
Václav Kubernát0b0272f2018-06-13 14:13:08 +020028x3::rule<leaf_path_class, path_> const leafPath = "leafPath";
Václav Kubernátebca2552018-06-08 19:06:02 +020029
30x3::rule<leaf_data_class, leaf_data_> const leaf_data = "leaf_data";
31x3::rule<leaf_data_enum_class, enum_> const leaf_data_enum = "leaf_data_enum";
32x3::rule<leaf_data_decimal_class, double> const leaf_data_decimal = "leaf_data_decimal";
33x3::rule<leaf_data_bool_class, bool> const leaf_data_bool = "leaf_data_bool";
34x3::rule<leaf_data_int_class, int32_t> const leaf_data_int = "leaf_data_int";
35x3::rule<leaf_data_uint_class, uint32_t> const leaf_data_uint = "leaf_data_uint";
36x3::rule<leaf_data_string_class, std::string> const leaf_data_string = "leaf_data_string";
37
Václav Kubernát11afac72018-07-18 14:59:53 +020038x3::rule<ls_class, ls_> const ls = "ls";
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020039x3::rule<cd_class, cd_> const cd = "cd";
Václav Kubernát07204242018-06-04 18:12:09 +020040x3::rule<set_class, set_> const set = "set";
Václav Kubernátb6ff0b62018-08-30 16:14:53 +020041x3::rule<get_class, get_> const get = "get";
Václav Kubernátb61336d2018-05-28 17:35:03 +020042x3::rule<create_class, create_> const create = "create";
43x3::rule<delete_class, delete_> const delete_rule = "delete_rule";
Václav Kubernát812ee282018-08-30 17:10:03 +020044x3::rule<commit_class, commit_> const commit = "commit";
Václav Kubernátb61336d2018-05-28 17:35:03 +020045x3::rule<command_class, command_> const command = "command";
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020046
Václav Kubernát41378452018-06-06 16:29:40 +020047#if __clang__
48#pragma GCC diagnostic push
49#pragma GCC diagnostic ignored "-Woverloaded-shift-op-parentheses"
50#endif
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020051
52auto const keyValue_def =
Václav Kubernát41378452018-06-06 16:29:40 +020053 lexeme[+alnum > '=' > +alnum];
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020054
Václav Kubernát744f57f2018-06-29 22:46:26 +020055auto const module_identifier_def =
56 lexeme[
57 ((alpha | char_("_")) >> *(alnum | char_("_") | char_("-") | char_(".")))
58 ];
59
60auto const node_identifier_def =
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020061 lexeme[
62 ((alpha | char_("_")) >> *(alnum | char_("_") | char_("-") | char_(".")))
63 ];
64
65auto const listPrefix_def =
Václav Kubernát744f57f2018-06-29 22:46:26 +020066 node_identifier >> '[';
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020067
Václav Kubernát7e4e82f2018-05-14 20:04:58 +020068// 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 +020069auto const listSuffix_def =
Václav Kubernát7e4e82f2018-05-14 20:04:58 +020070 *keyValue > ']';
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020071
72auto const listElement_def =
73 listPrefix > listSuffix;
74
Václav Kubernát60d6f292018-05-25 09:45:32 +020075auto const nodeup_def =
76 lit("..") > x3::attr(nodeup_());
77
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020078auto const container_def =
Václav Kubernát744f57f2018-06-29 22:46:26 +020079 node_identifier;
80
81auto const module_def =
82 module_identifier >> x3::no_skip[':'] >> !x3::no_skip[space];
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020083
Václav Kubernát07204242018-06-04 18:12:09 +020084auto const leaf_def =
Václav Kubernát744f57f2018-06-29 22:46:26 +020085 node_identifier;
Václav Kubernát07204242018-06-04 18:12:09 +020086
87// 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 +020088auto const node_def =
89 -(module) >> x3::expect[container | listElement | nodeup | leaf];
90
Václav Kubernát37171a12018-08-31 17:01:48 +020091auto const absoluteStart_def =
92 x3::omit['/'] >> x3::attr(Scope::Absolute);
93
94// 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 +020095auto const path_def =
Václav Kubernát37171a12018-08-31 17:01:48 +020096 absoluteStart >> x3::attr(decltype(path_::m_nodes)()) >> x3::eoi |
97 -(absoluteStart) >> node % '/';
Václav Kubernát07204242018-06-04 18:12:09 +020098
Václav Kubernát0b0272f2018-06-13 14:13:08 +020099auto const leafPath_def =
100 path;
101
Václav Kubernátebca2552018-06-08 19:06:02 +0200102auto const leaf_data_enum_def =
103 +char_;
104auto const leaf_data_decimal_def =
105 double_;
106
107struct bool_symbol_table : x3::symbols<bool> {
108 bool_symbol_table()
109 {
110 add
111 ("true", true)
112 ("false", false);
113 }
114} const bool_rule;
115
116auto const leaf_data_bool_def =
117 bool_rule;
118auto const leaf_data_int_def =
119 int_;
120auto const leaf_data_uint_def =
121 uint_;
122auto const leaf_data_string_def =
123 *char_;
124
125auto const leaf_data_def =
Václav Kubernát0b0272f2018-06-13 14:13:08 +0200126x3::expect[
Václav Kubernátebca2552018-06-08 19:06:02 +0200127 leaf_data_enum |
128 leaf_data_decimal |
129 leaf_data_bool |
130 leaf_data_int |
131 leaf_data_uint |
Václav Kubernát0b0272f2018-06-13 14:13:08 +0200132 leaf_data_string];
Václav Kubernát07204242018-06-04 18:12:09 +0200133
134auto const space_separator =
135 x3::omit[x3::no_skip[space]];
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200136
Václav Kubernát11afac72018-07-18 14:59:53 +0200137auto const ls_def =
138 lit("ls") >> -path;
139
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200140auto const cd_def =
Václav Kubernát41378452018-06-06 16:29:40 +0200141 lit("cd") >> space_separator > path;
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200142
Václav Kubernátb61336d2018-05-28 17:35:03 +0200143auto const create_def =
Václav Kubernát41378452018-06-06 16:29:40 +0200144 lit("create") >> space_separator > path;
Václav Kubernátb61336d2018-05-28 17:35:03 +0200145
146auto const delete_rule_def =
Václav Kubernát41378452018-06-06 16:29:40 +0200147 lit("delete") >> space_separator > path;
Václav Kubernát07204242018-06-04 18:12:09 +0200148
Václav Kubernátb6ff0b62018-08-30 16:14:53 +0200149auto const get_def =
150 lit("get") >> -path;
151
Václav Kubernát07204242018-06-04 18:12:09 +0200152auto const set_def =
Václav Kubernát0b0272f2018-06-13 14:13:08 +0200153 lit("set") >> space_separator > leafPath > leaf_data;
Václav Kubernátb61336d2018-05-28 17:35:03 +0200154
Václav Kubernát812ee282018-08-30 17:10:03 +0200155auto const commit_def =
156 lit("commit") >> x3::attr(commit_());
157
Václav Kubernátb61336d2018-05-28 17:35:03 +0200158auto const command_def =
Václav Kubernátb6ff0b62018-08-30 16:14:53 +0200159 x3::expect[cd | create | delete_rule | set | commit | get | ls] >> x3::eoi;
Václav Kubernát41378452018-06-06 16:29:40 +0200160
161#if __clang__
162#pragma GCC diagnostic pop
163#endif
Václav Kubernátb61336d2018-05-28 17:35:03 +0200164
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200165BOOST_SPIRIT_DEFINE(keyValue)
Václav Kubernát744f57f2018-06-29 22:46:26 +0200166BOOST_SPIRIT_DEFINE(node_identifier)
167BOOST_SPIRIT_DEFINE(module_identifier)
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200168BOOST_SPIRIT_DEFINE(listPrefix)
169BOOST_SPIRIT_DEFINE(listSuffix)
170BOOST_SPIRIT_DEFINE(listElement)
Václav Kubernát60d6f292018-05-25 09:45:32 +0200171BOOST_SPIRIT_DEFINE(nodeup)
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200172BOOST_SPIRIT_DEFINE(container)
Václav Kubernát07204242018-06-04 18:12:09 +0200173BOOST_SPIRIT_DEFINE(leaf)
Václav Kubernát0b0272f2018-06-13 14:13:08 +0200174BOOST_SPIRIT_DEFINE(leafPath)
Václav Kubernát744f57f2018-06-29 22:46:26 +0200175BOOST_SPIRIT_DEFINE(node)
Václav Kubernát37171a12018-08-31 17:01:48 +0200176BOOST_SPIRIT_DEFINE(absoluteStart)
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200177BOOST_SPIRIT_DEFINE(path)
Václav Kubernát744f57f2018-06-29 22:46:26 +0200178BOOST_SPIRIT_DEFINE(module)
Václav Kubernátebca2552018-06-08 19:06:02 +0200179BOOST_SPIRIT_DEFINE(leaf_data)
180BOOST_SPIRIT_DEFINE(leaf_data_enum)
181BOOST_SPIRIT_DEFINE(leaf_data_decimal)
182BOOST_SPIRIT_DEFINE(leaf_data_bool)
183BOOST_SPIRIT_DEFINE(leaf_data_int)
184BOOST_SPIRIT_DEFINE(leaf_data_uint)
185BOOST_SPIRIT_DEFINE(leaf_data_string)
Václav Kubernát07204242018-06-04 18:12:09 +0200186BOOST_SPIRIT_DEFINE(set)
Václav Kubernát812ee282018-08-30 17:10:03 +0200187BOOST_SPIRIT_DEFINE(commit)
Václav Kubernátb6ff0b62018-08-30 16:14:53 +0200188BOOST_SPIRIT_DEFINE(get)
Václav Kubernát11afac72018-07-18 14:59:53 +0200189BOOST_SPIRIT_DEFINE(ls)
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200190BOOST_SPIRIT_DEFINE(cd)
Václav Kubernátb61336d2018-05-28 17:35:03 +0200191BOOST_SPIRIT_DEFINE(create)
192BOOST_SPIRIT_DEFINE(delete_rule)
193BOOST_SPIRIT_DEFINE(command)