aboutsummaryrefslogtreecommitdiff
path: root/src/openvic-dataloader/v2script/LuaDefinesGrammar.hpp
diff options
context:
space:
mode:
author George L. Albany <Megacake1234@gmail.com>2024-05-10 20:57:47 +0200
committer GitHub <noreply@github.com>2024-05-10 20:57:47 +0200
commit725310939d2b324d79ea4193a72000e21dcc1a2a (patch)
treee07390b682052129c91f4b157068bcdd84ceecb4 /src/openvic-dataloader/v2script/LuaDefinesGrammar.hpp
parent7211a228e68c8a6b1ad1c1c5ec68c8d720b6d2ba (diff)
parent757114a3c5b748567b42f273c7b78ca039ae983c (diff)
Merge pull request #37 from OpenVicProject/add/dryad
Move to dryad library
Diffstat (limited to 'src/openvic-dataloader/v2script/LuaDefinesGrammar.hpp')
-rw-r--r--src/openvic-dataloader/v2script/LuaDefinesGrammar.hpp82
1 files changed, 40 insertions, 42 deletions
diff --git a/src/openvic-dataloader/v2script/LuaDefinesGrammar.hpp b/src/openvic-dataloader/v2script/LuaDefinesGrammar.hpp
index b64d0f9..4d27d3e 100644
--- a/src/openvic-dataloader/v2script/LuaDefinesGrammar.hpp
+++ b/src/openvic-dataloader/v2script/LuaDefinesGrammar.hpp
@@ -1,11 +1,25 @@
#pragma once
+#include <lexy/_detail/config.hpp>
#include <lexy/dsl.hpp>
+#include "openvic-dataloader/v2script/AbstractSyntaxTree.hpp"
+
#include "SimpleGrammar.hpp"
-#include "detail/LexyLitRange.hpp"
+#include "detail/dsl.hpp"
namespace ovdl::v2script::lua::grammar {
+ template<typename ReturnType, typename... Callback>
+ constexpr auto callback(Callback... cb) {
+ return dsl::callback<ReturnType>(cb...);
+ }
+
+ template<typename T>
+ constexpr auto construct = v2script::grammar::construct<T>;
+
+ template<typename T>
+ constexpr auto construct_list = v2script::grammar::construct_list<T>;
+
struct ParseOptions {
};
@@ -17,20 +31,20 @@ namespace ovdl::v2script::lua::grammar {
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));
+ static constexpr auto value = callback<ast::IdentifierValue*>(
+ [](ast::ParseState& state, auto lexeme) {
+ auto value = state.ast().intern(lexeme.data(), lexeme.size());
+ return state.ast().create<ast::IdentifierValue>(lexeme.begin(), lexeme.end(), value);
});
};
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));
+ static constexpr auto value = callback<ast::IdentifierValue*>(
+ [](ast::ParseState& state, auto lexeme) {
+ auto value = state.ast().intern(lexeme.data(), lexeme.size());
+ return state.ast().create<ast::IdentifierValue>(lexeme.begin(), lexeme.end(), value);
});
};
@@ -38,66 +52,50 @@ namespace ovdl::v2script::lua::grammar {
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;
+ auto c = dsl::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));
+ callback<ast::StringValue*>(
+ [](ast::ParseState& state, const char* begin, const std::string& str, const char* end) {
+ auto value = state.ast().intern(str.data(), str.length());
+ return state.ast().create<ast::StringValue>(begin, end, value);
});
};
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>;
+ static constexpr auto value = lexy::forward<ast::Value*>;
};
template<ParseOptions Options>
struct AssignmentStatement {
static constexpr auto rule =
- lexy::dsl::position(lexy::dsl::p<Identifier<Options>>) >>
+ 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));
+ static constexpr auto value = callback<ast::AssignStatement*>(
+ [](ast::ParseState& state, const char* pos, ast::IdentifierValue* name, ast::Value* initializer) {
+ return state.ast().create<ast::AssignStatement>(pos, name, initializer);
});
};
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());
+ dsl::curly_bracketed(
+ lexy::dsl::opt(
+ lexy::dsl::list(
+ lexy::dsl::recurse_branch<AssignmentStatement<Options>>,
+ lexy::dsl::trailing_sep(lexy::dsl::lit_c<','>))));
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);
- });
+ lexy::as_list<ast::AssignStatementList> >> construct_list<ast::ListValue>;
};
template<ParseOptions Options = ParseOptions {}>
@@ -107,6 +105,6 @@ namespace ovdl::v2script::lua::grammar {
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>;
+ static constexpr auto value = lexy::as_list<ast::AssignStatementList> >> construct<ast::FileTree>;
};
} \ No newline at end of file