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 | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 26 | x3::rule<path_class, path_> const path = "path"; |
Václav Kubernát | 0b0272f | 2018-06-13 14:13:08 +0200 | [diff] [blame] | 27 | x3::rule<leaf_path_class, path_> const leafPath = "leafPath"; |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 28 | |
| 29 | x3::rule<leaf_data_class, leaf_data_> const leaf_data = "leaf_data"; |
| 30 | x3::rule<leaf_data_enum_class, enum_> const leaf_data_enum = "leaf_data_enum"; |
| 31 | x3::rule<leaf_data_decimal_class, double> const leaf_data_decimal = "leaf_data_decimal"; |
| 32 | x3::rule<leaf_data_bool_class, bool> const leaf_data_bool = "leaf_data_bool"; |
| 33 | x3::rule<leaf_data_int_class, int32_t> const leaf_data_int = "leaf_data_int"; |
| 34 | x3::rule<leaf_data_uint_class, uint32_t> const leaf_data_uint = "leaf_data_uint"; |
| 35 | x3::rule<leaf_data_string_class, std::string> const leaf_data_string = "leaf_data_string"; |
| 36 | |
Václav Kubernát | 11afac7 | 2018-07-18 14:59:53 +0200 | [diff] [blame] | 37 | x3::rule<ls_class, ls_> const ls = "ls"; |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 38 | x3::rule<cd_class, cd_> const cd = "cd"; |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 39 | x3::rule<set_class, set_> const set = "set"; |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 40 | x3::rule<create_class, create_> const create = "create"; |
| 41 | x3::rule<delete_class, delete_> const delete_rule = "delete_rule"; |
| 42 | x3::rule<command_class, command_> const command = "command"; |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 43 | |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 44 | #if __clang__ |
| 45 | #pragma GCC diagnostic push |
| 46 | #pragma GCC diagnostic ignored "-Woverloaded-shift-op-parentheses" |
| 47 | #endif |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 48 | |
| 49 | auto const keyValue_def = |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 50 | lexeme[+alnum > '=' > +alnum]; |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 51 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 52 | auto const module_identifier_def = |
| 53 | lexeme[ |
| 54 | ((alpha | char_("_")) >> *(alnum | char_("_") | char_("-") | char_("."))) |
| 55 | ]; |
| 56 | |
| 57 | auto const node_identifier_def = |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 58 | lexeme[ |
| 59 | ((alpha | char_("_")) >> *(alnum | char_("_") | char_("-") | char_("."))) |
| 60 | ]; |
| 61 | |
| 62 | auto const listPrefix_def = |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 63 | node_identifier >> '['; |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 64 | |
Václav Kubernát | 7e4e82f | 2018-05-14 20:04:58 +0200 | [diff] [blame] | 65 | // 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] | 66 | auto const listSuffix_def = |
Václav Kubernát | 7e4e82f | 2018-05-14 20:04:58 +0200 | [diff] [blame] | 67 | *keyValue > ']'; |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 68 | |
| 69 | auto const listElement_def = |
| 70 | listPrefix > listSuffix; |
| 71 | |
Václav Kubernát | 60d6f29 | 2018-05-25 09:45:32 +0200 | [diff] [blame] | 72 | auto const nodeup_def = |
| 73 | lit("..") > x3::attr(nodeup_()); |
| 74 | |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 75 | auto const container_def = |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 76 | node_identifier; |
| 77 | |
| 78 | auto const module_def = |
| 79 | module_identifier >> x3::no_skip[':'] >> !x3::no_skip[space]; |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 80 | |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 81 | auto const leaf_def = |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 82 | node_identifier; |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 83 | |
| 84 | // 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] | 85 | auto const node_def = |
| 86 | -(module) >> x3::expect[container | listElement | nodeup | leaf]; |
| 87 | |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 88 | auto const path_def = |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 89 | node % '/'; |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 90 | |
Václav Kubernát | 0b0272f | 2018-06-13 14:13:08 +0200 | [diff] [blame] | 91 | auto const leafPath_def = |
| 92 | path; |
| 93 | |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 94 | auto const leaf_data_enum_def = |
| 95 | +char_; |
| 96 | auto const leaf_data_decimal_def = |
| 97 | double_; |
| 98 | |
| 99 | struct bool_symbol_table : x3::symbols<bool> { |
| 100 | bool_symbol_table() |
| 101 | { |
| 102 | add |
| 103 | ("true", true) |
| 104 | ("false", false); |
| 105 | } |
| 106 | } const bool_rule; |
| 107 | |
| 108 | auto const leaf_data_bool_def = |
| 109 | bool_rule; |
| 110 | auto const leaf_data_int_def = |
| 111 | int_; |
| 112 | auto const leaf_data_uint_def = |
| 113 | uint_; |
| 114 | auto const leaf_data_string_def = |
| 115 | *char_; |
| 116 | |
| 117 | auto const leaf_data_def = |
Václav Kubernát | 0b0272f | 2018-06-13 14:13:08 +0200 | [diff] [blame] | 118 | x3::expect[ |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 119 | leaf_data_enum | |
| 120 | leaf_data_decimal | |
| 121 | leaf_data_bool | |
| 122 | leaf_data_int | |
| 123 | leaf_data_uint | |
Václav Kubernát | 0b0272f | 2018-06-13 14:13:08 +0200 | [diff] [blame] | 124 | leaf_data_string]; |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 125 | |
| 126 | auto const space_separator = |
| 127 | x3::omit[x3::no_skip[space]]; |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 128 | |
Václav Kubernát | 11afac7 | 2018-07-18 14:59:53 +0200 | [diff] [blame] | 129 | auto const ls_def = |
| 130 | lit("ls") >> -path; |
| 131 | |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 132 | auto const cd_def = |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 133 | lit("cd") >> space_separator > path; |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 134 | |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 135 | auto const create_def = |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 136 | lit("create") >> space_separator > path; |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 137 | |
| 138 | auto const delete_rule_def = |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 139 | lit("delete") >> space_separator > path; |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 140 | |
| 141 | auto const set_def = |
Václav Kubernát | 0b0272f | 2018-06-13 14:13:08 +0200 | [diff] [blame] | 142 | lit("set") >> space_separator > leafPath > leaf_data; |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 143 | |
| 144 | auto const command_def = |
Václav Kubernát | 11afac7 | 2018-07-18 14:59:53 +0200 | [diff] [blame] | 145 | x3::expect[cd | create | delete_rule | set | ls] >> x3::eoi; |
Václav Kubernát | 4137845 | 2018-06-06 16:29:40 +0200 | [diff] [blame] | 146 | |
| 147 | #if __clang__ |
| 148 | #pragma GCC diagnostic pop |
| 149 | #endif |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 150 | |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 151 | BOOST_SPIRIT_DEFINE(keyValue) |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 152 | BOOST_SPIRIT_DEFINE(node_identifier) |
| 153 | BOOST_SPIRIT_DEFINE(module_identifier) |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 154 | BOOST_SPIRIT_DEFINE(listPrefix) |
| 155 | BOOST_SPIRIT_DEFINE(listSuffix) |
| 156 | BOOST_SPIRIT_DEFINE(listElement) |
Václav Kubernát | 60d6f29 | 2018-05-25 09:45:32 +0200 | [diff] [blame] | 157 | BOOST_SPIRIT_DEFINE(nodeup) |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 158 | BOOST_SPIRIT_DEFINE(container) |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 159 | BOOST_SPIRIT_DEFINE(leaf) |
Václav Kubernát | 0b0272f | 2018-06-13 14:13:08 +0200 | [diff] [blame] | 160 | BOOST_SPIRIT_DEFINE(leafPath) |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 161 | BOOST_SPIRIT_DEFINE(node) |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 162 | BOOST_SPIRIT_DEFINE(path) |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 163 | BOOST_SPIRIT_DEFINE(module) |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 164 | BOOST_SPIRIT_DEFINE(leaf_data) |
| 165 | BOOST_SPIRIT_DEFINE(leaf_data_enum) |
| 166 | BOOST_SPIRIT_DEFINE(leaf_data_decimal) |
| 167 | BOOST_SPIRIT_DEFINE(leaf_data_bool) |
| 168 | BOOST_SPIRIT_DEFINE(leaf_data_int) |
| 169 | BOOST_SPIRIT_DEFINE(leaf_data_uint) |
| 170 | BOOST_SPIRIT_DEFINE(leaf_data_string) |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 171 | BOOST_SPIRIT_DEFINE(set) |
Václav Kubernát | 11afac7 | 2018-07-18 14:59:53 +0200 | [diff] [blame] | 172 | BOOST_SPIRIT_DEFINE(ls) |
Václav Kubernát | 0a2a2e8 | 2018-05-11 13:59:12 +0200 | [diff] [blame] | 173 | BOOST_SPIRIT_DEFINE(cd) |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 174 | BOOST_SPIRIT_DEFINE(create) |
| 175 | BOOST_SPIRIT_DEFINE(delete_rule) |
| 176 | BOOST_SPIRIT_DEFINE(command) |