From 5aacd90c3ef565c5bcd3c337dfd444a8fccc03f8 Mon Sep 17 00:00:00 2001 From: Spartan322 Date: Mon, 9 Oct 2023 06:00:12 -0400 Subject: Add Lua Defines parsing --- .../v2script/LuaDefinesGrammar.hpp | 112 +++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 src/openvic-dataloader/v2script/LuaDefinesGrammar.hpp (limited to 'src/openvic-dataloader/v2script/LuaDefinesGrammar.hpp') 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 + +#include "SimpleGrammar.hpp" +#include "detail/LexyLitRange.hpp" + +namespace ovdl::v2script::lua::grammar { + struct ParseOptions { + }; + + template + struct StatementListBlock; + + static constexpr auto comment_specifier = LEXY_LIT("--") >> lexy::dsl::until(lexy::dsl::newline).or_eof(); + + template + 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( + [](auto lexeme) { + std::string str(lexeme.data(), lexeme.size()); + return ast::make_node_ptr(ast::NodeLocation { lexeme.begin(), lexeme.end() }, LEXY_MOV(str)); + }); + }; + + template + 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( + [](auto lexeme) { + std::string str(lexeme.data(), lexeme.size()); + return ast::make_node_ptr(ast::NodeLocation { lexeme.begin(), lexeme.end() }, LEXY_MOV(str)); + }); + }; + + template + 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 >> + lexy::callback( + [](const char* begin, auto&& str, const char* end) { + return ast::make_node_ptr(ast::NodeLocation::make_from(begin, end), LEXY_MOV(str)); + }); + }; + + template + struct Expression { + static constexpr auto rule = lexy::dsl::p> | lexy::dsl::p>; + static constexpr auto value = lexy::forward; + }; + + template + struct AssignmentStatement { + static constexpr auto rule = + lexy::dsl::position(lexy::dsl::p>) >> + lexy::dsl::equal_sign >> + (lexy::dsl::p> | lexy::dsl::recurse_branch>); + + static constexpr auto value = lexy::callback( + [](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(pos, LEXY_MOV(name), LEXY_MOV(initalizer)); + }); + }; + + template + struct StatementListBlock { + static constexpr auto rule = + lexy::dsl::position(lexy::dsl::curly_bracketed.open()) >> + lexy::dsl::opt( + lexy::dsl::list( + lexy::dsl::recurse_branch>, + lexy::dsl::trailing_sep(lexy::dsl::lit_c<','>))) >> + lexy::dsl::position(lexy::dsl::curly_bracketed.close()); + + static constexpr auto value = + lexy::as_list> >> + lexy::callback( + [](const char* begin, lexy::nullopt, const char* end) { + return ast::make_node_ptr(ast::NodeLocation::make_from(begin, end)); + }, + [](const char* begin, auto&& list, const char* end) { + return ast::make_node_ptr(ast::NodeLocation::make_from(begin, end), LEXY_MOV(list)); + }, + [](const char* begin, auto& list, const char* end) { + return ast::make_node_ptr(ast::NodeLocation::make_from(begin, end), list); + }); + }; + + template + 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>); + + static constexpr auto value = lexy::as_list> >> lexy::new_; + }; +} \ No newline at end of file -- cgit v1.2.3-56-ga3b1