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/Parser.cpp | |
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/Parser.cpp')
-rw-r--r-- | src/openvic-dataloader/v2script/Parser.cpp | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/src/openvic-dataloader/v2script/Parser.cpp b/src/openvic-dataloader/v2script/Parser.cpp index 07d6455..1258bca 100644 --- a/src/openvic-dataloader/v2script/Parser.cpp +++ b/src/openvic-dataloader/v2script/Parser.cpp @@ -12,6 +12,7 @@ #include <openvic-dataloader/ParseWarning.hpp> #include <openvic-dataloader/detail/Concepts.hpp> #include <openvic-dataloader/v2script/AbstractSyntaxTree.hpp> +#include <openvic-dataloader/v2script/NodeLocationMap.hpp> #include <lexy/action/parse.hpp> #include <lexy/encoding.hpp> @@ -57,8 +58,14 @@ public: return _root; } + ast::NodeLocationMap<decltype(_buffer)>& get_location_map() { + return _location_map; + } + private: + friend class ::ovdl::v2script::ast::Node; std::unique_ptr<ast::Node> _root; + ast::NodeLocationMap<decltype(_buffer)> _location_map; }; /// BufferHandler /// @@ -221,4 +228,60 @@ bool Parser::decision_parse() { const FileNode* Parser::get_file_node() const { return _file_node.get(); +} + +void Parser::generate_node_location_map() { + _buffer_handler->get_location_map().clear(); + _buffer_handler->get_location_map().generate_location_map(_buffer_handler->get_buffer(), get_file_node()); +} + +const ast::Node::line_col Parser::get_node_begin(const ast::NodeCPtr node) const { + if (!node) return { 0, 0 }; + return node->get_begin_line_col(*this); +} + +const ast::Node::line_col Parser::get_node_end(const ast::NodeCPtr node) const { + if (!node) return { 0, 0 }; + return node->get_end_line_col(*this); +} + +const ast::Node::line_col ast::Node::get_begin_line_col(const Parser& parser) const { + if (!parser._buffer_handler->is_valid() || parser._buffer_handler->_location_map.empty()) return {}; + line_col result; + auto [itr, range_end] = parser._buffer_handler->_location_map.equal_range(this); + if (itr != range_end) { + result.line = itr->second.line_nr(); + result.column = itr->second.column_nr(); + } + // Standard doesn't really guarantee the direction of the range's sequence, but only GCC goes backwards + // TODO: DON'T USE STANDARD UNORDERED_MULTIMAP +#if defined(__GNUC__) && !defined(__clang__) + itr++; + if (itr != range_end) { + result.line = itr->second.line_nr(); + result.column = itr->second.column_nr(); + } +#endif + return result; +} + +const ast::Node::line_col ast::Node::get_end_line_col(const Parser& parser) const { + if (!parser._buffer_handler->is_valid() || parser._buffer_handler->_location_map.empty()) return {}; + line_col result; + auto [itr, range_end] = parser._buffer_handler->_location_map.equal_range(this); + if (itr != range_end) { + result.line = itr->second.line_nr(); + result.column = itr->second.column_nr(); + } + // Standard doesn't really guarantee the direction of the range's sequence, but only GCC goes backwards + // TODO: DON'T USE STANDARD UNORDERED_MULTIMAP +#if defined(__GNUC__) && !defined(__clang__) + return result; +#endif + itr++; + if (itr != range_end) { + result.line = itr->second.line_nr(); + result.column = itr->second.column_nr(); + } + return result; }
\ No newline at end of file |