Skip to content

Commit

Permalink
Merge pull request #3 from obhi-d/upstream
Browse files Browse the repository at this point in the history
Upstream
  • Loading branch information
obhi-d authored Dec 22, 2024
2 parents 31702bf + fdb9060 commit 1f0c6e8
Show file tree
Hide file tree
Showing 9 changed files with 295 additions and 5,587 deletions.
27 changes: 0 additions & 27 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ set(CMAKE_CXX_STANDARD 20)
option(ACL_BUILD_TESTS "Build the unit tests when BUILD_TESTING is enabled." OFF)
option(ASAN_ENABLED "Build this target with AddressSanitizer" OFF)
option(ACL_REC_STATS "No stats for allocator" OFF)
option(ACL_GENERATE_YAML_PARSER "Generate parsers." OFF)
option(ACL_USE_SSE2 "Math library should use SSE2." OFF)
option(ACL_USE_SSE3 "Math library should use SSE3." OFF)
option(ACL_USE_AVX "Math library should use AVX." OFF)
Expand All @@ -25,30 +24,6 @@ set(ACL_SOURCE_BUILD_DIR "${PROJECT_SOURCE_DIR}/src")

include(GNUInstallDirs)

if (ACL_GENERATE_YAML_PARSER OR (NOT EXISTS "${ACL_SOURCE_BUILD_DIR}/acl/dsl/yaml_parser_impl.cpp") OR (NOT EXISTS "${ACL_SOURCE_BUILD_DIR}/acl/dsl/yaml_lexer_impl.cpp"))
add_custom_command(
OUTPUT
"${ACL_SOURCE_BUILD_DIR}/acl/dsl/yaml_parser_impl.cpp"

COMMAND ${CMAKE_COMMAND} -E echo "Generating yaml_parser_impl.cpp"
COMMAND ${ACL_BISON_EXE} -o "${ACL_SOURCE_BUILD_DIR}/acl/dsl/yaml_parser_impl.cpp" "${ACL_SOURCE_BUILD_DIR}/acl/dsl/yaml.yy"

DEPENDS
"${ACL_SOURCE_BUILD_DIR}/acl/dsl/yaml.yy"
)

add_custom_command(
OUTPUT
"${ACL_SOURCE_BUILD_DIR}/acl/dsl/yaml_lexer_impl.cpp"

COMMAND ${CMAKE_COMMAND} -E echo "Generating yaml_lexer_impl.cpp"
COMMAND ${ACL_FLEX_EXE} -o "${ACL_SOURCE_BUILD_DIR}/acl/dsl/yaml_lexer_impl.cpp" -L "${ACL_SOURCE_BUILD_DIR}/acl/dsl/yaml.l"

DEPENDS
"${ACL_SOURCE_BUILD_DIR}/acl/dsl/yaml.l"
"${ACL_SOURCE_BUILD_DIR}/acl/dsl/yaml_parser_impl.cpp"
)
endif()
##
## TARGET
##
Expand All @@ -58,8 +33,6 @@ 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"
"${ACL_SOURCE_BUILD_DIR}/acl/scheduler/event_types.cpp"
"${ACL_SOURCE_BUILD_DIR}/acl/utils/string_utils.cpp"
Expand Down
158 changes: 80 additions & 78 deletions include/acl/dsl/yaml.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,6 @@

namespace acl::yaml
{
struct location_type
{
uint32_t begin = 0;
uint32_t end = 0;
inline friend std::ostream& operator<<(std::ostream& yyo, location_type const& l) noexcept
{
return yyo << l.begin << ':' << l.end;
}
};

struct string_slice
{
Expand All @@ -44,108 +35,119 @@ class context
virtual void set_value(std::string_view slice) = 0;
};

;
class istream
{
public:
using indent = uint32_t;

void set_idention_level(uint16_t level);
void set_idention_level(string_slice level);
void add_dash_indention(string_slice dash_level);

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 explicit istream(std::string_view content) : content_(content) {}

void close_all_mappings();
// Main parse function that processes the YAML content
void parse(context& ctx);

inline void throw_error(location_type const& loc, std::string_view error, std::string_view context) const
void set_handler(context* ctx)
{
throw std::runtime_error(std::format("[{}] token@{}: \"{}\" - {}", context, loc.begin,
contents.substr(loc.begin, loc.end - loc.begin), error));
ctx_ = ctx;
}

inline auto location() const noexcept
private:
enum class token_type : uint8_t
{
return location_type{current_token.start, current_token.start + current_token.count};
}
indent, // Whitespace at start of line
key, // Key followed by colon
value, // Simple scalar value
dash, // Array item marker
pipe, // | for literal block scalar
gt, // > for folded block scalar
newline, // Line ending
eof // End of input
};

inline string_slice get_view() const
enum class parse_state : uint8_t
{
return current_token;
}
none,
in_key,
in_value,
in_block_scalar,
in_array
};

