Tatooine
includetree.h
Go to the documentation of this file.
1#ifndef TATOOINE_GL_INCLUDE_TREE_H
2#define TATOOINE_GL_INCLUDE_TREE_H
3//==============================================================================
4#include <filesystem>
5#include <iostream>
6#include <list>
7#include <string>
8//==============================================================================
9namespace tatooine::gl {
10//==============================================================================
12 private:
14 std::size_t m_num_lines;
15 std::filesystem::path m_path;
16 std::list<include_tree> m_nested_include_trees;
18
19 public:
20 auto line_number() const { return m_line_number; }
21 auto line_number() -> auto& { return m_line_number; }
22 auto num_lines() const { return m_num_lines; }
23 auto num_lines() ->auto& { return m_num_lines; }
24 auto path() const -> auto const& { return m_path; }
25 auto path() -> auto& { return m_path; }
26 auto nested_include_trees() const -> auto const& {
28 }
29 auto nested_include_trees() -> auto& { return m_nested_include_trees; }
30 auto parent() const -> auto const& { return *m_parent; }
31 auto has_parent() const -> bool { return m_parent != nullptr; }
32
33 public:
35 //----------------------------------------------------------------------------
36 include_tree(std::size_t line_number, std::size_t num_lines,
37 std::filesystem::path const& path,
38 std::list<include_tree> const& nested_include_trees,
39 include_tree const& parent)
40 : m_line_number{static_cast<int>(line_number)},
42 m_path{path},
44 m_parent{&parent} {}
45 //----------------------------------------------------------------------------
46 include_tree(std::size_t line_number, std::size_t num_lines,
47 std::filesystem::path const& path,
48 std::list<include_tree> const& nested_include_trees)
49 : m_line_number{static_cast<int>(line_number)},
51 m_path{path},
53 //----------------------------------------------------------------------------
54 include_tree(std::size_t line_number, std::size_t num_lines,
55 std::filesystem::path const& path)
56 : m_line_number{static_cast<int>(line_number)},
58 m_path{path} {}
59 //----------------------------------------------------------------------------
60 include_tree(std::size_t line_number, std::size_t num_lines,
61 std::filesystem::path const& path, include_tree const& parent)
62 : m_line_number{static_cast<int>(line_number)},
64 m_path{path},
65 m_parent{&parent} {}
66 //----------------------------------------------------------------------------
68 auto parse_line(std::size_t n) const -> std::pair<include_tree const&, std::size_t> {
69 auto cur_offset = std::size_t{};
70 for (auto const& nesting : m_nested_include_trees) {
71 auto const line_number = static_cast<std::size_t>(nesting.m_line_number);
72 if (n >= line_number + cur_offset &&
73 n < line_number + cur_offset + nesting.num_lines_with_includes()) {
74 return nesting.parse_line(n - cur_offset - line_number);
75 } else {
76 cur_offset += nesting.num_lines_with_includes() - 1;
77 }
78 }
79 return {*this, n - cur_offset};
80 }
81 //----------------------------------------------------------------------------
82 auto num_lines_with_includes() const -> std::size_t {
83 auto n = m_num_lines;
84 n -= m_nested_include_trees.size();
85 for (auto const& nesting : m_nested_include_trees)
86 n += nesting.num_lines_with_includes();
87 return n;
88 }
89 //----------------------------------------------------------------------------
90 auto print(std::size_t indent = 0) const -> void {
91 for (std::size_t i = 0; i < indent; ++i) {
92 std::cerr << ' ';
93 }
94 std::cerr << m_line_number << "/" << std::to_string(m_num_lines) << ": "
95 << m_path << '\n';
96 for (auto const& nesting : m_nested_include_trees) {
97 nesting.print(indent + 2);
98 }
99 }
100};
101//==============================================================================
102} // namespace tatooine::gl
103//==============================================================================
104#endif
Definition: ansiformat.h:6
Definition: includetree.h:11
auto num_lines() const
Definition: includetree.h:22
auto has_parent() const -> bool
Definition: includetree.h:31
include_tree const * m_parent
Definition: includetree.h:17
include_tree()
Definition: includetree.h:34
auto parse_line(std::size_t n) const -> std::pair< include_tree const &, std::size_t >
returns file and line number
Definition: includetree.h:68
include_tree(std::size_t line_number, std::size_t num_lines, std::filesystem::path const &path)
Definition: includetree.h:54
include_tree(std::size_t line_number, std::size_t num_lines, std::filesystem::path const &path, include_tree const &parent)
Definition: includetree.h:60
auto path() const -> auto const &
Definition: includetree.h:24
auto parent() const -> auto const &
Definition: includetree.h:30
auto nested_include_trees() -> auto &
Definition: includetree.h:29
auto line_number() const
Definition: includetree.h:20
include_tree(std::size_t line_number, std::size_t num_lines, std::filesystem::path const &path, std::list< include_tree > const &nested_include_trees, include_tree const &parent)
Definition: includetree.h:36
auto nested_include_trees() const -> auto const &
Definition: includetree.h:26
int m_line_number
Definition: includetree.h:13
std::filesystem::path m_path
Definition: includetree.h:15
auto num_lines_with_includes() const -> std::size_t
Definition: includetree.h:82
auto path() -> auto &
Definition: includetree.h:25
std::size_t m_num_lines
Definition: includetree.h:14
auto print(std::size_t indent=0) const -> void
Definition: includetree.h:90
include_tree(std::size_t line_number, std::size_t num_lines, std::filesystem::path const &path, std::list< include_tree > const &nested_include_trees)
Definition: includetree.h:46
auto num_lines() -> auto &
Definition: includetree.h:23
std::list< include_tree > m_nested_include_trees
Definition: includetree.h:16
auto line_number() -> auto &
Definition: includetree.h:21