From 6027bfada148bf220c93495ece3173a5f011a958 Mon Sep 17 00:00:00 2001 From: LinZhihao-723 Date: Mon, 25 Aug 2025 22:00:41 -0400 Subject: [PATCH] Update serialization methods. --- src/spider/tdl/parser/SourceLocation.hpp | 7 + .../tdl/parser/ast/node_impl/Function.cpp | 14 +- .../tdl/parser/ast/node_impl/Identifier.cpp | 7 +- .../tdl/parser/ast/node_impl/NamedVar.cpp | 3 +- .../tdl/parser/ast/node_impl/Namespace.cpp | 7 +- .../tdl/parser/ast/node_impl/StructSpec.cpp | 7 +- .../parser/ast/node_impl/TranslationUnit.cpp | 3 +- .../parser/ast/node_impl/type_impl/Struct.cpp | 3 +- .../type_impl/container_impl/List.cpp | 3 +- .../type_impl/container_impl/Map.cpp | 3 +- .../type_impl/container_impl/Tuple.cpp | 10 +- .../type_impl/primitive_impl/Bool.cpp | 6 +- .../type_impl/primitive_impl/Float.cpp | 3 +- .../type_impl/primitive_impl/Int.cpp | 3 +- tests/tdl/test-parser-ast.cpp | 258 ++++++++++-------- tests/tdl/test-parser.cpp | 217 ++++++++------- 16 files changed, 318 insertions(+), 236 deletions(-) diff --git a/src/spider/tdl/parser/SourceLocation.hpp b/src/spider/tdl/parser/SourceLocation.hpp index e029a876c..7307998c1 100644 --- a/src/spider/tdl/parser/SourceLocation.hpp +++ b/src/spider/tdl/parser/SourceLocation.hpp @@ -2,6 +2,9 @@ #define SPIDER_TDL_PARSER_SOURCELOCATION_HPP #include +#include + +#include namespace spider::tdl::parser { class SourceLocation { @@ -14,6 +17,10 @@ class SourceLocation { [[nodiscard]] auto get_column() const noexcept -> size_t { return m_column; } + [[nodiscard]] auto serialize_to_str() const -> std::string { + return fmt::format("({}:{})", m_line, m_column); + } + [[nodiscard]] auto operator==(SourceLocation const& other) const noexcept -> bool { return m_line == other.m_line && m_column == other.m_column; } diff --git a/src/spider/tdl/parser/ast/node_impl/Function.cpp b/src/spider/tdl/parser/ast/node_impl/Function.cpp index 50a585f9c..705cc5051 100644 --- a/src/spider/tdl/parser/ast/node_impl/Function.cpp +++ b/src/spider/tdl/parser/ast/node_impl/Function.cpp @@ -102,10 +102,13 @@ auto Function::serialize_to_str(size_t indentation_level) const if (false == serialized_params.empty()) { return fmt::format( - "{}[Function]:\n{}Name:{}\n{}Return:\n{}\n{}", + "{}[Function]{}:\n{}Name:\n{}\n{}Return:\n{}\n{}", create_indentation(indentation_level), + get_source_location().serialize_to_str(), create_indentation(indentation_level + 1), - get_name(), + YSTDLIB_ERROR_HANDLING_TRYX( + get_child_unsafe(0)->serialize_to_str(indentation_level + 2) + ), create_indentation(indentation_level + 1), serialized_return_type, fmt::join(serialized_params, "\n") @@ -113,10 +116,13 @@ auto Function::serialize_to_str(size_t indentation_level) const } return fmt::format( - "{}[Function]:\n{}Name:{}\n{}Return:\n{}\n{}No Params", + "{}[Function]{}:\n{}Name:\n{}\n{}Return:\n{}\n{}No Params", create_indentation(indentation_level), + get_source_location().serialize_to_str(), create_indentation(indentation_level + 1), - get_name(), + YSTDLIB_ERROR_HANDLING_TRYX( + get_child_unsafe(0)->serialize_to_str(indentation_level + 2) + ), create_indentation(indentation_level + 1), serialized_return_type, create_indentation(indentation_level + 1) diff --git a/src/spider/tdl/parser/ast/node_impl/Identifier.cpp b/src/spider/tdl/parser/ast/node_impl/Identifier.cpp index b3e4ad9cd..0b9743b55 100644 --- a/src/spider/tdl/parser/ast/node_impl/Identifier.cpp +++ b/src/spider/tdl/parser/ast/node_impl/Identifier.cpp @@ -11,6 +11,11 @@ namespace spider::tdl::parser::ast::node_impl { auto Identifier::serialize_to_str(size_t indentation_level) const -> ystdlib::error_handling::Result { - return fmt::format("{}[Identifier]:{}", create_indentation(indentation_level), m_name); + return fmt::format( + "{}[Identifier]{}:{}", + create_indentation(indentation_level), + get_source_location().serialize_to_str(), + m_name + ); } } // namespace spider::tdl::parser::ast::node_impl diff --git a/src/spider/tdl/parser/ast/node_impl/NamedVar.cpp b/src/spider/tdl/parser/ast/node_impl/NamedVar.cpp index 8a11d9576..418be3eff 100644 --- a/src/spider/tdl/parser/ast/node_impl/NamedVar.cpp +++ b/src/spider/tdl/parser/ast/node_impl/NamedVar.cpp @@ -32,8 +32,9 @@ auto NamedVar::create( auto NamedVar::serialize_to_str(size_t indentation_level) const -> ystdlib::error_handling::Result { return fmt::format( - "{}[NamedVar]:\n{}Id:\n{}\n{}Type:\n{}", + "{}[NamedVar]{}:\n{}Id:\n{}\n{}Type:\n{}", create_indentation(indentation_level), + get_source_location().serialize_to_str(), create_indentation(indentation_level + 1), YSTDLIB_ERROR_HANDLING_TRYX(get_id()->serialize_to_str(indentation_level + 2)), create_indentation(indentation_level + 1), diff --git a/src/spider/tdl/parser/ast/node_impl/Namespace.cpp b/src/spider/tdl/parser/ast/node_impl/Namespace.cpp index 9c75e9e80..8fb58da95 100644 --- a/src/spider/tdl/parser/ast/node_impl/Namespace.cpp +++ b/src/spider/tdl/parser/ast/node_impl/Namespace.cpp @@ -91,10 +91,13 @@ auto Namespace::serialize_to_str(size_t indentation_level) const ); return fmt::format( - "{}[Namespace]:\n{}Name:{}\n{}", + "{}[Namespace]{}:\n{}Name:\n{}\n{}", create_indentation(indentation_level), + get_source_location().serialize_to_str(), create_indentation(indentation_level + 1), - get_name(), + YSTDLIB_ERROR_HANDLING_TRYX( + get_child_unsafe(0)->serialize_to_str(indentation_level + 2) + ), fmt::join(serialized_funcs, "\n") ); } diff --git a/src/spider/tdl/parser/ast/node_impl/StructSpec.cpp b/src/spider/tdl/parser/ast/node_impl/StructSpec.cpp index 62a33d140..445f17e8c 100644 --- a/src/spider/tdl/parser/ast/node_impl/StructSpec.cpp +++ b/src/spider/tdl/parser/ast/node_impl/StructSpec.cpp @@ -91,10 +91,13 @@ auto StructSpec::serialize_to_str(size_t indentation_level) const }) ); return fmt::format( - "{}[StructSpec]:\n{}Name:{}\n{}", + "{}[StructSpec]{}:\n{}Name:\n{}\n{}", create_indentation(indentation_level), + get_source_location().serialize_to_str(), create_indentation(indentation_level + 1), - get_name(), + YSTDLIB_ERROR_HANDLING_TRYX( + get_child_unsafe(0)->serialize_to_str(indentation_level + 2) + ), fmt::join(serialized_fields, "\n") ); } diff --git a/src/spider/tdl/parser/ast/node_impl/TranslationUnit.cpp b/src/spider/tdl/parser/ast/node_impl/TranslationUnit.cpp index 09981cdec..658de72d5 100644 --- a/src/spider/tdl/parser/ast/node_impl/TranslationUnit.cpp +++ b/src/spider/tdl/parser/ast/node_impl/TranslationUnit.cpp @@ -71,8 +71,9 @@ auto TranslationUnit::serialize_to_str(size_t indentation_level) const ); return fmt::format( - "{}[TranslationUnit]:\n{}StructSpecs:\n{}\n{}Namespaces:\n{}", + "{}[TranslationUnit]{}:\n{}StructSpecs:\n{}\n{}Namespaces:\n{}", create_indentation(indentation_level), + get_source_location().serialize_to_str(), create_indentation(indentation_level + 1), fmt::join(serialized_struct_specs, "\n"), create_indentation(indentation_level + 1), diff --git a/src/spider/tdl/parser/ast/node_impl/type_impl/Struct.cpp b/src/spider/tdl/parser/ast/node_impl/type_impl/Struct.cpp index 3bf488d5c..8f6f263c0 100644 --- a/src/spider/tdl/parser/ast/node_impl/type_impl/Struct.cpp +++ b/src/spider/tdl/parser/ast/node_impl/type_impl/Struct.cpp @@ -50,8 +50,9 @@ auto Struct::create(std::unique_ptr name, SourceLocation source_location) auto Struct::serialize_to_str(size_t indentation_level) const -> ystdlib::error_handling::Result { return fmt::format( - "{}[Type[Struct]]:\n{}Name:\n{}", + "{}[Type[Struct]]{}:\n{}Name:\n{}", create_indentation(indentation_level), + get_source_location().serialize_to_str(), create_indentation(indentation_level + 1), YSTDLIB_ERROR_HANDLING_TRYX( // The factory function ensures that the first child is of type `Identifier`. diff --git a/src/spider/tdl/parser/ast/node_impl/type_impl/container_impl/List.cpp b/src/spider/tdl/parser/ast/node_impl/type_impl/container_impl/List.cpp index dd77f4cf0..ba6c0a7ea 100644 --- a/src/spider/tdl/parser/ast/node_impl/type_impl/container_impl/List.cpp +++ b/src/spider/tdl/parser/ast/node_impl/type_impl/container_impl/List.cpp @@ -26,8 +26,9 @@ auto List::create(std::unique_ptr element_type, SourceLocation source_loca auto List::serialize_to_str(size_t indentation_level) const -> ystdlib::error_handling::Result { return fmt::format( - "{}[Type[Container[List]]]:\n{}ElementType:\n{}", + "{}[Type[Container[List]]]{}:\n{}ElementType:\n{}", create_indentation(indentation_level), + get_source_location().serialize_to_str(), create_indentation(indentation_level + 1), YSTDLIB_ERROR_HANDLING_TRYX(get_element_type()->serialize_to_str(indentation_level + 2)) ); diff --git a/src/spider/tdl/parser/ast/node_impl/type_impl/container_impl/Map.cpp b/src/spider/tdl/parser/ast/node_impl/type_impl/container_impl/Map.cpp index 5f13b542b..174e04ed7 100644 --- a/src/spider/tdl/parser/ast/node_impl/type_impl/container_impl/Map.cpp +++ b/src/spider/tdl/parser/ast/node_impl/type_impl/container_impl/Map.cpp @@ -90,8 +90,9 @@ auto Map::create( auto Map::serialize_to_str(size_t indentation_level) const -> ystdlib::error_handling::Result { return fmt::format( - "{}[Type[Container[Map]]]:\n{}KeyType:\n{}\n{}ValueType:\n{}", + "{}[Type[Container[Map]]]{}:\n{}KeyType:\n{}\n{}ValueType:\n{}", create_indentation(indentation_level), + get_source_location().serialize_to_str(), create_indentation(indentation_level + 1), YSTDLIB_ERROR_HANDLING_TRYX(get_key_type()->serialize_to_str(indentation_level + 2)), create_indentation(indentation_level + 1), diff --git a/src/spider/tdl/parser/ast/node_impl/type_impl/container_impl/Tuple.cpp b/src/spider/tdl/parser/ast/node_impl/type_impl/container_impl/Tuple.cpp index aa59e8969..22cc3449d 100644 --- a/src/spider/tdl/parser/ast/node_impl/type_impl/container_impl/Tuple.cpp +++ b/src/spider/tdl/parser/ast/node_impl/type_impl/container_impl/Tuple.cpp @@ -35,7 +35,12 @@ auto Tuple::serialize_to_str(size_t indentation_level) const constexpr std::string_view cTypeTag{"[Type[Container[Tuple]]]"}; if (is_empty()) { - return fmt::format("{}{}:Empty", create_indentation(indentation_level), cTypeTag); + return fmt::format( + "{}{}{}:Empty", + create_indentation(indentation_level), + cTypeTag, + get_source_location().serialize_to_str() + ); } std::vector serialized_children; @@ -55,9 +60,10 @@ auto Tuple::serialize_to_str(size_t indentation_level) const }) ); return fmt::format( - "{}{}:\n{}", + "{}{}{}:\n{}", create_indentation(indentation_level), cTypeTag, + get_source_location().serialize_to_str(), fmt::join(serialized_children, "\n") ); } diff --git a/src/spider/tdl/parser/ast/node_impl/type_impl/primitive_impl/Bool.cpp b/src/spider/tdl/parser/ast/node_impl/type_impl/primitive_impl/Bool.cpp index d19a1def0..930a2964e 100644 --- a/src/spider/tdl/parser/ast/node_impl/type_impl/primitive_impl/Bool.cpp +++ b/src/spider/tdl/parser/ast/node_impl/type_impl/primitive_impl/Bool.cpp @@ -11,6 +11,10 @@ namespace spider::tdl::parser::ast::node_impl::type_impl::primitive_impl { auto Bool::serialize_to_str(size_t indentation_level) const -> ystdlib::error_handling::Result { - return fmt::format("{}[Type[Primitive[Bool]]]", create_indentation(indentation_level)); + return fmt::format( + "{}[Type[Primitive[Bool]]]{}", + create_indentation(indentation_level), + get_source_location().serialize_to_str() + ); } } // namespace spider::tdl::parser::ast::node_impl::type_impl::primitive_impl diff --git a/src/spider/tdl/parser/ast/node_impl/type_impl/primitive_impl/Float.cpp b/src/spider/tdl/parser/ast/node_impl/type_impl/primitive_impl/Float.cpp index d433aa4e0..1be267bc2 100644 --- a/src/spider/tdl/parser/ast/node_impl/type_impl/primitive_impl/Float.cpp +++ b/src/spider/tdl/parser/ast/node_impl/type_impl/primitive_impl/Float.cpp @@ -12,8 +12,9 @@ namespace spider::tdl::parser::ast::node_impl::type_impl::primitive_impl { auto Float::serialize_to_str(size_t indentation_level) const -> ystdlib::error_handling::Result { return fmt::format( - "{}[Type[Primitive[Float]]]:{}", + "{}[Type[Primitive[Float]]]{}:{}", create_indentation(indentation_level), + get_source_location().serialize_to_str(), YSTDLIB_ERROR_HANDLING_TRYX(serialize_float_spec(m_spec)) ); } diff --git a/src/spider/tdl/parser/ast/node_impl/type_impl/primitive_impl/Int.cpp b/src/spider/tdl/parser/ast/node_impl/type_impl/primitive_impl/Int.cpp index 26aa853c3..a7695cd91 100644 --- a/src/spider/tdl/parser/ast/node_impl/type_impl/primitive_impl/Int.cpp +++ b/src/spider/tdl/parser/ast/node_impl/type_impl/primitive_impl/Int.cpp @@ -12,8 +12,9 @@ namespace spider::tdl::parser::ast::node_impl::type_impl::primitive_impl { auto Int::serialize_to_str(size_t indentation_level) const -> ystdlib::error_handling::Result { return fmt::format( - "{}[Type[Primitive[Int]]]:{}", + "{}[Type[Primitive[Int]]]{}:{}", create_indentation(indentation_level), + get_source_location().serialize_to_str(), YSTDLIB_ERROR_HANDLING_TRYX(serialize_int_spec(m_spec)) ); } diff --git a/tests/tdl/test-parser-ast.cpp b/tests/tdl/test-parser-ast.cpp index d8c0e9317..bd07006e3 100644 --- a/tests/tdl/test-parser-ast.cpp +++ b/tests/tdl/test-parser-ast.cpp @@ -181,7 +181,7 @@ TEST_CASE("test-ast-node", "[tdl][ast][Node]") { SECTION("Identifier") { constexpr std::string_view cTestName{"test_name"}; - constexpr std::string_view cSerializedIdentifier{"[Identifier]:test_name"}; + constexpr std::string_view cSerializedIdentifier{"[Identifier](0:0):test_name"}; auto const node{Identifier::create(std::string{cTestName}, create_source_location())}; auto const* identifier{dynamic_cast(node.get())}; @@ -197,10 +197,19 @@ TEST_CASE("test-ast-node", "[tdl][ast][Node]") { SECTION("Type Int") { auto const [int_spec, expected_serialized_result] = GENERATE( - std::make_pair(IntSpec::Int8, std::string_view{"[Type[Primitive[Int]]]:int8"}), - std::make_pair(IntSpec::Int16, std::string_view{"[Type[Primitive[Int]]]:int16"}), - std::make_pair(IntSpec::Int32, std::string_view{"[Type[Primitive[Int]]]:int32"}), - std::make_pair(IntSpec::Int64, std::string_view{"[Type[Primitive[Int]]]:int64"}) + std::make_pair(IntSpec::Int8, std::string_view{"[Type[Primitive[Int]]](0:0):int8"}), + std::make_pair( + IntSpec::Int16, + std::string_view{"[Type[Primitive[Int]]](0:0):int16"} + ), + std::make_pair( + IntSpec::Int32, + std::string_view{"[Type[Primitive[Int]]](0:0):int32"} + ), + std::make_pair( + IntSpec::Int64, + std::string_view{"[Type[Primitive[Int]]](0:0):int64"} + ) ); auto const node{Int::create(int_spec, create_source_location())}; @@ -219,11 +228,11 @@ TEST_CASE("test-ast-node", "[tdl][ast][Node]") { auto const [float_spec, expected_serialized_result] = GENERATE( std::make_pair( FloatSpec::Float, - std::string_view{"[Type[Primitive[Float]]]:float"} + std::string_view{"[Type[Primitive[Float]]](0:0):float"} ), std::make_pair( FloatSpec::Double, - std::string_view{"[Type[Primitive[Float]]]:double"} + std::string_view{"[Type[Primitive[Float]]](0:0):double"} ) ); @@ -246,7 +255,7 @@ TEST_CASE("test-ast-node", "[tdl][ast][Node]") { REQUIRE(bool_node->get_num_children() == 0); - constexpr std::string_view cExpectedSerializedResult{"[Type[Primitive[Bool]]]"}; + constexpr std::string_view cExpectedSerializedResult{"[Type[Primitive[Bool]]](0:0)"}; auto const serialized_result{bool_node->serialize_to_str(0)}; REQUIRE_FALSE(serialized_result.has_error()); REQUIRE(serialized_result.value() == cExpectedSerializedResult); @@ -267,13 +276,13 @@ TEST_CASE("test-ast-node", "[tdl][ast][Node]") { REQUIRE(list_node->get_num_children() == 1); constexpr std::string_view cExpectedSerializedResult{ - "[Type[Container[List]]]:\n" + "[Type[Container[List]]](0:0):\n" " ElementType:\n" - " [Type[Container[Map]]]:\n" + " [Type[Container[Map]]](0:0):\n" " KeyType:\n" - " [Type[Primitive[Int]]]:int64\n" + " [Type[Primitive[Int]]](0:0):int64\n" " ValueType:\n" - " [Type[Primitive[Float]]]:double" + " [Type[Primitive[Float]]](0:0):double" }; auto const serialized_result{list_node->serialize_to_str(0)}; REQUIRE_FALSE(serialized_result.has_error()); @@ -303,15 +312,15 @@ TEST_CASE("test-ast-node", "[tdl][ast][Node]") { REQUIRE(map_node->get_num_children() == 2); constexpr std::string_view cExpectedSerializedResult{ - "[Type[Container[Map]]]:\n" + "[Type[Container[Map]]](0:0):\n" " KeyType:\n" - " [Type[Container[List]]]:\n" + " [Type[Container[List]]](0:0):\n" " ElementType:\n" - " [Type[Primitive[Int]]]:int8\n" + " [Type[Primitive[Int]]](0:0):int8\n" " ValueType:\n" - " [Type[Container[List]]]:\n" + " [Type[Container[List]]](0:0):\n" " ElementType:\n" - " [Type[Primitive[Float]]]:float" + " [Type[Primitive[Float]]](0:0):float" }; auto const serialized_result{map_node->serialize_to_str(0)}; REQUIRE_FALSE(serialized_result.has_error()); @@ -394,15 +403,15 @@ TEST_CASE("test-ast-node", "[tdl][ast][Node]") { REQUIRE(named_var_node->get_num_children() == 2); constexpr std::string_view cExpectedSerializedResult{ - "[NamedVar]:\n" + "[NamedVar](0:0):\n" " Id:\n" - " [Identifier]:TestId\n" + " [Identifier](0:0):TestId\n" " Type:\n" - " [Type[Container[Map]]]:\n" + " [Type[Container[Map]]](0:0):\n" " KeyType:\n" - " [Type[Primitive[Int]]]:int64\n" + " [Type[Primitive[Int]]](0:0):int64\n" " ValueType:\n" - " [Type[Primitive[Float]]]:double" + " [Type[Primitive[Float]]](0:0):double" }; auto const serialized_result{named_var_node->serialize_to_str(0)}; REQUIRE_FALSE(serialized_result.has_error()); @@ -418,7 +427,9 @@ TEST_CASE("test-ast-node", "[tdl][ast][Node]") { REQUIRE(tuple_node->get_num_children() == 0); - constexpr std::string_view cExpectedSerializedResult{"[Type[Container[Tuple]]]:Empty"}; + constexpr std::string_view cExpectedSerializedResult{ + "[Type[Container[Tuple]]](0:0):Empty" + }; auto const serialized_result{tuple_node->serialize_to_str(0)}; REQUIRE_FALSE(serialized_result.has_error()); REQUIRE(serialized_result.value() == cExpectedSerializedResult); @@ -445,17 +456,17 @@ TEST_CASE("test-ast-node", "[tdl][ast][Node]") { REQUIRE(tuple_node->get_num_children() == 3); constexpr std::string_view cExpectedSerializedResult{ - "[Type[Container[Tuple]]]:\n" + "[Type[Container[Tuple]]](0:0):\n" " Element[0]:\n" - " [Type[Primitive[Int]]]:int64\n" + " [Type[Primitive[Int]]](0:0):int64\n" " Element[1]:\n" - " [Type[Primitive[Float]]]:double\n" + " [Type[Primitive[Float]]](0:0):double\n" " Element[2]:\n" - " [Type[Container[Map]]]:\n" + " [Type[Container[Map]]](0:0):\n" " KeyType:\n" - " [Type[Primitive[Int]]]:int64\n" + " [Type[Primitive[Int]]](0:0):int64\n" " ValueType:\n" - " [Type[Primitive[Float]]]:double" + " [Type[Primitive[Float]]](0:0):double" }; auto const serialized_result{tuple_node->serialize_to_str(0)}; REQUIRE_FALSE(serialized_result.has_error()); @@ -511,30 +522,31 @@ TEST_CASE("test-ast-node", "[tdl][ast][Node]") { REQUIRE(struct_spec_node->get_name() == cTestStructName); constexpr std::string_view cExpectedSerializedResult{ - "[StructSpec]:\n" - " Name:TestStruct\n" + "[StructSpec](0:0):\n" + " Name:\n" + " [Identifier](0:0):TestStruct\n" " Fields[0]:\n" - " [NamedVar]:\n" + " [NamedVar](0:0):\n" " Id:\n" - " [Identifier]:m_int\n" + " [Identifier](0:0):m_int\n" " Type:\n" - " [Type[Primitive[Int]]]:int64\n" + " [Type[Primitive[Int]]](0:0):int64\n" " Fields[1]:\n" - " [NamedVar]:\n" + " [NamedVar](0:0):\n" " Id:\n" - " [Identifier]:m_float\n" + " [Identifier](0:0):m_float\n" " Type:\n" - " [Type[Primitive[Float]]]:double\n" + " [Type[Primitive[Float]]](0:0):double\n" " Fields[2]:\n" - " [NamedVar]:\n" + " [NamedVar](0:0):\n" " Id:\n" - " [Identifier]:m_map\n" + " [Identifier](0:0):m_map\n" " Type:\n" - " [Type[Container[Map]]]:\n" + " [Type[Container[Map]]](0:0):\n" " KeyType:\n" - " [Type[Primitive[Int]]]:int64\n" + " [Type[Primitive[Int]]](0:0):int64\n" " ValueType:\n" - " [Type[Primitive[Float]]]:double" + " [Type[Primitive[Float]]](0:0):double" }; auto const serialized_result{struct_spec_node->serialize_to_str(0)}; REQUIRE_FALSE(serialized_result.has_error()); @@ -609,9 +621,11 @@ TEST_CASE("test-ast-node", "[tdl][ast][Node]") { REQUIRE(cTestStructName == struct_node->get_name()); REQUIRE(nullptr == struct_node->get_spec()); - constexpr std::string_view cExpectedSerializedResult{"[Type[Struct]]:\n" - " Name:\n" - " [Identifier]:TestStruct"}; + constexpr std::string_view cExpectedSerializedResult{ + "[Type[Struct]](0:0):\n" + " Name:\n" + " [Identifier](0:0):TestStruct" + }; auto const serialized_result{struct_node->serialize_to_str(0)}; REQUIRE_FALSE(serialized_result.has_error()); REQUIRE(serialized_result.value() == cExpectedSerializedResult); @@ -690,32 +704,33 @@ TEST_CASE("test-ast-node", "[tdl][ast][Node]") { REQUIRE(nullptr != func_node->get_return_type()); constexpr std::string_view cExpectedSerializedResult{ - "[Function]:\n" - " Name:test_function\n" + "[Function](0:0):\n" + " Name:\n" + " [Identifier](0:0):test_function\n" " Return:\n" - " [Type[Container[Tuple]]]:\n" + " [Type[Container[Tuple]]](0:0):\n" " Element[0]:\n" - " [Type[Primitive[Int]]]:int64\n" + " [Type[Primitive[Int]]](0:0):int64\n" " Element[1]:\n" - " [Type[Struct]]:\n" + " [Type[Struct]](0:0):\n" " Name:\n" - " [Identifier]:TestStruct\n" + " [Identifier](0:0):TestStruct\n" " Element[2]:\n" - " [Type[Primitive[Bool]]]\n" + " [Type[Primitive[Bool]]](0:0)\n" " Params[0]:\n" - " [NamedVar]:\n" + " [NamedVar](0:0):\n" " Id:\n" - " [Identifier]:param_0\n" + " [Identifier](0:0):param_0\n" " Type:\n" - " [Type[Primitive[Int]]]:int64\n" + " [Type[Primitive[Int]]](0:0):int64\n" " Params[1]:\n" - " [NamedVar]:\n" + " [NamedVar](0:0):\n" " Id:\n" - " [Identifier]:param_1\n" + " [Identifier](0:0):param_1\n" " Type:\n" - " [Type[Struct]]:\n" + " [Type[Struct]](0:0):\n" " Name:\n" - " [Identifier]:TestStruct" + " [Identifier](0:0):TestStruct" }; auto const serialized_result{func_node->serialize_to_str(0)}; REQUIRE_FALSE(serialized_result.has_error()); @@ -745,24 +760,25 @@ TEST_CASE("test-ast-node", "[tdl][ast][Node]") { REQUIRE(nullptr == func_node->get_return_type()); constexpr std::string_view cExpectedSerializedResult{ - "[Function]:\n" - " Name:test_function\n" + "[Function](0:0):\n" + " Name:\n" + " [Identifier](0:0):test_function\n" " Return:\n" " void\n" " Params[0]:\n" - " [NamedVar]:\n" + " [NamedVar](0:0):\n" " Id:\n" - " [Identifier]:param_0\n" + " [Identifier](0:0):param_0\n" " Type:\n" - " [Type[Primitive[Int]]]:int64\n" + " [Type[Primitive[Int]]](0:0):int64\n" " Params[1]:\n" - " [NamedVar]:\n" + " [NamedVar](0:0):\n" " Id:\n" - " [Identifier]:param_1\n" + " [Identifier](0:0):param_1\n" " Type:\n" - " [Type[Struct]]:\n" + " [Type[Struct]](0:0):\n" " Name:\n" - " [Identifier]:TestStruct" + " [Identifier](0:0):TestStruct" }; auto const serialized_result{func_node->serialize_to_str(0)}; REQUIRE_FALSE(serialized_result.has_error()); @@ -791,18 +807,19 @@ TEST_CASE("test-ast-node", "[tdl][ast][Node]") { REQUIRE(nullptr != func_node->get_return_type()); constexpr std::string_view cExpectedSerializedResult{ - "[Function]:\n" - " Name:test_function\n" + "[Function](0:0):\n" + " Name:\n" + " [Identifier](0:0):test_function\n" " Return:\n" - " [Type[Container[Tuple]]]:\n" + " [Type[Container[Tuple]]](0:0):\n" " Element[0]:\n" - " [Type[Primitive[Int]]]:int64\n" + " [Type[Primitive[Int]]](0:0):int64\n" " Element[1]:\n" - " [Type[Struct]]:\n" + " [Type[Struct]](0:0):\n" " Name:\n" - " [Identifier]:TestStruct\n" + " [Identifier](0:0):TestStruct\n" " Element[2]:\n" - " [Type[Primitive[Bool]]]\n" + " [Type[Primitive[Bool]]](0:0)\n" " No Params" }; auto const serialized_result{func_node->serialize_to_str(0)}; @@ -827,11 +844,14 @@ TEST_CASE("test-ast-node", "[tdl][ast][Node]") { REQUIRE(func_node->get_name() == cTestFuncName); REQUIRE(nullptr == func_node->get_return_type()); - constexpr std::string_view cExpectedSerializedResult{"[Function]:\n" - " Name:test_function\n" - " Return:\n" - " void\n" - " No Params"}; + constexpr std::string_view cExpectedSerializedResult{ + "[Function](0:0):\n" + " Name:\n" + " [Identifier](0:0):test_function\n" + " Return:\n" + " void\n" + " No Params" + }; auto const serialized_result{func_node->serialize_to_str(0)}; REQUIRE_FALSE(serialized_result.has_error()); REQUIRE(serialized_result.value() == cExpectedSerializedResult); @@ -881,19 +901,22 @@ TEST_CASE("test-ast-node", "[tdl][ast][Node]") { REQUIRE(namespace_node->get_num_children() == 3); constexpr std::string_view cExpectedSerializedResult{ - "[Namespace]:\n" - " Name:TestNamespace\n" + "[Namespace](0:0):\n" + " Name:\n" + " [Identifier](0:0):TestNamespace\n" " Func[0]:\n" - " [Function]:\n" - " Name:func_0\n" + " [Function](0:0):\n" + " Name:\n" + " [Identifier](0:0):func_0\n" " Return:\n" - " [Type[Container[Tuple]]]:Empty\n" + " [Type[Container[Tuple]]](0:0):Empty\n" " No Params\n" " Func[1]:\n" - " [Function]:\n" - " Name:func_1\n" + " [Function](0:0):\n" + " Name:\n" + " [Identifier](0:0):func_1\n" " Return:\n" - " [Type[Container[Tuple]]]:Empty\n" + " [Type[Container[Tuple]]](0:0):Empty\n" " No Params" }; auto const serialized_result{namespace_node->serialize_to_str(0)}; @@ -941,48 +964,55 @@ TEST_CASE("test-ast-node", "[tdl][ast][Node]") { SECTION("Serialization") { constexpr std::string_view cExpectedSerializedResult{ - "[TranslationUnit]:\n" + "[TranslationUnit](0:0):\n" " StructSpecs:\n" - " [StructSpec]:\n" - " Name:Struct0\n" + " [StructSpec](0:0):\n" + " Name:\n" + " [Identifier](0:0):Struct0\n" " Fields[0]:\n" - " [NamedVar]:\n" + " [NamedVar](0:0):\n" " Id:\n" - " [Identifier]:member_0\n" + " [Identifier](0:0):member_0\n" " Type:\n" - " [Type[Primitive[Int]]]:int32\n" - " [StructSpec]:\n" - " Name:Struct1\n" + " [Type[Primitive[Int]]](0:0):int32\n" + " [StructSpec](0:0):\n" + " Name:\n" + " [Identifier](0:0):Struct1\n" " Fields[0]:\n" - " [NamedVar]:\n" + " [NamedVar](0:0):\n" " Id:\n" - " [Identifier]:member_0\n" + " [Identifier](0:0):member_0\n" " Type:\n" - " [Type[Primitive[Int]]]:int32\n" - " [StructSpec]:\n" - " Name:Struct2\n" + " [Type[Primitive[Int]]](0:0):int32\n" + " [StructSpec](0:0):\n" + " Name:\n" + " [Identifier](0:0):Struct2\n" " Fields[0]:\n" - " [NamedVar]:\n" + " [NamedVar](0:0):\n" " Id:\n" - " [Identifier]:member_0\n" + " [Identifier](0:0):member_0\n" " Type:\n" - " [Type[Primitive[Int]]]:int32\n" + " [Type[Primitive[Int]]](0:0):int32\n" " Namespaces:\n" - " [Namespace]:\n" - " Name:ns0\n" + " [Namespace](0:0):\n" + " Name:\n" + " [Identifier](0:0):ns0\n" " Func[0]:\n" - " [Function]:\n" - " Name:func_0\n" + " [Function](0:0):\n" + " Name:\n" + " [Identifier](0:0):func_0\n" " Return:\n" - " [Type[Container[Tuple]]]:Empty\n" + " [Type[Container[Tuple]]](0:0):Empty\n" " No Params\n" - " [Namespace]:\n" - " Name:ns1\n" + " [Namespace](0:0):\n" + " Name:\n" + " [Identifier](0:0):ns1\n" " Func[0]:\n" - " [Function]:\n" - " Name:func_0\n" + " [Function](0:0):\n" + " Name:\n" + " [Identifier](0:0):func_0\n" " Return:\n" - " [Type[Container[Tuple]]]:Empty\n" + " [Type[Container[Tuple]]](0:0):Empty\n" " No Params" }; auto const serialized_result{translation_unit->serialize_to_str(0)}; diff --git a/tests/tdl/test-parser.cpp b/tests/tdl/test-parser.cpp index 45731c03d..62f2ea2b2 100644 --- a/tests/tdl/test-parser.cpp +++ b/tests/tdl/test-parser.cpp @@ -13,7 +13,7 @@ namespace { using spider::tdl::parser::parse_translation_unit_from_istream; using spider::tdl::parser::SourceLocation; -constexpr std::string_view cTestInput1{R"( +constexpr std::string_view cTestInput1{R"(// Start of a TDL file. This is line#1. namespace test1 { // Function with no parameters and no return type fn empty_func(); @@ -61,201 +61,212 @@ TEST_CASE("Parsing `cTestInput1`", "[tdl][parser]") { auto const& translation_unit{parse_result.value()}; constexpr std::string_view cExpectedSerializedAst{ - "[TranslationUnit]:\n" + "[TranslationUnit](2:0):\n" " StructSpecs:\n" - " [StructSpec]:\n" - " Name:Input\n" + " [StructSpec](19:0):\n" + " Name:\n" + " [Identifier](19:7):Input\n" " Fields[0]:\n" - " [NamedVar]:\n" + " [NamedVar](20:4):\n" " Id:\n" - " [Identifier]:field_0\n" + " [Identifier](20:4):field_0\n" " Type:\n" - " [Type[Primitive[Int]]]:int8\n" + " [Type[Primitive[Int]]](20:13):int8\n" " Fields[1]:\n" - " [NamedVar]:\n" + " [NamedVar](21:4):\n" " Id:\n" - " [Identifier]:field_1\n" + " [Identifier](21:4):field_1\n" " Type:\n" - " [Type[Primitive[Int]]]:int16\n" + " [Type[Primitive[Int]]](21:13):int16\n" " Fields[2]:\n" - " [NamedVar]:\n" + " [NamedVar](22:4):\n" " Id:\n" - " [Identifier]:field_2\n" + " [Identifier](22:4):field_2\n" " Type:\n" - " [Type[Primitive[Int]]]:int32\n" + " [Type[Primitive[Int]]](22:13):int32\n" " Fields[3]:\n" - " [NamedVar]:\n" + " [NamedVar](23:4):\n" " Id:\n" - " [Identifier]:field_3\n" + " [Identifier](23:4):field_3\n" " Type:\n" - " [Type[Primitive[Int]]]:int64\n" + " [Type[Primitive[Int]]](23:13):int64\n" " Fields[4]:\n" - " [NamedVar]:\n" + " [NamedVar](24:4):\n" " Id:\n" - " [Identifier]:field_4\n" + " [Identifier](24:4):field_4\n" " Type:\n" - " [Type[Primitive[Float]]]:float\n" + " [Type[Primitive[Float]]](24:13):float\n" " Fields[5]:\n" - " [NamedVar]:\n" + " [NamedVar](25:4):\n" " Id:\n" - " [Identifier]:field_5\n" + " [Identifier](25:4):field_5\n" " Type:\n" - " [Type[Primitive[Float]]]:double\n" + " [Type[Primitive[Float]]](25:13):double\n" " Fields[6]:\n" - " [NamedVar]:\n" + " [NamedVar](26:4):\n" " Id:\n" - " [Identifier]:field_6\n" + " [Identifier](26:4):field_6\n" " Type:\n" - " [Type[Primitive[Bool]]]\n" + " [Type[Primitive[Bool]]](26:13)\n" " Fields[7]:\n" - " [NamedVar]:\n" + " [NamedVar](27:4):\n" " Id:\n" - " [Identifier]:field_7\n" + " [Identifier](27:4):field_7\n" " Type:\n" - " [Type[Container[List]]]:\n" + " [Type[Container[List]]](27:13):\n" " ElementType:\n" - " [Type[Primitive[Int]]]:int8\n" + " [Type[Primitive[Int]]](27:18):int8\n" " Fields[8]:\n" - " [NamedVar]:\n" + " [NamedVar](28:4):\n" " Id:\n" - " [Identifier]:field_8\n" + " [Identifier](28:4):field_8\n" " Type:\n" - " [Type[Container[Map]]]:\n" + " [Type[Container[Map]]](28:13):\n" " KeyType:\n" - " [Type[Container[List]]]:\n" + " [Type[Container[List]]](28:17):\n" " ElementType:\n" - " [Type[Primitive[Int]]]:int8\n" + " [Type[Primitive[Int]]](28:22):int8\n" " ValueType:\n" - " [Type[Primitive[Float]]]:double\n" - " [StructSpec]:\n" - " Name:Output\n" + " [Type[Primitive[Float]]](28:29):double\n" + " [StructSpec](31:0):\n" + " Name:\n" + " [Identifier](31:7):Output\n" " Fields[0]:\n" - " [NamedVar]:\n" + " [NamedVar](33:4):\n" " Id:\n" - " [Identifier]:processed_input\n" + " [Identifier](33:4):processed_input\n" " Type:\n" - " [Type[Container[Map]]]:\n" + " [Type[Container[Map]]](33:21):\n" " KeyType:\n" - " [Type[Primitive[Int]]]:int64\n" + " [Type[Primitive[Int]]](33:25):int64\n" " ValueType:\n" - " [Type[Struct]]:\n" + " [Type[Struct]](33:32):\n" " Name:\n" - " [Identifier]:Input\n" + " [Identifier](33:32):Input\n" " Namespaces:\n" - " [Namespace]:\n" - " Name:test1\n" + " [Namespace](2:0):\n" + " Name:\n" + " [Identifier](2:10):test1\n" " Func[0]:\n" - " [Function]:\n" - " Name:empty_func\n" + " [Function](4:4):\n" + " Name:\n" + " [Identifier](4:7):empty_func\n" " Return:\n" " void\n" " No Params\n" " Func[1]:\n" - " [Function]:\n" - " Name:add\n" + " [Function](7:4):\n" + " Name:\n" + " [Identifier](7:7):add\n" " Return:\n" - " [Type[Primitive[Int]]]:int64\n" + " [Type[Primitive[Int]]](7:34):int64\n" " Params[0]:\n" - " [NamedVar]:\n" + " [NamedVar](7:11):\n" " Id:\n" - " [Identifier]:a\n" + " [Identifier](7:11):a\n" " Type:\n" - " [Type[Primitive[Int]]]:int32\n" + " [Type[Primitive[Int]]](7:14):int32\n" " Params[1]:\n" - " [NamedVar]:\n" + " [NamedVar](7:21):\n" " Id:\n" - " [Identifier]:b\n" + " [Identifier](7:21):b\n" " Type:\n" - " [Type[Primitive[Int]]]:int32\n" + " [Type[Primitive[Int]]](7:24):int32\n" " Func[2]:\n" - " [Function]:\n" - " Name:return_empty_tuple\n" + " [Function](10:4):\n" + " Name:\n" + " [Identifier](10:7):return_empty_tuple\n" " Return:\n" - " [Type[Container[Tuple]]]:Empty\n" + " [Type[Container[Tuple]]](10:31):Empty\n" " No Params\n" " Func[3]:\n" - " [Function]:\n" - " Name:return_singleton_tuple\n" + " [Function](13:4):\n" + " Name:\n" + " [Identifier](13:7):return_singleton_tuple\n" " Return:\n" - " [Type[Container[Tuple]]]:\n" + " [Type[Container[Tuple]]](13:43):\n" " Element[0]:\n" - " [Type[Primitive[Int]]]:int32\n" + " [Type[Primitive[Int]]](13:49):int32\n" " Params[0]:\n" - " [NamedVar]:\n" + " [NamedVar](13:30):\n" " Id:\n" - " [Identifier]:a\n" + " [Identifier](13:30):a\n" " Type:\n" - " [Type[Primitive[Int]]]:int64\n" + " [Type[Primitive[Int]]](13:33):int64\n" " Func[4]:\n" - " [Function]:\n" - " Name:return_tuple_of_containers\n" + " [Function](16:4):\n" + " Name:\n" + " [Identifier](16:7):return_tuple_of_containers\n" " Return:\n" - " [Type[Container[Tuple]]]:\n" + " [Type[Container[Tuple]]](16:39):\n" " Element[0]:\n" - " [Type[Container[List]]]:\n" + " [Type[Container[List]]](16:45):\n" " ElementType:\n" - " [Type[Primitive[Int]]]:int8\n" + " [Type[Primitive[Int]]](16:50):int8\n" " Element[1]:\n" - " [Type[Container[Map]]]:\n" + " [Type[Container[Map]]](16:57):\n" " KeyType:\n" - " [Type[Container[List]]]:\n" + " [Type[Container[List]]](16:61):\n" " ElementType:\n" - " [Type[Primitive[Int]]]:int8\n" + " [Type[Primitive[Int]]](16:66):int8\n" " ValueType:\n" - " [Type[Container[Map]]]:\n" + " [Type[Container[Map]]](16:73):\n" " KeyType:\n" - " [Type[Primitive[Int]]]:int64\n" + " [Type[Primitive[Int]]](16:77):int64\n" " ValueType:\n" - " [Type[Container[List]]]:\n" + " [Type[Container[List]]](16:84):\n" " ElementType:\n" - " [Type[Primitive[Int]]]:int8\n" + " [Type[Primitive[Int]]](16:89):int8\n" " No Params\n" - " [Namespace]:\n" - " Name:test2\n" + " [Namespace](36:0):\n" + " Name:\n" + " [Identifier](36:10):test2\n" " Func[0]:\n" - " [Function]:\n" - " Name:process_input\n" + " [Function](37:4):\n" + " Name:\n" + " [Identifier](37:7):process_input\n" " Return:\n" - " [Type[Struct]]:\n" + " [Type[Struct]](37:54):\n" " Name:\n" - " [Identifier]:Output\n" + " [Identifier](37:54):Output\n" " Params[0]:\n" - " [NamedVar]:\n" + " [NamedVar](37:21):\n" " Id:\n" - " [Identifier]:input\n" + " [Identifier](37:21):input\n" " Type:\n" - " [Type[Struct]]:\n" + " [Type[Struct]](37:28):\n" " Name:\n" - " [Identifier]:Input\n" + " [Identifier](37:28):Input\n" " Params[1]:\n" - " [NamedVar]:\n" + " [NamedVar](37:35):\n" " Id:\n" - " [Identifier]:task_id\n" + " [Identifier](37:35):task_id\n" " Type:\n" - " [Type[Primitive[Int]]]:int64\n" + " [Type[Primitive[Int]]](37:44):int64\n" " Func[1]:\n" - " [Function]:\n" - " Name:process_inputs\n" + " [Function](38:4):\n" + " Name:\n" + " [Identifier](38:7):process_inputs\n" " Return:\n" - " [Type[Struct]]:\n" + " [Type[Struct]](38:62):\n" " Name:\n" - " [Identifier]:Output\n" + " [Identifier](38:62):Output\n" " Params[0]:\n" - " [NamedVar]:\n" + " [NamedVar](38:22):\n" " Id:\n" - " [Identifier]:inputs\n" + " [Identifier](38:22):inputs\n" " Type:\n" - " [Type[Container[List]]]:\n" + " [Type[Container[List]]](38:30):\n" " ElementType:\n" - " [Type[Struct]]:\n" + " [Type[Struct]](38:35):\n" " Name:\n" - " [Identifier]:Input\n" + " [Identifier](38:35):Input\n" " Params[1]:\n" - " [NamedVar]:\n" + " [NamedVar](38:43):\n" " Id:\n" - " [Identifier]:task_id\n" + " [Identifier](38:43):task_id\n" " Type:\n" - " [Type[Primitive[Int]]]:int64" + " [Type[Primitive[Int]]](38:52):int64" }; auto const serialize_result{translation_unit->serialize_to_str(0)}; REQUIRE_FALSE(serialize_result.has_error());