tan  0.0.1
source_file.h
1 #ifndef TAN_READER_READER_H
2 #define TAN_READER_READER_H
3 #include "config.h"
4 #include "base/container.h"
5 
6 namespace tanlang {
7 
8 class SrcLoc;
9 
10 class SourceFile final {
11 public:
12  SourceFile() = default;
13  void open(const str &filename);
14  void from_string(const str &code);
15 
16  /// \brief Return the number of lines of code of the current file
17  [[nodiscard]] size_t size() const { return _lines.size(); }
18 
19  /** \brief Return source at a specific line
20  * \param index line of code starting from 0
21  */
22  [[nodiscard]] str get_line(size_t index) const;
23  [[nodiscard]] char at(const SrcLoc &ptr) const;
24 
25  /**
26  * \brief Get a substring from start to the end of the current line
27  * \param start start of the string, inclusive
28  * */
29  [[nodiscard]] str substr(const SrcLoc &start) const;
30 
31  /**
32  * \brief Get a substring from the source code
33  * \param start start of the string, inclusive
34  * \param end end of the string, exclusive
35  * */
36  [[nodiscard]] str substr(const SrcLoc &start, const SrcLoc &end) const;
37 
38  /**
39  * \brief Check if a cursor is in bound
40  */
41  [[nodiscard]] bool is_cursor_valid(const SrcLoc &c) const;
42 
43  /**
44  * \brief The start of source file (inclusive).
45  */
46  [[nodiscard]] SrcLoc begin() const;
47 
48  /**
49  * \brief The end of source file (exclusive).
50  */
51  [[nodiscard]] SrcLoc end() const;
52 
53  /// \brief Return a copy of code_ptr that points to the next character
54  [[nodiscard]] SrcLoc forward(SrcLoc c);
55 
56  [[nodiscard]] str get_filename() const;
57 
58 private:
59  vector<str> _lines{};
60  str _filename = "memory";
61 };
62 
63 class SrcLoc {
64 public:
65  friend class SourceFile;
66  friend class SourceSpan;
67  uint32_t l = 0;
68  uint32_t c = 0;
69 
70 public:
71  SrcLoc(uint32_t r, uint32_t c, const SourceFile *src);
72 
73 public:
74  SrcLoc() = delete;
75  SrcLoc &operator=(const SrcLoc &other) = default;
76  SrcLoc(const SrcLoc &other) = default;
77  ~SrcLoc() = default;
78  bool operator==(const SrcLoc &other) const;
79  bool operator!=(const SrcLoc &other) const;
80  bool operator<=(const SrcLoc &other) const;
81  bool operator<(const SrcLoc &other) const;
82  bool operator>(const SrcLoc &other) const;
83  // prefix increment
84  SrcLoc &operator++();
85  // postfix increment
86  SrcLoc operator++(int);
87  char operator*();
88 
89 private:
90  SourceFile *_src = nullptr;
91 };
92 
93 /**
94  * \brief A span of source code tokens, inclusive on both ends.
95  */
96 class SourceSpan {
97 public:
98  SourceSpan() = delete;
99  SourceSpan(const SourceFile *src, uint32_t line, uint32_t col);
100  SourceSpan(const SourceFile *src, uint32_t start_line, uint32_t start_col, uint32_t end_line, uint32_t end_col);
101  SourceSpan(const SrcLoc &start, const SrcLoc &end);
102  [[nodiscard]] SourceFile *src() const;
103  [[nodiscard]] const SrcLoc &start() const;
104  [[nodiscard]] const SrcLoc &end() const;
105 
106 private:
107  SrcLoc _start;
108  SrcLoc _end;
109 };
110 
111 } // namespace tanlang
112 
113 #endif // TAN_READER_READER_H
bool is_cursor_valid(const SrcLoc &c) const
Check if a cursor is in bound.
Definition: source_file.cpp:47
SrcLoc begin() const
The start of source file (inclusive).
str get_line(size_t index) const
Return source at a specific line.
str substr(const SrcLoc &start) const
Get a substring from start to the end of the current line.
Definition: source_file.cpp:87
SrcLoc end() const
The end of source file (exclusive).
size_t size() const
Return the number of lines of code of the current file.
Definition: source_file.h:17
SrcLoc forward(SrcLoc c)
Return a copy of code_ptr that points to the next character.
Definition: source_file.cpp:92
A span of source code tokens, inclusive on both ends.
Definition: source_file.h:96