Skip to content

Commit

Permalink
Partial implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
obhi-d committed Dec 22, 2024
1 parent 4c1d404 commit 31702bf
Show file tree
Hide file tree
Showing 18 changed files with 1,444 additions and 537 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ message("Target name: ${ACL_TARGET_NAME}")
add_library( ${ACL_TARGET_NAME}

"${ACL_SOURCE_BUILD_DIR}/acl/dsl/microexpr.cpp"
"${ACL_SOURCE_BUILD_DIR}/acl/dsl/yaml.cpp"
"${ACL_SOURCE_BUILD_DIR}/acl/dsl/yaml_lexer_impl.cpp"
"${ACL_SOURCE_BUILD_DIR}/acl/dsl/yaml_parser_impl.cpp"
"${ACL_SOURCE_BUILD_DIR}/acl/scheduler/scheduler.cpp"
Expand Down
3 changes: 3 additions & 0 deletions CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@
"CMAKE_BUILD_TYPE": "Debug",
"CMAKE_EXPORT_COMPILE_COMMANDS": "ON",
"CMAKE_INSTALL_PREFIX": "${sourceDir}/install/${presetName}",
"ACL_BISON_EXE": "C:\\repos\\winflexbison\\win_bison.exe",
"ACL_FLEX_EXE": "C:\\repos\\winflexbison\\win_flex.exe",
"ACL_GENERATE_YAML_PARSER": "ON",
"ASAN_ENABLED": "ON",
"ACL_BUILD_TESTS": "ON"
},
Expand Down
1 change: 0 additions & 1 deletion compile_commands.json

This file was deleted.

248 changes: 72 additions & 176 deletions include/acl/dsl/yaml.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,252 +4,148 @@
#include <cassert>
#include <compare>
#include <cstdint>
#include <format>
#include <ostream>
#include <stdexcept>
#include <string>
#include <string_view>

