diff --git a/src/core/json/include/sourcemeta/core/json.h b/src/core/json/include/sourcemeta/core/json.h index c772021e0..c901afb00 100644 --- a/src/core/json/include/sourcemeta/core/json.h +++ b/src/core/json/include/sourcemeta/core/json.h @@ -44,7 +44,7 @@ namespace sourcemeta::core { /// assert(document.is_array()); /// ``` /// -/// If parsing fails, sourcemeta::core::ParseError will be thrown. +/// If parsing fails, sourcemeta::core::JSONParseError will be thrown. SOURCEMETA_CORE_JSON_EXPORT auto parse_json(std::basic_istream &stream, const JSON::ParseCallback &callback = nullptr) -> JSON; @@ -63,7 +63,7 @@ auto parse_json(std::basic_istream &stream, /// assert(document.is_array()); /// ``` /// -/// If parsing fails, sourcemeta::core::ParseError will be thrown. +/// If parsing fails, sourcemeta::core::JSONParseError will be thrown. SOURCEMETA_CORE_JSON_EXPORT auto parse_json(const std::basic_string &input, const JSON::ParseCallback &callback = nullptr) -> JSON; @@ -125,7 +125,7 @@ auto parse_json(const std::basic_string &input, /// std::cout << std::endl; /// ``` /// -/// If parsing fails, sourcemeta::core::ParseError will be thrown. +/// If parsing fails, sourcemeta::core::JSONParseError will be thrown. SOURCEMETA_CORE_JSON_EXPORT auto read_json(const std::filesystem::path &path) -> JSON; @@ -147,7 +147,7 @@ auto read_json(const std::filesystem::path &path) -> JSON; /// std::cout << std::endl; /// ``` /// -/// If parsing fails, sourcemeta::core::ParseError will be thrown. +/// If parsing fails, sourcemeta::core::JSONParseError will be thrown. SOURCEMETA_CORE_JSON_EXPORT auto read_file(const std::filesystem::path &path) -> std::basic_ifstream; diff --git a/src/core/json/include/sourcemeta/core/json_error.h b/src/core/json/include/sourcemeta/core/json_error.h index 86fe57861..2eef13e3c 100644 --- a/src/core/json/include/sourcemeta/core/json_error.h +++ b/src/core/json/include/sourcemeta/core/json_error.h @@ -20,10 +20,10 @@ namespace sourcemeta::core { /// @ingroup json /// This class represents a parsing error -class SOURCEMETA_CORE_JSON_EXPORT ParseError : public std::exception { +class SOURCEMETA_CORE_JSON_EXPORT JSONParseError : public std::exception { public: /// Create a parsing error - ParseError(const std::uint64_t line, const std::uint64_t column) + JSONParseError(const std::uint64_t line, const std::uint64_t column) : line_{line}, column_{column} {} [[nodiscard]] auto what() const noexcept -> const char * override { @@ -45,16 +45,17 @@ class SOURCEMETA_CORE_JSON_EXPORT ParseError : public std::exception { /// @ingroup json /// This class represents a parsing error occurring from parsing a file -class SOURCEMETA_CORE_JSON_EXPORT FileParseError : public ParseError { +class SOURCEMETA_CORE_JSON_EXPORT JSONFileParseError : public JSONParseError { public: /// Create a file parsing error - FileParseError(const std::filesystem::path &path, const std::uint64_t line, - const std::uint64_t column) - : ParseError{line, column}, path_{path} {} + JSONFileParseError(const std::filesystem::path &path, + const std::uint64_t line, const std::uint64_t column) + : JSONParseError{line, column}, path_{path} {} /// Create a file parsing error from a parse error - FileParseError(const std::filesystem::path &path, const ParseError &parent) - : ParseError{parent.line(), parent.column()}, path_{path} {} + JSONFileParseError(const std::filesystem::path &path, + const JSONParseError &parent) + : JSONParseError{parent.line(), parent.column()}, path_{path} {} /// Get the fiel path of the error [[nodiscard]] auto path() const noexcept -> const std::filesystem::path { diff --git a/src/core/json/json.cc b/src/core/json/json.cc index 6a52bc7de..ed50f604e 100644 --- a/src/core/json/json.cc +++ b/src/core/json/json.cc @@ -54,9 +54,9 @@ auto read_json(const std::filesystem::path &path) -> JSON { auto stream{read_file(path)}; try { return parse_json(stream); - } catch (const ParseError &error) { + } catch (const JSONParseError &error) { // For producing better error messages - throw FileParseError(path, error); + throw JSONFileParseError(path, error); } } diff --git a/src/core/json/parser.h b/src/core/json/parser.h index 29a78b632..c656c5c82 100644 --- a/src/core/json/parser.h +++ b/src/core/json/parser.h @@ -28,7 +28,7 @@ inline auto parse_null( 1)) { column += 1; if (stream.get() != character) { - throw ParseError(line, column); + throw JSONParseError(line, column); } } @@ -45,7 +45,7 @@ inline auto parse_boolean_true( 1)) { column += 1; if (stream.get() != character) { - throw ParseError(line, column); + throw JSONParseError(line, column); } } @@ -62,7 +62,7 @@ inline auto parse_boolean_false( 1)) { column += 1; if (stream.get() != character) { - throw ParseError(line, column); + throw JSONParseError(line, column); } } @@ -97,7 +97,7 @@ auto parse_string_unicode_code_point( if (std::isxdigit(code_point[code_point_size])) { code_point_size += 1; } else { - throw ParseError(line, column); + throw JSONParseError(line, column); } } @@ -127,13 +127,13 @@ auto parse_string_unicode( // Next, we expect "\" column += 1; if (stream.get() != internal::token_string_escape) { - throw ParseError(line, column); + throw JSONParseError(line, column); } // Next, we expect "u" column += 1; if (stream.get() != internal::token_string_escape_unicode) { - throw ParseError(line, column); + throw JSONParseError(line, column); } // Finally, get the low code point of the surrogate and calculate @@ -147,7 +147,7 @@ auto parse_string_unicode( code_point = 0x10000 + ((code_point - 0xD800) << 10) + (low_code_point - 0xDC00); } else { - throw ParseError(line, column); + throw JSONParseError(line, column); } } @@ -218,7 +218,7 @@ auto parse_string_escape( return; default: - throw ParseError(line, column); + throw JSONParseError(line, column); } } @@ -276,14 +276,14 @@ auto parse_string( case '\u001E': case '\u001F': case static_cast(JSON::CharTraits::eof()): - throw ParseError(line, column); + throw JSONParseError(line, column); default: result.put(character); break; } } - throw ParseError(line, column); + throw JSONParseError(line, column); } template @@ -293,7 +293,7 @@ auto parse_number_integer(const std::uint64_t line, const std::uint64_t column, try { return std::stoll(string); } catch (const std::out_of_range &) { - throw ParseError(line, column); + throw JSONParseError(line, column); } } @@ -304,7 +304,7 @@ auto parse_number_real(const std::uint64_t line, const std::uint64_t column, try { return std::stod(string); } catch (const std::out_of_range &) { - throw ParseError(line, column); + throw JSONParseError(line, column); } } @@ -338,7 +338,7 @@ auto parse_number_exponent_rest( } } - throw ParseError(line, column); + throw JSONParseError(line, column); } auto parse_number_exponent( @@ -366,7 +366,7 @@ auto parse_number_exponent( return parse_number_exponent_rest(line, column, original_column, stream, result); default: - throw ParseError(line, column); + throw JSONParseError(line, column); } } @@ -405,7 +405,7 @@ auto parse_number_exponent_first( return parse_number_exponent_rest(line, column, original_column, stream, result); default: - throw ParseError(line, column); + throw JSONParseError(line, column); } } @@ -450,7 +450,7 @@ auto parse_number_fractional( } } - throw ParseError(line, column); + throw JSONParseError(line, column); } auto parse_number_fractional_first( @@ -469,7 +469,7 @@ auto parse_number_fractional_first( case internal::token_number_decimal_point: case static_cast(JSON::CharTraits::eof()): column += 1; - throw ParseError(line, column); + throw JSONParseError(line, column); case internal::token_number_zero: case internal::token_number_one: case internal::token_number_two: @@ -526,7 +526,7 @@ auto parse_number_maybe_fractional( case internal::token_number_eight: case internal::token_number_nine: column += 1; - throw ParseError(line, column); + throw JSONParseError(line, column); default: return JSON{parse_number_integer(line, original_column, result.str())}; } @@ -578,7 +578,7 @@ auto parse_number_any_rest( } } - throw ParseError(line, column); + throw JSONParseError(line, column); } auto parse_number_any_negative_first( @@ -612,7 +612,7 @@ auto parse_number_any_negative_first( return parse_number_any_rest(line, column, original_column, stream, result); default: - throw ParseError(line, column); + throw JSONParseError(line, column); } } @@ -783,7 +783,7 @@ auto internal_parse_json( case internal::token_whitespace_space: goto do_parse; default: - throw ParseError(line, column); + throw JSONParseError(line, column); } /* @@ -828,7 +828,7 @@ auto internal_parse_json( CALLBACK_POST(Array, frames.top().get()); goto do_parse_container_end; } else { - throw ParseError(line, column); + throw JSONParseError(line, column); } // Values @@ -1172,7 +1172,7 @@ auto internal_parse_json( frames.pop(); } - throw ParseError(line, column); + throw JSONParseError(line, column); do_parse_container_end: assert(!levels.empty()); diff --git a/src/core/jsonl/include/sourcemeta/core/jsonl.h b/src/core/jsonl/include/sourcemeta/core/jsonl.h index 6d5e02b34..72f8e5982 100644 --- a/src/core/jsonl/include/sourcemeta/core/jsonl.h +++ b/src/core/jsonl/include/sourcemeta/core/jsonl.h @@ -44,7 +44,7 @@ class SOURCEMETA_CORE_JSONL_EXPORT JSONL { /// } /// ``` /// - /// If parsing fails, sourcemeta::core::ParseError will be thrown. + /// If parsing fails, sourcemeta::core::JSONParseError will be thrown. JSONL(std::basic_istream &stream); using const_iterator = ConstJSONLIterator; diff --git a/src/core/jsonl/iterator.cc b/src/core/jsonl/iterator.cc index 604ef7470..cff1a71ab 100644 --- a/src/core/jsonl/iterator.cc +++ b/src/core/jsonl/iterator.cc @@ -64,7 +64,7 @@ auto ConstJSONLIterator::operator++() -> ConstJSONLIterator & { goto end; default: this->column += 1; - throw ParseError(this->line, this->column); + throw JSONParseError(this->line, this->column); } element: diff --git a/src/core/jsonpointer/include/sourcemeta/core/jsonpointer_error.h b/src/core/jsonpointer/include/sourcemeta/core/jsonpointer_error.h index d84bea154..12c812fc8 100644 --- a/src/core/jsonpointer/include/sourcemeta/core/jsonpointer_error.h +++ b/src/core/jsonpointer/include/sourcemeta/core/jsonpointer_error.h @@ -13,10 +13,13 @@ namespace sourcemeta::core { /// @ingroup jsonpointer /// This class represents a parsing error. -class SOURCEMETA_CORE_JSONPOINTER_EXPORT PointerParseError : public ParseError { +class SOURCEMETA_CORE_JSONPOINTER_EXPORT PointerParseError + // TODO: It makes no sense for a JSON Pointer error to inherit from a JSON + // error. Make them independent + : public JSONParseError { public: /// Create a parsing error - PointerParseError(const std::uint64_t column) : ParseError{1, column} {} + PointerParseError(const std::uint64_t column) : JSONParseError{1, column} {} [[nodiscard]] auto what() const noexcept -> const char * override { return "The input is not a valid JSON Pointer"; diff --git a/src/core/yaml/yaml.cc b/src/core/yaml/yaml.cc index c77b90bb8..39635d4d9 100644 --- a/src/core/yaml/yaml.cc +++ b/src/core/yaml/yaml.cc @@ -27,7 +27,7 @@ static auto yaml_node_to_json(yaml_node_t *const node, // Looks like it is very hard in YAML, given a scalar value, to // determine whether it is a string or something else without attempting // to parsing it and potentially failing to do so - } catch (const sourcemeta::core::ParseError &) { + } catch (const sourcemeta::core::JSONParseError &) { return sourcemeta::core::JSON{input}; } } diff --git a/test/json/json_error_test.cc b/test/json/json_error_test.cc index 23bf477a2..18b172a62 100644 --- a/test/json/json_error_test.cc +++ b/test/json/json_error_test.cc @@ -6,10 +6,11 @@ #include // std::is_base_of_v TEST(JSON_error, parse_error) { - static_assert(std::is_base_of_v, - "Must subclass std::exception"); - auto exception{sourcemeta::core::ParseError(5, 6)}; - EXPECT_THROW(throw exception, sourcemeta::core::ParseError); + static_assert( + std::is_base_of_v, + "Must subclass std::exception"); + auto exception{sourcemeta::core::JSONParseError(5, 6)}; + EXPECT_THROW(throw exception, sourcemeta::core::JSONParseError); EXPECT_EQ(std::string{exception.what()}, "Failed to parse the JSON document"); EXPECT_EQ(exception.line(), 5); EXPECT_EQ(exception.column(), 6); diff --git a/test/json/json_parse_error_test.cc b/test/json/json_parse_error_test.cc index 30f04b15e..3ffecfa4e 100644 --- a/test/json/json_parse_error_test.cc +++ b/test/json/json_parse_error_test.cc @@ -8,7 +8,7 @@ try { \ sourcemeta::core::parse_json((input)); \ FAIL() << "The parse function was expected to throw"; \ - } catch (const sourcemeta::core::ParseError &error) { \ + } catch (const sourcemeta::core::JSONParseError &error) { \ EXPECT_EQ(error.line(), expected_line); \ EXPECT_EQ(error.column(), expected_column); \ SUCCEED(); \ @@ -633,7 +633,7 @@ TEST(JSON_parse_error, read_json_invalid) { try { sourcemeta::core::read_json(std::filesystem::path{TEST_DIRECTORY} / "stub_invalid.json"); - } catch (const sourcemeta::core::FileParseError &error) { + } catch (const sourcemeta::core::JSONFileParseError &error) { EXPECT_EQ(error.path(), std::filesystem::path{TEST_DIRECTORY} / "stub_invalid.json"); EXPECT_EQ(error.line(), 3); diff --git a/test/json/jsontestsuite.cc b/test/json/jsontestsuite.cc index c712fd83c..743d30f8c 100644 --- a/test/json/jsontestsuite.cc +++ b/test/json/jsontestsuite.cc @@ -40,7 +40,7 @@ class JSONTest : public testing::Test { while (!stream.eof()) { sourcemeta::core::parse_json(stream); } - } catch (const sourcemeta::core::ParseError &) { + } catch (const sourcemeta::core::JSONParseError &) { SUCCEED(); } catch (const std::exception &) { FAIL() << "The parse function threw an unexpected error"; diff --git a/test/jsonl/jsonl_parse_error_test.cc b/test/jsonl/jsonl_parse_error_test.cc index 341878211..41e735824 100644 --- a/test/jsonl/jsonl_parse_error_test.cc +++ b/test/jsonl/jsonl_parse_error_test.cc @@ -12,7 +12,7 @@ while (iterator != parser.cend()) \ ++iterator; \ FAIL() << "The JSONL parser was expected to throw"; \ - } catch (const sourcemeta::core::ParseError &error) { \ + } catch (const sourcemeta::core::JSONParseError &error) { \ EXPECT_EQ(error.line(), expected_line); \ EXPECT_EQ(error.column(), expected_column); \ SUCCEED(); \ diff --git a/test/jsonpointer/jsonpointer_parse_error_test.cc b/test/jsonpointer/jsonpointer_parse_error_test.cc index 6aab1633b..81d27e8a1 100644 --- a/test/jsonpointer/jsonpointer_parse_error_test.cc +++ b/test/jsonpointer/jsonpointer_parse_error_test.cc @@ -12,7 +12,7 @@ stream << "\"" << (input) << "\""; \ sourcemeta::core::parse_json(stream.str()); \ FAIL() << "The parse function was expected to throw"; \ - } catch (const sourcemeta::core::ParseError &) { \ + } catch (const sourcemeta::core::JSONParseError &) { \ SUCCEED(); \ } catch (const std::exception &) { \ FAIL() << "The parse operation threw an unexpected error"; \