Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
fe0650f
Add ast base implementation.
LinZhihao-723 Aug 7, 2025
fc5a746
Fix
LinZhihao-723 Aug 7, 2025
8dad68a
Remove children vector construction...
LinZhihao-723 Aug 7, 2025
208b76f
Apply coderabbit comments.
LinZhihao-723 Aug 7, 2025
c2672f4
Add serialization method.
LinZhihao-723 Aug 7, 2025
7e8c46f
Remove default constructor.
LinZhihao-723 Aug 7, 2025
802f7d4
Use consistently.
LinZhihao-723 Aug 7, 2025
f8b6898
Implement identifier.
LinZhihao-723 Aug 7, 2025
dbbc492
Fix cmake format.
LinZhihao-723 Aug 7, 2025
fea5b89
Update the header guard.
LinZhihao-723 Aug 7, 2025
1a26b6e
Update factory function's return type...
LinZhihao-723 Aug 7, 2025
9f94867
Update serialization format.
LinZhihao-723 Aug 7, 2025
4105889
Implement types.
LinZhihao-723 Aug 7, 2025
08ed47b
Merge branch 'main' into ast-identifier
LinZhihao-723 Aug 7, 2025
1058e9e
Add unit tests.
LinZhihao-723 Aug 7, 2025
4cb87dc
Merge branch 'main' into ast-type
LinZhihao-723 Aug 7, 2025
357df42
Update src/spider/CMakeLists.txt
LinZhihao-723 Aug 7, 2025
625c846
Fix the header guard.
LinZhihao-723 Aug 7, 2025
744ccf9
Fix typo.
LinZhihao-723 Aug 7, 2025
345788a
Merge branch 'ast-identifier' into ast-type
LinZhihao-723 Aug 7, 2025
60bfe23
Merge branch 'ast-type' of https://github.com/LinZhihao-723/spider in…
LinZhihao-723 Aug 7, 2025
e6283a5
Fix the docstring.
LinZhihao-723 Aug 7, 2025
0bea88e
WIP.
LinZhihao-723 Aug 7, 2025
a7931e0
Merge branch 'main' into ast-type
LinZhihao-723 Aug 8, 2025
1bfaf5a
Apply code review comments.
LinZhihao-723 Aug 8, 2025
1bca375
Implement NamedVar
LinZhihao-723 Aug 8, 2025
e299270
Merge branch 'ast-type' into ast-namaed-var
LinZhihao-723 Aug 8, 2025
e866db1
Add unit tests
LinZhihao-723 Aug 8, 2025
3d6664d
Merge oss-main
LinZhihao-723 Aug 8, 2025
7a2e7b3
Fix unit tests
LinZhihao-723 Aug 8, 2025
7216617
Update src/spider/tdl/parser/ast/node_impl/NamedVar.hpp
LinZhihao-723 Aug 8, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/spider/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ add_library(spider::spider ALIAS spider_client)
set(SPIDER_TDL_SHARED_SOURCES
tdl/parser/ast/Node.cpp
tdl/parser/ast/node_impl/Identifier.cpp
tdl/parser/ast/node_impl/NamedVar.cpp
tdl/parser/ast/node_impl/type_impl/container_impl/List.cpp
tdl/parser/ast/node_impl/type_impl/container_impl/Map.cpp
tdl/parser/ast/node_impl/type_impl/primitive_impl/Bool.cpp
Expand All @@ -213,6 +214,7 @@ set(SPIDER_TDL_SHARED_HEADERS
tdl/parser/ast/FloatSpec.hpp
tdl/parser/ast/IntSpec.hpp
tdl/parser/ast/node_impl/Identifier.hpp
tdl/parser/ast/node_impl/NamedVar.hpp
tdl/parser/ast/node_impl/Type.hpp
tdl/parser/ast/node_impl/type_impl/Container.hpp
tdl/parser/ast/node_impl/type_impl/container_impl/List.hpp
Expand Down
2 changes: 1 addition & 1 deletion src/spider/tdl/parser/ast/node_impl/Identifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
namespace spider::tdl::parser::ast::node_impl {
auto Identifier::serialize_to_str(size_t indentation_level) const
-> ystdlib::error_handling::Result<std::string> {
return fmt::format("{}[Identifier]: {}", create_indentation(indentation_level), m_name);
return fmt::format("{}[Identifier]:{}", create_indentation(indentation_level), m_name);
}
} // namespace spider::tdl::parser::ast::node_impl
39 changes: 39 additions & 0 deletions src/spider/tdl/parser/ast/node_impl/NamedVar.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include "NamedVar.hpp"

#include <cstddef>
#include <memory>
#include <string>
#include <utility>

#include <fmt/format.h>
#include <ystdlib/error_handling/Result.hpp>

#include <spider/tdl/parser/ast/Node.hpp>
#include <spider/tdl/parser/ast/node_impl/Identifier.hpp>
#include <spider/tdl/parser/ast/node_impl/Type.hpp>
#include <spider/tdl/parser/ast/utils.hpp>

namespace spider::tdl::parser::ast::node_impl {
auto NamedVar::create(std::unique_ptr<Node> id, std::unique_ptr<Node> type)
-> ystdlib::error_handling::Result<std::unique_ptr<Node>> {
YSTDLIB_ERROR_HANDLING_TRYV(validate_child_node_type<Identifier>(id.get()));
YSTDLIB_ERROR_HANDLING_TRYV(validate_child_node_type<Type>(type.get()));

auto named_var{std::make_unique<NamedVar>(NamedVar{})};
YSTDLIB_ERROR_HANDLING_TRYV(named_var->add_child(std::move(id)));
YSTDLIB_ERROR_HANDLING_TRYV(named_var->add_child(std::move(type)));
return named_var;
}

auto NamedVar::serialize_to_str(size_t indentation_level) const
-> ystdlib::error_handling::Result<std::string> {
return fmt::format(
"{}[NamedVar]:\n{}Id:\n{}\n{}Type:\n{}",
create_indentation(indentation_level),
create_indentation(indentation_level + 1),
YSTDLIB_ERROR_HANDLING_TRYX(get_id()->serialize_to_str(indentation_level + 2)),
create_indentation(indentation_level + 1),
YSTDLIB_ERROR_HANDLING_TRYX(get_type()->serialize_to_str(indentation_level + 2))
);
}
} // namespace spider::tdl::parser::ast::node_impl
54 changes: 54 additions & 0 deletions src/spider/tdl/parser/ast/node_impl/NamedVar.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#ifndef SPIDER_TDL_PARSER_AST_NODE_IMPL_NAMEDVAR_HPP
#define SPIDER_TDL_PARSER_AST_NODE_IMPL_NAMEDVAR_HPP

#include <cstddef>
#include <memory>
#include <string>

#include <ystdlib/error_handling/Result.hpp>

#include <spider/tdl/parser/ast/Node.hpp>
#include <spider/tdl/parser/ast/node_impl/Identifier.hpp>
#include <spider/tdl/parser/ast/node_impl/Type.hpp>

namespace spider::tdl::parser::ast::node_impl {
/**
* Represents a named variable in the AST. A named variable contains an identifier and a type.
*/
class NamedVar : public Node {
public:
// Factory function
/**
* @param id
* @param type
* @return A result containing a unique pointer to a new `NamedVar` instance with the given name
* on success, or an error code indicating the failure:
* - Forwards `validate_child_node_type`'s return values.
*/
[[nodiscard]] static auto create(std::unique_ptr<Node> id, std::unique_ptr<Node> type)
-> ystdlib::error_handling::Result<std::unique_ptr<Node>>;

// Methods implementing `Node`
[[nodiscard]] auto serialize_to_str(size_t indentation_level) const
-> ystdlib::error_handling::Result<std::string> override;

// Methods
[[nodiscard]] auto get_id() const noexcept -> Identifier const* {
// The factory function ensures that the first child is of type `Identifier`.
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-static-cast-downcast)
return static_cast<Identifier const*>(get_child_unsafe(0));
}

[[nodiscard]] auto get_type() const noexcept -> Type const* {
// The factory function ensures that the second child is of type `Type`.
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-static-cast-downcast)
return static_cast<Type const*>(get_child_unsafe(1));
}

private:
// Constructor
NamedVar() = default;
};
} // namespace spider::tdl::parser::ast::node_impl

