diff options
author | Spartan322 <Megacake1234@gmail.com> | 2023-10-09 12:00:12 +0200 |
---|---|---|
committer | Spartan322 <Megacake1234@gmail.com> | 2023-10-12 17:29:10 +0200 |
commit | 5aacd90c3ef565c5bcd3c337dfd444a8fccc03f8 (patch) | |
tree | 13683fd58e9ef79e388fba9b3067cc1f5cc345ed | |
parent | 4009e1d576ad177aff59c8fce0339963303fc6e5 (diff) |
Add Lua Defines parsing
-rw-r--r-- | include/openvic-dataloader/v2script/Parser.hpp | 1 | ||||
-rw-r--r-- | src/headless/main.cpp | 39 | ||||
-rw-r--r-- | src/openvic-dataloader/v2script/LuaDefinesGrammar.hpp | 112 | ||||
-rw-r--r-- | src/openvic-dataloader/v2script/Parser.cpp | 23 |
4 files changed, 175 insertions, 0 deletions
diff --git a/include/openvic-dataloader/v2script/Parser.hpp b/include/openvic-dataloader/v2script/Parser.hpp index fb9022a..5e9717f 100644 --- a/include/openvic-dataloader/v2script/Parser.hpp +++ b/include/openvic-dataloader/v2script/Parser.hpp @@ -40,6 +40,7 @@ namespace ovdl::v2script { bool simple_parse(); bool event_parse(); bool decision_parse(); + bool lua_defines_parse(); const FileNode* get_file_node() const; diff --git a/src/headless/main.cpp b/src/headless/main.cpp index 2075d9b..1f0b4c3 100644 --- a/src/headless/main.cpp +++ b/src/headless/main.cpp @@ -64,6 +64,42 @@ 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); + if (parser.has_error()) { + return 1; + } + + parser.lua_defines_parse(); + if (parser.has_error()) { + return 2; + } + + if (parser.has_warning()) { + for (auto& warning : parser.get_warnings()) { + std::cerr << "Warning: " << warning.message << std::endl; + } + } + + 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; + } + } + } + } + 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); if (parser.has_error()) { @@ -103,6 +139,9 @@ int print_v2script_simple(const std::string_view path) { int main(int argc, char** argv) { switch (argc) { case 2: + if (insenitive_trim_eq(std::filesystem::path(argv[1]).extension().string(), ".lua")) { + return print_lua(argv[1]); + } return print_v2script_simple(argv[1]); case 4: if (insenitive_trim_eq(argv[1], "csv") && insenitive_trim_eq(argv[2], "utf")) diff --git a/src/openvic-dataloader/v2script/LuaDefinesGrammar.hpp b/src/openvic-dataloader/v2script/LuaDefinesGrammar.hpp new file mode 100644 index 0000000..b64d0f9 --- /dev/null +++ b/src/openvic-dataloader/v2script/LuaDefinesGrammar.hpp @@ -0,0 +1,112 @@ +#pragma once + +#include <lexy/dsl.hpp> + +#include "SimpleGrammar.hpp" +#include "detail/LexyLitRange.hpp" + +namespace ovdl::v2script::lua::grammar { + struct ParseOptions { + }; + + template<ParseOptions Options> + struct StatementListBlock; + + static constexpr auto comment_specifier = LEXY_LIT("--") >> lexy::dsl::until(lexy::dsl::newline).or_eof(); + + template<ParseOptions Options> + struct Identifier { + static constexpr auto rule = lexy::dsl::identifier(lexy::dsl::ascii::alpha_underscore, lexy::dsl::ascii::alpha_digit_underscore); + static constexpr auto value = lexy::callback<ast::NodePtr>( + [](auto lexeme) { + std::string str(lexeme.data(), lexeme.size()); + return ast::make_node_ptr<ast::IdentifierNode>(ast::NodeLocation { lexeme.begin(), lexeme.end() }, LEXY_MOV(str)); + }); + }; + + template<ParseOptions Options> + struct Value { + static constexpr auto rule = lexy::dsl::identifier(lexy::dsl::ascii::digit / lexy::dsl::lit_c<'.'> / lexy::dsl::lit_c<'-'>); + static constexpr auto value = lexy::callback<ast::NodePtr>( + [](auto lexeme) { + std::string str(lexeme.data(), lexeme.size()); + return ast::make_node_ptr<ast::IdentifierNode>(ast::NodeLocation { lexeme.begin(), lexeme.end() }, LEXY_MOV(str)); + }); + }; + + template<ParseOptions Options> + struct String { + static constexpr auto rule = [] { + // Arbitrary code points that aren't control characters. + auto c = ovdl::detail::lexydsl::make_range<0x20, 0xFF>() - lexy::dsl::ascii::control; + + return lexy::dsl::delimited(lexy::dsl::position(lexy::dsl::lit_b<'"'>))(c) | lexy::dsl::delimited(lexy::dsl::position(lexy::dsl::lit_b<'\''>))(c); + }(); + + static constexpr auto value = + lexy::as_string<std::string> >> + lexy::callback<ast::NodePtr>( + [](const char* begin, auto&& str, const char* end) { + return ast::make_node_ptr<ast::StringNode>(ast::NodeLocation::make_from(begin, end), LEXY_MOV(str)); + }); + }; + + template<ParseOptions Options> + struct Expression { + static constexpr auto rule = lexy::dsl::p<Value<Options>> | lexy::dsl::p<String<Options>>; + static constexpr auto value = lexy::forward<ast::NodePtr>; + }; + + template<ParseOptions Options> + struct AssignmentStatement { + static constexpr auto rule = + lexy::dsl::position(lexy::dsl::p<Identifier<Options>>) >> + lexy::dsl::equal_sign >> + (lexy::dsl::p<Expression<Options>> | lexy::dsl::recurse_branch<StatementListBlock<Options>>); + + static constexpr auto value = lexy::callback<ast::NodePtr>( + [](const char* pos, auto name, lexy::nullopt = {}) { + return LEXY_MOV(name); + }, + [](auto name, lexy::nullopt = {}) { + return LEXY_MOV(name); + }, + [](const char* pos, auto name, auto&& initalizer) { + return ast::make_node_ptr<ast::AssignNode>(pos, LEXY_MOV(name), LEXY_MOV(initalizer)); + }); + }; + + template<ParseOptions Options> + struct StatementListBlock { + static constexpr auto rule = + lexy::dsl::position(lexy::dsl::curly_bracketed.open()) >> + lexy::dsl::opt( + lexy::dsl::list( + lexy::dsl::recurse_branch<AssignmentStatement<Options>>, + lexy::dsl::trailing_sep(lexy::dsl::lit_c<','>))) >> + lexy::dsl::position(lexy::dsl::curly_bracketed.close()); + + static constexpr auto value = + lexy::as_list<std::vector<ast::NodePtr>> >> + lexy::callback<ast::NodePtr>( + [](const char* begin, lexy::nullopt, const char* end) { + return ast::make_node_ptr<ast::ListNode>(ast::NodeLocation::make_from(begin, end)); + }, + [](const char* begin, auto&& list, const char* end) { + return ast::make_node_ptr<ast::ListNode>(ast::NodeLocation::make_from(begin, end), LEXY_MOV(list)); + }, + [](const char* begin, auto& list, const char* end) { + return ast::make_node_ptr<ast::ListNode>(ast::NodeLocation::make_from(begin, end), list); + }); + }; + + template<ParseOptions Options = ParseOptions {}> + struct File { + // Allow arbitrary spaces between individual tokens. + static constexpr auto whitespace = ovdl::v2script::grammar::whitespace_specifier | comment_specifier; + + static constexpr auto rule = lexy::dsl::position + lexy::dsl::terminator(lexy::dsl::eof).opt_list(lexy::dsl::p<AssignmentStatement<Options>>); + + static constexpr auto value = lexy::as_list<std::vector<ast::NodePtr>> >> lexy::new_<ast::FileNode, ast::NodePtr>; + }; +}
\ No newline at end of file diff --git a/src/openvic-dataloader/v2script/Parser.cpp b/src/openvic-dataloader/v2script/Parser.cpp index 9e74e32..d7e4106 100644 --- a/src/openvic-dataloader/v2script/Parser.cpp +++ b/src/openvic-dataloader/v2script/Parser.cpp @@ -31,6 +31,7 @@ #include "detail/Warnings.hpp" #include "v2script/DecisionGrammar.hpp" #include "v2script/EventGrammar.hpp" +#include "v2script/LuaDefinesGrammar.hpp" using namespace ovdl; using namespace ovdl::v2script; @@ -232,6 +233,28 @@ bool Parser::decision_parse() { return true; } +bool Parser::lua_defines_parse() { + if (!_buffer_handler->is_valid()) { + return false; + } + + if (_buffer_handler->is_exclusive_utf8()) { + _warnings.push_back(warnings::make_utf8_warning(_file_path)); + } + + auto errors = _buffer_handler->parse<lua::grammar::File<>>(ovdl::detail::ReporError.path(_file_path).to(detail::OStreamOutputIterator { _error_stream })); + if (errors) { + _errors.reserve(errors->size()); + for (auto& err : errors.value()) { + _has_fatal_error |= err.type == ParseError::Type::Fatal; + _errors.push_back(err); + } + return false; + } + _file_node.reset(static_cast<ast::FileNode*>(_buffer_handler->get_root().release())); + return true; +} + const FileNode* Parser::get_file_node() const { return _file_node.get(); } |