|
|
@ -165,16 +165,49 @@ public: |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
class ParserState |
|
|
|
{ |
|
|
|
public: |
|
|
|
cm::string_view Keyword; |
|
|
|
std::size_t Pos = 0; |
|
|
|
std::size_t KeywordValuesSeen = 0; |
|
|
|
std::size_t KeywordValuesExpected = 0; |
|
|
|
std::function<Continue(cm::string_view)> KeywordValueFunc = nullptr; |
|
|
|
|
|
|
|
ActionMap const& Bindings; |
|
|
|
void* Result = nullptr; |
|
|
|
bool DoneWithPositional = false; |
|
|
|
|
|
|
|
ParserState(ActionMap const& bindings, void* result) |
|
|
|
: Bindings(bindings) |
|
|
|
, Result(result) |
|
|
|
{ |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
class Instance |
|
|
|
{ |
|
|
|
public: |
|
|
|
Instance(ActionMap const& bindings, ParseResult* parseResult, |
|
|
|
std::vector<std::string>* unparsedArguments, void* result = nullptr) |
|
|
|
: Bindings(bindings) |
|
|
|
, ParseResults(parseResult) |
|
|
|
: ParseResults(parseResult) |
|
|
|
, UnparsedArguments(unparsedArguments) |
|
|
|
, Result(result) |
|
|
|
{ |
|
|
|
PushState(bindings, result); |
|
|
|
} |
|
|
|
|
|
|
|
ParserState& GetState() { return this->Stack.back(); } |
|
|
|
|
|
|
|
void PushState(ActionMap const& bindings, void* result) |
|
|
|
{ |
|
|
|
this->Stack.emplace_back(bindings, result); |
|
|
|
} |
|
|
|
|
|
|
|
void PopState() |
|
|
|
{ |
|
|
|
if (!this->Stack.empty()) { |
|
|
|
this->Stack.pop_back(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
void Bind(std::function<Continue(cm::string_view)> f, ExpectAtLeast expect); |
|
|
@ -185,6 +218,7 @@ public: |
|
|
|
void Bind(MaybeEmpty<std::vector<std::string>>& val); |
|
|
|
void Bind(NonEmpty<std::vector<std::string>>& val); |
|
|
|
void Bind(std::vector<std::vector<std::string>>& val); |
|
|
|
|
|
|
|
template <typename U> |
|
|
|
void Bind(NonEmpty<std::vector<std::pair<std::string, U>>>& val, |
|
|
|
U const& context) |
|
|
@ -217,27 +251,43 @@ public: |
|
|
|
} |
|
|
|
|
|
|
|
template <typename Range> |
|
|
|
void Parse(Range const& args, std::size_t pos = 0) |
|
|
|
void Parse(Range& args, std::size_t const pos = 0) |
|
|
|
{ |
|
|
|
GetState().Pos = pos; |
|
|
|
for (cm::string_view arg : args) { |
|
|
|
this->Consume(pos++, arg); |
|
|
|
for (size_t j = 0; j < FindKeywordOwnerLevel(arg); ++j) { |
|
|
|
this->PopState(); |
|
|
|
} |
|
|
|
|
|
|
|
this->Consume(arg); |
|
|
|
GetState().Pos++; |
|
|
|
} |
|
|
|
|
|
|
|
this->FinishKeyword(); |
|
|
|
|
|
|
|
while (this->Stack.size() > 1) { |
|
|
|
this->FinishKeyword(); |
|
|
|
this->PopState(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
std::size_t FindKeywordOwnerLevel(cm::string_view arg) const |
|
|
|
{ |
|
|
|
for (std::size_t i = Stack.size(); i--;) { |
|
|
|
if (this->Stack[i].Bindings.Keywords.Find(arg) != |
|
|
|
this->Stack[i].Bindings.Keywords.end()) { |
|
|
|
return (this->Stack.size() - 1) - i; |
|
|
|
} |
|
|
|
} |
|
|
|
return 0; |
|
|
|
} |
|
|
|
|
|
|
|
private: |
|
|
|
ActionMap const& Bindings; |
|
|
|
std::vector<ParserState> Stack; |
|
|
|
ParseResult* ParseResults = nullptr; |
|
|
|
std::vector<std::string>* UnparsedArguments = nullptr; |
|
|
|
void* Result = nullptr; |
|
|
|
|
|
|
|
cm::string_view Keyword; |
|
|
|
std::size_t KeywordValuesSeen = 0; |
|
|
|
std::size_t KeywordValuesExpected = 0; |
|
|
|
std::function<Continue(cm::string_view)> KeywordValueFunc; |
|
|
|
bool DoneWithPositional = false; |
|
|
|
|
|
|
|
void Consume(std::size_t pos, cm::string_view arg); |
|
|
|
void Consume(cm::string_view arg); |
|
|
|
void FinishKeyword(); |
|
|
|
|
|
|
|
template <typename Result> |
|
|
@ -250,6 +300,8 @@ template <typename Result> |
|
|
|
class cmArgumentParser : private ArgumentParser::Base |
|
|
|
{ |
|
|
|
public: |
|
|
|
using Base::Bindings; |
|
|
|
|
|
|
|
// I *think* this function could be made `constexpr` when the code is |
|
|
|
// compiled as C++20. This would allow building a parser at compile time. |
|
|
|
template <typename T, typename cT = cm::member_pointer_class_t<T>, |
|
|
@ -259,7 +311,7 @@ public: |
|
|
|
cmArgumentParser& Bind(cm::static_string_view name, T member) |
|
|
|
{ |
|
|
|
this->Base::Bind(name, [member](Instance& instance) { |
|
|
|
instance.Bind(static_cast<Result*>(instance.Result)->*member); |
|
|
|
instance.Bind(static_cast<Result*>(instance.GetState().Result)->*member); |
|
|
|
}); |
|
|
|
return *this; |
|
|
|
} |
|
|
@ -269,7 +321,7 @@ public: |
|
|
|
T Result::*member, U Result::*context) |
|
|
|
{ |
|
|
|
this->Base::Bind(name, [member, context](Instance& instance) { |
|
|
|
auto* result = static_cast<Result*>(instance.Result); |
|
|
|
auto* result = static_cast<Result*>(instance.GetState().Result); |
|
|
|
instance.Bind(result->*member, result->*context); |
|
|
|
}); |
|
|
|
return *this; |
|
|
@ -280,7 +332,7 @@ public: |
|
|
|
ExpectAtLeast expect = { 1 }) |
|
|
|
{ |
|
|
|
this->Base::Bind(name, [member, expect](Instance& instance) { |
|
|
|
Result* result = static_cast<Result*>(instance.Result); |
|
|
|
Result* result = static_cast<Result*>(instance.GetState().Result); |
|
|
|
instance.Bind( |
|
|
|
[result, member](cm::string_view arg) -> Continue { |
|
|
|
return (result->*member)(arg); |
|
|
@ -296,8 +348,8 @@ public: |
|
|
|
ExpectAtLeast expect = { 1 }) |
|
|
|
{ |
|
|
|
this->Base::Bind(name, [member, expect](Instance& instance) { |
|
|
|
Result* result = static_cast<Result*>(instance.Result); |
|
|
|
cm::string_view keyword = instance.Keyword; |
|
|
|
Result* result = static_cast<Result*>(instance.GetState().Result); |
|
|
|
cm::string_view keyword = instance.GetState().Keyword; |
|
|
|
instance.Bind( |
|
|
|
[result, member, keyword](cm::string_view arg) -> Continue { |
|
|
|
return (result->*member)(keyword, arg); |
|
|
@ -312,7 +364,7 @@ public: |
|
|
|
ExpectAtLeast expect = { 1 }) |
|
|
|
{ |
|
|
|
this->Base::Bind(name, [f, expect](Instance& instance) { |
|
|
|
Result* result = static_cast<Result*>(instance.Result); |
|
|
|
Result* result = static_cast<Result*>(instance.GetState().Result); |
|
|
|
instance.Bind( |
|
|
|
[result, &f](cm::string_view arg) -> Continue { |
|
|
|
return f(*result, arg); |
|
|
@ -328,8 +380,8 @@ public: |
|
|
|
ExpectAtLeast expect = { 1 }) |
|
|
|
{ |
|
|
|
this->Base::Bind(name, [f, expect](Instance& instance) { |
|
|
|
Result* result = static_cast<Result*>(instance.Result); |
|
|
|
cm::string_view keyword = instance.Keyword; |
|
|
|
Result* result = static_cast<Result*>(instance.GetState().Result); |
|
|
|
cm::string_view keyword = instance.GetState().Keyword; |
|
|
|
instance.Bind( |
|
|
|
[result, keyword, &f](cm::string_view arg) -> Continue { |
|
|
|
return f(*result, keyword, arg); |
|
|
@ -345,7 +397,7 @@ public: |
|
|
|
this->Base::Bind( |
|
|
|
position, |
|
|
|
[member](Instance& instance, std::size_t, cm::string_view arg) { |
|
|
|
Result* result = static_cast<Result*>(instance.Result); |
|
|
|
Result* result = static_cast<Result*>(instance.GetState().Result); |
|
|
|
result->*member = arg; |
|
|
|
}); |
|
|
|
return *this; |
|
|
@ -356,7 +408,8 @@ public: |
|
|
|
{ |
|
|
|
this->Base::BindParsedKeyword( |
|
|
|
[member](Instance& instance, cm::string_view arg) { |
|
|
|
(static_cast<Result*>(instance.Result)->*member).emplace_back(arg); |
|
|
|
(static_cast<Result*>(instance.GetState().Result)->*member) |
|
|
|
.emplace_back(arg); |
|
|
|
}); |
|
|
|
return *this; |
|
|
|
} |
|
|
@ -368,11 +421,44 @@ public: |
|
|
|
cmArgumentParser& BindTrailingArgs(T member) |
|
|
|
{ |
|
|
|
this->Base::BindTrailingArgs([member](Instance& instance) { |
|
|
|
instance.Bind(static_cast<Result*>(instance.Result)->*member); |
|
|
|
instance.Bind(static_cast<Result*>(instance.GetState().Result)->*member); |
|
|
|
}); |
|
|
|
return *this; |
|
|
|
} |
|
|
|
|
|
|
|
template <typename T, typename U> |
|
|
|
cmArgumentParser& BindSubParser(cm::static_string_view name, |
|
|
|
cmArgumentParser<T>& parser, |
|
|
|
cm::optional<T> U::*member) |
|
|
|
{ |
|
|
|
|
|
|
|
this->Base::Bind(name, [name, parser, member](Instance& instance) { |
|
|
|
auto* parentResult = static_cast<Result*>(instance.GetState().Result); |
|
|
|
auto& childResult = parentResult->*member; |
|
|
|
childResult.emplace(T()); |
|
|
|
instance.Bind(nullptr, ExpectAtLeast{ 0 }); |
|
|
|
instance.PushState(parser.Bindings, &(*childResult)); |
|
|
|
instance.Consume(name); |
|
|
|
}); |
|
|
|
|
|
|
|
return *this; |
|
|
|
} |
|
|
|
|
|
|
|
template <typename T, typename U> |
|
|
|
cmArgumentParser& BindSubParser(cm::static_string_view name, |
|
|
|
cmArgumentParser<T>& parser, T U::*member) |
|
|
|
{ |
|
|
|
this->Base::Bind(name, [name, parser, member](Instance& instance) { |
|
|
|
auto* parentResult = static_cast<Result*>(instance.GetState().Result); |
|
|
|
auto* childResult = &(parentResult->*member); |
|
|
|
instance.Bind(nullptr, ExpectAtLeast{ 1 }); |
|
|
|
instance.PushState(parser.Bindings, childResult); |
|
|
|
instance.Consume(name); |
|
|
|
}); |
|
|
|
|
|
|
|
return *this; |
|
|
|
} |
|
|
|
|
|
|
|
template <typename Range> |
|
|
|
bool Parse(Result& result, Range const& args, |
|
|
|
std::vector<std::string>* unparsedArguments, |
|
|
@ -405,6 +491,8 @@ template <> |
|
|
|
class cmArgumentParser<void> : private ArgumentParser::Base |
|
|
|
{ |
|
|
|
public: |
|
|
|
using Base::Bindings; |
|
|
|
|
|
|
|
template <typename T> |
|
|
|
cmArgumentParser& Bind(cm::static_string_view name, T& ref) |
|
|
|
{ |
|
|
@ -429,7 +517,7 @@ public: |
|
|
|
ExpectAtLeast expect = { 1 }) |
|
|
|
{ |
|
|
|
this->Base::Bind(name, [f, expect](Instance& instance) { |
|
|
|
cm::string_view keyword = instance.Keyword; |
|
|
|
cm::string_view keyword = instance.GetState().Keyword; |
|
|
|
instance.Bind( |
|
|
|
[keyword, &f](cm::string_view arg) -> Continue { |
|
|
|
return f(keyword, arg); |
|
|
@ -463,6 +551,32 @@ public: |
|
|
|
return *this; |
|
|
|
} |
|
|
|
|
|
|
|
template <typename T, typename U> |
|
|
|
cmArgumentParser& BindSubParser(cm::static_string_view name, |
|
|
|
cmArgumentParser<T>& parser, |
|
|
|
cm::optional<U>& subResult) |
|
|
|
{ |
|
|
|
this->Base::Bind(name, [name, parser, &subResult](Instance& instance) { |
|
|
|
subResult.emplace(U()); |
|
|
|
instance.Bind(nullptr, ExpectAtLeast{ 0 }); |
|
|
|
instance.PushState(parser.Bindings, &(*subResult)); |
|
|
|
instance.Consume(name); |
|
|
|
}); |
|
|
|
return *this; |
|
|
|
} |
|
|
|
|
|
|
|
template <typename T, typename U> |
|
|
|
cmArgumentParser& BindSubParser(cm::static_string_view name, |
|
|
|
cmArgumentParser<T>& parser, U& subResult) |
|
|
|
{ |
|
|
|
this->Base::Bind(name, [name, parser, &subResult](Instance& instance) { |
|
|
|
instance.Bind(nullptr, ExpectAtLeast{ 1 }); |
|
|
|
instance.PushState(parser.Bindings, &subResult); |
|
|
|
instance.Consume(name); |
|
|
|
}); |
|
|
|
return *this; |
|
|
|
} |
|
|
|
|
|
|
|
template <typename Range> |
|
|
|
ParseResult Parse(Range const& args, |
|
|
|
std::vector<std::string>* unparsedArguments, |
|
|
|