cmJSONState.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #pragma once
  4. #include "cmConfigure.h" // IWYU pragma: keep
  5. #include <cstddef>
  6. #include <string>
  7. #include <utility>
  8. #include <vector>
  9. #include "cmJSONState.h"
  10. #include "cmStringAlgorithms.h"
  11. namespace Json {
  12. class Value;
  13. }
  14. class cmJSONState
  15. {
  16. using Location = struct
  17. {
  18. int line;
  19. int column;
  20. };
  21. public:
  22. using JsonPair = std::pair<const std::string, const Json::Value*>;
  23. cmJSONState() = default;
  24. cmJSONState(const std::string& filename, Json::Value* root);
  25. void AddError(std::string const& errMsg);
  26. void AddErrorAtValue(std::string const& errMsg, const Json::Value* value);
  27. void AddErrorAtOffset(std::string const& errMsg, std::ptrdiff_t offset);
  28. std::string GetErrorMessage(bool showContext = true);
  29. std::string key();
  30. std::string key_after(std::string const& key);
  31. const Json::Value* value_after(std::string const& key);
  32. void push_stack(std::string const& key, const Json::Value* value);
  33. void pop_stack();
  34. class Error
  35. {
  36. public:
  37. Error(Location loc, std::string errMsg)
  38. : location(loc)
  39. , message(std::move(errMsg)){};
  40. Error(std::string errMsg)
  41. : location({ -1, -1 })
  42. , message(std::move(errMsg)){};
  43. std::string GetErrorMessage() const
  44. {
  45. std::string output = message;
  46. if (location.line > 0) {
  47. output = cmStrCat("Error: @", location.line, ",", location.column,
  48. ": ", output);
  49. }
  50. return output;
  51. }
  52. Location GetLocation() const { return location; }
  53. private:
  54. Location location;
  55. std::string message;
  56. };
  57. std::vector<JsonPair> parseStack;
  58. std::vector<Error> errors;
  59. std::string doc;
  60. private:
  61. std::string GetJsonContext(Location loc);
  62. Location LocateInDocument(ptrdiff_t offset);
  63. };