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. |
| 33 | File(const std::string& file_path, const std::string& file_content); |
| 34 | ~File(); |
| 35 | |
| 36 | const std::string path; /// file path (optional) |
| 37 | const std::string content; /// file content |
| 38 | const std::vector<std::string> lines; /// |content| split by lines |
| 39 | }; |
| 40 | |
| 41 | /// Location holds a 1-based line and column index. |
| 42 | /// 0's for |line| or |column| represent invalid values. |
| 43 | class Location { |
| 44 | public: |
| 45 | size_t line = 0; |
| 46 | size_t column = 0; |
| 47 | }; |
| 48 | |
| 49 | /// Range holds a Location interval described by [begin, end). |
| 50 | class Range { |
| 51 | public: |
| 52 | /// Constructs a zero initialized Range. |
| 53 | inline Range() = default; |
| 54 | |
| 55 | /// Constructs a zero-length Range starting at |loc|. |
| 56 | inline explicit Range(const Location& loc) : begin(loc), end(loc) {} |
| 57 | |
| 58 | /// Constructs the Range beginning at |b| and ending at |e|. |
| 59 | inline Range(const Location& b, const Location& e) : begin(b), end(e) {} |
| 60 | |
| 61 | Location begin; /// The location of the first character in the range. |
| 62 | Location end; /// The location of one-past the last character in the range. |
| 63 | }; |
| 64 | |
| 65 | /// Constructs the Source with an zero initialized Range and null File. |
| 66 | inline Source() = default; |
| 67 | |
| 68 | /// Constructs the Source with the Range |rng| and a null File. |
| 69 | inline explicit Source(const Range& rng) : range(rng) {} |
| 70 | |
| 71 | /// Constructs the Source with the Range |loc| and a null File. |
| 72 | inline explicit Source(const Location& loc) : range(Range(loc)) {} |
| 73 | |
| 74 | /// Constructs the Source with the Range |rng| and File |f|. |
| 75 | inline Source(const Range& rng, File const* f) : range(rng), file(f) {} |
| 76 | |
| 77 | /// Constructs the Source with the zero-length range starting at |line| and |
| 78 | /// |column| with a null File. |
| 79 | /// TODO(bclayton): Remove this constructor. |
| 80 | /// It purely exists to break up changes into bite sized pieces. |
| 81 | inline explicit Source(size_t line, size_t column) |
| 82 | : Source(Location{line, column}) {} |
| 83 | |
| 84 | Range range; |
| 85 | File const* file = nullptr; |
Dan Sinclair | 6e58189 | 2020-03-02 15:47:43 -0500 | [diff] [blame] | 86 | }; |
| 87 | |
| 88 | } // namespace tint |
| 89 | |
| 90 | #endif // SRC_SOURCE_H_ |