Imported Upstream version 3.25.0
[platform/upstream/cmake.git] / Source / cmArgumentParser.cxx
1 /* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2    file Copyright.txt or https://cmake.org/licensing for details.  */
3 #include "cmArgumentParser.h"
4
5 #include <algorithm>
6
7 #include "cmArgumentParserTypes.h"
8 #include "cmMakefile.h"
9 #include "cmMessageType.h"
10 #include "cmStringAlgorithms.h"
11
12 namespace ArgumentParser {
13
14 auto KeywordActionMap::Emplace(cm::string_view name, KeywordAction action)
15   -> std::pair<iterator, bool>
16 {
17   auto const it =
18     std::lower_bound(this->begin(), this->end(), name,
19                      [](value_type const& elem, cm::string_view const& k) {
20                        return elem.first < k;
21                      });
22   return (it != this->end() && it->first == name)
23     ? std::make_pair(it, false)
24     : std::make_pair(this->emplace(it, name, std::move(action)), true);
25 }
26
27 auto KeywordActionMap::Find(cm::string_view name) const -> const_iterator
28 {
29   auto const it =
30     std::lower_bound(this->begin(), this->end(), name,
31                      [](value_type const& elem, cm::string_view const& k) {
32                        return elem.first < k;
33                      });
34   return (it != this->end() && it->first == name) ? it : this->end();
35 }
36
37 auto PositionActionMap::Emplace(std::size_t pos, PositionAction action)
38   -> std::pair<iterator, bool>
39 {
40   auto const it = std::lower_bound(
41     this->begin(), this->end(), pos,
42     [](value_type const& elem, std::size_t k) { return elem.first < k; });
43   return (it != this->end() && it->first == pos)
44     ? std::make_pair(it, false)
45     : std::make_pair(this->emplace(it, pos, std::move(action)), true);
46 }
47
48 auto PositionActionMap::Find(std::size_t pos) const -> const_iterator
49 {
50   auto const it = std::lower_bound(
51     this->begin(), this->end(), pos,
52     [](value_type const& elem, std::size_t k) { return elem.first < k; });
53   return (it != this->end() && it->first == pos) ? it : this->end();
54 }
55
56 void Instance::Bind(std::function<Continue(cm::string_view)> f,
57                     ExpectAtLeast expect)
58 {
59   this->KeywordValueFunc = std::move(f);
60   this->KeywordValuesExpected = expect.Count;
61 }
62
63 void Instance::Bind(bool& val)
64 {
65   val = true;
66   this->Bind(nullptr, ExpectAtLeast{ 0 });
67 }
68
69 void Instance::Bind(std::string& val)
70 {
71   this->Bind(
72     [&val](cm::string_view arg) -> Continue {
73       val = std::string(arg);
74       return Continue::No;
75     },
76     ExpectAtLeast{ 1 });
77 }
78
79 void Instance::Bind(NonEmpty<std::string>& val)
80 {
81   this->Bind(
82     [this, &val](cm::string_view arg) -> Continue {
83       if (arg.empty() && this->ParseResults) {
84         this->ParseResults->AddKeywordError(this->Keyword,
85                                             "  empty string not allowed\n");
86       }
87       val.assign(std::string(arg));
88       return Continue::No;
89     },
90     ExpectAtLeast{ 1 });
91 }
92
93 void Instance::Bind(Maybe<std::string>& val)
94 {
95   this->Bind(
96     [&val](cm::string_view arg) -> Continue {
97       static_cast<std::string&>(val) = std::string(arg);
98       return Continue::No;
99     },
100     ExpectAtLeast{ 0 });
101 }
102
103 void Instance::Bind(MaybeEmpty<std::vector<std::string>>& val)
104 {
105   this->Bind(
106     [&val](cm::string_view arg) -> Continue {
107       val.emplace_back(arg);
108       return Continue::Yes;
109     },
110     ExpectAtLeast{ 0 });
111 }
112
113 void Instance::Bind(NonEmpty<std::vector<std::string>>& val)
114 {
115   this->Bind(
116     [&val](cm::string_view arg) -> Continue {
117       val.emplace_back(arg);
118       return Continue::Yes;
119     },
120     ExpectAtLeast{ 1 });
121 }
122
123 void Instance::Bind(std::vector<std::vector<std::string>>& multiVal)
124 {
125   multiVal.emplace_back();
126   std::vector<std::string>& val = multiVal.back();
127   this->Bind(
128     [&val](cm::string_view arg) -> Continue {
129       val.emplace_back(arg);
130       return Continue::Yes;
131     },
132     ExpectAtLeast{ 0 });
133 }
134
135 void Instance::Consume(std::size_t pos, cm::string_view arg)
136 {
137   auto const it = this->Bindings.Keywords.Find(arg);
138   if (it != this->Bindings.Keywords.end()) {
139     this->FinishKeyword();
140     this->Keyword = it->first;
141     this->KeywordValuesSeen = 0;
142     this->DoneWithPositional = true;
143     if (this->Bindings.ParsedKeyword) {
144       this->Bindings.ParsedKeyword(*this, it->first);
145     }
146     it->second(*this);
147     return;
148   }
149
150   if (this->KeywordValueFunc) {
151     switch (this->KeywordValueFunc(arg)) {
152       case Continue::Yes:
153         break;
154       case Continue::No:
155         this->KeywordValueFunc = nullptr;
156         break;
157     }
158     ++this->KeywordValuesSeen;
159     return;
160   }
161
162   if (!this->DoneWithPositional) {
163     auto const pit = this->Bindings.Positions.Find(pos);
164     if (pit != this->Bindings.Positions.end()) {
165       pit->second(*this, pos, arg);
166       return;
167     }
168   }
169
170   if (this->UnparsedArguments != nullptr) {
171     this->UnparsedArguments->emplace_back(arg);
172   }
173 }
174
175 void Instance::FinishKeyword()
176 {
177   if (this->Keyword.empty()) {
178     return;
179   }
180   if (this->KeywordValuesSeen < this->KeywordValuesExpected) {
181     if (this->ParseResults != nullptr) {
182       this->ParseResults->AddKeywordError(this->Keyword,
183                                           "  missing required value\n");
184     }
185     if (this->Bindings.KeywordMissingValue) {
186       this->Bindings.KeywordMissingValue(*this, this->Keyword);
187     }
188   }
189 }
190
191 bool ParseResult::MaybeReportError(cmMakefile& mf) const
192 {
193   if (*this) {
194     return false;
195   }
196   std::string e;
197   for (auto const& ke : this->KeywordErrors) {
198     e = cmStrCat(e, "Error after keyword \"", ke.first, "\":\n", ke.second);
199   }
200   mf.IssueMessage(MessageType::FATAL_ERROR, e);
201   return true;
202 }
203
204 } // namespace ArgumentParser