diff --git a/.clang-format b/.clang-format index 1f3620a..f0399f9 100644 --- a/.clang-format +++ b/.clang-format @@ -7,7 +7,7 @@ AlignConsecutiveAssignments: false AlignConsecutiveDeclarations: false AlignEscapedNewlinesLeft: false AlignOperands: false -AlignTrailingComments: false +AlignTrailingComments: true AllowAllParametersOfDeclarationOnNextLine: false AllowShortBlocksOnASingleLine: true AllowShortCaseLabelsOnASingleLine: true diff --git a/src/fdt/fdt-generator-qt.cpp b/src/fdt/fdt-generator-qt.cpp index 52b0520..7d5f709 100644 --- a/src/fdt/fdt-generator-qt.cpp +++ b/src/fdt/fdt-generator-qt.cpp @@ -1,9 +1,11 @@ #include "fdt-generator-qt.hpp" -#include "fdt/fdt-parser-v2.hpp" +#include "fdt/fdt-parser-tokens.hpp" #include "fdt/fdt-property-types.hpp" #include -qt_tree_fdt_generator::qt_tree_fdt_generator(tree_info &reference, tree_widget *target, string &&name, string &&id) { +using namespace fdt::qt_wrappers; + +tree_generator::tree_generator(tree_info &reference, tree_widget *target, string &&name, string &&id) { m_root = [&]() { if (reference.root) return reference.root; @@ -21,7 +23,7 @@ qt_tree_fdt_generator::qt_tree_fdt_generator(tree_info &reference, tree_widget * m_root->setSelected(true); } -void qt_tree_fdt_generator::begin_node(std::string_view vname) noexcept { +void tree_generator::begin_node(std::string_view vname) noexcept { const auto name = QString::fromUtf8(vname.data(), vname.size()); auto child = [&]() { @@ -51,11 +53,11 @@ void qt_tree_fdt_generator::begin_node(std::string_view vname) noexcept { m_tree_stack.emplace(child); } -void qt_tree_fdt_generator::end_node() noexcept { +void tree_generator::end_node() noexcept { m_tree_stack.pop(); } -void qt_tree_fdt_generator::insert_property(const fdt::tokenizer::types::property &prop) noexcept { +void tree_generator::insert_property(const fdt::parser::token_types::property &prop) noexcept { auto item = new QTreeWidgetItem(m_tree_stack.top()); fdt::qt_wrappers::property property{ diff --git a/src/fdt/fdt-generator-qt.hpp b/src/fdt/fdt-generator-qt.hpp index 3616576..19b28b8 100644 --- a/src/fdt/fdt-generator-qt.hpp +++ b/src/fdt/fdt-generator-qt.hpp @@ -1,6 +1,5 @@ #pragma once -#include #include #include #include @@ -8,7 +7,7 @@ #include #include #include -#include "fdt/fdt-parser-v2.hpp" +#include "fdt/fdt-parser-tokens.hpp" #include #include @@ -38,12 +37,14 @@ struct tree_info { using tree_map = hash_map; -struct qt_tree_fdt_generator : public iface_fdt_generator { - qt_tree_fdt_generator(tree_info &reference, tree_widget *target, string &&name, string &&id); +namespace fdt::qt_wrappers { - void begin_node(std::string_view) noexcept final; - void end_node() noexcept final; - void insert_property(const fdt::tokenizer::types::property &) noexcept final; +struct tree_generator { + tree_generator(tree_info &reference, tree_widget *target, string &&name, string &&id); + + void begin_node(std::string_view) noexcept; + void end_node() noexcept; + void insert_property(const fdt::parser::token_types::property &) noexcept; auto root() { return m_root; } @@ -51,3 +52,5 @@ struct qt_tree_fdt_generator : public iface_fdt_generator { QTreeWidgetItem *m_root{nullptr}; std::stack m_tree_stack; }; + +} // namespace fdt::qt_wrappers diff --git a/src/fdt/fdt-generator.hpp b/src/fdt/fdt-generator.hpp deleted file mode 100644 index 0e6999a..0000000 --- a/src/fdt/fdt-generator.hpp +++ /dev/null @@ -1,13 +0,0 @@ -#pragma once - -#include -#include -#include - -#include "fdt/fdt-parser-v2.hpp" - -struct iface_fdt_generator { - virtual void begin_node(std::string_view) noexcept = 0; - virtual void end_node() noexcept = 0; - virtual void insert_property(const fdt::tokenizer::types::property &) noexcept = 0; -}; diff --git a/src/fdt/fdt-header.hpp b/src/fdt/fdt-header.hpp index c96cb51..73e37d5 100644 --- a/src/fdt/fdt-header.hpp +++ b/src/fdt/fdt-header.hpp @@ -4,6 +4,7 @@ namespace fdt { +namespace raw { struct header { u32 magic; u32 totalsize; @@ -21,17 +22,18 @@ struct property { u32 len; u32 nameoff; }; +}; // namespace raw -constexpr auto is_magic_invalid(const header &v) -> bool { +constexpr auto is_magic_invalid(const fdt::raw::header &v) -> bool { constexpr auto header_magic_value = 0xD00DFEED; return v.magic != header_magic_value; } -constexpr auto is_version_unsupported(const header &v) -> bool { +constexpr auto is_version_unsupported(const fdt::raw::header &v) -> bool { constexpr auto header_support_above = 16; return v.version <= header_support_above; } - -static_assert(sizeof(header) == 40); -static_assert(sizeof(property) == 8); }; // namespace fdt + +static_assert(sizeof(fdt::raw::header) == 40); +static_assert(sizeof(fdt::raw::property) == 8); diff --git a/src/fdt/fdt-parser-context.hpp b/src/fdt/fdt-parser-context.hpp new file mode 100644 index 0000000..a8095f3 --- /dev/null +++ b/src/fdt/fdt-parser-context.hpp @@ -0,0 +1,19 @@ + +#pragma once + +#include "fdt/fdt-parser-tokens.hpp" + +namespace fdt::parser { + +struct context { + std::string_view structs; + std::string_view strings; + fdt::parser::tokens &tokens; + + struct { + const char *data{nullptr}; + int skip{}; + } state; +}; + +} // namespace fdt::parser diff --git a/src/fdt/fdt-parser-v2.hpp b/src/fdt/fdt-parser-tokens.hpp similarity index 56% rename from src/fdt/fdt-parser-v2.hpp rename to src/fdt/fdt-parser-tokens.hpp index c094bce..ce5620c 100644 --- a/src/fdt/fdt-parser-v2.hpp +++ b/src/fdt/fdt-parser-tokens.hpp @@ -8,9 +8,8 @@ using u32 = std::uint32_t; -namespace fdt::tokenizer { +namespace fdt::parser::token_types { -namespace types { struct node_begin { std::string name; }; @@ -28,25 +27,23 @@ constexpr auto id_of(property) -> u32 { return 0x03; }; constexpr auto id_of(nop) -> u32 { return 0x04; }; constexpr auto id_of(end) -> u32 { return 0x09; }; -} // namespace types +} // namespace fdt::parser::token_types -using token = std::variant; -using token_list = std::vector; +namespace fdt::parser { -struct context { - std::string_view structs; - std::string_view strings; - token_list &tokens; +using token = std::variant< + token_types::property, // + token_types::node_begin, // + token_types::node_end, // + token_types::nop, // + token_types::end // + >; - struct { - const char *data{nullptr}; - int skip{}; - } state; -}; +using tokens = std::vector; template concept Tokenizable = requires(T t) { - { fdt::tokenizer::types::id_of(t) } -> std::convertible_to; + { fdt::parser::token_types::id_of(t) } -> std::convertible_to; }; -} // namespace fdt::tokenizer +} // namespace fdt::parser diff --git a/src/fdt/fdt-parser.cpp b/src/fdt/fdt-parser.cpp index cf4968c..7d850b5 100644 --- a/src/fdt/fdt-parser.cpp +++ b/src/fdt/fdt-parser.cpp @@ -1,6 +1,7 @@ #include "fdt-parser.hpp" #include "fdt/fdt-header.hpp" -#include "fdt-parser-v2.hpp" +#include "fdt/fdt-parser-tokens.hpp" +#include "fdt-parser-context.hpp" #include "endian-conversions.hpp" #include @@ -21,22 +22,19 @@ auto align(const std::size_t size) { namespace fdt::parser { -using namespace fdt::tokenizer; -using namespace fdt::tokenizer::types; - -auto parse(node_begin &&token, context &ctx) -> fdt::tokenizer::token { +auto parse(token_types::node_begin &&token, context &ctx) -> fdt::parser::token { const auto size = std::strlen(ctx.state.data); token.name = std::string_view(ctx.state.data, size); ctx.state.skip += align(size + 1); return {std::move(token)}; } -auto parse(node_end &&token, context &ctx) -> fdt::tokenizer::token { +auto parse(token_types::node_end &&token, context &) -> fdt::parser::token { return {std::move(token)}; } -auto parse(types::property &&token, context &ctx) -> fdt::tokenizer::token { - const auto header = read_data_32be(ctx.state.data); +auto parse(token_types::property &&token, context &ctx) -> fdt::parser::token { + const auto header = read_data_32be(ctx.state.data); ctx.state.skip += align(sizeof(header)) + align(header.len); ctx.state.data += sizeof(header); @@ -50,19 +48,19 @@ auto parse(types::property &&token, context &ctx) -> fdt::tokenizer::token { return {std::move(token)}; } -auto parse(nop &&token, context &ctx) -> fdt::tokenizer::token { +auto parse(token_types::nop &&token, context &) -> fdt::parser::token { return {std::move(token)}; } -auto parse(end &&token, context &ctx) -> fdt::tokenizer::token { +auto parse(token_types::end &&token, context &) -> fdt::parser::token { return {std::move(token)}; } } // namespace fdt::parser -template -auto foreach_token_type(std::variant, const u32 token_id, fdt::tokenizer::context &ctx) { +template +auto foreach_token_type(std::variant, const u32 token_id, fdt::parser::context &ctx) { auto conditional_parse = [&](auto &&token) { - if (fdt::tokenizer::types::id_of(token) == token_id) { + if (fdt::parser::token_types::id_of(token) == token_id) { ctx.tokens.emplace_back(fdt::parser::parse(std::move(token), ctx)); return true; } @@ -71,41 +69,40 @@ auto foreach_token_type(std::variant, const u32 token_id, fdt::tokenizer: return (conditional_parse(Ts{}) || ...); } -auto fdt::tokenizer::generator(std::string_view view, std::string_view root_name) -> std::expected { - if (view.size() < sizeof(fdt::header)) - return std::unexpected(fdt::error::invalid_header); +auto fdt::parser::parse(std::string_view view, std::string_view root_name) -> std::expected { + using error = fdt::parser::error; + + if (view.size() < sizeof(fdt::raw::header)) + return std::unexpected(error::invalid_header); - const auto header = read_data_32be(view.data()); + const auto header = read_data_32be(view.data()); if (fdt::is_magic_invalid(header)) - return std::unexpected(fdt::error::invalid_magic); + return std::unexpected(error::invalid_magic); if (view.size() < header.totalsize) - return std::unexpected(fdt::error::data_truncated); + return std::unexpected(error::data_truncated); if (fdt::is_version_unsupported(header)) - return std::unexpected(fdt::error::unsupported_version); + return std::unexpected(error::unsupported_version); const auto dt_struct = view.data() + header.off_dt_struct; const auto dt_strings = view.data() + header.off_dt_strings; - fdt::tokenizer::token_list tokens; - tokens.reserve(50000); - - using namespace fdt::tokenizer; - using namespace fdt::tokenizer::types; - - context ctx{ - .structs = {dt_struct, header.size_dt_struct}, // + fdt::parser::tokens tokens; + fdt::parser::context ctx{ + .structs = {dt_struct, header.size_dt_struct}, // .strings = {dt_strings, header.size_dt_strings}, // .tokens = tokens, }; + tokens.reserve(50000); + const auto begin = reinterpret_cast(dt_struct); const auto end = reinterpret_cast(dt_struct) + header.size_dt_struct / sizeof(u32); if (header.size_dt_struct % sizeof(u32) != 0) - return std::unexpected(fdt::error::data_unaligned); + return std::unexpected(error::data_unaligned); for (auto iter = begin; iter != end;) { const auto id = static_cast(convert(*iter)); @@ -113,14 +110,14 @@ auto fdt::tokenizer::generator(std::string_view view, std::string_view root_name ctx.state.skip = 0; if (!foreach_token_type(token{}, id, ctx)) - return std::unexpected(fdt::error::invalid_token); + return std::unexpected(error::invalid_token); iter += ctx.state.skip; - if (std::holds_alternative(tokens.back())) { - auto &prop = std::get(tokens.back()); + if (std::holds_alternative(tokens.back())) { + auto &prop = std::get(tokens.back()); - if (auto dtb = fdt::tokenizer::generator(prop.data, prop.name); dtb.has_value()) { + if (auto dtb = fdt::parser::parse(prop.data, prop.name); dtb.has_value()) { auto &embedded_tokens = dtb.value(); std::move(std::begin(embedded_tokens), std::end(embedded_tokens), std::back_inserter(tokens)); } @@ -129,8 +126,8 @@ auto fdt::tokenizer::generator(std::string_view view, std::string_view root_name // change property name for first node for (auto &&token : tokens) - if (std::holds_alternative(token)) { - std::get(token).name = root_name; + if (std::holds_alternative(token)) { + std::get(token).name = root_name; break; } @@ -142,8 +139,8 @@ struct overloaded : Ts... { using Ts::operator()...; }; -auto fdt::tokenizer::validate(const fdt::tokenizer::token_list &tokens) -> bool { - using namespace fdt::tokenizer; +auto fdt::parser::validate(const fdt::parser::tokens &tokens) -> bool { + using namespace fdt::parser; bool valid_depth_test{true}; std::int32_t node_scope_depth{}; @@ -155,19 +152,19 @@ auto fdt::tokenizer::validate(const fdt::tokenizer::token_list &tokens) -> bool for (auto &&token : tokens) { std::visit(overloaded{ - [&](const types::node_begin &arg) { + [&](const token_types::node_begin &) { node_begin_count++; node_scope_depth++; }, - [&](const types::node_end &arg) { + [&](const token_types::node_end &) { node_end_count++; node_scope_depth--; }, - [&](const types::property &arg) { + [&](const token_types::property &) { property_count++; }, - [&](const types::nop &) { nop_count++; }, - [&](const types::end &) { end_count++; }, + [&](const token_types::nop &) { nop_count++; }, + [&](const token_types::end &) { end_count++; }, }, token); diff --git a/src/fdt/fdt-parser.hpp b/src/fdt/fdt-parser.hpp index fd356b4..cc72188 100644 --- a/src/fdt/fdt-parser.hpp +++ b/src/fdt/fdt-parser.hpp @@ -1,11 +1,11 @@ #pragma once -#include "fdt/fdt-parser-v2.hpp" +#include "fdt/fdt-parser-tokens.hpp" #include #include -namespace fdt { +namespace fdt::parser { enum class error { invalid_header, @@ -16,8 +16,6 @@ enum class error { unsupported_version, }; -namespace tokenizer { -auto generator(std::string_view view, std::string_view root_name) -> std::expected; -auto validate(const fdt::tokenizer::token_list &tokens) -> bool; -} // namespace tokenizer -} // namespace fdt +auto parse(std::string_view view, std::string_view root_name) -> std::expected; +auto validate(const tokens &) -> bool; +} // namespace fdt::parser diff --git a/src/fdt/fdt-property-types.hpp b/src/fdt/fdt-property-types.hpp index a52f7d6..0efd1b4 100644 --- a/src/fdt/fdt-property-types.hpp +++ b/src/fdt/fdt-property-types.hpp @@ -1,6 +1,5 @@ #pragma once -#include #include #include diff --git a/src/fdt/fdt-view.cpp b/src/fdt/fdt-view.cpp index 3b1da84..4af780d 100644 --- a/src/fdt/fdt-view.cpp +++ b/src/fdt/fdt-view.cpp @@ -8,7 +8,7 @@ #include #include -#include "fdt/fdt-parser-v2.hpp" +#include "fdt/fdt-parser-tokens.hpp" #include "fdt/fdt-property-types.hpp" #include "qnamespace.h" #include @@ -99,25 +99,26 @@ struct overloaded : Ts... { }; bool fdt::viewer::load(QByteArray &&data, string &&name, string &&id) { - qt_tree_fdt_generator generator(m_tree[id], m_target, std::move(name), std::move(id)); + using namespace fdt::parser; + using namespace fdt::qt_wrappers; - const auto tokens = fdt::tokenizer::generator({data.data(), data.size()}, name.toStdString()); + const auto tokens = parse({data.data(), data.size()}, name.toStdString()); if (!tokens) return false; - if (!fdt::tokenizer::validate(tokens.value())) + if (!validate(tokens.value())) return false; - using namespace fdt::tokenizer; + tree_generator generator(m_tree[id], m_target, std::move(name), std::move(id)); for (auto &&token : tokens.value()) std::visit(overloaded{ - [&](const types::node_begin &arg) { generator.begin_node(arg.name); }, - [&](const types::node_end &) { generator.end_node(); }, - [&](const types::property &arg) { generator.insert_property(arg); }, - [&](const types::nop &) {}, - [&](const types::end &) {}, + [&](const token_types::node_begin &arg) { generator.begin_node(arg.name); }, + [&](const token_types::node_end &) { generator.end_node(); }, + [&](const token_types::property &arg) { generator.insert_property(arg); }, + [&](const token_types::nop &) {}, + [&](const token_types::end &) {}, }, token); diff --git a/src/main-window.cpp b/src/main-window.cpp index ecffdc3..6fd703e 100644 --- a/src/main-window.cpp +++ b/src/main-window.cpp @@ -1,5 +1,5 @@ #include "main-window.hpp" -#include "fdt/fdt-parser-v2.hpp" +#include "fdt/fdt-parser-tokens.hpp" #include "fdt/fdt-property-types.hpp" #include "ui_main-window.h"