blob: 53864177cb9efa7298e59ce7823ce333432417f8 [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";
16x3::rule<identifier_class, std::string> const identifier = "identifier";
17x3::rule<listPrefix_class, std::string> const listPrefix = "listPrefix";
18x3::rule<listSuffix_class, std::vector<keyValue_>> const listSuffix = "listSuffix";
19x3::rule<listElement_class, listElement_> const listElement = "listElement";
Václav Kubernát60d6f292018-05-25 09:45:32 +020020x3::rule<nodeup_class, nodeup_> const nodeup = "nodeup";
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020021x3::rule<container_class, container_> const container = "container";
Václav Kubernát07204242018-06-04 18:12:09 +020022x3::rule<leaf_class, leaf_> const leaf = "leaf";
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020023x3::rule<path_class, path_> const path = "path";
Václav Kubernát0b0272f2018-06-13 14:13:08 +020024x3::rule<leaf_path_class, path_> const leafPath = "leafPath";
Václav Kubernátebca2552018-06-08 19:06:02 +020025
26x3::rule<leaf_data_class, leaf_data_> const leaf_data = "leaf_data";
27x3::rule<leaf_data_enum_class, enum_> const leaf_data_enum = "leaf_data_enum";
28x3::rule<leaf_data_decimal_class, double> const leaf_data_decimal = "leaf_data_decimal";
29x3::rule<leaf_data_bool_class, bool> const leaf_data_bool = "leaf_data_bool";
30x3::rule<leaf_data_int_class, int32_t> const leaf_data_int = "leaf_data_int";
31x3::rule<leaf_data_uint_class, uint32_t> const leaf_data_uint = "leaf_data_uint";
32x3::rule<leaf_data_string_class, std::string> const leaf_data_string = "leaf_data_string";
33
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020034x3::rule<cd_class, cd_> const cd = "cd";
Václav Kubernát07204242018-06-04 18:12:09 +020035x3::rule<set_class, set_> const set = "set";
Václav Kubernátb61336d2018-05-28 17:35:03 +020036x3::rule<create_class, create_> const create = "create";
37x3::rule<delete_class, delete_> const delete_rule = "delete_rule";
38x3::rule<command_class, command_> const command = "command";
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020039
Václav Kubernát41378452018-06-06 16:29:40 +020040#if __clang__
41#pragma GCC diagnostic push
42#pragma GCC diagnostic ignored "-Woverloaded-shift-op-parentheses"
43#endif
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020044
45auto const keyValue_def =
Václav Kubernát41378452018-06-06 16:29:40 +020046 lexeme[+alnum > '=' > +alnum];
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020047
48auto const identifier_def =
49 lexeme[
50 ((alpha | char_("_")) >> *(alnum | char_("_") | char_("-") | char_(".")))
51 ];
52
53auto const listPrefix_def =
54 identifier >> '[';
55
Václav Kubernát7e4e82f2018-05-14 20:04:58 +020056// 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 +020057auto const listSuffix_def =
Václav Kubernát7e4e82f2018-05-14 20:04:58 +020058 *keyValue > ']';
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020059
60auto const listElement_def =
61 listPrefix > listSuffix;
62
Václav Kubernát60d6f292018-05-25 09:45:32 +020063auto const nodeup_def =
64 lit("..") > x3::attr(nodeup_());
65
Václav Kubernát0a2a2e82018-05-11 13:59:12 +020066auto const container_def =
67 identifier;
68
Václav Kubernát07204242018-06-04 18:12:09 +020069auto const leaf_def =
70 identifier;
71
72// 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át0a2a2e82018-05-11 13:59:12 +020073auto const path_def =
Václav Kubernát41378452018-06-06 16:29:40 +020074 (x3::expect[container | listElement | nodeup | leaf]) % '/';
Václav Kubernát07204242018-06-04 18:12:09 +020075
Václav Kubernát0b0272f2018-06-13 14:13:08 +020076auto const leafPath_def =
77 path;
78
Václav Kubernátebca2552018-06-08 19:06:02 +020079auto const leaf_data_enum_def =
80 +char_;
81auto const leaf_data_decimal_def =
82 double_;
83
84struct bool_symbol_table : x3::symbols<bool> {
85 bool_symbol_table()
86 {
87 add
88 ("true", true)
89 ("false", false);
90 }
91} const bool_rule;
92
93auto const leaf_data_bool_def =
94 bool_rule;
95auto const leaf_data_int_def =
96 int_;
97auto const leaf_data_uint_def =
98 uint_;
99auto const leaf_data_string_def =
100 *char_;
101
102auto const leaf_data_def =
Václav Kubernát0b0272f2018-06-13 14:13:08 +0200103x3::expect[
Václav Kubernátebca2552018-06-08 19:06:02 +0200104 leaf_data_enum |
105 leaf_data_decimal |
106 leaf_data_bool |
107 leaf_data_int |
108 leaf_data_uint |
Václav Kubernát0b0272f2018-06-13 14:13:08 +0200109 leaf_data_string];
Václav Kubernát07204242018-06-04 18:12:09 +0200110
111auto const space_separator =
112 x3::omit[x3::no_skip[space]];
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200113
114auto const cd_def =
Václav Kubernát41378452018-06-06 16:29:40 +0200115 lit("cd") >> space_separator > path;
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200116
Václav Kubernátb61336d2018-05-28 17:35:03 +0200117auto const create_def =
Václav Kubernát41378452018-06-06 16:29:40 +0200118 lit("create") >> space_separator > path;
Václav Kubernátb61336d2018-05-28 17:35:03 +0200119
120auto const delete_rule_def =
Václav Kubernát41378452018-06-06 16:29:40 +0200121 lit("delete") >> space_separator > path;
Václav Kubernát07204242018-06-04 18:12:09 +0200122
123auto const set_def =
Václav Kubernát0b0272f2018-06-13 14:13:08 +0200124 lit("set") >> space_separator > leafPath > leaf_data;
Václav Kubernátb61336d2018-05-28 17:35:03 +0200125
126auto const command_def =
Václav Kubernát41378452018-06-06 16:29:40 +0200127 x3::expect[cd | create | delete_rule | set] >> x3::eoi;
128
129#if __clang__
130#pragma GCC diagnostic pop
131#endif
Václav Kubernátb61336d2018-05-28 17:35:03 +0200132
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200133BOOST_SPIRIT_DEFINE(keyValue)
134BOOST_SPIRIT_DEFINE(identifier)
135BOOST_SPIRIT_DEFINE(listPrefix)
136BOOST_SPIRIT_DEFINE(listSuffix)
137BOOST_SPIRIT_DEFINE(listElement)
Václav Kubernát60d6f292018-05-25 09:45:32 +0200138BOOST_SPIRIT_DEFINE(nodeup)
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200139BOOST_SPIRIT_DEFINE(container)
Václav Kubernát07204242018-06-04 18:12:09 +0200140BOOST_SPIRIT_DEFINE(leaf)
Václav Kubernát0b0272f2018-06-13 14:13:08 +0200141BOOST_SPIRIT_DEFINE(leafPath)
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200142BOOST_SPIRIT_DEFINE(path)
Václav Kubernátebca2552018-06-08 19:06:02 +0200143BOOST_SPIRIT_DEFINE(leaf_data)
144BOOST_SPIRIT_DEFINE(leaf_data_enum)
145BOOST_SPIRIT_DEFINE(leaf_data_decimal)
146BOOST_SPIRIT_DEFINE(leaf_data_bool)
147BOOST_SPIRIT_DEFINE(leaf_data_int)
148BOOST_SPIRIT_DEFINE(leaf_data_uint)
149BOOST_SPIRIT_DEFINE(leaf_data_string)
Václav Kubernát07204242018-06-04 18:12:09 +0200150BOOST_SPIRIT_DEFINE(set)
Václav Kubernát0a2a2e82018-05-11 13:59:12 +0200151BOOST_SPIRIT_DEFINE(cd)
Václav Kubernátb61336d2018-05-28 17:35:03 +0200152BOOST_SPIRIT_DEFINE(create)
153BOOST_SPIRIT_DEFINE(delete_rule)
154BOOST_SPIRIT_DEFINE(command)