#endif // SPIDER_TDL_PARSER_AST_NODE_IMPL_NAMEDVAR_HPP
33 changes: 32 additions & 1 deletion tests/tdl/test-parser-ast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <spider/tdl/parser/ast/IntSpec.hpp>
#include <spider/tdl/parser/ast/Node.hpp>
#include <spider/tdl/parser/ast/node_impl/Identifier.hpp>
#include <spider/tdl/parser/ast/node_impl/NamedVar.hpp>
#include <spider/tdl/parser/ast/node_impl/type_impl/container_impl/List.hpp>
#include <spider/tdl/parser/ast/node_impl/type_impl/container_impl/Map.hpp>
#include <spider/tdl/parser/ast/node_impl/type_impl/primitive_impl/Bool.hpp>
Expand All @@ -24,6 +25,7 @@ TEST_CASE("test-ast-node", "[tdl][ast][Node]") {
using spider::tdl::parser::ast::IntSpec;
using spider::tdl::parser::ast::Node;
using spider::tdl::parser::ast::node_impl::Identifier;
using spider::tdl::parser::ast::node_impl::NamedVar;
using spider::tdl::parser::ast::node_impl::type_impl::container_impl::List;
using spider::tdl::parser::ast::node_impl::type_impl::container_impl::Map;
using spider::tdl::parser::ast::node_impl::type_impl::primitive_impl::Bool;
Expand All @@ -33,7 +35,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]:test_name"};

auto const node{Identifier::create(std::string{cTestName})};
auto const* identifier{dynamic_cast<Identifier const*>(node.get())};
Expand Down Expand Up @@ -200,6 +202,35 @@ TEST_CASE("test-ast-node", "[tdl][ast][Node]") {
REQUIRE(unsupported_list_key_type_map_result.error()
== Map::ErrorCode{Map::ErrorCodeEnum::UnsupportedKeyType});
}

SECTION("NamedVar") {
auto id_result{Identifier::create("TestId")};
auto map_result{Map::create(Int::create(IntSpec::Int64), Float::create(FloatSpec::Double))};
REQUIRE_FALSE(map_result.has_error());
auto named_var_result{
NamedVar::create(std::move(id_result), std::move(map_result.value()))
};
REQUIRE_FALSE(named_var_result.has_error());
auto const* named_var_node{dynamic_cast<NamedVar const*>(named_var_result.value().get())};
REQUIRE(nullptr != named_var_node);

REQUIRE(named_var_node->get_num_children() == 2);

constexpr std::string_view cExpectedSerializedResult{
"[NamedVar]:\n"
" Id:\n"
" [Identifier]:TestId\n"
" Type:\n"
" [Type[Container[Map]]]:\n"
" KeyType:\n"
" [Type[Primitive[Int]]]:int64\n"
" ValueType:\n"
" [Type[Primitive[Float]]]:double"
};
auto const serialized_result{named_var_node->serialize_to_str(0)};
REQUIRE_FALSE(serialized_result.has_error());
REQUIRE(serialized_result.value() == cExpectedSerializedResult);
}
}
} // namespace

Expand Down