aboutsummaryrefslogtreecommitdiff
path: root/include/openvic-dataloader/NodeLocation.hpp
blob: 7f7a09cf9bb228551a5d7b7c2f4a585cb93a8a15 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#pragma once

#include <cstdint>

namespace ovdl {
   template<typename CharT>
   struct BasicNodeLocation {
      using char_type = CharT;

      const char_type* _begin = nullptr;
      const char_type* _end = nullptr;

      BasicNodeLocation() = default;
      BasicNodeLocation(const char_type* pos)
         : _begin(pos),
           _end(pos) {}
      BasicNodeLocation(const char_type* begin, const char_type* end)
         : _begin(begin),
           _end(end) {}

      BasicNodeLocation(const BasicNodeLocation&) noexcept = default;
      BasicNodeLocation& operator=(const BasicNodeLocation&) = default;

      BasicNodeLocation(BasicNodeLocation&&) = default;
      BasicNodeLocation& operator=(BasicNodeLocation&&) = default;

      template<typename OtherCharT>
      void set_from(const BasicNodeLocation<OtherCharT>& other) {
         if constexpr (sizeof(CharT) <= sizeof(OtherCharT)) {
            _begin = reinterpret_cast<const CharT*>(other.begin());
            if (other.begin() == other.end())
               _end = _begin;
            else
               _end = reinterpret_cast<const CharT*>(other.end()) + (sizeof(OtherCharT) - sizeof(CharT));
         } else {
            _begin = reinterpret_cast<const CharT*>(other.begin());
            if (other.end() - other.begin() <= 0) {
               _end = reinterpret_cast<const CharT*>(other.begin());
            } else {
               _end = reinterpret_cast<const CharT*>(other.end() - (sizeof(CharT) - sizeof(OtherCharT)));
            }
         }
      }

      template<typename OtherCharT>
      BasicNodeLocation(const BasicNodeLocation<OtherCharT>& other) {
         set_from(other);
      }

      template<typename OtherCharT>
      BasicNodeLocation& operator=(const BasicNodeLocation<OtherCharT>& other) {
         set_from(other);
         return *this;
      }

      const char_type* begin() const { return _begin; }
      const char_type* end() const { return _end; }

      bool is_synthesized() const { return _begin == nullptr && _end == nullptr; }

      static BasicNodeLocation make_from(const char_type* begin, const char_type* end) {
         end++;
         if (begin >= end) return BasicNodeLocation(begin);
         return BasicNodeLocation(begin, end);
      }
   };

   using NodeLocation = BasicNodeLocation<char>;

   struct FilePosition {
      std::uint32_t start_line = std::uint32_t(-1), end_line = std::uint32_t(-1), start_column = std::uint32_t(-1), end_column = std::uint32_t(-1);

      inline constexpr bool is_empty() { return start_line == std::uint32_t(-1) && end_line == std::uint32_t(-1) && start_column == std::uint32_t(-1) && end_column == std::uint32_t(-1); }
   };
}