blob: a6e78300ecf1058a78ee2cad3e68636c5a9a8d99 [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 Clayton5bee67f2020-10-30 20:44:53 +000021#include <string>
22#include <vector>
23
Dan Sinclair6e581892020-03-02 15:47:43 -050024namespace tint {
25
Ben Clayton5bee67f2020-10-30 20:44:53 +000026/// Source describes a range of characters within a source file.
27class 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 sinclair3d0e2732020-11-02 16:03:38 +000033 /// @param file_path the path for this file
34 /// @param file_content the file contents
Ben Clayton5bee67f2020-10-30 20:44:53 +000035 File(const std::string& file_path, const std::string& file_content);
36 ~File();
37
dan sinclair3d0e2732020-11-02 16:03:38 +000038 /// file path (optional)
39 const std::string path;
40 /// file content
41 const std::string content;
Ben Claytonf8971ae2020-12-02 15:31:08 +000042 /// #content split by lines
dan sinclair3d0e2732020-11-02 16:03:38 +000043 const std::vector<std::string> lines;
Ben Clayton5bee67f2020-10-30 20:44:53 +000044 };
45
46 /// Location holds a 1-based line and column index.
Ben Clayton5bee67f2020-10-30 20:44:53 +000047 class Location {
48 public:
Ben Clayton2d89d982020-11-02 18:02:18 +000049 /// the 1-based line number. 0 represents no line information.
Ben Clayton5bee67f2020-10-30 20:44:53 +000050 size_t line = 0;
Ben Clayton2d89d982020-11-02 18:02:18 +000051 /// the 1-based column number. 0 represents no column information.
Ben Clayton5bee67f2020-10-30 20:44:53 +000052 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 Claytonf8971ae2020-12-02 15:31:08 +000061 /// Constructs a zero-length Range starting at `loc`
62 /// @param loc the start and end location for the range
Ben Clayton5bee67f2020-10-30 20:44:53 +000063 inline explicit Range(const Location& loc) : begin(loc), end(loc) {}
64
Ben Claytonf8971ae2020-12-02 15:31:08 +000065 /// 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 Clayton5bee67f2020-10-30 20:44:53 +000068 inline Range(const Location& b, const Location& e) : begin(b), end(e) {}
69
dan sinclair3d0e2732020-11-02 16:03:38 +000070 /// 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 Clayton5bee67f2020-10-30 20:44:53 +000074 };
75
76 /// Constructs the Source with an zero initialized Range and null File.
77 inline Source() = default;
78
Ben Claytonf8971ae2020-12-02 15:31:08 +000079 /// Constructs the Source with the Range `rng` and a null File
Ben Clayton2d89d982020-11-02 18:02:18 +000080 /// @param rng the source range
Ben Clayton5bee67f2020-10-30 20:44:53 +000081 inline explicit Source(const Range& rng) : range(rng) {}
82
Ben Claytonf8971ae2020-12-02 15:31:08 +000083 /// Constructs the Source with the Range `loc` and a null File
Ben Clayton2d89d982020-11-02 18:02:18 +000084 /// @param loc the start and end location for the source range
Ben Clayton5bee67f2020-10-30 20:44:53 +000085 inline explicit Source(const Location& loc) : range(Range(loc)) {}
86
Ben Claytonf8971ae2020-12-02 15:31:08 +000087 /// Constructs the Source with the Range `rng` and File `f`
Ben Clayton2d89d982020-11-02 18:02:18 +000088 /// @param rng the source range
89 /// @param f the source file
Ben Clayton5bee67f2020-10-30 20:44:53 +000090 inline Source(const Range& rng, File const* f) : range(rng), file(f) {}
91
Ben Claytonf8971ae2020-12-02 15:31:08 +000092 /// range is the span of text this source refers to in #file
Ben Clayton5bee67f2020-10-30 20:44:53 +000093 Range range;
Ben Clayton2d89d982020-11-02 18:02:18 +000094 /// file is the source file this source refers to
Ben Clayton5bee67f2020-10-30 20:44:53 +000095 File const* file = nullptr;
Dan Sinclair6e581892020-03-02 15:47:43 -050096};
97
98} // namespace tint
99
100#endif // SRC_SOURCE_H_