Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1 | |
| 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 | |
| 25 | namespace tint { |
| 26 | |
| 27 | /// Source describes a range of characters within a source file. |
| 28 | class Source { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 29 | 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 Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 36 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 37 | /// Copy constructor |
| 38 | /// @param rhs the FileContent to copy |
| 39 | FileContent(const FileContent& rhs); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 40 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 41 | /// Destructor |
| 42 | ~FileContent(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 43 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 44 | /// 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 Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 51 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 52 | /// 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 Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 59 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 60 | /// Copy constructor |
| 61 | File(const File&) = default; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 62 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 63 | /// Move constructor |
| 64 | File(File&&) = default; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 65 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 66 | /// Destructor |
| 67 | ~File(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 68 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 69 | /// file path |
| 70 | const std::string path; |
| 71 | /// file content |
| 72 | const FileContent content; |
| 73 | }; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 74 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 75 | /// 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 Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 83 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 84 | /// 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 Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 90 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 91 | /// 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 Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 97 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 98 | /// 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 Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 103 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 104 | /// 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 Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 109 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 110 | /// 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 Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 113 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 114 | /// 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 Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 118 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 119 | /// 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 Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 167 | /// @param n the number of characters to shift by |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 168 | /// @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 Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 179 | } |
| 180 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 181 | /// 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 Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 188 | } |
| 189 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 190 | /// 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 Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 194 | }; |
| 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 sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 200 | inline std::ostream& operator<<(std::ostream& out, const Source::Location& loc) { |
| 201 | out << loc.line << ":" << loc.column; |
| 202 | return out; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 203 | } |
| 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 |
| 209 | inline std::ostream& operator<<(std::ostream& out, const Source::Range& range) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 210 | out << "[" << range.begin << ", " << range.end << "]"; |
| 211 | return out; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 212 | } |
| 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 |
| 218 | std::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 sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 224 | inline std::ostream& operator<<(std::ostream& out, const Source::FileContent& content) { |
| 225 | out << content.data; |
| 226 | return out; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | } // namespace tint |
| 230 | |
| 231 | #endif // SRC_TINT_SOURCE_H_ |