Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions src/spider/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ set(SPIDER_TDL_SHARED_HEADERS
tdl/parser/ast/node_impl/type_impl/primitive_impl/Float.hpp
tdl/parser/ast/node_impl/type_impl/primitive_impl/Int.hpp
tdl/parser/ast/node_impl/type_impl/Struct.hpp
tdl/parser/ast/SourceLocation.hpp
Comment thread
LinZhihao-723 marked this conversation as resolved.
tdl/parser/ast/utils.hpp
CACHE INTERNAL
"spider task definition language shared header files"
Expand Down
9 changes: 8 additions & 1 deletion src/spider/tdl/parser/ast/Node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#include <ystdlib/error_handling/ErrorCode.hpp>
#include <ystdlib/error_handling/Result.hpp>

#include <spider/tdl/parser/ast/SourceLocation.hpp>

namespace spider::tdl::parser::ast {
/**
* Abstracted base class for all AST nodes in the TDL.
Expand Down Expand Up @@ -90,9 +92,13 @@ class Node {
-> ystdlib::error_handling::Result<std::string>
= 0;

[[nodiscard]] auto get_source_location() const noexcept -> SourceLocation {
return m_source_location;
}

protected:
// Constructor
Node() = default;
explicit Node(SourceLocation source_location) : m_source_location{source_location} {}

/**
* Adds a child node to this AST node.
Expand All @@ -118,6 +124,7 @@ class Node {
// Variables
std::vector<std::unique_ptr<Node>> m_children;
Node const* m_parent = nullptr;
SourceLocation m_source_location;
};
} // namespace spider::tdl::parser::ast

Expand Down
24 changes: 24 additions & 0 deletions src/spider/tdl/parser/ast/SourceLocation.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef SPIDER_TDL_AST_SOURCELOCATION_HPP
#define SPIDER_TDL_AST_SOURCELOCATION_HPP

#include <cstddef>

namespace spider::tdl::parser::ast {
class SourceLocation {
public:
// Constructor
SourceLocation(size_t line, size_t column) : m_line{line}, m_column{column} {}

// Methods
[[nodiscard]] auto get_line() const noexcept -> size_t { return m_line; }

[[nodiscard]] auto get_column() const noexcept -> size_t { return m_column; }

private:
// Variables
size_t m_line;
size_t m_column;
};
} // namespace spider::tdl::parser::ast

#endif // SPIDER_TDL_AST_SOURCELOCATION_HPP
6 changes: 4 additions & 2 deletions src/spider/tdl/parser/ast/node_impl/Function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#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.hpp>
#include <spider/tdl/parser/ast/SourceLocation.hpp>
#include <spider/tdl/parser/ast/utils.hpp>

using spider::tdl::parser::ast::node_impl::Function;
Expand All @@ -41,7 +42,8 @@ namespace spider::tdl::parser::ast::node_impl {
auto Function::create(
std::unique_ptr<Node> name,
std::unique_ptr<Node> return_type,
std::vector<std::unique_ptr<Node>> params
std::vector<std::unique_ptr<Node>> params,
SourceLocation source_location
) -> ystdlib::error_handling::Result<std::unique_ptr<Node>> {
Comment thread
LinZhihao-723 marked this conversation as resolved.
YSTDLIB_ERROR_HANDLING_TRYV(validate_child_node_type<Identifier>(name.get()));

Expand All @@ -61,7 +63,7 @@ auto Function::create(
param_names.emplace(param_name);
}

auto function{std::make_unique<Function>(Function{has_return})};
auto function{std::make_unique<Function>(Function{has_return, source_location})};
YSTDLIB_ERROR_HANDLING_TRYV(function->add_child(std::move(name)));
if (has_return) {
YSTDLIB_ERROR_HANDLING_TRYV(function->add_child(std::move(return_type)));
Expand Down
9 changes: 7 additions & 2 deletions src/spider/tdl/parser/ast/node_impl/Function.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#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.hpp>
#include <spider/tdl/parser/ast/SourceLocation.hpp>

namespace spider::tdl::parser::ast::node_impl {
class Function : public Node {
Expand All @@ -32,6 +33,7 @@ class Function : public Node {
* @param name
* @param return_type
* @param params
* @param source_location
* @return A result containing a unique pointer to a new `Function` instance with the given
* name, return type, and parameters on success, or an error code indicating the failure:
* - ErrorCodeEnum::DuplicatedParamName if `params` contains duplicated parameter names.
Expand All @@ -40,7 +42,8 @@ class Function : public Node {
[[nodiscard]] static auto create(
std::unique_ptr<Node> name,
std::unique_ptr<Node> return_type,
std::vector<std::unique_ptr<Node>> params
std::vector<std::unique_ptr<Node>> params,
SourceLocation source_location
) -> ystdlib::error_handling::Result<std::unique_ptr<Node>>;

// Methods implementing `Node`
Expand Down Expand Up @@ -101,7 +104,9 @@ class Function : public Node {

private:
// Constructor
explicit Function(bool has_return) : m_has_return{has_return} {}
Function(bool has_return, SourceLocation source_location)
: Node{source_location},
m_has_return{has_return} {}

// Methods
[[nodiscard]] auto get_num_non_param_children() const noexcept -> size_t {
Expand Down
10 changes: 7 additions & 3 deletions src/spider/tdl/parser/ast/node_impl/Identifier.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,19 @@
#include <ystdlib/error_handling/Result.hpp>

#include <spider/tdl/parser/ast/Node.hpp>
#include <spider/tdl/parser/ast/SourceLocation.hpp>

namespace spider::tdl::parser::ast::node_impl {
class Identifier : public Node {
public:
// Factory function
/**
* @param name
* @param source_location
* @return A unique pointer to a new `Identifier` instance with the given name.
*/
static auto create(std::string name) -> std::unique_ptr<Node> {
return std::make_unique<Identifier>(Identifier{std::move(name)});
static auto create(std::string name, SourceLocation source_location) -> std::unique_ptr<Node> {
return std::make_unique<Identifier>(Identifier{std::move(name), source_location});
}

// Methods implementing `Node`
Expand All @@ -32,7 +34,9 @@ class Identifier : public Node {

private:
// Constructor
explicit Identifier(std::string name) noexcept : m_name{std::move(name)} {}
Identifier(std::string name, SourceLocation source_location) noexcept
: Node{source_location},
m_name{std::move(name)} {}

// Variables
std::string m_name;
Expand Down
10 changes: 7 additions & 3 deletions src/spider/tdl/parser/ast/node_impl/NamedVar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,19 @@
#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/SourceLocation.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>> {
auto NamedVar::create(
std::unique_ptr<Node> id,
std::unique_ptr<Node> type,
SourceLocation source_location
) -> 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{})};
auto named_var{std::make_unique<NamedVar>(NamedVar{source_location})};
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;
Expand Down
7 changes: 5 additions & 2 deletions src/spider/tdl/parser/ast/node_impl/NamedVar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#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/SourceLocation.hpp>

namespace spider::tdl::parser::ast::node_impl {
/**
Expand All @@ -21,11 +22,13 @@ class NamedVar : public Node {
/**
* @param id
* @param type
* @param source_location
* @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)
[[nodiscard]] static auto
create(std::unique_ptr<Node> id, std::unique_ptr<Node> type, SourceLocation source_location)
-> ystdlib::error_handling::Result<std::unique_ptr<Node>>;

// Methods implementing `Node`
Expand All @@ -47,7 +50,7 @@ class NamedVar : public Node {

private:
// Constructor
NamedVar() = default;
explicit NamedVar(SourceLocation source_location) : Node{source_location} {}
};
} // namespace spider::tdl::parser::ast::node_impl

Expand Down
10 changes: 7 additions & 3 deletions src/spider/tdl/parser/ast/node_impl/Namespace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <spider/tdl/parser/ast/node_impl/Function.hpp>
#include <spider/tdl/parser/ast/node_impl/Identifier.hpp>
#include <spider/tdl/parser/ast/node_impl/Namespace.hpp>
#include <spider/tdl/parser/ast/SourceLocation.hpp>
#include <spider/tdl/parser/ast/utils.hpp>

using spider::tdl::parser::ast::node_impl::Namespace;
Expand All @@ -40,8 +41,11 @@ auto NamespaceErrorCodeCategory::message(Namespace::ErrorCodeEnum error_enum) co
}

namespace spider::tdl::parser::ast::node_impl {
auto Namespace::create(std::unique_ptr<Node> name, std::vector<std::unique_ptr<Node>> functions)
-> ystdlib::error_handling::Result<std::unique_ptr<Node>> {
auto Namespace::create(
std::unique_ptr<Node> name,
std::vector<std::unique_ptr<Node>> functions,
SourceLocation source_location
) -> ystdlib::error_handling::Result<std::unique_ptr<Node>> {
Comment thread
LinZhihao-723 marked this conversation as resolved.
YSTDLIB_ERROR_HANDLING_TRYV(validate_child_node_type<Identifier>(name.get()));

if (functions.empty()) {
Expand All @@ -59,7 +63,7 @@ auto Namespace::create(std::unique_ptr<Node> name, std::vector<std::unique_ptr<N
fun_names.emplace(func_name);
}

auto function{std::make_unique<Namespace>(Namespace{})};
auto function{std::make_unique<Namespace>(Namespace{source_location})};
YSTDLIB_ERROR_HANDLING_TRYV(function->add_child(std::move(name)));
for (auto& func : functions) {
YSTDLIB_ERROR_HANDLING_TRYV(function->add_child(std::move(func)));
Expand Down
12 changes: 8 additions & 4 deletions src/spider/tdl/parser/ast/node_impl/Namespace.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <spider/tdl/parser/ast/Node.hpp>
#include <spider/tdl/parser/ast/node_impl/Function.hpp>
#include <spider/tdl/parser/ast/node_impl/Identifier.hpp>
#include <spider/tdl/parser/ast/SourceLocation.hpp>

namespace spider::tdl::parser::ast::node_impl {
/**
Expand All @@ -34,16 +35,19 @@ class Namespace : public Node {
/**
* @param name
* @param functions
* @param source_location
* @return A result containing a unique pointer to a new `Namespace` instance with the given
* name and functions on success, or an error code indicating the failure:
* - ErrorCodeEnum::DuplicatedFunctionName if the `functions` contains duplicated function
* names.
* - ErrorCodeEnum::EmptyNamespace if the `functions` is empty.
* - Forwards `validate_child_node_type`'s return values.
*/
[[nodiscard]] static auto
create(std::unique_ptr<Node> name, std::vector<std::unique_ptr<Node>> functions)
-> ystdlib::error_handling::Result<std::unique_ptr<Node>>;
[[nodiscard]] static auto create(
std::unique_ptr<Node> name,
std::vector<std::unique_ptr<Node>> functions,
SourceLocation source_location
) -> ystdlib::error_handling::Result<std::unique_ptr<Node>>;

// Methods implementing `Node`
[[nodiscard]] auto serialize_to_str(size_t indentation_level) const
Expand Down Expand Up @@ -85,7 +89,7 @@ class Namespace : public Node {

private:
// Constructor
Namespace() = default;
explicit Namespace(SourceLocation source_location) : Node{source_location} {}
};
} // namespace spider::tdl::parser::ast::node_impl

Expand Down
10 changes: 7 additions & 3 deletions src/spider/tdl/parser/ast/node_impl/StructSpec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#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/SourceLocation.hpp>
#include <spider/tdl/parser/ast/utils.hpp>

using spider::tdl::parser::ast::node_impl::StructSpec;
Expand All @@ -41,8 +42,11 @@ auto StructSpecErrorCodeCategory::message(StructSpec::ErrorCodeEnum error_enum)
}

namespace spider::tdl::parser::ast::node_impl {
auto StructSpec::create(std::unique_ptr<Node> name, std::vector<std::unique_ptr<Node>> fields)
-> ystdlib::error_handling::Result<std::shared_ptr<StructSpec>> {
auto StructSpec::create(
std::unique_ptr<Node> name,
std::vector<std::unique_ptr<Node>> fields,
SourceLocation source_location
) -> ystdlib::error_handling::Result<std::shared_ptr<StructSpec>> {
YSTDLIB_ERROR_HANDLING_TRYV(validate_child_node_type<Identifier>(name.get()));

if (fields.empty()) {
Expand All @@ -60,7 +64,7 @@ auto StructSpec::create(std::unique_ptr<Node> name, std::vector<std::unique_ptr<
field_names.emplace(field_name);
}

auto struct_spec{std::make_shared<StructSpec>(StructSpec{})};
auto struct_spec{std::make_shared<StructSpec>(StructSpec{source_location})};
YSTDLIB_ERROR_HANDLING_TRYV(struct_spec->add_child(std::move(name)));
for (auto& field : fields) {
YSTDLIB_ERROR_HANDLING_TRYV(struct_spec->add_child(std::move(field)));
Expand Down
12 changes: 8 additions & 4 deletions src/spider/tdl/parser/ast/node_impl/StructSpec.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#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/SourceLocation.hpp>

namespace spider::tdl::parser::ast::node_impl {
/**
Expand All @@ -34,16 +35,19 @@ class StructSpec : public Node {
/**
* @param name
* @param fields
* @param source_location
* @return A result containing a shared pointer to a new `StructSpec` instance with the name and
* fields on success, or an error code indicating the failure:
* - StructSpec::ErrorCodeEnum::DuplicatedFieldName if the `fields` contains duplicated field
* names.
* - StructSpec::ErrorCodeEnum::EmptyStruct if the `fields` is empty.
* - Forwards `validate_child_node_type`'s return values.
*/
[[nodiscard]] static auto
create(std::unique_ptr<Node> name, std::vector<std::unique_ptr<Node>> fields)
-> ystdlib::error_handling::Result<std::shared_ptr<StructSpec>>;
[[nodiscard]] static auto create(
std::unique_ptr<Node> name,
std::vector<std::unique_ptr<Node>> fields,
SourceLocation source_location
) -> ystdlib::error_handling::Result<std::shared_ptr<StructSpec>>;

// Methods implementing `Node`
[[nodiscard]] auto serialize_to_str(size_t indentation_level) const
Expand Down Expand Up @@ -85,7 +89,7 @@ class StructSpec : public Node {

private:
// Constructor
StructSpec() = default;
explicit StructSpec(SourceLocation source_location) : Node{source_location} {}
};
} // namespace spider::tdl::parser::ast::node_impl

Expand Down
7 changes: 6 additions & 1 deletion src/spider/tdl/parser/ast/node_impl/Type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@
#define SPIDER_TDL_PARSER_AST_NODE_IMPL_TYPE_HPP

#include <spider/tdl/parser/ast/Node.hpp>
#include <spider/tdl/parser/ast/SourceLocation.hpp>

namespace spider::tdl::parser::ast::node_impl {
// Abstract base class for all type nodes in the AST.
class Type : public Node {};
class Type : public Node {
protected:
// Constructor
explicit Type(SourceLocation source_location) : Node{source_location} {}
};
} // namespace spider::tdl::parser::ast::node_impl

#endif // SPIDER_TDL_PARSER_AST_NODE_IMPL_TYPE_HPP
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@
#define SPIDER_TDL_PARSER_AST_NODE_IMPL_TYPE_IMPL_CONTAINER_HPP

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

namespace spider::tdl::parser::ast::node_impl::type_impl {
// Abstract base class for all container type nodes in the AST.
class Container : public Type {};
class Container : public Type {
protected:
// Constructor
explicit Container(SourceLocation source_location) : Type{source_location} {}
};
} // namespace spider::tdl::parser::ast::node_impl::type_impl

#endif // SPIDER_TDL_PARSER_AST_NODE_IMPL_TYPE_IMPL_CONTAINER_HPP
Loading