object-introspection/test/TypeGraphParser.h
Alastair Robertson 4d96848bdb TypeGraphParser: Throw custom error types
We can catch these exceptions and print clearer failure messages.

Before:
  unknown file: Failure
  C++ exception with description "Invalid type for child" thrown in the test body.

After:
  ../test/type_graph_utils.cpp:44: Failure
  Failed
  Error parsing input graph: Invalid type for child
2023-07-18 17:18:28 +01:00

44 lines
1.1 KiB
C++

#pragma once
#include <functional>
#include <unordered_map>
#include "oi/type_graph/Types.h"
using namespace type_graph;
namespace type_graph {
class TypeGraph;
} // namespace type_graph
/*
* TypeGraphParser
*
* Parses a textual type graph, as emitted by Printer.
*/
class TypeGraphParser {
public:
TypeGraphParser(TypeGraph& typeGraph) : typeGraph_(typeGraph) {
}
void parse(std::string_view input);
private:
TypeGraph& typeGraph_;
std::unordered_map<NodeId, std::reference_wrapper<Type>> nodesById_;
Type& parseType(std::string_view& input, size_t rootIndent);
template <typename T>
void parseParams(T& c, std::string_view& input, size_t rootIndent);
void parseParents(Class& c, std::string_view& input, size_t rootIndent);
void parseMembers(Class& c, std::string_view& input, size_t rootIndent);
void parseFunctions(Class& c, std::string_view& input, size_t rootIndent);
void parseChildren(Class& c, std::string_view& input, size_t rootIndent);
};
class TypeGraphParserError : public std::runtime_error {
public:
TypeGraphParserError(const std::string& msg) : std::runtime_error{msg} {
}
};