Skip to content

feat(tdl): Add Tuple AST node. - #189

Merged
LinZhihao-723 merged 3 commits into
y-scope:mainfrom
LinZhihao-723:ast-tuple
Aug 11, 2025
Merged

feat(tdl): Add Tuple AST node.#189
LinZhihao-723 merged 3 commits into
y-scope:mainfrom
LinZhihao-723:ast-tuple

Conversation

@LinZhihao-723

@LinZhihao-723 LinZhihao-723 commented Aug 11, 2025

Copy link
Copy Markdown
Member

Description

This PR introduces the Tuple container type. A tuple can contain a list of element types. We will use a Tuple as a container to represent return values with multiple types.

Notice that a tuple can be empty, meaning that it contains no elements.

Checklist

  • The PR satisfies the contribution guidelines.
  • This is a breaking change and that has been indicated in the PR title, OR this isn't a
    breaking change.
  • Necessary docs have been updated, OR no docs need to be updated.

Validation performed

  • Ensure all workflows pass.
  • Add unit tests for Tuple nodes.

Summary by CodeRabbit

  • New Features

    • Added support for a Tuple container type with creation and readable serialization for empty and multi-element tuples.
    • Improves consistency of container-type output across tools that display TDL structures.
  • Tests

    • Added tests covering Tuple creation, empty and populated cases, and exact serialization verification.

@LinZhihao-723
LinZhihao-723 requested review from a team and sitaowang1998 as code owners August 11, 2025 00:51
@coderabbitai

coderabbitai Bot commented Aug 11, 2025

Copy link
Copy Markdown
Contributor

Walkthrough

Adds a new Tuple container node to the TDL AST: CMake exports Tuple sources/headers, Tuple class and implementation (create, serialize_to_str, is_empty) are added, and unit tests are extended to exercise empty and populated Tuple serialization and child counts.

Changes

Cohort / File(s) Summary
Build configuration
src/spider/CMakeLists.txt
Adds tdl/parser/ast/node_impl/type_impl/container_impl/Tuple.cpp to SPIDER_TDL_SHARED_SOURCES and .../Tuple.hpp to SPIDER_TDL_SHARED_HEADERS.
Tuple type (header + impl)
src/spider/tdl/parser/ast/node_impl/type_impl/container_impl/Tuple.hpp, src/spider/tdl/parser/ast/node_impl/type_impl/container_impl/Tuple.cpp
Adds Tuple class deriving from Container with static create(std::vector<std::unique_ptr<Node>>) factory, serialize_to_str(size_t) override, and is_empty(); implements child-type validation, child attachment (move), and formatted serialization using Result-based error propagation.
Tests
tests/tdl/test-parser-ast.cpp
Adds includes and test cases for Tuple: verifies creation success, child counts (empty = 0, populated = 3) and exact serialization output for both empty and populated tuples.

Sequence Diagram(s)

sequenceDiagram
  participant Test as Test Code
  participant TupleFactory as Tuple::create
  participant Validator as validate_child_node_type
  participant Container as Container (add_child)
  participant Result as Result<unique_ptr<Node>>

  Test->>TupleFactory: create(vector<unique_ptr<Node>> elements)
  loop each element
    TupleFactory->>Validator: validate_child_node_type(element)
    Validator-->>TupleFactory: ok / error
  end
  TupleFactory->>Container: add_child(element) (move)
  TupleFactory-->>Result: Result<unique_ptr<Tuple>>
Loading
sequenceDiagram
  participant Test as Test Code
  participant TupleNode as Tuple::serialize_to_str
  participant Children as Child Nodes
  participant Result as Result<string>

  Test->>TupleNode: serialize_to_str(indentation)
  alt no children
    TupleNode-->>Result: Result<string>("[Type[Container[Tuple]]]:Empty")
  else has children
    TupleNode->>Children: visit_children -> serialize_to_str(indentation+1)
    Children-->>TupleNode: serialized parts / errors
    TupleNode-->>Result: Result<string>(joined formatted output)
  end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested reviewers

  • sitaowang1998

Note

🔌 MCP (Model Context Protocol) integration is now available in Early Access!

Pro users can now connect to remote MCP servers under the Integrations page to get reviews and chat conversations that understand additional development context.


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f477c7d and eaf21f2.

