Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 1 | /* |
| 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át | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 11 | #include "ast_commands.hpp" |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 12 | #include "ast_handlers.hpp" |
| 13 | |
Václav Kubernát | 60d6f29 | 2018-05-25 09:45:32 +0200 | [diff] [blame] | 14 | |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 15 | x3::rule<keyValue_class, keyValue_> const keyValue = "keyValue"; |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 16 | x3::rule<node_identifier_class, std::string> const node_identifier = "node_identifier"; |
| 17 | x3::rule<module_identifier_class, std::string> const module_identifier = "module_identifier"; |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 18 | x3::rule<listPrefix_class, std::string> const listPrefix = "listPrefix"; |
| 19 | x3::rule<listSuffix_class, std::vector<keyValue_>> const listSuffix = "listSuffix"; |
| 20 | x3::rule<listElement_class, listElement_> const listElement = "listElement"; |
Václav Kubernát | 60d6f29 | 2018-05-25 09:45:32 +0200 | [diff] [blame] | 21 | x3::rule<nodeup_class, nodeup_> const nodeup = "nodeup"; |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 22 | x3::rule<container_class, container_> const container = "container"; |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 23 | x3::rule<leaf_class, leaf_> const leaf = "leaf"; |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 24 | x3::rule<module_class, module_> const module = "module"; |
| 25 | x3::rule<node_class, node_> const node = "node"; |
Václav Kubernát | 37171a1 | 2018-08-31 17:01:48 +0200 | [diff] [blame] | 26 | x3::rule<absoluteStart_class, Scope> const absoluteStart = "absoluteStart"; |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 27 | x3::rule<path_class, path_> const path = "path"; |
Václav Kubernát | 0b0272f | 2018-06-13 14:13:08 +0200 | [diff] [blame] | 28 | x3::rule<leaf_path_class, path_> const leafPath = "leafPath"; |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 29 | |
| 30 | x3::rule<leaf_data_class, leaf_data_> const leaf_data = "leaf_data"; |
| 31 | x3::rule<leaf_data_enum_class, enum_> const leaf_data_enum = "leaf_data_enum"; |
| 32 | x3::rule<leaf_data_decimal_class, double> const leaf_data_decimal = "leaf_data_decimal"; |
| 33 | x3::rule<leaf_data_bool_class, bool> const leaf_data_bool = "leaf_data_bool"; |
| 34 | x3::rule<leaf_data_int_class, int32_t> const leaf_data_int = "leaf_data_int"; |
| 35 | x3::rule<leaf_data_uint_class, uint32_t> const leaf_data_uint = "leaf_data_uint"; |
| 36 | x3::rule<leaf_data_string_class, std::string> const leaf_data_string = "leaf_data_string"; |
| 37 | |
Václav Kubernát | 11afac7 | 2018-07-18 14:59:53 +0200 | [diff] [blame] | 38 | x3::rule<ls_class, ls_> const ls = "ls"; |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 39 | x3::rule<cd_class, cd_> const cd = "cd"; |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 40 | x3::rule<set_class, set_> const set = "set"; |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 41 | x3::rule<get_class, get_> const get = "get"; |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 42 | x3::rule<create_class, create_> const create = "create"; |
| 43 | x3::rule<delete_class, delete_> const delete_rule = "delete_rule"; |
Václav Kubernát | 812ee28 | 2018-08-30 17:10:03 +0200 | [diff] [blame] | 44 | x3::rule<commit_class, commit_> const commit = "commit"; |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 45 | x3::rule<command_class, command_> const command = "command"; |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 46 | |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 47 | #if __clang__ |
| 48 | #pragma GCC diagnostic push |
| 49 | #pragma GCC diagnostic ignored "-Woverloaded-shift-op-parentheses" |
| 50 | #endif |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 51 | |
| 52 | auto const keyValue_def = |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 53 | lexeme[+alnum > '=' > +alnum]; |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 54 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 55 | auto const module_identifier_def = |
| 56 | lexeme[ |
| 57 | ((alpha | char_("_")) >> *(alnum | char_("_") | char_("-") | char_("."))) |
| 58 | ]; |
| 59 | |
| 60 | auto const node_identifier_def = |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 61 | lexeme[ |
| 62 | ((alpha | char_("_")) >> *(alnum | char_("_") | char_("-") | char_("."))) |
| 63 | ]; |
| 64 | |
| 65 | auto const listPrefix_def = |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 66 | node_identifier >> '['; |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 67 | |
Václav Kubernát | 7e4e82f | 2018-05-14 20:04:58 +0200 | [diff] [blame] | 68 | // even though we don't allow no keys to be supplied, the star allows me to check which keys are missing |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 69 | auto const listSuffix_def = |
Václav Kubernát | 7e4e82f | 2018-05-14 20:04:58 +0200 | [diff] [blame] | 70 | *keyValue > ']'; |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 71 | |
| 72 | auto const listElement_def = |
| 73 | listPrefix > listSuffix; |
| 74 | |
Václav Kubernát | 60d6f29 | 2018-05-25 09:45:32 +0200 | [diff] [blame] | 75 | auto const nodeup_def = |
| 76 | lit("..") > x3::attr(nodeup_()); |
| 77 | |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 78 | auto const container_def = |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 79 | node_identifier; |
| 80 | |
| 81 | auto const module_def = |
| 82 | module_identifier >> x3::no_skip[':'] >> !x3::no_skip[space]; |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 83 | |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 84 | auto const leaf_def = |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 85 | node_identifier; |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 86 | |
| 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át | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 88 | auto const node_def = |
| 89 | -(module) >> x3::expect[container | listElement | nodeup | leaf]; |
| 90 | |
Václav Kubernát | 37171a1 | 2018-08-31 17:01:48 +0200 | [diff] [blame] | 91 | auto 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át | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 95 | auto const path_def = |
Václav Kubernát | 37171a1 | 2018-08-31 17:01:48 +0200 | [diff] [blame] | 96 | absoluteStart >> x3::attr(decltype(path_::m_nodes)()) >> x3::eoi | |
| 97 | -(absoluteStart) >> node % '/'; |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 98 | |
Václav Kubernát | 0b0272f | 2018-06-13 14:13:08 +0200 | [diff] [blame] | 99 | auto const leafPath_def = |
| 100 | path; |
| 101 | |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 102 | auto const leaf_data_enum_def = |
| 103 | +char_; |
| 104 | auto const leaf_data_decimal_def = |
| 105 | double_; |
| 106 | |
| 107 | struct 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 | |
| 116 | auto const leaf_data_bool_def = |
| 117 | bool_rule; |
| 118 | auto const leaf_data_int_def = |
| 119 | int_; |
| 120 | auto const leaf_data_uint_def = |
| 121 | uint_; |
| 122 | auto const leaf_data_string_def = |
| 123 | *char_; |
| 124 | |
| 125 | auto const leaf_data_def = |
Václav Kubernát | 0b0272f | 2018-06-13 14:13:08 +0200 | [diff] [blame] | 126 | x3::expect[ |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 127 | leaf_data_enum | |
| 128 | leaf_data_decimal | |
| 129 | leaf_data_bool | |
| 130 | leaf_data_int | |
| 131 | leaf_data_uint | |
Václav Kubernát | 0b0272f | 2018-06-13 14:13:08 +0200 | [diff] [blame] | 132 | leaf_data_string]; |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 133 | |
| 134 | auto const space_separator = |
| 135 | x3::omit[x3::no_skip[space]]; |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 136 | |
Václav Kubernát | 11afac7 | 2018-07-18 14:59:53 +0200 | [diff] [blame] | 137 | auto const ls_def = |
| 138 | lit("ls") >> -path; |
| 139 | |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 140 | auto const cd_def = |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 141 | lit("cd") >> space_separator > path; |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 142 | |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 143 | auto const create_def = |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 144 | lit("create") >> space_separator > path; |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 145 | |
| 146 | auto const delete_rule_def = |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 147 | lit("delete") >> space_separator > path; |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 148 | |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 149 | auto const get_def = |
| 150 | lit("get") >> -path; |
| 151 | |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 152 | auto const set_def = |
Václav Kubernát | 0b0272f | 2018-06-13 14:13:08 +0200 | [diff] [blame] | 153 | lit("set") >> space_separator > leafPath > leaf_data; |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 154 | |
Václav Kubernát | 812ee28 | 2018-08-30 17:10:03 +0200 | [diff] [blame] | 155 | auto const commit_def = |
| 156 | lit("commit") >> x3::attr(commit_()); |
| 157 | |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 158 | auto const command_def = |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 159 | x3::expect[cd | create | delete_rule | set | commit | get | ls] >> x3::eoi; |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 160 | |
| 161 | #if __clang__ |
| 162 | #pragma GCC diagnostic pop |
| 163 | #endif |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 164 | |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 165 | BOOST_SPIRIT_DEFINE(keyValue) |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 166 | BOOST_SPIRIT_DEFINE(node_identifier) |
| 167 | BOOST_SPIRIT_DEFINE(module_identifier) |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 168 | BOOST_SPIRIT_DEFINE(listPrefix) |
| 169 | BOOST_SPIRIT_DEFINE(listSuffix) |
| 170 | BOOST_SPIRIT_DEFINE(listElement) |
Václav Kubernát | 60d6f29 | 2018-05-25 09:45:32 +0200 | [diff] [blame] | 171 | BOOST_SPIRIT_DEFINE(nodeup) |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 172 | BOOST_SPIRIT_DEFINE(container) |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 173 | BOOST_SPIRIT_DEFINE(leaf) |
Václav Kubernát | 0b0272f | 2018-06-13 14:13:08 +0200 | [diff] [blame] | 174 | BOOST_SPIRIT_DEFINE(leafPath) |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 175 | BOOST_SPIRIT_DEFINE(node) |
Václav Kubernát | 37171a1 | 2018-08-31 17:01:48 +0200 | [diff] [blame] | 176 | BOOST_SPIRIT_DEFINE(absoluteStart) |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 177 | BOOST_SPIRIT_DEFINE(path) |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 178 | BOOST_SPIRIT_DEFINE(module) |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 179 | BOOST_SPIRIT_DEFINE(leaf_data) |
| 180 | BOOST_SPIRIT_DEFINE(leaf_data_enum) |
| 181 | BOOST_SPIRIT_DEFINE(leaf_data_decimal) |
| 182 | BOOST_SPIRIT_DEFINE(leaf_data_bool) |
| 183 | BOOST_SPIRIT_DEFINE(leaf_data_int) |
| 184 | BOOST_SPIRIT_DEFINE(leaf_data_uint) |
| 185 | BOOST_SPIRIT_DEFINE(leaf_data_string) |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 186 | BOOST_SPIRIT_DEFINE(set) |
Václav Kubernát | 812ee28 | 2018-08-30 17:10:03 +0200 | [diff] [blame] | 187 | BOOST_SPIRIT_DEFINE(commit) |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 188 | BOOST_SPIRIT_DEFINE(get) |
Václav Kubernát | 11afac7 | 2018-07-18 14:59:53 +0200 | [diff] [blame] | 189 | BOOST_SPIRIT_DEFINE(ls) |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 190 | BOOST_SPIRIT_DEFINE(cd) |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 191 | BOOST_SPIRIT_DEFINE(create) |
| 192 | BOOST_SPIRIT_DEFINE(delete_rule) |
| 193 | BOOST_SPIRIT_DEFINE(command) |