diff options
author | Spartan322 <Megacake1234@gmail.com> | 2023-09-03 05:56:26 +0200 |
---|---|---|
committer | Spartan322 <Megacake1234@gmail.com> | 2023-09-05 21:29:31 +0200 |
commit | 1d2c5ce39d12adcb584d586952a59e15f2495f67 (patch) | |
tree | f7569029f45ec019b0387e63aa7b94d1da7cc03a /src/openvic-dataloader/v2script/SimpleGrammar.hpp | |
parent | 238ab9dfaa8ec7a48142154d227605ae367d53d1 (diff) |
Add Node Line/Column Generator
Fix Errors.hpp dependency on v2script/Parser.hpp
Add node location print to headless/main.cpp
Add Node::line_col << operator
Add Node::cast_to
WARNING: Takes advantage of non-standard behavior in unordered_multimap
THIS IS A HACK FOR NOW
Only GCC unordered_multimap::equal_range sees elements backwards
Prefer moving off of unordered_multimap to something like EASTL hash_multimap
Diffstat (limited to 'src/openvic-dataloader/v2script/SimpleGrammar.hpp')
-rw-r--r-- | src/openvic-dataloader/v2script/SimpleGrammar.hpp | 56 |
1 files changed, 38 insertions, 18 deletions
diff --git a/src/openvic-dataloader/v2script/SimpleGrammar.hpp b/src/openvic-dataloader/v2script/SimpleGrammar.hpp index c91935e..3e9a96e 100644 --- a/src/openvic-dataloader/v2script/SimpleGrammar.hpp +++ b/src/openvic-dataloader/v2script/SimpleGrammar.hpp @@ -32,7 +32,11 @@ namespace ovdl::v2script::grammar { struct Identifier { static constexpr auto rule = lexy::dsl::identifier(data_char_class); - static constexpr auto value = lexy::as_string<std::string> | lexy::new_<ast::IdentifierNode, ast::NodePtr>; + 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)); + }); }; struct StringExpression { @@ -55,57 +59,73 @@ namespace ovdl::v2script::grammar { // or a Unicode code point of the form uXXXX. auto escape = lexy::dsl::backslash_escape // .symbol<escaped_symbols>(); - return lexy::dsl::quoted(c, escape); + return lexy::dsl::delimited(lexy::dsl::position(lexy::dsl::lit_b<'"'>))(c, escape); }(); - static constexpr auto value = lexy::as_string<std::string> >> lexy::new_<ast::StringNode, ast::NodePtr>; + 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)); + }); }; struct SimpleAssignmentStatement { static constexpr auto rule = - lexy::dsl::p<Identifier> >> + lexy::dsl::position(lexy::dsl::p<Identifier>) >> lexy::dsl::equal_sign + (lexy::dsl::p<Identifier> | lexy::dsl::p<StringExpression> | lexy::dsl::recurse_branch<StatementListBlock>); static constexpr auto value = lexy::callback<ast::NodePtr>( - [](auto name, auto&& initalizer) { - return make_node_ptr<ast::AssignNode>(LEXY_MOV(name), LEXY_MOV(initalizer)); + [](const char* pos, auto name, auto&& initalizer) { + return ast::make_node_ptr<ast::AssignNode>(pos, LEXY_MOV(name), LEXY_MOV(initalizer)); }); }; struct AssignmentStatement { static constexpr auto rule = - lexy::dsl::p<Identifier> >> + lexy::dsl::position(lexy::dsl::p<Identifier>) >> (lexy::dsl::equal_sign >> (lexy::dsl::p<Identifier> | lexy::dsl::p<StringExpression> | lexy::dsl::recurse_branch<StatementListBlock>) | lexy::dsl::else_ >> lexy::dsl::return_) | lexy::dsl::p<StringExpression>; 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); }, - [](auto name, auto&& initalizer) { - return make_node_ptr<ast::AssignNode>(LEXY_MOV(name), LEXY_MOV(initalizer)); + [](const char* pos, auto name, auto&& initalizer) { + return ast::make_node_ptr<ast::AssignNode>(pos, LEXY_MOV(name), LEXY_MOV(initalizer)); }); }; struct StatementListBlock { static constexpr auto rule = - lexy::dsl::curly_bracketed( - lexy::dsl::opt(lexy::dsl::list(lexy::dsl::p<AssignmentStatement>)) + lexy::dsl::opt(lexy::dsl::semicolon)); + lexy::dsl::position(lexy::dsl::curly_bracketed.open()) >> + lexy::dsl::opt(lexy::dsl::list(lexy::dsl::p<AssignmentStatement>)) + + lexy::dsl::opt(lexy::dsl::semicolon) >> + lexy::dsl::position(lexy::dsl::curly_bracketed.close()); static constexpr auto value = lexy::as_list<std::vector<ast::NodePtr>> >> lexy::callback<ast::NodePtr>( - [](lexy::nullopt = {}, lexy::nullopt = {}) { - return ast::make_node_ptr<ast::ListNode>(); + [](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, lexy::nullopt, lexy::nullopt, const char* end) { + return ast::make_node_ptr<ast::ListNode>(ast::NodeLocation::make_from(begin, end)); }, - [](auto&& list, lexy::nullopt = {}) { - return make_node_ptr<ast::ListNode>(LEXY_MOV(list)); + [](const char* begin, auto&& list, lexy::nullopt, const char* end) { + return ast::make_node_ptr<ast::ListNode>(ast::NodeLocation::make_from(begin, end), LEXY_MOV(list)); }, - [](auto& list) { - return make_node_ptr<ast::ListNode>(list); + [](const char* begin, auto& list, const char* end) { + return ast::make_node_ptr<ast::ListNode>(ast::NodeLocation::make_from(begin, end), list); }); }; @@ -113,7 +133,7 @@ namespace ovdl::v2script::grammar { // Allow arbitrary spaces between individual tokens. static constexpr auto whitespace = whitespace_specifier | comment_specifier; - static constexpr auto rule = lexy::dsl::terminator(lexy::dsl::eof).list(lexy::dsl::p<AssignmentStatement>); + static constexpr auto rule = lexy::dsl::position + lexy::dsl::terminator(lexy::dsl::eof).list(lexy::dsl::p<AssignmentStatement>); static constexpr auto value = lexy::as_list<std::vector<ast::NodePtr>> >> lexy::new_<ast::FileNode, ast::NodePtr>; }; |