Skip to content

Commit

Permalink
Fix A2LParser
Browse files Browse the repository at this point in the history
  • Loading branch information
christoph2 committed Oct 27, 2024
1 parent f06a8b2 commit 17c1ee9
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 34 deletions.
6 changes: 4 additions & 2 deletions pya2l/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,16 +135,18 @@ def import_a2l(
self.db.close()
print("Enter pre-processor...")

prepro = Preprocessor(loglevel=loglevel)

if encoding is None:
self.logger.info("Detecting encoding...")
encoding = detect_encoding(file_name=self._a2lfn)

prepro = Preprocessor(loglevel=loglevel)
prepro_result = prepro.process(str(self._a2lfn), encoding=encoding)
prepro.finalize()
filenames, line_map, ifdata_reader = prepro_result
a2l_parser = parsers.a2l(debug=debug, prepro_result=prepro_result)

self.logger.info("Parsing pre-processed data ...")

self.db = a2l_parser.parse(filename=filenames.a2l, dbname=str(self._dbfn), encoding=encoding)
self.session = self.db.session

Expand Down
30 changes: 20 additions & 10 deletions pya2l/extensions/a2lparser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ class A2LParser {

using value_table_t = std::tuple<std::string, std::string, std::vector<std::vector<AsamVariantType>>>;

explicit A2LParser(std::optional<preprocessor_result_t> prepro_result, const std::string& file_name, const std::string& encoding) :
explicit A2LParser(
std::optional<preprocessor_result_t> prepro_result, const std::string& file_name, const std::string& encoding
) :
m_prepro_result(prepro_result), m_keyword_counter(0), m_table(PARSER_TABLE), m_root("root"), m_finalized(false) {
kw_push(m_table);
m_value_stack.push(&m_root);
Expand Down Expand Up @@ -69,8 +71,12 @@ class A2LParser {

if (token_type() == A2LTokenType::BEGIN) {
m_reader->consume();
#if 0
if (kw_tos().contains(token_type())) {
} else {
std::cout << "Warning: unexpected token: " << token->toString() << std::endl;
}
#endif
}

// TODO: Factor out.
Expand Down Expand Up @@ -104,8 +110,12 @@ class A2LParser {
//
// TODO: Addressmapper
// auto msg {"[INFO (pya2l.parser)]"};

std::cerr << "Invalid token: " << token->toString() << std::endl;
auto kwt = kw_tos();
std::cerr << "[ERROR (pya2l.parser)] Invalid token: " << token->toString() << std::endl;
if ((token->getText() == "IF_DATA") && (kwt.m_name == "ROOT")) {
std::cerr << "[ERROR (pya2l.parser)] No top-level PROJECT element. This is probably an include file?\n";
}
break;
}

if (token->getText() == "IF_DATA") {
Expand Down Expand Up @@ -292,13 +302,13 @@ class A2LParser {
std::string m_encoding;
std::unique_ptr<TokenReader> m_reader;
std::size_t m_keyword_counter;
std::vector<Keyword> m_kw_stack;
std::stack<ValueContainer*> m_value_stack;
Keyword& m_table;
ValueContainer m_root;
std::vector<value_table_t> m_tables;
std::size_t m_table_count{ 0 };
bool m_finalized{false};
std::vector<Keyword> m_kw_stack;
std::stack<ValueContainer*> m_value_stack;
Keyword& m_table;
ValueContainer m_root;
std::vector<value_table_t> m_tables;
std::size_t m_table_count{ 0 };
bool m_finalized{ false };
};

// helper function to print a tuple of any size
Expand Down
14 changes: 13 additions & 1 deletion pya2l/extensions/ifdata.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,20 +153,31 @@ class IfDataReader : public IfDataBase {
void open() {
#if defined(_MSC_VER)
auto err = ::fopen_s(&m_file, m_file_name.c_str(), "rb");
if (err != 0) {
throw std::runtime_error("Could not open file '" + m_file_name + "'.\n");
}
#else
m_file = ::fopen(m_file_name.c_str(), "rb");
if (m_file == nullptr) {
throw std::runtime_error("Could not open file '" + m_file_name + "'.\n");
}
#endif

struct stat stat_buf;
int rc = stat(m_file_name.c_str(), &stat_buf);
m_size = rc == 0 ? stat_buf.st_size : -1;
m_file_open = true;
}

void close() {
::fclose(m_file);
if (m_file_open) {
::fclose(m_file);
m_file_open = false;
}
}

~IfDataReader() {
close();
}

std::optional<std::string> get(const line_type& line) {
Expand Down Expand Up @@ -225,6 +236,7 @@ class IfDataReader : public IfDataBase {
std::FILE* m_file{ nullptr };
map_type file_map;
std::size_t m_size{ 0 };
bool m_file_open{ false };
};

#endif // __IFDATA_HPP
44 changes: 23 additions & 21 deletions pya2l/parser_test/parser_test.cpp
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@

#include "preprocessor.hpp"
#include "a2lparser.hpp"

#include <iostream>

std::string ValueContainer::s_encoding{ "latin1" };
#include <filesystem>
#include <fstream>

int main(int argc, char** argv) {
std::cout << "Hello!!!\n";
bool cmd = false;
auto FN{ "C:\\csProjects\\pyA2L\\examples\\03G906021KE_9970_501409_P447_HAXN_EDC16U34_3.41.a2l" };
#include "a2lparser.hpp"
#include "preprocessor.hpp"

cmd = argc > 1;
std::string ValueContainer::s_encoding{ "latin1" };

void process(const std::string& file_name, const std::string& encoding) {
Preprocessor p{ "INFO" };

Preprocessor p{ "INFO" };
auto res = p.process(file_name, encoding);
auto& [fns, linemap, ifdr] = res;
p.finalize();
auto parser = A2LParser(res, fns.a2l, encoding);
const auto& values = parser.get_values();
}

if (cmd) {
auto res = p.process(argv[1], "UTF-8");
auto reader = std::get<2>(res);
p.finalize();
reader.open();
// auto id = reader.get({ 2297, 9, 2307, 20 });
// reader.close();
// ld(p, 3);
int main(int argc, char** argv) {
for (auto const & dir_entry : std::filesystem::directory_iterator{ "..\\..\\examples" }) {
auto pth = dir_entry.path();
auto ext = pth.extension();
if (ext != ".a2l") {
continue;
}
std::cout << pth << '\n';
process(pth.string(), "latin1");
}

return 0;
return 0;
}

0 comments on commit 17c1ee9

Please sign in to comment.