📒 Files selected for processing (2)
  • src/spider/tdl/parser/ast/node_impl/type_impl/container_impl/Tuple.cpp (1 hunks)
  • tests/tdl/test-parser-ast.cpp (4 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • src/spider/tdl/parser/ast/node_impl/type_impl/container_impl/Tuple.cpp
  • tests/tdl/test-parser-ast.cpp
✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (4)
tests/tdl/test-parser-ast.cpp (1)

239-252: Consider a clearer API for empty tuples

Tuple::create({}) works here (empty initializer_list implicitly constructs an empty vector), but it’s a bit subtle. A dedicated no-arg factory would make intent explicit and avoid relying on that implicit conversion.

Follow-up in Tuple.hpp/cpp below adds:

  • static create() -> Result<std::unique_ptr>

Also updates this test to call Tuple::create() directly.

Proposed diffs are provided in the Tuple.hpp/cpp comments; a small test tweak is included there as well.

src/spider/tdl/parser/ast/node_impl/type_impl/container_impl/Tuple.hpp (1)

24-33: Optional: Add a dedicated factory for empty tuples

A no-arg create() improves clarity for empty tuples and avoids relying on implicit vector construction from {}.

Apply this header diff:

 class Tuple : public Container {
 public:
     // Factory function
@@
     [[nodiscard]] static auto create(std::vector<std::unique_ptr<Node>> elements)
             -> ystdlib::error_handling::Result<std::unique_ptr<Node>>;
+
+    // Convenience factory for an empty Tuple
+    [[nodiscard]] static auto create()
+            -> ystdlib::error_handling::Result<std::unique_ptr<Node>>;
@@
     [[nodiscard]] auto is_empty() const -> bool { return 0 == get_num_children(); }

And in tests, switch Tuple::create({}) to Tuple::create() (see test diff in the tests file comment).

src/spider/tdl/parser/ast/node_impl/type_impl/container_impl/Tuple.cpp (2)

19-30: Factory logic is sound; add empty-tuple overload for ergonomics

Validation and move semantics are correct. To support a clearer empty-tuple creation, add a no-arg create().

Apply this addition:

 auto Tuple::create(std::vector<std::unique_ptr<Node>> elements)
         -> ystdlib::error_handling::Result<std::unique_ptr<Node>> {
@@
     return tuple;
 }
+
+auto Tuple::create()
+        -> ystdlib::error_handling::Result<std::unique_ptr<Node>> {
+    return std::make_unique<Tuple>(Tuple{});
+}

40-61: Minor perf nit: pre-allocate serialized_children capacity

Avoids potential reallocations when serialising many elements.

Apply this diff:

-    std::vector<std::string> serialized_children;
+    std::vector<std::string> serialized_children;
+    serialized_children.reserve(get_num_children());

Optional readability: consider naming serialized_children -> serialized_elements to reflect Tuple semantics.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between a373dfd and f477c7d.

📒 Files selected for processing (4)
  • src/spider/CMakeLists.txt (2 hunks)
  • src/spider/tdl/parser/ast/node_impl/type_impl/container_impl/Tuple.cpp (1 hunks)
  • src/spider/tdl/parser/ast/node_impl/type_impl/container_impl/Tuple.hpp (1 hunks)
  • tests/tdl/test-parser-ast.cpp (4 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (2)
src/spider/tdl/parser/ast/node_impl/type_impl/container_impl/Tuple.cpp (2)
src/spider/tdl/parser/ast/node_impl/type_impl/container_impl/Tuple.hpp (2)
  • elements (24-25)
  • indentation_level (28-29)
src/spider/tdl/parser/ast/Node.hpp (2)
  • visit_children (73-74)
  • child (103-104)
tests/tdl/test-parser-ast.cpp (4)
src/spider/tdl/parser/ast/node_impl/type_impl/container_impl/Tuple.hpp (2)
  • Tuple (36-36)
  • elements (24-25)
src/spider/tdl/parser/ast/node_impl/type_impl/container_impl/Tuple.cpp (2)
  • create (19-30)
  • create (19-20)
src/spider/tdl/parser/ast/node_impl/type_impl/container_impl/List.cpp (2)
  • create (16-23)
  • create (16-17)
src/spider/tdl/parser/ast/node_impl/type_impl/container_impl/Map.cpp (2)
  • create (69-84)
  • create (69-70)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
  • GitHub Check: non-storage-unit-tests (ubuntu-22.04)
  • GitHub Check: non-storage-unit-tests (ubuntu-24.04)
  • GitHub Check: lint
🔇 Additional comments (2)
src/spider/CMakeLists.txt (1)

204-204: CMake wiring for Tuple looks correct

Tuple.cpp and Tuple.hpp are added consistently alongside List/Map. No issues from a build/export standpoint.

Also applies to: 223-223

tests/tdl/test-parser-ast.cpp (1)

3-7: Includes and Tuple alias are appropriate

New headers and using-alias for Tuple are consistent with existing patterns.

Also applies to: 20-20, 35-35

Comment thread tests/tdl/test-parser-ast.cpp
@LinZhihao-723
LinZhihao-723 merged commit 662be0b into y-scope:main Aug 11, 2025
6 checks passed
@LinZhihao-723
LinZhihao-723 deleted the ast-tuple branch August 11, 2025 13:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants