blob: b77fa022d0618b033cec34bc9f3cbef24e2bca5a [file] [log] [blame]
Dan Sinclair6e581892020-03-02 15:47:43 -05001
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 Clayton1d982362021-02-18 21:40:19 +000021#include <ostream>
Ben Clayton5bee67f2020-10-30 20:44:53 +000022#include <string>
23#include <vector>
24
Dan Sinclair6e581892020-03-02 15:47:43 -050025namespace tint {
26
Ben Clayton5bee67f2020-10-30 20:44:53 +000027/// Source describes a range of characters within a source file.
28class Source {
29 public:
Ben Clayton1d982362021-02-18 21:40:19 +000030 /// FileContent describes the content of a source file.
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);
36
37 /// Destructor
38 ~FileContent();
39
40 /// un-split file content
41 const std::string data;
42 /// #data split by lines
43 const std::vector<std::string> lines;
44 };
45
Ben Clayton5bee67f2020-10-30 20:44:53 +000046 /// File describes a source file, including path and content.
47 class File {
48 public:
49 /// Constructs the File with the given file path and content.
Ben Clayton1d982362021-02-18 21:40:19 +000050 /// @param p the path for this file
51 /// @param c the file contents
52 inline File(const std::string& p, const std::string& c)
53 : path(p), content(c) {}
54
Ben Clayton5bee67f2020-10-30 20:44:53 +000055 ~File();
56
dan sinclair3d0e2732020-11-02 16:03:38 +000057 /// file path (optional)
58 const std::string path;
59 /// file content
Ben Clayton1d982362021-02-18 21:40:19 +000060 const FileContent content;
Ben Clayton5bee67f2020-10-30 20:44:53 +000061 };
62
63 /// Location holds a 1-based line and column index.
Ben Clayton5bee67f2020-10-30 20:44:53 +000064 class Location {
65 public:
Ben Clayton2d89d982020-11-02 18:02:18 +000066 /// the 1-based line number. 0 represents no line information.
Ben Clayton5bee67f2020-10-30 20:44:53 +000067 size_t line = 0;
Ben Clayton2d89d982020-11-02 18:02:18 +000068 /// the 1-based column number. 0 represents no column information.
Ben Clayton5bee67f2020-10-30 20:44:53 +000069 size_t column = 0;
70 };
71
72 /// Range holds a Location interval described by [begin, end).
73 class Range {
74 public:
75 /// Constructs a zero initialized Range.
76 inline Range() = default;
77
Ben Claytonf8971ae2020-12-02 15:31:08 +000078 /// Constructs a zero-length Range starting at `loc`
79 /// @param loc the start and end location for the range
Ben Clayton5bee67f2020-10-30 20:44:53 +000080 inline explicit Range(const Location& loc) : begin(loc), end(loc) {}
81
Ben Claytonf8971ae2020-12-02 15:31:08 +000082 /// Constructs the Range beginning at `b` and ending at `e`
83 /// @param b the range start location
84 /// @param e the range end location
Ben Clayton5bee67f2020-10-30 20:44:53 +000085 inline Range(const Location& b, const Location& e) : begin(b), end(e) {}
86
dan sinclair3d0e2732020-11-02 16:03:38 +000087 /// The location of the first character in the range.
88 Location begin;
89 /// The location of one-past the last character in the range.
90 Location end;
Ben Clayton5bee67f2020-10-30 20:44:53 +000091 };
92
93 /// Constructs the Source with an zero initialized Range and null File.
Ben Clayton1d982362021-02-18 21:40:19 +000094 inline Source() : range() {}
Ben Clayton5bee67f2020-10-30 20:44:53 +000095
Ben Claytonf8971ae2020-12-02 15:31:08 +000096 /// Constructs the Source with the Range `rng` and a null File
Ben Clayton2d89d982020-11-02 18:02:18 +000097 /// @param rng the source range
Ben Clayton5bee67f2020-10-30 20:44:53 +000098 inline explicit Source(const Range& rng) : range(rng) {}
99
Ben Claytonf8971ae2020-12-02 15:31:08 +0000100 /// Constructs the Source with the Range `loc` and a null File
Ben Clayton2d89d982020-11-02 18:02:18 +0000101 /// @param loc the start and end location for the source range
Ben Clayton5bee67f2020-10-30 20:44:53 +0000102 inline explicit Source(const Location& loc) : range(Range(loc)) {}
103
Ben Clayton1d982362021-02-18 21:40:19 +0000104 /// Constructs the Source with the Range `rng` and File `file`
Ben Clayton2d89d982020-11-02 18:02:18 +0000105 /// @param rng the source range
Ben Clayton1d982362021-02-18 21:40:19 +0000106 /// @param file the source file
107 inline Source(const Range& rng, File const* file)
108 : range(rng), file_path(file->path), file_content(&file->content) {}
Ben Clayton5bee67f2020-10-30 20:44:53 +0000109
Ben Clayton1d982362021-02-18 21:40:19 +0000110 /// Constructs the Source with the Range `rng`, file path `path` and content
111 /// `content`
112 /// @param rng the source range
113 /// @param path the source file path
114 /// @param content the source file content
115 inline Source(const Range& rng,
116 const std::string& path,
117 FileContent* content = nullptr)
118 : range(rng), file_path(path), file_content(content) {}
119
120 /// range is the span of text this source refers to in #file_path
Ben Clayton5bee67f2020-10-30 20:44:53 +0000121 Range range;
Ben Clayton1d982362021-02-18 21:40:19 +0000122 /// file is the optional file path this source refers to
123 std::string file_path;
124 /// file is the optional source content this source refers to
125 const FileContent* file_content = nullptr;
Dan Sinclair6e581892020-03-02 15:47:43 -0500126};
127
Ben Clayton1d982362021-02-18 21:40:19 +0000128/// Writes the Source::FileContent to the std::ostream.
129/// @param out the std::ostream to write to
130/// @param content the file content to write
131/// @returns out so calls can be chained
132inline std::ostream& operator<<(std::ostream& out,
133 const Source::FileContent& content) {
134 out << content.data;
135 return out;
136}
137
Dan Sinclair6e581892020-03-02 15:47:43 -0500138} // namespace tint
139
140#endif // SRC_SOURCE_H_