blob: ed21e316aa27868ec0e931b5aa61db6535b6aeb0 [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át0a2a2e82018-05-11 13:59:12 +020026x3::rule<path_class, path_> const path = "path";
Václav Kubernát0b0272f2018-06-13 14:13:08 +020027x3::rule<leaf_path_class, path_> const leafPath = "leafPath";
Václav Kubernátebca2552018-06-08 19:06:02 +020028
29x3::rule<leaf_data_class, leaf_data_> const leaf_data = "leaf_data";
30x3::rule<leaf_data_enum_class, enum_> const leaf_data_enum = "leaf_data_enum";
31x3::rule<leaf_data_decimal_class, double> const leaf_data_decimal = "leaf_data_decimal";
32x3::rule<leaf_data_bool_class, bool> const leaf_data_bool = "leaf_data_bool";
33x3::rule<leaf_data_int_class, int32_t> const leaf_data_int = "leaf_data_int";
34x3::rule<leaf_data_uint_class, uint32_t> const leaf_data_uint = "leaf_data_uint";
35x3::rule<leaf_data_string_class, std::string> const leaf_data_string = "leaf_data_string";
36
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020037x3::rule<cd_class, cd_> const cd = "cd";
Václav Kubernát07204242018-06-04 18:12:09 +020038x3::rule<set_class, set_> const set = "set";
Václav Kubernátb61336d2018-05-28 17:35:03 +020039x3::rule<create_class, create_> const create = "create";
40x3::rule<delete_class, delete_> const delete_rule = "delete_rule";
41x3::rule<command_class, command_> const command = "command";
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020042
Václav Kubernát41378452018-06-06 16:29:40 +020043#if __clang__
44#pragma GCC diagnostic push
45#pragma GCC diagnostic ignored "-Woverloaded-shift-op-parentheses"
46#endif
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020047
48auto const keyValue_def =
Václav Kubernát41378452018-06-06 16:29:40 +020049 lexeme[+alnum > '=' > +alnum];
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020050
Václav Kubernát744f57f2018-06-29 22:46:26 +020051auto const module_identifier_def =
52 lexeme[
53 ((alpha | char_("_")) >> *(alnum | char_("_") | char_("-") | char_(".")))
54 ];
55
56auto const node_identifier_def =
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020057 lexeme[
58 ((alpha | char_("_")) >> *(alnum | char_("_") | char_("-") | char_(".")))
59 ];
60
61auto const listPrefix_def =
Václav Kubernát744f57f2018-06-29 22:46:26 +020062 node_identifier >> '[';
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020063
Václav Kubernát7e4e82f2018-05-14 20:04:58 +020064// 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 +020065auto const listSuffix_def =
Václav Kubernát7e4e82f2018-05-14 20:04:58 +020066 *keyValue > ']';
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020067
68auto const listElement_def =
69 listPrefix > listSuffix;
70
Václav Kubernát60d6f292018-05-25 09:45:32 +020071auto const nodeup_def =
72 lit("..") > x3::attr(nodeup_());
73
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020074auto const container_def =
Václav Kubernát744f57f2018-06-29 22:46:26 +020075 node_identifier;
76
77auto const module_def =
78 module_identifier >> x3::no_skip[':'] >> !x3::no_skip[space];
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020079
Václav Kubernát07204242018-06-04 18:12:09 +020080auto const leaf_def =
Václav Kubernát744f57f2018-06-29 22:46:26 +020081 node_identifier;
Václav Kubernát07204242018-06-04 18:12:09 +020082
83// 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 +020084auto const node_def =
85 -(module) >> x3::expect[container | listElement | nodeup | leaf];
86
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020087auto const path_def =
Václav Kubernát744f57f2018-06-29 22:46:26 +020088 node % '/';
Václav Kubernát07204242018-06-04 18:12:09 +020089
Václav Kubernát0b0272f2018-06-13 14:13:08 +020090auto const leafPath_def =
91 path;
92
Václav Kubernátebca2552018-06-08 19:06:02 +020093auto const leaf_data_enum_def =
94 +char_;
95auto const leaf_data_decimal_def =
96 double_;
97
98struct bool_symbol_table : x3::symbols<bool> {
99 bool_symbol_table()
100 {
101 add
102 ("true", true)
103 ("false", false);
104 }
105} const bool_rule;
106
107auto const leaf_data_bool_def =
108 bool_rule;
109auto const leaf_data_int_def =
110 int_;
111auto const leaf_data_uint_def =
112 uint_;
113auto const leaf_data_string_def =
114 *char_;
115
116auto const leaf_data_def =
Václav Kubernát0b0272f2018-06-13 14:13:08 +0200117x3::expect[
Václav Kubernátebca2552018-06-08 19:06:02 +0200118 leaf_data_enum |
119 leaf_data_decimal |
120 leaf_data_bool |
121 leaf_data_int |
122 leaf_data_uint |
Václav Kubernát0b0272f2018-06-13 14:13:08 +0200123 leaf_data_string];
Václav Kubernát07204242018-06-04 18:12:09 +0200124
125auto const space_separator =
126 x3::omit[x3::no_skip[space]];
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200127
128auto const cd_def =
Václav Kubernát41378452018-06-06 16:29:40 +0200129 lit("cd") >> space_separator > path;
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200130
Václav Kubernátb61336d2018-05-28 17:35:03 +0200131auto const create_def =
Václav Kubernát41378452018-06-06 16:29:40 +0200132 lit("create") >> space_separator > path;
Václav Kubernátb61336d2018-05-28 17:35:03 +0200133
134auto const delete_rule_def =
Václav Kubernát41378452018-06-06 16:29:40 +0200135 lit("delete") >> space_separator > path;
Václav Kubernát07204242018-06-04 18:12:09 +0200136
137auto const set_def =
Václav Kubernát0b0272f2018-06-13 14:13:08 +0200138 lit("set") >> space_separator > leafPath > leaf_data;
Václav Kubernátb61336d2018-05-28 17:35:03 +0200139
140auto const command_def =
Václav Kubernát41378452018-06-06 16:29:40 +0200141 x3::expect[cd | create | delete_rule | set] >> x3::eoi;
142
143#if __clang__
144#pragma GCC diagnostic pop
145#endif
Václav Kubernátb61336d2018-05-28 17:35:03 +0200146
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200147BOOST_SPIRIT_DEFINE(keyValue)
Václav Kubernát744f57f2018-06-29 22:46:26 +0200148BOOST_SPIRIT_DEFINE(node_identifier)
149BOOST_SPIRIT_DEFINE(module_identifier)
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200150BOOST_SPIRIT_DEFINE(listPrefix)
151BOOST_SPIRIT_DEFINE(listSuffix)
152BOOST_SPIRIT_DEFINE(listElement)
Václav Kubernát60d6f292018-05-25 09:45:32 +0200153BOOST_SPIRIT_DEFINE(nodeup)
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200154BOOST_SPIRIT_DEFINE(container)
Václav Kubernát07204242018-06-04 18:12:09 +0200155BOOST_SPIRIT_DEFINE(leaf)
Václav Kubernát0b0272f2018-06-13 14:13:08 +0200156BOOST_SPIRIT_DEFINE(leafPath)
Václav Kubernát744f57f2018-06-29 22:46:26 +0200157BOOST_SPIRIT_DEFINE(node)
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200158BOOST_SPIRIT_DEFINE(path)
Václav Kubernát744f57f2018-06-29 22:46:26 +0200159BOOST_SPIRIT_DEFINE(module)
Václav Kubernátebca2552018-06-08 19:06:02 +0200160BOOST_SPIRIT_DEFINE(leaf_data)
161BOOST_SPIRIT_DEFINE(leaf_data_enum)
162BOOST_SPIRIT_DEFINE(leaf_data_decimal)
163BOOST_SPIRIT_DEFINE(leaf_data_bool)
164BOOST_SPIRIT_DEFINE(leaf_data_int)
165BOOST_SPIRIT_DEFINE(leaf_data_uint)
166BOOST_SPIRIT_DEFINE(leaf_data_string)
Václav Kubernát07204242018-06-04 18:12:09 +0200167BOOST_SPIRIT_DEFINE(set)
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200168BOOST_SPIRIT_DEFINE(cd)
Václav Kubernátb61336d2018-05-28 17:35:03 +0200169BOOST_SPIRIT_DEFINE(create)
170BOOST_SPIRIT_DEFINE(delete_rule)
171BOOST_SPIRIT_DEFINE(command)