diff --git a/CMakeLists.txt b/CMakeLists.txt index 16e5574..4edc200 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,7 +4,7 @@ project(pya2l_extensions LANGUAGES C CXX) cmake_policy(SET CMP0135 NEW) -# find_package(Python COMPONENTS Interpreter Development) +find_package(Python COMPONENTS Interpreter Development) find_package(pybind11 REQUIRED) set(ANTLR4_TAG 4.13.1) @@ -133,7 +133,7 @@ target_link_libraries(if_data_parser antlr4_static ${SYSTEM_LIBS}) target_include_directories(aml_tester PUBLIC ${ANTLR4_INCLUDE_DIRS} ${ANTLR_AMLParser_OUTPUT_DIR} ${PROJECT_SOURCE_DIR}/pya2l/aml) target_include_directories(if_data_parser PUBLIC ${ANTLR4_INCLUDE_DIRS} ${ANTLR_pyA2LLexer_OUTPUT_DIR} ${ANTLR_AMLParser_OUTPUT_DIR} ${PROJECT_SOURCE_DIR}/pya2l/aml) # !? -message("am_tester / output: " ${aml_tester_OUTPUT_DIRECTORY}) +message("aml_tester / output: " ${aml_tester_OUTPUT_DIRECTORY}) # include generated files in project environment diff --git a/pya2l/aml/unmarshal.hpp b/pya2l/aml/unmarshal.hpp index 8ac35f1..15fbc63 100644 --- a/pya2l/aml/unmarshal.hpp +++ b/pya2l/aml/unmarshal.hpp @@ -107,18 +107,18 @@ class Node { } else { for (auto entry : get_members()) { std::cout << static_cast(entry->node_type()) << " " << static_cast(entry->aml_type()) << std::endl; - if (entry->aml_type() == Node::AmlType::BLOCK) { + //if (entry->aml_type() == Node::AmlType::BLOCK) { if (entry->get_tag() == name) { return entry; } - } + //} } } return nullptr; } std::tuple, std::optional> member_or_type() const noexcept { - if (m_aml_type == AmlType::BLOCK) { + //if (m_aml_type == AmlType::BLOCK) { auto multiple = is_multiple(); const auto& member = m_map.at("MEMBER"); if (member.aml_type() == Node::AmlType::NONE) { @@ -127,7 +127,7 @@ class Node { } else { return { multiple, &member, std::nullopt }; } - } + //} return { false, {}, {} }; } @@ -157,7 +157,7 @@ class Node { } bool is_multiple() const noexcept { - if (m_node_type == NodeType::MAP) { + if ((m_node_type == NodeType::MAP) && (m_map.contains("MULTIPLE"))) { const auto& multiple = m_map.at("MULTIPLE"); return bool(std::get(multiple.value())); } @@ -165,20 +165,17 @@ class Node { } std::tuple, const Node*> get_type() const noexcept { - if (m_node_type == NodeType::MAP) { - if (m_map.contains("MEMBER")) { - std::vector arr_spec; - const auto& member = m_map.at("MEMBER"); - for (const auto& elem : member.map().at("ARR_SPEC").list()) { - arr_spec.push_back(std::get(elem.value())); - } - const auto& type = member.map().at("TYPE"); - return { arr_spec, &type }; - } else { - return {}; + std::vector arr_spec; + if ((m_map.contains("TYPE")) && (m_map.contains("ARR_SPEC"))) { + const auto& type = map().at("TYPE"); + for (const auto& elem : map().at("ARR_SPEC").list()) { + arr_spec.push_back(std::get(elem.value())); } + return { arr_spec, &type }; + } + else { + return {}; } - return {}; } std::vector get_members() const noexcept { @@ -210,9 +207,9 @@ class Node { const auto& ts_def = ts_member.map().at("DEFINITION"); const auto ts_mult = bool(ts_member.map().at("MULTIPLE").get_int()); - const auto& tsd_type = ts_def.map().at("TYPE"); + const auto& tsd_member = ts_def.map().at("MEMBER"); const auto tsd_mult = bool(ts_def.map().at("MULTIPLE").get_int()); - result.emplace(ts_tag, std::forward_as_tuple( & tsd_type, ts_mult, tsd_mult )); + result.emplace(ts_tag, std::forward_as_tuple( & tsd_member, ts_mult, tsd_mult )); } return result; } @@ -321,7 +318,7 @@ inline Node make_tagged_struct_definition(bool multiple, std::optional typ Node::map_t map = { { "MULTIPLE", Node(Node::AmlType::TERMINAL, multiple) }, - { "TYPE", type_node }, + { "MEMBER", type_node }, }; auto res = Node(Node::AmlType::TAGGED_STRUCT_DEFINITION, map); return res; diff --git a/pya2l/if_data_parser.cpp b/pya2l/if_data_parser.cpp index 38f385b..b7f1a53 100644 --- a/pya2l/if_data_parser.cpp +++ b/pya2l/if_data_parser.cpp @@ -31,6 +31,8 @@ class IfDataParser { } void parse() { + do_type(); +#if 0 auto token = current_token(); if (token) { auto [type, text] = *token; @@ -44,6 +46,7 @@ class IfDataParser { } parse(); } +#endif } token_t next_token() { @@ -106,7 +109,15 @@ class IfDataParser { break; } } else if (member) { - m_grammar.push(*member); + const auto mem_real = *member; + if (mem_real->aml_type() == Node::AmlType::TAGGED_STRUCT_MEMBER) { + const auto& def = mem_real->map().at("DEFINITION"); + const auto& [mmultiple, mmember, mtype] = def.member_or_type(); + m_grammar.push(*mtype); + do_type(); + m_grammar.pop(); + } + //m_grammar.push(*member); } m_grammar.pop(); } @@ -129,6 +140,14 @@ class IfDataParser { } void struct_type() { + const auto tos = top(); + for (const auto& member: tos->get_members()) { + auto token = current_token(); + if (token) { + auto [tp, text] = *token; + } + consume(); + } } void tagged_struct_type() { @@ -137,7 +156,13 @@ class IfDataParser { auto [tp, text] = *token; const auto tos = top(); const auto& ts_members = tos->get_tagged_struct_members(); - const auto& [type, b0, b1] = ts_members.at(text); + const auto& [member, b0, b1] = ts_members.at(text); + const auto& [arr_spec, type] = member->get_type(); + m_grammar.push(type); + consume(); + do_type(); + m_grammar.pop(); + } } @@ -145,8 +170,9 @@ class IfDataParser { auto token = current_token(); if (token) { auto [tp, text] = *token; - const auto member = top()->find_tag(text); - const auto& [arr_spec, type] = member->get_type(); + const auto tu_member = top()->find_tag(text); + const auto& member = tu_member->map().at("MEMBER"); + const auto& [arr_spec, type] = member.get_type(); m_grammar.push(type); consume(); do_type(); @@ -156,6 +182,19 @@ class IfDataParser { void do_type() { const auto tos = top(); + auto token = current_token(); + if (token) { + auto [type, text] = *token; + switch (type) { + case a2llg::BEGIN: + consume(); + block_type(); + break; + default: + std::cerr << "Unknown token type: " << type << std::endl; + } + } + switch (tos->aml_type()) { case Node::AmlType::STRUCT: struct_type(); @@ -197,6 +236,132 @@ class IfDataParser { const std::string BASE{ "C:/csProjects/" }; //const std::string BASE{ ""C:/Users/HP/PycharmProjects/" }; +const std::string CPLX_TEXT{ + "" \ +" /begin IF_DATA ASAP1B_CCP" \ +" /begin SOURCE" \ +" \"segment synchronous event channel\"" \ +" 103" \ +" 1" \ +" /begin QP_BLOB" \ +" 0" \ +" LENGTH 8" \ +" CAN_ID_FIXED 0x330" \ +" FIRST_PID 0" \ +" RASTER 0" \ +" /end QP_BLOB" \ +" /end SOURCE" \ +" " \ +" /begin SOURCE" \ +" \"10ms time synchronous event channel\"" \ +" 4" \ +" 1" \ +" /begin QP_BLOB" \ +" 1" \ +" LENGTH 12" \ +" CAN_ID_FIXED 0x340" \ +" FIRST_PID 8" \ +" RASTER 1" \ +" /end QP_BLOB" \ +" /end SOURCE" \ +" " \ +" /begin SOURCE" \ +" \"100ms time synchronous event channel\"" \ +" 4" \ +" 10" \ +" /begin QP_BLOB" \ +" 2" \ +" LENGTH 8" \ +" CAN_ID_FIXED 0x350" \ +" FIRST_PID 20" \ +" RASTER 2" \ +" /end QP_BLOB" \ +" /end SOURCE" \ +" " \ +" /begin RASTER" \ +" \"segment synchronous event channel\"" \ +" \"seg_sync\"" \ +" 0" \ +" 103" \ +" 1" \ +" /end RASTER" \ +" " \ +" /begin RASTER" \ +" \"10ms time synchronous event channel\"" \ +" \"10_ms\"" \ +" 1" \ +" 4" \ +" 1" \ +" /end RASTER" \ +" " \ +" /begin RASTER" \ +" \"100ms time synchronous event channel\"" \ +" \"100_ms\"" \ +" 2" \ +" 4" \ +" 10" \ +" /end RASTER" \ +" " \ +" /begin SEED_KEY" \ +" \"\"" \ +" \"\"" \ +" \"\"" \ +" /end SEED_KEY" \ +" " \ +" /begin TP_BLOB" \ +" 0x200" \ +" 0x202" \ +" 0x200" \ +" 0x210" \ +" 0x1234" \ +" 1" \ +" " \ +" /begin CAN_PARAM" \ +" 0x3E8" \ +" 0x40" \ +" 0x16" \ +" /end CAN_PARAM" \ +" " \ +" DAQ_MODE BURST" \ +" CONSISTENCY DAQ" \ +" " \ +" /begin CHECKSUM_PARAM " \ +" 0xC001" \ +" 0xFFFFFFFF" \ +" CHECKSUM_CALCULATION ACTIVE_PAGE" \ +" /end CHECKSUM_PARAM " \ +" " \ +" /begin DEFINED_PAGES" \ +" 1" \ +" \"reference page\"" \ +" 0x00" \ +" 0x8E0670" \ +" 0x1C26C" \ +" ROM" \ +" /end DEFINED_PAGES" \ +" " \ +" /begin DEFINED_PAGES" \ +" 2" \ +" \"working page\"" \ +" 0x00" \ +" 0x808E0670" \ +" 0x1C26C" \ +" RAM" \ +" RAM_INIT_BY_ECU" \ +" /end DEFINED_PAGES" \ +" " \ +" OPTIONAL_CMD 0x11 " \ +" OPTIONAL_CMD 0xE " \ +" OPTIONAL_CMD 0x19 " \ +" OPTIONAL_CMD 0x9 " \ +" OPTIONAL_CMD 0xC " \ +" OPTIONAL_CMD 0xD " \ +" OPTIONAL_CMD 0x12 " \ +" OPTIONAL_CMD 0x13 " \ +" /end TP_BLOB" \ +" /end IF_DATA" +}; + int main() { std::ifstream stream; @@ -214,7 +379,9 @@ int main() { "0x10000\n" "0x1E8\n" "/end IF_DATA"); - auto lex = IfDataParser(root, TEXT); + //auto lex = IfDataParser(root, TEXT); + auto lex = IfDataParser(root, CPLX_TEXT); + lex.parse(); return 0;