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 | 89728d8 | 2018-09-13 16:28:28 +0200 | [diff] [blame] | 16 | x3::rule<key_identifier_class, std::string> const key_identifier = "key_identifier"; |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 17 | x3::rule<node_identifier_class, std::string> const node_identifier = "node_identifier"; |
| 18 | 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] | 19 | x3::rule<listPrefix_class, std::string> const listPrefix = "listPrefix"; |
| 20 | x3::rule<listSuffix_class, std::vector<keyValue_>> const listSuffix = "listSuffix"; |
| 21 | x3::rule<listElement_class, listElement_> const listElement = "listElement"; |
Václav Kubernát | 60d6f29 | 2018-05-25 09:45:32 +0200 | [diff] [blame] | 22 | x3::rule<nodeup_class, nodeup_> const nodeup = "nodeup"; |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 23 | x3::rule<container_class, container_> const container = "container"; |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 24 | x3::rule<leaf_class, leaf_> const leaf = "leaf"; |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 25 | x3::rule<module_class, module_> const module = "module"; |
| 26 | x3::rule<node_class, node_> const node = "node"; |
Václav Kubernát | 37171a1 | 2018-08-31 17:01:48 +0200 | [diff] [blame] | 27 | x3::rule<absoluteStart_class, Scope> const absoluteStart = "absoluteStart"; |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 28 | x3::rule<path_class, path_> const path = "path"; |
Václav Kubernát | 0b0272f | 2018-06-13 14:13:08 +0200 | [diff] [blame] | 29 | x3::rule<leaf_path_class, path_> const leafPath = "leafPath"; |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 30 | |
| 31 | x3::rule<leaf_data_class, leaf_data_> const leaf_data = "leaf_data"; |
| 32 | x3::rule<leaf_data_enum_class, enum_> const leaf_data_enum = "leaf_data_enum"; |
| 33 | x3::rule<leaf_data_decimal_class, double> const leaf_data_decimal = "leaf_data_decimal"; |
| 34 | x3::rule<leaf_data_bool_class, bool> const leaf_data_bool = "leaf_data_bool"; |
| 35 | x3::rule<leaf_data_int_class, int32_t> const leaf_data_int = "leaf_data_int"; |
| 36 | x3::rule<leaf_data_uint_class, uint32_t> const leaf_data_uint = "leaf_data_uint"; |
| 37 | x3::rule<leaf_data_string_class, std::string> const leaf_data_string = "leaf_data_string"; |
| 38 | |
Václav Kubernát | 6d79143 | 2018-10-25 16:00:35 +0200 | [diff] [blame^] | 39 | x3::rule<discard_class, discard_> const discard = "discard"; |
Václav Kubernát | 11afac7 | 2018-07-18 14:59:53 +0200 | [diff] [blame] | 40 | x3::rule<ls_class, ls_> const ls = "ls"; |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 41 | x3::rule<cd_class, cd_> const cd = "cd"; |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 42 | x3::rule<set_class, set_> const set = "set"; |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 43 | x3::rule<get_class, get_> const get = "get"; |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 44 | x3::rule<create_class, create_> const create = "create"; |
| 45 | x3::rule<delete_class, delete_> const delete_rule = "delete_rule"; |
Václav Kubernát | 812ee28 | 2018-08-30 17:10:03 +0200 | [diff] [blame] | 46 | x3::rule<commit_class, commit_> const commit = "commit"; |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 47 | x3::rule<command_class, command_> const command = "command"; |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 48 | |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 49 | #if __clang__ |
| 50 | #pragma GCC diagnostic push |
| 51 | #pragma GCC diagnostic ignored "-Woverloaded-shift-op-parentheses" |
| 52 | #endif |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 53 | |
Václav Kubernát | 89728d8 | 2018-09-13 16:28:28 +0200 | [diff] [blame] | 54 | auto const key_identifier_def = |
| 55 | lexeme[ |
| 56 | ((alpha | char_("_")) >> *(alnum | char_("_") | char_("-") | char_("."))) |
| 57 | ]; |
| 58 | |
| 59 | auto const quotedValue = |
| 60 | ('\'' > +(char_-'\'') > '\'') | |
| 61 | ('\"' > +(char_-'\"') > '\"'); |
| 62 | |
| 63 | auto const number = |
| 64 | +x3::digit; |
| 65 | |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 66 | auto const keyValue_def = |
Václav Kubernát | 89728d8 | 2018-09-13 16:28:28 +0200 | [diff] [blame] | 67 | lexeme['[' > key_identifier > '=' > (quotedValue | number) > ']']; |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 68 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 69 | auto const module_identifier_def = |
| 70 | lexeme[ |
| 71 | ((alpha | char_("_")) >> *(alnum | char_("_") | char_("-") | char_("."))) |
| 72 | ]; |
| 73 | |
| 74 | auto const node_identifier_def = |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 75 | lexeme[ |
| 76 | ((alpha | char_("_")) >> *(alnum | char_("_") | char_("-") | char_("."))) |
| 77 | ]; |
| 78 | |
| 79 | auto const listPrefix_def = |
Václav Kubernát | 89728d8 | 2018-09-13 16:28:28 +0200 | [diff] [blame] | 80 | node_identifier >> &char_('['); |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 81 | |
Václav Kubernát | 7e4e82f | 2018-05-14 20:04:58 +0200 | [diff] [blame] | 82 | // 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] | 83 | auto const listSuffix_def = |
Václav Kubernát | 89728d8 | 2018-09-13 16:28:28 +0200 | [diff] [blame] | 84 | *keyValue; |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 85 | |
| 86 | auto const listElement_def = |
| 87 | listPrefix > listSuffix; |
| 88 | |
Václav Kubernát | 60d6f29 | 2018-05-25 09:45:32 +0200 | [diff] [blame] | 89 | auto const nodeup_def = |
| 90 | lit("..") > x3::attr(nodeup_()); |
| 91 | |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 92 | auto const container_def = |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 93 | node_identifier; |
| 94 | |
| 95 | auto const module_def = |
| 96 | module_identifier >> x3::no_skip[':'] >> !x3::no_skip[space]; |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 97 | |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 98 | auto const leaf_def = |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 99 | node_identifier; |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 100 | |
| 101 | // 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] | 102 | auto const node_def = |
| 103 | -(module) >> x3::expect[container | listElement | nodeup | leaf]; |
| 104 | |
Václav Kubernát | 37171a1 | 2018-08-31 17:01:48 +0200 | [diff] [blame] | 105 | auto const absoluteStart_def = |
| 106 | x3::omit['/'] >> x3::attr(Scope::Absolute); |
| 107 | |
| 108 | // 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] | 109 | auto const path_def = |
Václav Kubernát | 37171a1 | 2018-08-31 17:01:48 +0200 | [diff] [blame] | 110 | absoluteStart >> x3::attr(decltype(path_::m_nodes)()) >> x3::eoi | |
| 111 | -(absoluteStart) >> node % '/'; |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 112 | |
Václav Kubernát | 0b0272f | 2018-06-13 14:13:08 +0200 | [diff] [blame] | 113 | auto const leafPath_def = |
| 114 | path; |
| 115 | |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 116 | auto const leaf_data_enum_def = |
| 117 | +char_; |
| 118 | auto const leaf_data_decimal_def = |
| 119 | double_; |
| 120 | |
| 121 | struct bool_symbol_table : x3::symbols<bool> { |
| 122 | bool_symbol_table() |
| 123 | { |
| 124 | add |
| 125 | ("true", true) |
| 126 | ("false", false); |
| 127 | } |
| 128 | } const bool_rule; |
| 129 | |
| 130 | auto const leaf_data_bool_def = |
| 131 | bool_rule; |
| 132 | auto const leaf_data_int_def = |
| 133 | int_; |
| 134 | auto const leaf_data_uint_def = |
| 135 | uint_; |
| 136 | auto const leaf_data_string_def = |
| 137 | *char_; |
| 138 | |
| 139 | auto const leaf_data_def = |
Václav Kubernát | 0b0272f | 2018-06-13 14:13:08 +0200 | [diff] [blame] | 140 | x3::expect[ |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 141 | leaf_data_enum | |
| 142 | leaf_data_decimal | |
| 143 | leaf_data_bool | |
| 144 | leaf_data_int | |
| 145 | leaf_data_uint | |
Václav Kubernát | 0b0272f | 2018-06-13 14:13:08 +0200 | [diff] [blame] | 146 | leaf_data_string]; |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 147 | |
| 148 | auto const space_separator = |
| 149 | x3::omit[x3::no_skip[space]]; |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 150 | |
Václav Kubernát | e7d4aea | 2018-09-11 18:15:48 +0200 | [diff] [blame] | 151 | struct ls_options_table : x3::symbols<LsOption> { |
| 152 | ls_options_table() |
| 153 | { |
| 154 | add |
| 155 | ("--recursive", LsOption::Recursive); |
| 156 | } |
| 157 | } const ls_options; |
| 158 | |
Václav Kubernát | 11afac7 | 2018-07-18 14:59:53 +0200 | [diff] [blame] | 159 | auto const ls_def = |
Václav Kubernát | e7d4aea | 2018-09-11 18:15:48 +0200 | [diff] [blame] | 160 | lit("ls") >> *(space_separator >> ls_options) >> -(space_separator >> path); |
Václav Kubernát | 11afac7 | 2018-07-18 14:59:53 +0200 | [diff] [blame] | 161 | |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 162 | auto const cd_def = |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 163 | lit("cd") >> space_separator > path; |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 164 | |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 165 | auto const create_def = |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 166 | lit("create") >> space_separator > path; |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 167 | |
| 168 | auto const delete_rule_def = |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 169 | lit("delete") >> space_separator > path; |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 170 | |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 171 | auto const get_def = |
| 172 | lit("get") >> -path; |
| 173 | |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 174 | auto const set_def = |
Václav Kubernát | 0b0272f | 2018-06-13 14:13:08 +0200 | [diff] [blame] | 175 | lit("set") >> space_separator > leafPath > leaf_data; |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 176 | |
Václav Kubernát | 812ee28 | 2018-08-30 17:10:03 +0200 | [diff] [blame] | 177 | auto const commit_def = |
| 178 | lit("commit") >> x3::attr(commit_()); |
| 179 | |
Václav Kubernát | 6d79143 | 2018-10-25 16:00:35 +0200 | [diff] [blame^] | 180 | auto const discard_def = |
| 181 | lit("discard") >> x3::attr(discard_()); |
| 182 | |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 183 | auto const command_def = |
Václav Kubernát | 6d79143 | 2018-10-25 16:00:35 +0200 | [diff] [blame^] | 184 | x3::expect[cd | create | delete_rule | set | commit | get | ls | discard] >> x3::eoi; |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 185 | |
| 186 | #if __clang__ |
| 187 | #pragma GCC diagnostic pop |
| 188 | #endif |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 189 | |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 190 | BOOST_SPIRIT_DEFINE(keyValue) |
Václav Kubernát | 89728d8 | 2018-09-13 16:28:28 +0200 | [diff] [blame] | 191 | BOOST_SPIRIT_DEFINE(key_identifier) |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 192 | BOOST_SPIRIT_DEFINE(node_identifier) |
| 193 | BOOST_SPIRIT_DEFINE(module_identifier) |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 194 | BOOST_SPIRIT_DEFINE(listPrefix) |
| 195 | BOOST_SPIRIT_DEFINE(listSuffix) |
| 196 | BOOST_SPIRIT_DEFINE(listElement) |
Václav Kubernát | 60d6f29 | 2018-05-25 09:45:32 +0200 | [diff] [blame] | 197 | BOOST_SPIRIT_DEFINE(nodeup) |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 198 | BOOST_SPIRIT_DEFINE(container) |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 199 | BOOST_SPIRIT_DEFINE(leaf) |
Václav Kubernát | 0b0272f | 2018-06-13 14:13:08 +0200 | [diff] [blame] | 200 | BOOST_SPIRIT_DEFINE(leafPath) |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 201 | BOOST_SPIRIT_DEFINE(node) |
Václav Kubernát | 37171a1 | 2018-08-31 17:01:48 +0200 | [diff] [blame] | 202 | BOOST_SPIRIT_DEFINE(absoluteStart) |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 203 | BOOST_SPIRIT_DEFINE(path) |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 204 | BOOST_SPIRIT_DEFINE(module) |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 205 | BOOST_SPIRIT_DEFINE(leaf_data) |
| 206 | BOOST_SPIRIT_DEFINE(leaf_data_enum) |
| 207 | BOOST_SPIRIT_DEFINE(leaf_data_decimal) |
| 208 | BOOST_SPIRIT_DEFINE(leaf_data_bool) |
| 209 | BOOST_SPIRIT_DEFINE(leaf_data_int) |
| 210 | BOOST_SPIRIT_DEFINE(leaf_data_uint) |
| 211 | BOOST_SPIRIT_DEFINE(leaf_data_string) |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 212 | BOOST_SPIRIT_DEFINE(set) |
Václav Kubernát | 812ee28 | 2018-08-30 17:10:03 +0200 | [diff] [blame] | 213 | BOOST_SPIRIT_DEFINE(commit) |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 214 | BOOST_SPIRIT_DEFINE(get) |
Václav Kubernát | 11afac7 | 2018-07-18 14:59:53 +0200 | [diff] [blame] | 215 | BOOST_SPIRIT_DEFINE(ls) |
Václav Kubernát | 6d79143 | 2018-10-25 16:00:35 +0200 | [diff] [blame^] | 216 | BOOST_SPIRIT_DEFINE(discard) |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 217 | BOOST_SPIRIT_DEFINE(cd) |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 218 | BOOST_SPIRIT_DEFINE(create) |
| 219 | BOOST_SPIRIT_DEFINE(delete_rule) |
| 220 | BOOST_SPIRIT_DEFINE(command) |