Dan Sinclair | 6e58189 | 2020-03-02 15:47:43 -0500 | [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_SOURCE_H_ |
| 17 | #define SRC_SOURCE_H_ |
| 18 | |
| 19 | #include <stddef.h> |
| 20 | |
Ben Clayton | 5bee67f | 2020-10-30 20:44:53 +0000 | [diff] [blame] | 21 | #include <string> |
| 22 | #include <vector> |
| 23 | |
Dan Sinclair | 6e58189 | 2020-03-02 15:47:43 -0500 | [diff] [blame] | 24 | namespace tint { |
| 25 | |
Ben Clayton | 5bee67f | 2020-10-30 20:44:53 +0000 | [diff] [blame] | 26 | /// Source describes a range of characters within a source file. |
| 27 | class Source { |
| 28 | public: |
| 29 | /// File describes a source file, including path and content. |
| 30 | class File { |
| 31 | public: |
| 32 | /// Constructs the File with the given file path and content. |
dan sinclair | 3d0e273 | 2020-11-02 16:03:38 +0000 | [diff] [blame] | 33 | /// @param file_path the path for this file |
| 34 | /// @param file_content the file contents |
Ben Clayton | 5bee67f | 2020-10-30 20:44:53 +0000 | [diff] [blame] | 35 | File(const std::string& file_path, const std::string& file_content); |
| 36 | ~File(); |
| 37 | |
dan sinclair | 3d0e273 | 2020-11-02 16:03:38 +0000 | [diff] [blame] | 38 | /// file path (optional) |
| 39 | const std::string path; |
| 40 | /// file content |
| 41 | const std::string content; |
Ben Clayton | f8971ae | 2020-12-02 15:31:08 +0000 | [diff] [blame^] | 42 | /// #content split by lines |
dan sinclair | 3d0e273 | 2020-11-02 16:03:38 +0000 | [diff] [blame] | 43 | const std::vector<std::string> lines; |
Ben Clayton | 5bee67f | 2020-10-30 20:44:53 +0000 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | /// Location holds a 1-based line and column index. |
Ben Clayton | 5bee67f | 2020-10-30 20:44:53 +0000 | [diff] [blame] | 47 | class Location { |
| 48 | public: |
Ben Clayton | 2d89d98 | 2020-11-02 18:02:18 +0000 | [diff] [blame] | 49 | /// the 1-based line number. 0 represents no line information. |
Ben Clayton | 5bee67f | 2020-10-30 20:44:53 +0000 | [diff] [blame] | 50 | size_t line = 0; |
Ben Clayton | 2d89d98 | 2020-11-02 18:02:18 +0000 | [diff] [blame] | 51 | /// the 1-based column number. 0 represents no column information. |
Ben Clayton | 5bee67f | 2020-10-30 20:44:53 +0000 | [diff] [blame] | 52 | size_t column = 0; |
| 53 | }; |
| 54 | |
| 55 | /// Range holds a Location interval described by [begin, end). |
| 56 | class Range { |
| 57 | public: |
| 58 | /// Constructs a zero initialized Range. |
| 59 | inline Range() = default; |
| 60 | |
Ben Clayton | f8971ae | 2020-12-02 15:31:08 +0000 | [diff] [blame^] | 61 | /// Constructs a zero-length Range starting at `loc` |
| 62 | /// @param loc the start and end location for the range |
Ben Clayton | 5bee67f | 2020-10-30 20:44:53 +0000 | [diff] [blame] | 63 | inline explicit Range(const Location& loc) : begin(loc), end(loc) {} |
| 64 | |
Ben Clayton | f8971ae | 2020-12-02 15:31:08 +0000 | [diff] [blame^] | 65 | /// Constructs the Range beginning at `b` and ending at `e` |
| 66 | /// @param b the range start location |
| 67 | /// @param e the range end location |
Ben Clayton | 5bee67f | 2020-10-30 20:44:53 +0000 | [diff] [blame] | 68 | inline Range(const Location& b, const Location& e) : begin(b), end(e) {} |
| 69 | |
dan sinclair | 3d0e273 | 2020-11-02 16:03:38 +0000 | [diff] [blame] | 70 | /// The location of the first character in the range. |
| 71 | Location begin; |
| 72 | /// The location of one-past the last character in the range. |
| 73 | Location end; |
Ben Clayton | 5bee67f | 2020-10-30 20:44:53 +0000 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | /// Constructs the Source with an zero initialized Range and null File. |
| 77 | inline Source() = default; |
| 78 | |
Ben Clayton | f8971ae | 2020-12-02 15:31:08 +0000 | [diff] [blame^] | 79 | /// Constructs the Source with the Range `rng` and a null File |
Ben Clayton | 2d89d98 | 2020-11-02 18:02:18 +0000 | [diff] [blame] | 80 | /// @param rng the source range |
Ben Clayton | 5bee67f | 2020-10-30 20:44:53 +0000 | [diff] [blame] | 81 | inline explicit Source(const Range& rng) : range(rng) {} |
| 82 | |
Ben Clayton | f8971ae | 2020-12-02 15:31:08 +0000 | [diff] [blame^] | 83 | /// Constructs the Source with the Range `loc` and a null File |
Ben Clayton | 2d89d98 | 2020-11-02 18:02:18 +0000 | [diff] [blame] | 84 | /// @param loc the start and end location for the source range |
Ben Clayton | 5bee67f | 2020-10-30 20:44:53 +0000 | [diff] [blame] | 85 | inline explicit Source(const Location& loc) : range(Range(loc)) {} |
| 86 | |
Ben Clayton | f8971ae | 2020-12-02 15:31:08 +0000 | [diff] [blame^] | 87 | /// Constructs the Source with the Range `rng` and File `f` |
Ben Clayton | 2d89d98 | 2020-11-02 18:02:18 +0000 | [diff] [blame] | 88 | /// @param rng the source range |
| 89 | /// @param f the source file |
Ben Clayton | 5bee67f | 2020-10-30 20:44:53 +0000 | [diff] [blame] | 90 | inline Source(const Range& rng, File const* f) : range(rng), file(f) {} |
| 91 | |
Ben Clayton | f8971ae | 2020-12-02 15:31:08 +0000 | [diff] [blame^] | 92 | /// range is the span of text this source refers to in #file |
Ben Clayton | 5bee67f | 2020-10-30 20:44:53 +0000 | [diff] [blame] | 93 | Range range; |
Ben Clayton | 2d89d98 | 2020-11-02 18:02:18 +0000 | [diff] [blame] | 94 | /// file is the source file this source refers to |
Ben Clayton | 5bee67f | 2020-10-30 20:44:53 +0000 | [diff] [blame] | 95 | File const* file = nullptr; |
Dan Sinclair | 6e58189 | 2020-03-02 15:47:43 -0500 | [diff] [blame] | 96 | }; |
| 97 | |
| 98 | } // namespace tint |
| 99 | |
| 100 | #endif // SRC_SOURCE_H_ |