inline string_slice get_quoted_string() const
struct token
{
return {current_token.start + 1, current_token.count - 2};
}
token_type type;
string_slice content;
};

inline void set_token(uint32_t length) noexcept
// Token processing
std::optional<token> next_token();
void process_token(const std::optional<token>& tok);
// Context management
void handle_indent(int32_t new_indent);
void handle_key(string_slice key);
void handle_value(string_slice value);
void handle_dash();
void handle_block_scalar(token_type type);
void collect_block_scalar();
void close_context(int32_t new_indent);

// Utility functions
std::string_view get_view(string_slice slice) const
{
current_token.start = token_cursor;
current_token.count = length;
token_cursor += length;
return content_.substr(slice.start, slice.count);
}

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

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;
auto start = current_pos_;
while (current_pos_ < content_.length() && (content_[current_pos_] == ' ' || content_[current_pos_] == '\t'))
{
current_pos_++;
}
return {start, static_cast<uint32_t>(current_pos_ - start)};
}

inline void* get_scanner() const noexcept
void skip_whitespace()
{
return scanner;
while (current_pos_ < content_.length() && (content_[current_pos_] == ' ' || content_[current_pos_] == '\t'))
{
current_pos_++;
}
}

inline void set_handler(context* handler) noexcept
char peek(uint32_t offset) const
{
handler = handler;
return (current_pos_ + offset < content_.length()) ? content_[current_pos_ + offset] : '\0';
}

istream(std::string_view content) noexcept : contents(content) {}

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

private:
inline std::string_view get_view(string_slice slice) const noexcept
string_slice get_current_line()
{
return contents.substr(slice.start, slice.count);
auto start = current_pos_;
while (current_pos_ < content_.length() && content_[current_pos_] != '\n')
{
current_pos_++;
}
return {start, static_cast<uint32_t>(current_pos_ - start)};
}

enum class indent_type : uint8_t
private:
enum class container_type
{
none,
object,
array,
array
};
struct indent_entry
{
int32_t indent;
container_type type;
};

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

acl::small_vector<indent_type, 16> indent_stack;
std::string_view content_;
context* ctx_ = nullptr;
parse_state state_ = parse_state::none;
token_type block_style_ = token_type::eof;
int32_t indent_level_ = 0;
uint32_t current_pos_ = 0;
bool at_line_start_ = true;

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;
small_vector<indent_entry, 8> indent_stack_;
small_vector<string_slice, 8> block_lines_;
};

} // namespace acl::yaml
32 changes: 13 additions & 19 deletions include/acl/serializers/yaml_object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ struct context_base : public acl::yaml::context
{
using pop_fn = void (*)(context_base*);

parser_state& parser_state_;
context_base* parent_ = nullptr;
pop_fn pop_fn_ = nullptr;
uint32_t xvalue_ = 0;
bool is_null_ = false;
parser_state& parser_state_;
context_base* parent_ = nullptr;
pop_fn pop_fn_ = nullptr;
uint32_t xvalue_ = 0;
std::string_view key_ = {};
bool is_null_ = false;

context_base(parser_state& parser_state, context_base* parent) noexcept : parser_state_(parser_state), parent_(parent)
{}
Expand Down Expand Up @@ -97,8 +98,9 @@ class context : public context_base
return obj_;
}

void start_mapping(std::string_view key) override
void set_key(std::string_view key) override
{
key_ = key;
if constexpr (detail::BoundClass<class_type>)
{
read_bound_class(key);
Expand All @@ -117,20 +119,17 @@ class context : public context_base
}
}

void add_mapping_value(std::string_view slice) override
{
set_value(slice);
}
void begin_object() override {}

void end_mapping() override
void end_object() override
{
if (pop_fn_)
{
pop_fn_(this);
}
}

void start_sequence_item() override
void begin_array() override
{
if constexpr (detail::ContainerLike<class_type>)
{
Expand All @@ -142,20 +141,15 @@ class context : public context_base
}
}

void add_sequence_item(std::string_view slice) override
{
set_value(slice);
}

void end_sequence_item() override
void end_array() override
{
if (pop_fn_)
{
pop_fn_(this);
}
}

void set_value(std::string_view slice)
void set_value(std::string_view slice) override
{
if (slice == "null")
{
Expand Down
Loading

0 comments on commit 1f0c6e8

Please sign in to comment.