Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace {
- struct NodeContainerPrinter {
- std::ostream& out;
- void operator()(std::nullptr_t /* value */) const {
- out << "null"s;
- }
- void operator()(bool value) const {
- out << (value ? "true"s : "false"s);
- }
- void operator()(int value) const {...}
- void operator()(double value) const {...}
- void operator()(const std::string& value) const {...}
- void operator()(const Dict& map) const {...}
- void operator()(const Array& array) const {...}
- };
- /* Load methods */
- Node LoadNode(std::istream& input);
- std::string LoadLetters(std::istream& input) {
- std::string result;
- while (std::isalpha(input.peek()))
- result.push_back(static_cast<char>(input.get()));
- return result;
- }
- Node LoadBool(std::istream& input) {
- std::string value = LoadLetters(input);
- if (value == "true"s)
- return Node{true};
- if (value == "false"s)
- return Node{false};
- throw ParsingError(R"(Incorrect format for Boolean Node parsing. "true" or "false" expected)"s);
- }
- ...
- Node LoadNode(std::istream& input) {
- char symbol;
- if (!(input >> symbol))
- throw ParsingError("Incorrect format for Node parsing. Unexpected EOF"s);
- if (symbol == 'n') {
- input.putback(symbol);
- return LoadNull(input);
- } else if (symbol == 't' || symbol == 'f') {
- input.putback(symbol);
- return LoadBool(input);
- } else if (symbol == '[') {
- return LoadArray(input);
- } else if (symbol == '{') {
- return LoadDict(input);
- } else if (symbol == '"') {
- return LoadString(input);
- } else {
- input.putback(symbol);
- return LoadNumber(input);
- }
- }
- } // namespace
- /* Constructors */
- Node::Node(bool value) : data_(value) {}
- Node::Node(std::nullptr_t /* value*/) : Node() {}
- Node::Node(int value) : data_(value) {}
- Node::Node(double value) : data_(value) {}
- Node::Node(std::string value) : data_(std::move(value)) {}
- Node::Node(Dict map) : data_(std::move(map)) {}
- Node::Node(Array array) : data_(std::move(array)) {}
- /* Is-like methods */
- bool Node::IsBool() const {
- return std::holds_alternative<bool>(data_);
- }
- ...
- /* As-like methods */
- const NodeContainer& Node::AsPureNodeContainer() const {
- return data_;
- }
- bool Node::AsBool() const {
- if (auto* value = std::get_if<bool>(&data_))
- return *value;
- throw std::logic_error("Impossible to parse node as Boolean"s);
- }
- /* Operators */
- bool operator==(const Node& left, const Node& right) {
- return left.data_ == right.data_;
- }
- bool operator!=(const Node& left, const Node& right) {
- return !(left == right);
- }
Add Comment
Please, Sign In to add comment