namespace acl::yaml
{

struct position
struct location_type
{
uint32_t line = 1;
uint32_t character = 1;
inline auto operator<=>(position const&) const noexcept = default;

inline friend std::ostream& operator<<(std::ostream& yyo, position const& l) noexcept
uint32_t begin = 0;
uint32_t end = 0;
inline friend std::ostream& operator<<(std::ostream& yyo, location_type const& l) noexcept
{
yyo << l.line << ':' << l.character;
return yyo;
return yyo << l.begin << ':' << l.end;
}
};

class location
{
public:
location() noexcept = default;
location(std::string_view source_name) noexcept : source_name(source_name) {}
inline void step() noexcept
{
begin = end;
}

inline void columns(std::uint32_t l) noexcept
{
end.character += l;
}

inline void lines(std::uint32_t l) noexcept
{
end.line += l;
end.character = 0;
}

inline operator std::string() const noexcept
{
std::string value = std::string(source_name.empty() ? "istream" : source_name);
value += "(" + std::to_string(begin.line) + ":" + std::to_string(begin.character) + "-" + std::to_string(end.line) +
":" + std::to_string(end.character) + "): ";
return value;
}

inline friend std::ostream& operator<<(std::ostream& yyo, location const& l) noexcept

{
std::string value = std::string(l.source_name.empty() ? "istream" : l.source_name);
if (l.begin == l.end)
yyo << "<" << value << '-' << l.begin << ">";
else
yyo << "<" << value << '-' << l.begin << "-" << l.end << ">";
return yyo;
}

position next_line() const noexcept
{
return position{begin.line + 1, 0};
}

inline auto operator<=>(location const&) const noexcept = default;

std::string_view source_name;
position begin;
position end;
};

struct string_slice
{
uint32_t start = 0;
uint32_t count = 0;

auto operator<=>(string_slice const&) const = default;
};

using string_slice_array = acl::small_vector<string_slice, 8>;

class context
{
public:
virtual ~context() noexcept = default;
virtual void start_mapping(std::string_view slice) = 0;
virtual void add_mapping_value(std::string_view slice) = 0;
virtual void end_mapping() = 0;
virtual void start_sequence() = 0;
virtual void end_sequence() = 0;
virtual void start_sequence_item() = 0;
virtual void add_sequence_item(std::string_view slice) = 0;
virtual void end_sequence_item() = 0;
virtual ~context() noexcept = default;
virtual void begin_object() = 0;
virtual void end_object() = 0;
virtual void begin_array() = 0;
virtual void end_array() = 0;
virtual void set_key(std::string_view slice) = 0;
virtual void set_value(std::string_view slice) = 0;
};

;
class istream
{
public:
inline void start_mapping(string_slice slice) const
{
if (handler_)
handler_->start_mapping(get_view(slice));
}

inline void add_mapping_value(string_slice slice) const
{
if (handler_)
handler_->add_mapping_value(get_view(slice));
}

inline void end_mapping() const
{
if (handler_)
handler_->end_mapping();
}

inline void start_sequence() const
{
if (handler_)
handler_->start_sequence();
}

inline void end_sequence() const
{
if (handler_)
handler_->end_sequence();
}

inline void start_sequence_item() const
{
if (handler_)
handler_->start_sequence_item();
}
using indent = uint32_t;

inline void add_sequence_item(string_slice slice) const
{
if (handler_)
handler_->add_sequence_item(get_view(slice));
}
void set_idention_level(uint16_t level);
void set_idention_level(string_slice level);
void add_dash_indention(string_slice dash_level);

inline void end_sequence_item() const
{
if (handler_)
handler_->end_sequence_item();
}
void add_new_mapping(string_slice key);
void add_new_mapped_sequence(string_slice key);
void add_mapping_value(string_slice value);
void add_mapping_value(string_slice_array value);
void begin_array();
void end_array();
void add_new_sequence_value(string_slice value);
void add_new_sequence_value(string_slice_array value);

inline std::string_view get_file_name() const noexcept
{
return source_.source_name;
}
void close_all_mappings();

inline void throw_error(location const& loc, std::string_view error, std::string_view context) const
inline void throw_error(location_type const& loc, std::string_view error, std::string_view context) const
{
auto str = (std::string)loc;
str += '[';
str += context;
str += ']';
str += error;

throw std::runtime_error(str);
throw std::runtime_error(std::format("[{}] token@{}: \"{}\" - {}", context, loc.begin,
contents.substr(loc.begin, loc.end - loc.begin), error));
}

inline auto const& location() const noexcept
inline auto location() const noexcept
{
return source_;
return location_type{current_token.start, current_token.start + current_token.count};
}

inline uint32_t peek_indent() const noexcept
inline string_slice get_view() const
{
return indent_stack.empty() ? 0 : indent_stack.back();
return current_token;
}

inline void push_indent(uint32_t indent) noexcept
inline string_slice get_quoted_string() const
{
indent_stack.push_back(indent);
return {current_token.start + 1, current_token.count - 2};
}

inline void pop_indent() noexcept
inline void set_token(uint32_t length) noexcept
{
indent_stack.pop_back();
}

inline string_slice get_view(uint32_t length) const
{
return {cursor_, length};
}

inline void move_ahead(uint32_t length) noexcept
{
cursor_ += length;
current_token.start = token_cursor;
current_token.count = length;
token_cursor += length;
}

inline int read(char* istream, int siz) noexcept
{
if (cursor_ >= contents_.size())
if (read_cursor >= contents.size())
return 0;

siz = std::min(siz, static_cast<int>(contents_.size() - cursor_));
contents_.copy(istream, siz, cursor_);
cursor_ += siz;
siz = std::min(siz - 1, static_cast<int>(contents.size() - read_cursor));
contents.copy(istream, siz, read_cursor);
istream[siz] = 0;
read_cursor += siz;
return siz;
}

inline auto const& get_source() const
{
return source_;
}

inline void* get_scanner() const noexcept
{
return scanner;
}

inline void set_current_key(string_slice slice) noexcept
{
current_key_ = slice;
}

inline string_slice get_current_key() const noexcept
{
return current_key_;
}

inline void set_handler(context* handler) noexcept
{
handler_ = handler;
handler = handler;
}

istream(std::string_view source, std::string_view content) noexcept : source_(source), contents_(content) {}
istream(std::string_view content) noexcept : contents(content) {}

void parse(context& handler_);
void parse(context& handler);
void begin_scan();
void end_scan();

private:
inline std::string_view get_view(string_slice slice) const noexcept
{
return contents_.substr(slice.start, slice.count);
return contents.substr(slice.start, slice.count);
}

void* scanner = nullptr;
acl::small_vector<uint32_t, 8> indent_stack;
enum class indent_type : uint8_t
{
none,
object,
array,
};

void* scanner = nullptr;
indent current_indent_level = {};
indent test_indent_level = {};

acl::small_vector<indent_type, 16> indent_stack;

string_slice current_key_;
uint32_t cursor_ = 0;
context* handler_ = nullptr;
yaml::location source_;
std::string_view contents_;
string_slice current_key;
string_slice current_token;
uint32_t read_cursor = 0;
uint32_t token_cursor = 0;
context* handler = nullptr;
std::string_view contents;
};

} // namespace acl::yaml
} // namespace acl::yaml
7 changes: 3 additions & 4 deletions include/acl/serializers/binary_input_serializer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,7 @@ class binary_input_serializer
[]<bool flag = false>()
{
static_assert(flag, "This type is not serializable");
}
();
}();
return false;
}
}
Expand Down Expand Up @@ -183,7 +182,7 @@ class binary_input_serializer
read(count);

