diff options
Diffstat (limited to 'src/headless')
-rw-r--r-- | src/headless/main.cpp | 112 |
1 files changed, 57 insertions, 55 deletions
diff --git a/src/headless/main.cpp b/src/headless/main.cpp index 688b479..7279a6e 100644 --- a/src/headless/main.cpp +++ b/src/headless/main.cpp @@ -12,6 +12,11 @@ #include <openvic-dataloader/v2script/AbstractSyntaxTree.hpp> #include <openvic-dataloader/v2script/Parser.hpp> +enum class VisualizationType { + Native, // <name> = { <contents> } + List // - <type> : <multiline contents> +}; + std::string_view trim(std::string_view str) { std::string_view::iterator begin = str.begin(); std::string_view::iterator end = str.end(); @@ -38,7 +43,8 @@ bool insenitive_trim_eq(std::string_view lhs, std::string_view rhs) { template<ovdl::csv::EncodingType Encoding> int print_csv(const std::string_view path) { - auto parser = ovdl::csv::Parser<Encoding>::from_file(path); + auto parser = ovdl::csv::Parser<Encoding>(std::cerr); + parser.load_from_file(path); if (parser.has_error()) { return 1; } @@ -49,9 +55,7 @@ int print_csv(const std::string_view path) { } if (parser.has_warning()) { - for (auto& warning : parser.get_warnings()) { - std::cerr << "Warning: " << warning.message << std::endl; - } + parser.print_errors_to(std::cerr); } std::cout << "lines:\t\t" << parser.get_lines().size() << std::endl; @@ -62,8 +66,9 @@ int print_csv(const std::string_view path) { return EXIT_SUCCESS; } -int print_lua(const std::string_view path) { - auto parser = ovdl::v2script::Parser::from_file(path); +int print_lua(const std::string_view path, VisualizationType visual_type) { + auto parser = ovdl::v2script::Parser(std::cerr); + parser.load_from_file(path); if (parser.has_error()) { return 1; } @@ -74,32 +79,20 @@ int print_lua(const std::string_view path) { } if (parser.has_warning()) { - for (auto& warning : parser.get_warnings()) { - std::cerr << "Warning: " << warning.message << std::endl; - } + parser.print_errors_to(std::cerr); } - parser.generate_node_location_map(); - - for (const auto& node : parser.get_file_node()->_statements) { - std::cout << node->get_type() << ": " << parser.get_node_begin(node.get()) << std::endl; - if (auto assign_node = node->cast_to<ovdl::v2script::ast::AssignNode>(); assign_node) { - auto lnode_ptr = assign_node->_initializer.get(); - std::cout << lnode_ptr->get_type() << " begin: " << parser.get_node_begin(lnode_ptr) << std::endl; - std::cout << lnode_ptr->get_type() << " end: " << parser.get_node_end(lnode_ptr) << std::endl; - if (auto list_node = lnode_ptr->cast_to<ovdl::v2script::ast::AbstractListNode>(); list_node) { - for (const auto& inode : list_node->_statements) { - std::cout << inode->get_type() << ": " << parser.get_node_begin(inode.get()) << std::endl; - } - } - } + switch (visual_type) { + using enum VisualizationType; + case Native: std::cout << parser.make_native_string() << '\n'; break; + case List: std::cout << parser.make_list_string() << '\n'; break; } - std::cout << parser.get_file_node() << std::endl; return EXIT_SUCCESS; } -int print_v2script_simple(const std::string_view path) { - auto parser = ovdl::v2script::Parser::from_file(path); +int print_v2script_simple(const std::string_view path, VisualizationType visual_type) { + auto parser = ovdl::v2script::Parser(std::cerr); + parser.load_from_file(path); if (parser.has_error()) { return 1; } @@ -110,50 +103,59 @@ int print_v2script_simple(const std::string_view path) { } if (parser.has_warning()) { - for (auto& warning : parser.get_warnings()) { - std::cerr << "Warning: " << warning.message << std::endl; - } + parser.print_errors_to(std::cerr); } - parser.generate_node_location_map(); - - for (const auto& node : parser.get_file_node()->_statements) { - std::cout << node->get_type() << ": " << parser.get_node_begin(node.get()) << std::endl; - if (auto assign_node = node->cast_to<ovdl::v2script::ast::AssignNode>(); assign_node) { - auto lnode_ptr = assign_node->_initializer.get(); - std::cout << lnode_ptr->get_type() << " begin: " << parser.get_node_begin(lnode_ptr) << std::endl; - std::cout << lnode_ptr->get_type() << " end: " << parser.get_node_end(lnode_ptr) << std::endl; - if (auto list_node = lnode_ptr->cast_to<ovdl::v2script::ast::AbstractListNode>(); list_node) { - for (const auto& inode : list_node->_statements) { - std::cout << inode->get_type() << ": " << parser.get_node_begin(inode.get()) << std::endl; - } - } - } + switch (visual_type) { + using enum VisualizationType; + case Native: std::cout << parser.make_native_string() << '\n'; break; + case List: std::cout << parser.make_list_string() << '\n'; break; } - std::cout << parser.get_file_node() << std::endl; return EXIT_SUCCESS; } int main(int argc, char** argv) { - switch (argc) { + std::vector<std::string> args; + args.reserve(argc); + for (size_t index = 0; index < argc; index++) { + args.push_back(argv[index]); + } + + VisualizationType type = VisualizationType::Native; + if (args.size() >= 2) { + std::string_view type_str = args[1]; + if (insenitive_trim_eq(type_str, "list")) { + type = VisualizationType::List; + args.erase(args.begin() + 1); + } else if (insenitive_trim_eq(type_str, "native")) { + type = VisualizationType::Native; + args.erase(args.begin() + 1); + } + } + + switch (args.size()) { case 2: - if (insenitive_trim_eq(std::filesystem::path(argv[1]).extension().string(), ".lua")) { - return print_lua(argv[1]); + if (insenitive_trim_eq(std::filesystem::path(args[1]).extension().string(), ".lua")) { + return print_lua(args[1], type); } - return print_v2script_simple(argv[1]); + return print_v2script_simple(args[1], type); case 4: - if (insenitive_trim_eq(argv[1], "csv") && insenitive_trim_eq(argv[2], "utf")) - return print_csv<ovdl::csv::EncodingType::Utf8>(argv[3]); + if (insenitive_trim_eq(args[1], "csv") && insenitive_trim_eq(args[2], "utf")) + return print_csv<ovdl::csv::EncodingType::Utf8>(args[3]); goto default_jump; case 3: - if (insenitive_trim_eq(argv[1], "csv")) - return print_csv<ovdl::csv::EncodingType::Windows1252>(argv[2]); + if (insenitive_trim_eq(args[1], "csv")) + return print_csv<ovdl::csv::EncodingType::Windows1252>(args[2]); + if (insenitive_trim_eq(args[1], "lua")) + return print_lua(args[2], type); [[fallthrough]]; default: default_jump: - std::fprintf(stderr, "usage: %s <filename>\n", argv[0]); - std::fprintf(stderr, "usage: %s csv <filename>\n", argv[0]); - std::fprintf(stderr, "usage: %s csv utf <filename>", argv[0]); + std::fprintf(stderr, "usage: %s <filename>\n", args[0].c_str()); + std::fprintf(stderr, "usage: %s list <options> <filename>\n", args[0].c_str()); + std::fprintf(stderr, "usage: %s native <options> <filename>\n", args[0].c_str()); + std::fprintf(stderr, "usage: %s lua <filename>\n", args[0].c_str()); + std::fprintf(stderr, "usage: %s csv [utf] <filename>\n", args[0].c_str()); return EXIT_FAILURE; } |