blob: 734e6936d193c80f8abf1485c9e4bc1038948990 [file] [log] [blame]
Ryan Harrisondbc13af2022-02-21 15:19:07 +00001
2// Copyright 2020 The Tint Authors.
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16#ifndef SRC_TINT_SOURCE_H_
17#define SRC_TINT_SOURCE_H_
18
19#include <iostream>
20#include <string>
21#include <string_view>
22#include <tuple>
23#include <vector>
24
25namespace tint {
26
27/// Source describes a range of characters within a source file.
28class Source {
dan sinclair41e4d9a2022-05-01 14:40:55 +000029 public:
30 /// FileContent describes the content of a source file encoded using utf-8.
31 class FileContent {
32 public:
33 /// Constructs the FileContent with the given file content.
34 /// @param data the file contents
35 explicit FileContent(const std::string& data);
Ryan Harrisondbc13af2022-02-21 15:19:07 +000036
dan sinclair41e4d9a2022-05-01 14:40:55 +000037 /// Copy constructor
38 /// @param rhs the FileContent to copy
39 FileContent(const FileContent& rhs);
Ryan Harrisondbc13af2022-02-21 15:19:07 +000040
dan sinclair41e4d9a2022-05-01 14:40:55 +000041 /// Destructor
42 ~FileContent();
Ryan Harrisondbc13af2022-02-21 15:19:07 +000043
dan sinclair41e4d9a2022-05-01 14:40:55 +000044 /// The original un-split file content
45 const std::string data;
46 /// A string_view over #data
47 const std::string_view data_view;
48 /// #data split by lines
49 const std::vector<std::string_view> lines;
50 };
Ryan Harrisondbc13af2022-02-21 15:19:07 +000051
dan sinclair41e4d9a2022-05-01 14:40:55 +000052 /// File describes a source file, including path and content.
53 class File {
54 public:
55 /// Constructs the File with the given file path and content.
56 /// @param p the path for this file
57 /// @param c the file contents
58 inline File(const std::string& p, const std::string& c) : path(p), content(c) {}
Ryan Harrisondbc13af2022-02-21 15:19:07 +000059
dan sinclair41e4d9a2022-05-01 14:40:55 +000060 /// Copy constructor
61 File(const File&) = default;
Ryan Harrisondbc13af2022-02-21 15:19:07 +000062
dan sinclair41e4d9a2022-05-01 14:40:55 +000063 /// Move constructor
64 File(File&&) = default;
Ryan Harrisondbc13af2022-02-21 15:19:07 +000065
dan sinclair41e4d9a2022-05-01 14:40:55 +000066 /// Destructor
67 ~File();
Ryan Harrisondbc13af2022-02-21 15:19:07 +000068
dan sinclair41e4d9a2022-05-01 14:40:55 +000069 /// file path
70 const std::string path;
71 /// file content
72 const FileContent content;
73 };
Ryan Harrisondbc13af2022-02-21 15:19:07 +000074
dan sinclair41e4d9a2022-05-01 14:40:55 +000075 /// Location holds a 1-based line and column index.
76 class Location {
77 public:
78 /// the 1-based line number. 0 represents no line information.
79 size_t line = 0;
80 /// the 1-based column number in utf8-code units (bytes).
81 /// 0 represents no column information.
82 size_t column = 0;
Ryan Harrisondbc13af2022-02-21 15:19:07 +000083
dan sinclair41e4d9a2022-05-01 14:40:55 +000084 /// Returns true of `this` location is lexicographically less than `rhs`
85 /// @param rhs location to compare against
86 /// @returns true if `this` < `rhs`
87 inline bool operator<(const Source::Location& rhs) {
88 return std::tie(line, column) < std::tie(rhs.line, rhs.column);
89 }
Ryan Harrisondbc13af2022-02-21 15:19:07 +000090
dan sinclair41e4d9a2022-05-01 14:40:55 +000091 /// Returns true of `this` location is equal to `rhs`
92 /// @param rhs location to compare against
93 /// @returns true if `this` == `rhs`
94 inline bool operator==(const Location& rhs) const {
95 return line == rhs.line && column == rhs.column;
96 }
Ryan Harrisondbc13af2022-02-21 15:19:07 +000097
dan sinclair41e4d9a2022-05-01 14:40:55 +000098 /// Returns true of `this` location is not equal to `rhs`
99 /// @param rhs location to compare against
100 /// @returns true if `this` != `rhs`
101 inline bool operator!=(const Location& rhs) const { return !(*this == rhs); }
102 };
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000103
dan sinclair41e4d9a2022-05-01 14:40:55 +0000104 /// Range holds a Location interval described by [begin, end).
105 class Range {
106 public:
107 /// Constructs a zero initialized Range.
108 inline Range() = default;
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000109
dan sinclair41e4d9a2022-05-01 14:40:55 +0000110 /// Constructs a zero-length Range starting at `loc`
111 /// @param loc the start and end location for the range
112 inline constexpr explicit Range(const Location& loc) : begin(loc), end(loc) {}
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000113
dan sinclair41e4d9a2022-05-01 14:40:55 +0000114 /// Constructs the Range beginning at `b` and ending at `e`
115 /// @param b the range start location
116 /// @param e the range end location
117 inline constexpr Range(const Location& b, const Location& e) : begin(b), end(e) {}
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000118
dan sinclair41e4d9a2022-05-01 14:40:55 +0000119 /// Return a column-shifted Range
120 /// @param n the number of characters to shift by
121 /// @returns a Range with a #begin and #end column shifted by `n`
122 inline Range operator+(size_t n) const {
123 return Range{{begin.line, begin.column + n}, {end.line, end.column + n}};
124 }
125
126 /// Returns true of `this` range is not equal to `rhs`
127 /// @param rhs range to compare against
128 /// @returns true if `this` != `rhs`
129 inline bool operator==(const Range& rhs) const {
130 return begin == rhs.begin && end == rhs.end;
131 }
132
133 /// Returns true of `this` range is equal to `rhs`
134 /// @param rhs range to compare against
135 /// @returns true if `this` == `rhs`
136 inline bool operator!=(const Range& rhs) const { return !(*this == rhs); }
137
138 /// The location of the first character in the range.
139 Location begin;
140 /// The location of one-past the last character in the range.
141 Location end;
142 };
143
144 /// Constructs the Source with an zero initialized Range and null File.
145 inline Source() : range() {}
146
147 /// Constructs the Source with the Range `rng` and a null File
148 /// @param rng the source range
149 inline explicit Source(const Range& rng) : range(rng) {}
150
151 /// Constructs the Source with the Range `loc` and a null File
152 /// @param loc the start and end location for the source range
153 inline explicit Source(const Location& loc) : range(Range(loc)) {}
154
155 /// Constructs the Source with the Range `rng` and File `file`
156 /// @param rng the source range
157 /// @param f the source file
158 inline Source(const Range& rng, File const* f) : range(rng), file(f) {}
159
160 /// @returns a Source that points to the begin range of this Source.
161 inline Source Begin() const { return Source(Range{range.begin}, file); }
162
163 /// @returns a Source that points to the end range of this Source.
164 inline Source End() const { return Source(Range{range.end}, file); }
165
166 /// Return a column-shifted Source
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000167 /// @param n the number of characters to shift by
dan sinclair41e4d9a2022-05-01 14:40:55 +0000168 /// @returns a Source with the range's columns shifted by `n`
169 inline Source operator+(size_t n) const { return Source(range + n, file); }
170
171 /// Returns true of `this` Source is lexicographically less than `rhs`
172 /// @param rhs source to compare against
173 /// @returns true if `this` < `rhs`
174 inline bool operator<(const Source& rhs) {
175 if (file != rhs.file) {
176 return false;
177 }
178 return range.begin < rhs.range.begin;
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000179 }
180
dan sinclair41e4d9a2022-05-01 14:40:55 +0000181 /// Helper function that returns the range union of two source locations. The
182 /// `start` and `end` locations are assumed to refer to the same source file.
183 /// @param start the start source of the range
184 /// @param end the end source of the range
185 /// @returns the combined source
186 inline static Source Combine(const Source& start, const Source& end) {
187 return Source(Source::Range(start.range.begin, end.range.end), start.file);
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000188 }
189
dan sinclair41e4d9a2022-05-01 14:40:55 +0000190 /// range is the span of text this source refers to in #file
191 Range range;
192 /// file is the optional source content this source refers to
193 const File* file = nullptr;
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000194};
195
196/// Writes the Source::Location to the std::ostream.
197/// @param out the std::ostream to write to
198/// @param loc the location to write
199/// @returns out so calls can be chained
dan sinclair41e4d9a2022-05-01 14:40:55 +0000200inline std::ostream& operator<<(std::ostream& out, const Source::Location& loc) {
201 out << loc.line << ":" << loc.column;
202 return out;
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000203}
204
205/// Writes the Source::Range to the std::ostream.
206/// @param out the std::ostream to write to
207/// @param range the range to write
208/// @returns out so calls can be chained
209inline std::ostream& operator<<(std::ostream& out, const Source::Range& range) {
dan sinclair41e4d9a2022-05-01 14:40:55 +0000210 out << "[" << range.begin << ", " << range.end << "]";
211 return out;
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000212}
213
214/// Writes the Source to the std::ostream.
215/// @param out the std::ostream to write to
216/// @param source the source to write
217/// @returns out so calls can be chained
218std::ostream& operator<<(std::ostream& out, const Source& source);
219
220/// Writes the Source::FileContent to the std::ostream.
221/// @param out the std::ostream to write to
222/// @param content the file content to write
223/// @returns out so calls can be chained
dan sinclair41e4d9a2022-05-01 14:40:55 +0000224inline std::ostream& operator<<(std::ostream& out, const Source::FileContent& content) {
225 out << content.data;
226 return out;
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000227}
228
229} // namespace tint
230
231#endif // SRC_TINT_SOURCE_H_