detail::reserve(obj, count);
if constexpr (!detail::HasEmplaceFn<Class, detail::array_value_type<Class>>)
if constexpr (!detail::ContainerCanAppendValue<Class>)
detail::resize(obj, count);
if constexpr (detail::LinearArrayLike<Class, Serializer> && has_fast_path)
{
Expand All @@ -200,7 +199,7 @@ class binary_input_serializer
get().error(type_name<Class>(), make_error_code(serializer_error::corrupt_array_item));
return false;
}
if constexpr (detail::HasEmplaceFn<Class, detail::array_value_type<Class>>)
if constexpr (detail::ContainerCanAppendValue<Class>)
{
detail::emplace(obj, std::move(stream_val));
}
Expand Down
4 changes: 2 additions & 2 deletions include/acl/serializers/binary_output_serializer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class binary_output_serializer
write_string_view_transformable(obj);
else if constexpr (detail::TransformToString<Class>)
write_string_transformable(obj);
else if constexpr (detail::StringLike<Class>)
else if constexpr (detail::ContainerIsStringLike<Class>)
write_string(obj);
else if constexpr (detail::BoolLike<Class>)
write_bool(obj);
Expand Down Expand Up @@ -301,7 +301,7 @@ concept OutputSerializable =
detail::BoundClass<Class> || detail::OutputSerializableClass<Class, detail::empty_output_streamer> ||
detail::TupleLike<Class> || detail::ContainerLike<Class> || detail::VariantLike<Class> ||
detail::CastableToStringView<Class> || detail::CastableToString<Class> || detail::TransformToStringView<Class> ||
detail::TransformToString<Class> || detail::StringLike<Class> || detail::BoolLike<Class> ||
detail::TransformToString<Class> || detail::ContainerIsStringLike<Class> || detail::BoolLike<Class> ||
detail::IntegerLike<Class> || detail::EnumLike<Class> || detail::FloatLike<Class> || detail::PointerLike<Class> ||
detail::OptionalLike<Class> || detail::MonostateLike<Class>;

Expand Down
Loading

0 comments on commit 31702bf

Please sign in to comment.