From 757114a3c5b748567b42f273c7b78ca039ae983c Mon Sep 17 00:00:00 2001 From: Spartan322 Date: Tue, 28 Nov 2023 05:09:26 -0500 Subject: Add `deps/dryad` -> https://github.com/Spartan322/dryad Add `deps/fmt` -> https://github.com/fmtlib/fmt Add `deps/range-v3` -> https://github.com/ericniebler/range-v3 Improve parser error and warning support Update .clang-format Update `deps/SCsub` --- src/headless/main.cpp | 112 +++++++++++++++++++++++++------------------------- 1 file changed, 57 insertions(+), 55 deletions(-) (limited to 'src/headless') 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 #include +enum class VisualizationType { + Native, // = { } + List // - : +}; + 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 int print_csv(const std::string_view path) { - auto parser = ovdl::csv::Parser::from_file(path); + auto parser = ovdl::csv::Parser(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(); 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(); 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(); 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(); 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 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(argv[3]); + if (insenitive_trim_eq(args[1], "csv") && insenitive_trim_eq(args[2], "utf")) + return print_csv(args[3]); goto default_jump; case 3: - if (insenitive_trim_eq(argv[1], "csv")) - return print_csv(argv[2]); + if (insenitive_trim_eq(args[1], "csv")) + return print_csv(args[2]); + if (insenitive_trim_eq(args[1], "lua")) + return print_lua(args[2], type); [[fallthrough]]; default: default_jump: - std::fprintf(stderr, "usage: %s \n", argv[0]); - std::fprintf(stderr, "usage: %s csv \n", argv[0]); - std::fprintf(stderr, "usage: %s csv utf ", argv[0]); + std::fprintf(stderr, "usage: %s \n", args[0].c_str()); + std::fprintf(stderr, "usage: %s list \n", args[0].c_str()); + std::fprintf(stderr, "usage: %s native \n", args[0].c_str()); + std::fprintf(stderr, "usage: %s lua \n", args[0].c_str()); + std::fprintf(stderr, "usage: %s csv [utf] \n", args[0].c_str()); return EXIT_FAILURE; } -- cgit v1.2.3-56-ga3b1