2023-02-13 10:57:10 +08:00
|
|
|
#pragma once
|
|
|
|
|
2024-08-24 23:05:41 +08:00
|
|
|
#include <YYCCommonplace.hpp>
|
2023-02-13 10:57:10 +08:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2023-03-03 11:06:26 +08:00
|
|
|
#include <deque>
|
2024-08-24 21:27:23 +08:00
|
|
|
#include <map>
|
2024-08-24 23:05:41 +08:00
|
|
|
#include <set>
|
|
|
|
#include <functional>
|
2023-08-27 12:30:12 +08:00
|
|
|
#include <initializer_list>
|
2024-08-24 21:27:23 +08:00
|
|
|
#include <type_traits>
|
2024-08-24 23:05:41 +08:00
|
|
|
#include <stdexcept>
|
2024-08-24 21:27:23 +08:00
|
|
|
#include <memory>
|
2023-03-03 11:06:26 +08:00
|
|
|
|
|
|
|
namespace Unvirt::CmdHelper {
|
|
|
|
|
|
|
|
class CmdSplitter {
|
|
|
|
public:
|
2024-08-24 21:27:23 +08:00
|
|
|
using Result_t = std::deque<std::u8string>;
|
|
|
|
private:
|
2023-03-03 11:06:26 +08:00
|
|
|
enum class StateType : int {
|
|
|
|
SPACE,
|
|
|
|
SINGLE,
|
|
|
|
DOUBLE,
|
|
|
|
ESCAPE,
|
|
|
|
NORMAL
|
|
|
|
};
|
2024-08-24 21:27:23 +08:00
|
|
|
|
2023-08-27 12:30:12 +08:00
|
|
|
public:
|
|
|
|
CmdSplitter() :
|
2024-08-24 21:27:23 +08:00
|
|
|
m_CurrentChar(u8'\0'), m_Buffer(), m_Result(), m_ValidResult(false),
|
|
|
|
m_State(StateType::NORMAL), m_PrevState(StateType::NORMAL) {}
|
2023-08-27 12:30:12 +08:00
|
|
|
~CmdSplitter() {}
|
2024-08-26 21:28:13 +08:00
|
|
|
YYCC_DEF_CLS_COPY_MOVE(CmdSplitter);
|
2023-08-27 12:30:12 +08:00
|
|
|
|
2024-08-26 21:28:13 +08:00
|
|
|
bool Lex(const std::u8string& u8cmd);
|
2024-08-24 21:27:23 +08:00
|
|
|
const Result_t& GetResult() const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
void ProcSpace();
|
|
|
|
void ProcSingle();
|
|
|
|
void ProcDouble();
|
|
|
|
void ProcEscape();
|
|
|
|
void ProcNormal();
|
|
|
|
|
|
|
|
char8_t m_CurrentChar;
|
|
|
|
std::u8string m_Buffer;
|
|
|
|
Result_t m_Result;
|
|
|
|
bool m_ValidResult;
|
|
|
|
StateType m_State, m_PrevState;
|
|
|
|
};
|
|
|
|
|
2024-08-24 23:05:41 +08:00
|
|
|
namespace AMItems {
|
2024-08-24 21:27:23 +08:00
|
|
|
|
|
|
|
class AbstractItem {
|
|
|
|
public:
|
|
|
|
AbstractItem() {}
|
|
|
|
virtual ~AbstractItem() {}
|
|
|
|
YYCC_DEF_CLS_COPY_MOVE(AbstractItem);
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename _Ty, std::enable_if_t<std::is_arithmetic_v<_Ty>, int> = 0>
|
|
|
|
class ArithmeticItem : public AbstractItem {
|
|
|
|
public:
|
|
|
|
ArithmeticItem(_Ty value) : AbstractItem(), m_Data(value) {}
|
|
|
|
virtual ~ArithmeticItem() {}
|
|
|
|
YYCC_DEF_CLS_COPY_MOVE(ArithmeticItem);
|
|
|
|
public:
|
|
|
|
_Ty Get() const { return m_Data; }
|
|
|
|
protected:
|
|
|
|
_Ty m_Data;
|
|
|
|
};
|
2023-08-27 12:30:12 +08:00
|
|
|
|
2024-08-24 21:27:23 +08:00
|
|
|
template<typename _Ty, std::enable_if_t<std::is_arithmetic_v<_Ty>, int> = 0>
|
|
|
|
class ArithmeticArrayItem : public AbstractItem {
|
|
|
|
public:
|
|
|
|
ArithmeticArrayItem(const std::vector<_Ty>& values) : AbstractItem(), m_Data(values) {}
|
|
|
|
virtual ~ArithmeticArrayItem() {}
|
|
|
|
YYCC_DEF_CLS_COPY_MOVE(ArithmeticArrayItem);
|
|
|
|
public:
|
|
|
|
const std::vector<_Ty>& Get() const { return m_Data; }
|
|
|
|
protected:
|
|
|
|
std::vector<_Ty> m_Data;
|
|
|
|
};
|
|
|
|
|
|
|
|
class StringItem : public AbstractItem {
|
|
|
|
public:
|
|
|
|
StringItem(const std::u8string_view& value) : AbstractItem(), m_Data(value) {}
|
|
|
|
virtual ~StringItem() {}
|
|
|
|
YYCC_DEF_CLS_COPY_MOVE(StringItem);
|
|
|
|
public:
|
|
|
|
const std::u8string& Get() const { return m_Data; }
|
|
|
|
protected:
|
|
|
|
std::u8string m_Data;
|
|
|
|
};
|
2023-03-03 11:06:26 +08:00
|
|
|
|
2024-08-24 21:27:23 +08:00
|
|
|
class StringArrayItem : public AbstractItem {
|
|
|
|
public:
|
|
|
|
StringArrayItem(const std::vector<std::u8string>& value) : AbstractItem(), m_Data(value) {}
|
|
|
|
virtual ~StringArrayItem() {}
|
|
|
|
YYCC_DEF_CLS_COPY_MOVE(StringArrayItem);
|
|
|
|
public:
|
|
|
|
const std::vector<std::u8string>& Get() const { return m_Data; }
|
|
|
|
protected:
|
|
|
|
std::vector<std::u8string> m_Data;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
class ArgumentsMap {
|
|
|
|
public:
|
2024-08-24 23:05:41 +08:00
|
|
|
ArgumentsMap();
|
|
|
|
~ArgumentsMap();
|
2024-08-24 21:27:23 +08:00
|
|
|
YYCC_DEF_CLS_COPY_MOVE(ArgumentsMap);
|
|
|
|
|
|
|
|
protected:
|
2024-08-24 23:05:41 +08:00
|
|
|
std::map<std::u8string, std::shared_ptr<AMItems::AbstractItem>> m_Data;
|
2024-08-24 21:27:23 +08:00
|
|
|
|
|
|
|
public:
|
2024-08-24 23:05:41 +08:00
|
|
|
template<class _Ty, class... _Types, std::enable_if_t<std::is_base_of_v<AMItems::AbstractItem, _Ty>, int> = 0>
|
2024-08-24 21:27:23 +08:00
|
|
|
void Add(const std::u8string_view& key, _Types&&... args) {
|
|
|
|
// check argument
|
|
|
|
if (key.empty())
|
|
|
|
throw std::invalid_argument("argument key should not be empty");
|
|
|
|
// insert into data
|
2024-08-24 23:05:41 +08:00
|
|
|
auto result = m_Data.try_emplace(std::u8string(key), std::make_shared<_Ty>(std::forward<_Types>(args)...));
|
2024-08-24 21:27:23 +08:00
|
|
|
if (!result.second)
|
|
|
|
throw std::runtime_error("try to add an existing key.");
|
|
|
|
}
|
2024-08-24 23:05:41 +08:00
|
|
|
template<class _Ty, std::enable_if_t<std::is_base_of_v<AMItems::AbstractItem, _Ty>, int> = 0>
|
2024-08-25 22:27:17 +08:00
|
|
|
const _Ty& Get(const std::u8string_view& key) const {
|
2024-08-24 21:27:23 +08:00
|
|
|
// check argument
|
|
|
|
if (key.empty())
|
|
|
|
throw std::invalid_argument("argument key should not be empty");
|
|
|
|
// find key first
|
|
|
|
auto finder = m_Data.find(std::u8string(key));
|
|
|
|
if (finder == m_Data.end())
|
|
|
|
throw std::runtime_error("try to get a non-existent key.");
|
|
|
|
// get stored value data
|
2024-08-26 12:24:09 +08:00
|
|
|
const _Ty* value = static_cast<const _Ty*>(finder->second.get());
|
|
|
|
return *value;
|
2024-08-24 21:27:23 +08:00
|
|
|
}
|
|
|
|
void Remove(const std::u8string_view& key) {
|
|
|
|
// check argument
|
|
|
|
if (key.empty())
|
|
|
|
throw std::invalid_argument("argument key should not be empty");
|
2024-08-24 23:05:41 +08:00
|
|
|
// remove it from map and check whether remove item
|
2024-08-24 21:27:23 +08:00
|
|
|
if (m_Data.erase(std::u8string(key)) == 0u)
|
|
|
|
throw std::runtime_error("try to delete a non-existent key.");
|
|
|
|
}
|
2023-08-27 12:30:12 +08:00
|
|
|
};
|
|
|
|
|
2024-08-26 12:24:09 +08:00
|
|
|
// Forward declaration for Nodes::AbstractNode
|
|
|
|
namespace Nodes { class AbstractNode; }
|
|
|
|
|
2023-08-27 12:30:12 +08:00
|
|
|
class HelpDocument {
|
2024-08-26 12:24:09 +08:00
|
|
|
friend class Nodes::AbstractNode;
|
2023-08-27 12:30:12 +08:00
|
|
|
public:
|
2023-08-27 14:21:44 +08:00
|
|
|
HelpDocument();
|
|
|
|
~HelpDocument();
|
2024-08-24 21:27:23 +08:00
|
|
|
YYCC_DEF_CLS_COPY_MOVE(HelpDocument);
|
2023-08-27 12:30:12 +08:00
|
|
|
|
2024-08-24 21:27:23 +08:00
|
|
|
public:
|
2024-08-26 12:24:09 +08:00
|
|
|
void Print();
|
|
|
|
|
|
|
|
// AbstractNode used
|
|
|
|
protected:
|
2024-08-24 21:27:23 +08:00
|
|
|
void Push(const std::u8string& arg_name, const std::u8string& arg_desc);
|
2023-08-27 14:21:44 +08:00
|
|
|
void Pop();
|
2024-08-24 21:27:23 +08:00
|
|
|
void Terminate(std::u8string& command_desc);
|
2023-08-27 12:30:12 +08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
struct StackItem {
|
2024-08-24 21:27:23 +08:00
|
|
|
StackItem();
|
|
|
|
StackItem(const std::u8string& name, const std::u8string& desc);
|
2024-08-17 20:43:27 +08:00
|
|
|
YYCC_DEF_CLS_COPY_MOVE(StackItem);
|
2024-08-24 21:27:23 +08:00
|
|
|
|
|
|
|
std::u8string m_Name;
|
|
|
|
std::u8string m_Desc;
|
2023-08-27 12:30:12 +08:00
|
|
|
};
|
|
|
|
std::deque<StackItem> m_Stack;
|
2024-08-24 21:27:23 +08:00
|
|
|
|
2023-08-27 12:30:12 +08:00
|
|
|
struct ResultItem {
|
2024-08-24 21:27:23 +08:00
|
|
|
ResultItem();
|
|
|
|
ResultItem(const std::u8string& cmd_desc, const std::deque<StackItem>& arg_desc);
|
2024-08-17 20:43:27 +08:00
|
|
|
YYCC_DEF_CLS_COPY_MOVE(ResultItem);
|
2024-08-24 21:27:23 +08:00
|
|
|
|
|
|
|
std::u8string m_CmdDesc;
|
2023-08-27 12:30:12 +08:00
|
|
|
std::vector<StackItem> m_ArgDesc;
|
|
|
|
};
|
|
|
|
std::vector<ResultItem> m_Results;
|
|
|
|
};
|
|
|
|
|
2024-08-25 22:27:17 +08:00
|
|
|
class ConflictSet {
|
|
|
|
public:
|
|
|
|
ConflictSet();
|
|
|
|
~ConflictSet();
|
|
|
|
YYCC_DEF_CLS_COPY_MOVE(ConflictSet);
|
|
|
|
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* @brief Add literal item into conflict set.
|
|
|
|
* @param[in] value Literal item.
|
|
|
|
* @remarks
|
|
|
|
* \li Literal item is the string input in command line.
|
|
|
|
* \li The word in Literal, and the vocabulary in Choice should be put by this function.
|
|
|
|
* \li Added item will add \c literal: prefix to make it in literal scope,
|
|
|
|
* so that it will not be compared with argument name items.
|
|
|
|
* Because we allow 2 literal item and argument name item have same name.
|
|
|
|
*/
|
|
|
|
void AddLiteral(const std::u8string_view& value);
|
|
|
|
/**
|
|
|
|
* @brief Add argument name item into conflict
|
|
|
|
* @param[in] value Argument name item.
|
|
|
|
* @remarks
|
|
|
|
* \li Argument name item is the key name put in ArgumentsMap.
|
|
|
|
* \li The argument name in Choice and Argument should be put by this function.
|
|
|
|
* \li Added item will add \c argument: prefix to make it in argument name scope,
|
|
|
|
* so that it will not be compared with literal items.
|
|
|
|
* Because we allow 2 literal item and argument name item have same name.
|
|
|
|
*/
|
|
|
|
void AddArgument(const std::u8string_view& value);
|
|
|
|
/**
|
|
|
|
* @brief Check whether this set is conflict with another.
|
|
|
|
* @param[in] rhs The set to be compared.
|
|
|
|
* @return True if they have conflict.
|
|
|
|
* @remarks
|
|
|
|
* This function simply compute the intersection of two set.
|
|
|
|
* If the result is not empty, it means that there is a conflict.
|
|
|
|
*/
|
|
|
|
bool IsConflictWith(const ConflictSet& rhs) const;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
std::set<std::u8string> m_ConflictSet;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Forward declaration of CommandParser for Nodes::RootNode
|
|
|
|
class CommandParser;
|
|
|
|
|
2024-08-24 23:05:41 +08:00
|
|
|
namespace Nodes {
|
2023-08-27 12:30:12 +08:00
|
|
|
|
2024-08-24 23:05:41 +08:00
|
|
|
class AbstractNode {
|
|
|
|
public:
|
2024-08-26 21:28:13 +08:00
|
|
|
using FctExecution_t = std::function<void(const ArgumentsMap&)>;
|
2023-08-27 12:30:12 +08:00
|
|
|
|
2024-08-24 23:05:41 +08:00
|
|
|
public:
|
|
|
|
AbstractNode();
|
|
|
|
virtual ~AbstractNode();
|
|
|
|
YYCC_DEF_CLS_COPY_MOVE(AbstractNode);
|
2023-08-27 12:30:12 +08:00
|
|
|
|
2024-08-24 23:05:41 +08:00
|
|
|
protected:
|
|
|
|
std::vector<std::shared_ptr<AbstractNode>> m_Nodes;
|
|
|
|
FctExecution_t m_Execution;
|
|
|
|
std::u8string m_ExecutionDesc;
|
|
|
|
std::u8string m_Comment;
|
2023-08-27 12:30:12 +08:00
|
|
|
|
2024-08-24 23:05:41 +08:00
|
|
|
protected:
|
|
|
|
/**
|
|
|
|
* @brief The core function to generate help document by following hierarchy.
|
|
|
|
* @param[in] doc The generating help document.
|
|
|
|
*/
|
|
|
|
void Help(HelpDocument& doc);
|
|
|
|
/**
|
|
|
|
* @brief The core function to consume splitted commands by following hierarchy.
|
|
|
|
* @param[in] al The splitted commands deque.
|
|
|
|
* @param[in] am Argument map for operating.
|
|
|
|
* @return True if we reach a legal terminal, otherwise false.
|
|
|
|
* Once this function return true, there is no need to process other nodes.
|
|
|
|
* Because the final command processor has been executed when this function return true.
|
|
|
|
*/
|
|
|
|
bool Consume(CmdSplitter::Result_t& al, ArgumentsMap& am);
|
2023-08-27 12:30:12 +08:00
|
|
|
|
2024-08-24 23:05:41 +08:00
|
|
|
protected:
|
2024-08-25 22:27:17 +08:00
|
|
|
/**
|
|
|
|
* @brief Check whether current node is root node.
|
|
|
|
* @return True if it is, otherwise false.
|
|
|
|
* @remarks
|
|
|
|
* This function usually return false.
|
|
|
|
* It only return true when using special node called root node with CommandParser.
|
|
|
|
*/
|
|
|
|
virtual bool IsRootNode() = 0;
|
2024-08-24 23:05:41 +08:00
|
|
|
/**
|
|
|
|
* @brief Check whether current node is argument.
|
2024-08-25 22:27:17 +08:00
|
|
|
* @return True if it is, otherwise false.
|
2024-08-24 23:05:41 +08:00
|
|
|
* @remakrs
|
|
|
|
* \li Sub-class must implement this function.
|
|
|
|
* \li This function is used internally because when consuming nodes,
|
|
|
|
* we need consume literal and choice first, then consume argument.
|
|
|
|
*/
|
|
|
|
virtual bool IsArgument() = 0;
|
|
|
|
/**
|
|
|
|
* @brief Get a set of identifier used for checking node conflict.
|
|
|
|
* @return The set of identifier.
|
|
|
|
* @remarks
|
|
|
|
* Sub-class must implement this function.
|
|
|
|
* \par
|
|
|
|
* This function return the reference to the set.
|
|
|
|
* It means that sub-class need allocate some memory by themselves
|
|
|
|
* to store the value returned by this function.
|
|
|
|
* \par
|
|
|
|
* When adding new nodes, we use this function to check whether there is conflict.
|
|
|
|
* If the intersection between 2 sets coming from different nodes is not empty,
|
|
|
|
* it means that they have conflict, the process of adding will be aborted.
|
|
|
|
* \par
|
|
|
|
* In details:
|
|
|
|
* \li Literal: Put its literal in set directly.
|
|
|
|
* \li Choice: Put its vocabulary and associated argument name in set.
|
|
|
|
* \li Argument: Put its argument name in set.
|
|
|
|
*/
|
2024-08-25 22:27:17 +08:00
|
|
|
virtual const ConflictSet& GetConflictSet() = 0;
|
2024-08-24 23:05:41 +08:00
|
|
|
/**
|
|
|
|
* @brief Get the string presented in syntax part in help messages.
|
|
|
|
* @return The string presented in syntax part in help messages.
|
|
|
|
* @remarks Sub-class must implement this function.
|
|
|
|
* \li Literal: Return its literal directly.
|
|
|
|
* \li Choice: Join vocabulary with \c \| then brack it with square bracker.
|
|
|
|
* \li Argument: Bracket its argument name with sharp bracket.
|
|
|
|
*/
|
|
|
|
virtual std::u8string GetHelpSymbol() = 0;
|
|
|
|
/**
|
|
|
|
* @brief Try consume given command for this node.
|
|
|
|
* @param[in] cur_cmd The command hope to be accepted.
|
|
|
|
* @param[in] am Argument map for operating.
|
|
|
|
* @return True if this node accept this command.
|
|
|
|
* @remarks
|
|
|
|
* Sub-class must implement this function.
|
|
|
|
* \par
|
|
|
|
* For choice and argument, if given command is accepted,
|
|
|
|
* implementation should insert data into argument map.
|
|
|
|
* So that user can visit it.
|
|
|
|
*/
|
|
|
|
virtual bool BeginConsume(const std::u8string& cur_cmd, ArgumentsMap& am) = 0;
|
|
|
|
/**
|
|
|
|
* @brief End the consume of this node.
|
|
|
|
* @param[in] am Argument map for operating.
|
|
|
|
* @remarks
|
|
|
|
* Sub-class must implement this function.
|
|
|
|
* \par
|
|
|
|
* If BeginConsume() return false, this function will not be called.
|
|
|
|
* \par
|
|
|
|
* For choice and argument, if you have accepted one command,
|
|
|
|
* implementation should remove data from argument map.
|
|
|
|
* So that following nodes (not belongs to this tree) may add the argument with same name.
|
|
|
|
*/
|
|
|
|
virtual void EndConsume(ArgumentsMap& am) = 0;
|
2023-08-27 12:30:12 +08:00
|
|
|
|
2024-08-24 23:05:41 +08:00
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* @brief Add a new node as child nodes for this node.
|
|
|
|
* @tparam _Ty The child class type of AbstractNode.
|
|
|
|
* @param[in] node The node instance to be added.
|
|
|
|
* @return Return self for chain calling.
|
|
|
|
*/
|
2024-08-25 22:27:17 +08:00
|
|
|
template<class _Ty, std::enable_if_t<std::is_base_of_v<AbstractNode, _Ty> && !std::is_same_v<AbstractNode, _Ty>, int> = 0>
|
2024-08-26 12:24:09 +08:00
|
|
|
AbstractNode& Then(AbstractNode& node) {
|
2024-08-24 23:05:41 +08:00
|
|
|
// create node first
|
2024-08-26 12:24:09 +08:00
|
|
|
auto new_node = std::make_shared<_Ty>(static_cast<_Ty&>(node));
|
|
|
|
// get its abstract pointer for checking
|
|
|
|
AbstractNode* new_node_ptr = new_node.get();
|
2024-08-25 22:27:17 +08:00
|
|
|
// check root node.
|
2024-08-26 12:24:09 +08:00
|
|
|
if (new_node_ptr->IsRootNode())
|
2024-08-25 22:27:17 +08:00
|
|
|
throw std::invalid_argument("root node should not be inserted as child node.");
|
2024-08-24 23:05:41 +08:00
|
|
|
// check conflict
|
2024-08-26 12:24:09 +08:00
|
|
|
const auto& new_node_set = new_node_ptr->GetConflictSet();
|
2024-08-25 22:27:17 +08:00
|
|
|
for (auto& node : m_Nodes) {
|
2024-08-24 23:05:41 +08:00
|
|
|
const auto& node_set = node->GetConflictSet();
|
2024-08-25 22:27:17 +08:00
|
|
|
if (new_node_set.IsConflictWith(node_set))
|
2024-08-24 23:05:41 +08:00
|
|
|
throw std::invalid_argument("try to add a conflict node. please check your code.");
|
|
|
|
}
|
|
|
|
// add into list
|
|
|
|
m_Nodes.emplace_back(std::move(new_node));
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @brief Setup execution infomation for this node.
|
|
|
|
* @param[in] fct Associated execution function. nullptr is not allowed.
|
|
|
|
* @param[in] exec_desc Associated execution message presented in help message.
|
|
|
|
* @return Return self for chain calling.
|
|
|
|
* @remarks This function only can be called once for one node.
|
|
|
|
*/
|
|
|
|
AbstractNode& Executes(FctExecution_t fct, const std::u8string_view& exec_desc = u8"");
|
|
|
|
/**
|
|
|
|
* @brief Setup command for this node.
|
|
|
|
* @param[in] comment The command of current node.
|
|
|
|
* @return Return self for chain calling.
|
|
|
|
*/
|
|
|
|
AbstractNode& Comment(const std::u8string_view& comment = u8"");
|
2023-08-27 12:30:12 +08:00
|
|
|
|
2024-08-24 23:05:41 +08:00
|
|
|
};
|
2023-08-27 12:30:12 +08:00
|
|
|
|
2024-08-25 22:27:17 +08:00
|
|
|
class RootNode : public AbstractNode {
|
2024-08-27 20:48:27 +08:00
|
|
|
friend class ::Unvirt::CmdHelper::CommandParser;
|
2024-08-25 22:27:17 +08:00
|
|
|
public:
|
|
|
|
RootNode();
|
|
|
|
virtual ~RootNode();
|
|
|
|
YYCC_DEF_CLS_COPY_MOVE(RootNode);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual bool IsRootNode() override;
|
|
|
|
virtual bool IsArgument() override;
|
|
|
|
virtual const ConflictSet& GetConflictSet() override;
|
|
|
|
virtual std::u8string GetHelpSymbol() override;
|
|
|
|
virtual bool BeginConsume(const std::u8string& cur_cmd, ArgumentsMap& am) override;
|
|
|
|
virtual void EndConsume(ArgumentsMap& am) override;
|
|
|
|
};
|
|
|
|
|
2024-08-24 23:05:41 +08:00
|
|
|
class Literal : public AbstractNode {
|
|
|
|
public:
|
|
|
|
Literal(const std::u8string_view& words);
|
|
|
|
virtual ~Literal();
|
|
|
|
YYCC_DEF_CLS_COPY_MOVE(Literal);
|
2023-08-27 12:30:12 +08:00
|
|
|
|
2024-08-24 23:05:41 +08:00
|
|
|
protected:
|
2024-08-25 22:27:17 +08:00
|
|
|
virtual bool IsRootNode() override;
|
2024-08-24 23:05:41 +08:00
|
|
|
virtual bool IsArgument() override;
|
2024-08-25 22:27:17 +08:00
|
|
|
virtual const ConflictSet& GetConflictSet() override;
|
2024-08-24 23:05:41 +08:00
|
|
|
virtual std::u8string GetHelpSymbol() override;
|
|
|
|
virtual bool BeginConsume(const std::u8string& cur_cmd, ArgumentsMap& am) override;
|
|
|
|
virtual void EndConsume(ArgumentsMap& am) override;
|
|
|
|
|
|
|
|
std::u8string m_Literal;
|
2024-08-25 22:27:17 +08:00
|
|
|
ConflictSet m_ConflictSet;
|
2024-08-24 23:05:41 +08:00
|
|
|
};
|
2023-08-27 12:30:12 +08:00
|
|
|
|
2024-08-24 23:05:41 +08:00
|
|
|
class Choice : public AbstractNode {
|
|
|
|
public:
|
2024-08-25 22:27:17 +08:00
|
|
|
using ArgValue_t = AMItems::ArithmeticItem<size_t>;
|
2024-08-24 23:05:41 +08:00
|
|
|
public:
|
|
|
|
Choice(const std::u8string_view& argname, const std::initializer_list<std::u8string>& vocabulary);
|
|
|
|
virtual ~Choice();
|
|
|
|
YYCC_DEF_CLS_COPY_MOVE(Choice);
|
2024-08-25 22:27:17 +08:00
|
|
|
|
2024-08-24 23:05:41 +08:00
|
|
|
protected:
|
2024-08-25 22:27:17 +08:00
|
|
|
virtual bool IsRootNode() override;
|
2024-08-24 23:05:41 +08:00
|
|
|
virtual bool IsArgument() override;
|
2024-08-25 22:27:17 +08:00
|
|
|
virtual const ConflictSet& GetConflictSet() override;
|
2024-08-24 23:05:41 +08:00
|
|
|
virtual std::u8string GetHelpSymbol() override;
|
|
|
|
virtual bool BeginConsume(const std::u8string& cur_cmd, ArgumentsMap& am) override;
|
|
|
|
virtual void EndConsume(ArgumentsMap& am) override;
|
|
|
|
|
|
|
|
std::u8string m_ChoiceName;
|
|
|
|
std::vector<std::u8string> m_Vocabulary;
|
2024-08-25 22:27:17 +08:00
|
|
|
ConflictSet m_ConflictSet;
|
2024-08-24 23:05:41 +08:00
|
|
|
};
|
2023-08-27 12:30:12 +08:00
|
|
|
|
2024-08-24 23:05:41 +08:00
|
|
|
class AbstractArgument : public AbstractNode {
|
|
|
|
public:
|
|
|
|
AbstractArgument(const std::u8string_view& argname);
|
|
|
|
virtual ~AbstractArgument();
|
|
|
|
YYCC_DEF_CLS_COPY_MOVE(AbstractArgument);
|
2024-08-25 22:27:17 +08:00
|
|
|
|
2024-08-24 23:05:41 +08:00
|
|
|
// AbstractArgument do not implement BeginConsume().
|
|
|
|
// Because it involve the detail of data parsing.
|
|
|
|
// However, other parts are shared by all argument type.
|
2023-08-27 12:30:12 +08:00
|
|
|
|
2024-08-24 23:05:41 +08:00
|
|
|
protected:
|
2024-08-25 22:27:17 +08:00
|
|
|
virtual bool IsRootNode() override;
|
2024-08-24 23:05:41 +08:00
|
|
|
virtual bool IsArgument() override;
|
2024-08-25 22:27:17 +08:00
|
|
|
virtual const ConflictSet& GetConflictSet() override;
|
2024-08-24 23:05:41 +08:00
|
|
|
virtual std::u8string GetHelpSymbol() override;
|
2024-08-25 22:27:17 +08:00
|
|
|
//virtual bool BeginConsume(const std::u8string& cur_cmd, ArgumentsMap& am) override;
|
2024-08-24 23:05:41 +08:00
|
|
|
virtual void EndConsume(ArgumentsMap& am) override;
|
|
|
|
|
2024-08-25 22:27:17 +08:00
|
|
|
std::u8string m_ArgumentName;
|
|
|
|
ConflictSet m_ConflictSet;
|
2024-08-24 23:05:41 +08:00
|
|
|
};
|
2023-08-27 12:30:12 +08:00
|
|
|
|
2024-08-25 22:27:17 +08:00
|
|
|
template<typename _Ty, std::enable_if_t<std::is_arithmetic_v<_Ty>, int> = 0>
|
|
|
|
class ArithmeticArgument : public AbstractArgument {
|
2024-08-24 23:05:41 +08:00
|
|
|
public:
|
2024-08-25 22:27:17 +08:00
|
|
|
using ArgValue_t = AMItems::ArithmeticItem<_Ty>;
|
|
|
|
using Constraint_t = YYCC::Constraints::Constraint<_Ty>;
|
|
|
|
public:
|
|
|
|
ArithmeticArgument(const std::u8string_view& argname, Constraint_t constraint = Constraint_t {}) :
|
|
|
|
AbstractArgument(argname), m_Constraint(constraint) {}
|
|
|
|
virtual ~ArithmeticArgument() {}
|
|
|
|
YYCC_DEF_CLS_COPY_MOVE(ArithmeticArgument);
|
2023-08-27 12:30:12 +08:00
|
|
|
|
2024-08-24 23:05:41 +08:00
|
|
|
protected:
|
2024-08-25 22:27:17 +08:00
|
|
|
virtual bool BeginConsume(const std::u8string& cur_cmd, ArgumentsMap& am) override {
|
|
|
|
// try parse
|
|
|
|
_Ty result;
|
|
|
|
if (!YYCC::ParserHelper::TryParse<_Ty>(cur_cmd, result))
|
|
|
|
return false;
|
|
|
|
// check constraint
|
|
|
|
if (m_Constraint.IsValid() && !m_Constraint.m_CheckFct(result))
|
|
|
|
return false;
|
|
|
|
// okey
|
|
|
|
am.Add<ArgValue_t>(m_ArgumentName, result);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
protected:
|
|
|
|
Constraint_t m_Constraint;
|
2024-08-24 23:05:41 +08:00
|
|
|
};
|
2023-08-27 14:21:44 +08:00
|
|
|
|
2024-08-24 23:05:41 +08:00
|
|
|
class StringArgument : public AbstractArgument {
|
|
|
|
public:
|
2024-08-25 22:27:17 +08:00
|
|
|
using ArgValue_t = AMItems::StringItem;
|
|
|
|
using Constraint_t = YYCC::Constraints::Constraint<std::u8string>;
|
|
|
|
public:
|
2024-08-27 11:25:53 +08:00
|
|
|
StringArgument(const std::u8string_view& argname, Constraint_t constraint = Constraint_t {});
|
|
|
|
virtual ~StringArgument();
|
2024-08-25 22:27:17 +08:00
|
|
|
YYCC_DEF_CLS_COPY_MOVE(StringArgument);
|
2023-08-27 12:30:12 +08:00
|
|
|
|
2024-08-24 23:05:41 +08:00
|
|
|
protected:
|
2024-08-25 22:27:17 +08:00
|
|
|
virtual bool BeginConsume(const std::u8string& cur_cmd, ArgumentsMap& am) override;
|
|
|
|
Constraint_t m_Constraint;
|
2024-08-24 23:05:41 +08:00
|
|
|
};
|
2023-08-27 12:30:12 +08:00
|
|
|
|
2024-08-24 23:05:41 +08:00
|
|
|
}
|
2023-02-13 10:57:10 +08:00
|
|
|
|
2024-08-24 23:05:41 +08:00
|
|
|
class CommandParser {
|
2023-03-03 11:06:26 +08:00
|
|
|
public:
|
2024-08-24 23:05:41 +08:00
|
|
|
CommandParser();
|
|
|
|
~CommandParser();
|
2023-08-27 12:30:12 +08:00
|
|
|
|
2023-03-03 11:06:26 +08:00
|
|
|
public:
|
2024-08-25 22:27:17 +08:00
|
|
|
bool Parse(const CmdSplitter::Result_t& cmds);
|
2024-08-24 23:05:41 +08:00
|
|
|
HelpDocument Help();
|
2024-08-25 22:27:17 +08:00
|
|
|
Nodes::RootNode& GetRoot();
|
2023-08-27 12:30:12 +08:00
|
|
|
|
2024-08-24 23:05:41 +08:00
|
|
|
private:
|
2024-08-25 22:27:17 +08:00
|
|
|
Nodes::RootNode m_RootNode;
|
2023-03-03 11:06:26 +08:00
|
|
|
};
|
2023-02-13 22:13:30 +08:00
|
|
|
|
2023-08-27 12:30:12 +08:00
|
|
|
}
|