|
1 /** |
|
2 * Relational pipes |
|
3 * Copyright © 2021 František Kučera (Frantovo.cz, GlobalCode.info) |
|
4 * |
|
5 * This program is free software: you can redistribute it and/or modify |
|
6 * it under the terms of the GNU General Public License as published by |
|
7 * the Free Software Foundation, version 3 of the License. |
|
8 * |
|
9 * This program is distributed in the hope that it will be useful, |
|
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 * GNU General Public License for more details. |
|
13 * |
|
14 * You should have received a copy of the GNU General Public License |
|
15 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 */ |
|
17 #pragma once |
|
18 |
|
19 #include <vector> |
|
20 |
|
21 #include <relpipe/common/type/typedefs.h> |
|
22 #include <relpipe/cli/CLI.h> |
|
23 #include <relpipe/cli/RelpipeCLIException.h> |
|
24 |
|
25 #include "Configuration.h" |
|
26 |
|
27 namespace relpipe { |
|
28 namespace tr { |
|
29 namespace sed { |
|
30 |
|
31 class CLIParser { |
|
32 private: |
|
33 |
|
34 relpipe::common::type::StringX readNext(std::vector<relpipe::common::type::StringX> arguments, int& i) { |
|
35 if (i < arguments.size()) return arguments[i++]; |
|
36 else throw relpipe::cli::RelpipeCLIException(L"Missing CLI argument" + (i > 0 ? (L" after " + arguments[i - 1]) : L""), relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS); |
|
37 } |
|
38 |
|
39 /** |
|
40 * TODO: use a common method |
|
41 */ |
|
42 bool parseBoolean(const relpipe::common::type::StringX& value) { |
|
43 if (value == L"true") return true; |
|
44 else if (value == L"false") return false; |
|
45 else throw relpipe::cli::RelpipeCLIException(L"Unable to parse boolean value: " + value + L" (expecting true or false)", relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS); |
|
46 } |
|
47 |
|
48 ENTITY parseEntity(const relpipe::common::type::StringX& value) { |
|
49 if (value == L"relation") return ENTITY::RELATION; |
|
50 else if (value == L"attribute") return ENTITY::ATTRIBUTE; |
|
51 else if (value == L"value") return ENTITY::VALUE; |
|
52 else throw relpipe::cli::RelpipeCLIException(L"Unable to parse entity value: " + value + L" (expecting „relation“, „attribute“ or „value“)", relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS); |
|
53 } |
|
54 |
|
55 void addRule(RelationConfiguration& currentRelation, RewriteRule& currentRule) { |
|
56 if (currentRule.attribute.size()) { |
|
57 currentRule.attributePattern = currentRule.caseSensitive[ENTITY::ATTRIBUTE] ? std::wregex(currentRule.attribute) : std::wregex(currentRule.attribute, std::regex_constants::icase); |
|
58 currentRule.valuePattern = currentRule.caseSensitive[ENTITY::VALUE] ? std::wregex(currentRule.value) : std::wregex(currentRule.value, std::regex_constants::icase); |
|
59 currentRelation.rules.push_back(currentRule); |
|
60 currentRule = RewriteRule(currentRelation.caseSensitive, currentRelation.invertMatch); |
|
61 } |
|
62 } |
|
63 |
|
64 void addRelation(Configuration& c, RelationConfiguration& currentRelation, RewriteRule& currentRule) { |
|
65 if (currentRelation.relation.size()) { |
|
66 currentRelation.relationPattern = currentRelation.caseSensitive[ENTITY::RELATION] ? std::wregex(currentRelation.relation) : std::wregex(currentRelation.relation, std::regex_constants::icase); |
|
67 addRule(currentRelation, currentRule); // last rule |
|
68 c.relationConfigurations.push_back(currentRelation); |
|
69 currentRelation = RelationConfiguration(); |
|
70 } |
|
71 } |
|
72 |
|
73 public: |
|
74 |
|
75 static const relpipe::common::type::StringX OPTION_RELATION; |
|
76 static const relpipe::common::type::StringX OPTION_ATTRIBUTE; |
|
77 static const relpipe::common::type::StringX OPTION_VALUE; |
|
78 static const relpipe::common::type::StringX OPTION_REPLACEMENT; |
|
79 static const relpipe::common::type::StringX OPTION_CASE_SENSITIVE; |
|
80 static const relpipe::common::type::StringX OPTION_INVERT_MATCH; |
|
81 |
|
82 Configuration parse(const std::vector<relpipe::common::type::StringX>& arguments) { |
|
83 Configuration c; |
|
84 RelationConfiguration currentRelation; |
|
85 RewriteRule currentRule(currentRelation.caseSensitive, currentRelation.invertMatch); |
|
86 |
|
87 for (int i = 0; i < arguments.size();) { |
|
88 relpipe::common::type::StringX option = readNext(arguments, i); |
|
89 |
|
90 if (option == OPTION_RELATION) { |
|
91 addRelation(c, currentRelation, currentRule); // previous relation |
|
92 currentRelation.relation = readNext(arguments, i); |
|
93 } else if (option == OPTION_ATTRIBUTE) { |
|
94 addRule(currentRelation, currentRule); // previous rule |
|
95 currentRule.attribute = readNext(arguments, i); |
|
96 } else if (option == OPTION_VALUE) { |
|
97 currentRule.value = readNext(arguments, i); |
|
98 } else if (option == OPTION_REPLACEMENT) { |
|
99 currentRule.replacement = readNext(arguments, i); |
|
100 } else if (option == OPTION_CASE_SENSITIVE) { |
|
101 ENTITY entity = parseEntity(readNext(arguments, i)); |
|
102 bool value = parseBoolean(readNext(arguments, i)); |
|
103 if (currentRule.attribute.size()) currentRule.caseSensitive[entity] = value; |
|
104 else currentRelation.caseSensitive[entity] = value; |
|
105 } else if (option == OPTION_INVERT_MATCH) { |
|
106 ENTITY entity = parseEntity(readNext(arguments, i)); |
|
107 bool value = parseBoolean(readNext(arguments, i)); |
|
108 if (currentRule.attribute.size()) currentRule.invertMatch[entity] = value; |
|
109 else currentRelation.invertMatch[entity] = value; |
|
110 } else throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS); |
|
111 } |
|
112 addRelation(c, currentRelation, currentRule); // last relation |
|
113 |
|
114 return c; |
|
115 } |
|
116 |
|
117 virtual ~CLIParser() { |
|
118 } |
|
119 }; |
|
120 |
|
121 const relpipe::common::type::StringX CLIParser::OPTION_RELATION = L"--relation"; |
|
122 const relpipe::common::type::StringX CLIParser::OPTION_ATTRIBUTE = L"--attribute"; |
|
123 const relpipe::common::type::StringX CLIParser::OPTION_VALUE = L"--value"; |
|
124 const relpipe::common::type::StringX CLIParser::OPTION_REPLACEMENT = L"--replacement"; |
|
125 const relpipe::common::type::StringX CLIParser::OPTION_CASE_SENSITIVE = L"--case-sensitive"; |
|
126 const relpipe::common::type::StringX CLIParser::OPTION_INVERT_MATCH = L"--invert-match"; |
|
127 |
|
128 } |
|
129 } |
|
130 } |