#ifndef UI_ACCESSIBILITY_PLATFORM_INSPECT_AX_PROPERTY_NODE_H_
#define UI_ACCESSIBILITY_PLATFORM_INSPECT_AX_PROPERTY_NODE_H_
#include <memory>
#include <optional>
#include <string>
#include <utility>
#include <vector>
#include "base/component_export.h"
namespace ui {
struct AXPropertyFilter;
class COMPONENT_EXPORT(AX_PLATFORM) AXPropertyNode final {
public:
static AXPropertyNode From(const std::string& property,
const std::vector<std::string>& line_indexes = {});
static AXPropertyNode From(const AXPropertyFilter& filter);
AXPropertyNode();
AXPropertyNode(AXPropertyNode&&);
~AXPropertyNode();
AXPropertyNode& operator=(AXPropertyNode&& other);
explicit operator bool() const;
std::string key;
std::string name_or_value;
std::vector<AXPropertyNode> arguments;
std::unique_ptr<AXPropertyNode> next;
std::unique_ptr<AXPropertyNode> rvalue;
std::string original_property;
std::vector<std::string> line_indexes;
template <class... Args>
AXPropertyNode* ConnectTo(bool chained, Args&&... args) {
return chained ? ChainToLastArgument(std::forward<Args>(args)...)
: AppendToArguments(std::forward<Args>(args)...);
}
template <class... Args>
AXPropertyNode* AppendToArguments(Args&&... args) {
arguments.emplace_back(std::forward<Args>(args)...);
return &arguments.back();
}
template <class... Args>
AXPropertyNode* ChainToLastArgument(Args&&... args) {
auto* last = &arguments.back();
while (last->next) {
last = last->next.get();
}
last->next = std::make_unique<AXPropertyNode>(
AXPropertyNode(std::forward<Args>(args)...));
return last->next.get();
}
bool IsMatching(const std::string& pattern) const;
bool IsTarget() const { return !!next; }
bool IsArray() const;
bool IsDict() const;
std::optional<int> AsInt() const;
std::string AsString() const;
const AXPropertyNode* FindKey(const char* refkey) const;
std::optional<std::string> FindStringKey(const char* refkey) const;
std::optional<int> FindIntKey(const char* key) const;
std::string ToString() const;
std::string ToFlatString() const;
std::string ToTreeString(const std::string& indent = "") const;
using iterator = std::string::const_iterator;
explicit AXPropertyNode(iterator key_begin,
iterator key_end,
const std::string&);
AXPropertyNode(iterator begin, iterator end);
AXPropertyNode(iterator key_begin,
iterator key_end,
iterator value_begin,
iterator value_end);
private:
enum ParseState {
kArgument,
kChain,
};
static iterator Parse(AXPropertyNode* node, iterator begin, iterator end);
};
}
#endif