blob: ffc873ba1b407f6304afa7f7b96ba1967465e8a5 [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;
42 /// |content| split by lines
43 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.
47 /// 0's for |line| or |column| represent invalid values.
48 class Location {
49 public:
dan sinclair3d0e2732020-11-02 16:03:38 +000050 /// The line number, 1-based
Ben Clayton5bee67f2020-10-30 20:44:53 +000051 size_t line = 0;
dan sinclair3d0e2732020-11-02 16:03:38 +000052 /// The column number, 1-based
Ben Clayton5bee67f2020-10-30 20:44:53 +000053 size_t column = 0;
54 };
55
56 /// Range holds a Location interval described by [begin, end).
57 class Range {
58 public:
59 /// Constructs a zero initialized Range.
60 inline Range() = default;
61
62 /// Constructs a zero-length Range starting at |loc|.
dan sinclair3d0e2732020-11-02 16:03:38 +000063 /// @param loc the location to use to build the range
Ben Clayton5bee67f2020-10-30 20:44:53 +000064 inline explicit Range(const Location& loc) : begin(loc), end(loc) {}
65
66 /// Constructs the Range beginning at |b| and ending at |e|.
dan sinclair3d0e2732020-11-02 16:03:38 +000067 /// @param b the beginning of the range
68 /// @param e the end of the range
Ben Clayton5bee67f2020-10-30 20:44:53 +000069 inline Range(const Location& b, const Location& e) : begin(b), end(e) {}
70
dan sinclair3d0e2732020-11-02 16:03:38 +000071 /// The location of the first character in the range.
72 Location begin;
73 /// The location of one-past the last character in the range.
74 Location end;
Ben Clayton5bee67f2020-10-30 20:44:53 +000075 };
76
77 /// Constructs the Source with an zero initialized Range and null File.
78 inline Source() = default;
79
80 /// Constructs the Source with the Range |rng| and a null File.
dan sinclair3d0e2732020-11-02 16:03:38 +000081 /// @param rng the range to assign to the source
Ben Clayton5bee67f2020-10-30 20:44:53 +000082 inline explicit Source(const Range& rng) : range(rng) {}
83
84 /// Constructs the Source with the Range |loc| and a null File.
dan sinclair3d0e2732020-11-02 16:03:38 +000085 /// @param loc the location to assign to the source
Ben Clayton5bee67f2020-10-30 20:44:53 +000086 inline explicit Source(const Location& loc) : range(Range(loc)) {}
87
88 /// Constructs the Source with the Range |rng| and File |f|.
dan sinclair3d0e2732020-11-02 16:03:38 +000089 /// @param rng the range for the source
90 /// @param f the file for the source
Ben Clayton5bee67f2020-10-30 20:44:53 +000091 inline Source(const Range& rng, File const* f) : range(rng), file(f) {}
92
dan sinclair3d0e2732020-11-02 16:03:38 +000093 /// Line/column range for this source
Ben Clayton5bee67f2020-10-30 20:44:53 +000094 Range range;
dan sinclair3d0e2732020-11-02 16:03:38 +000095 /// Source file
Ben Clayton5bee67f2020-10-30 20:44:53 +000096 File const* file = nullptr;
Dan Sinclair6e581892020-03-02 15:47:43 -050097};
98
99} // namespace tint
100
101#endif // SRC_SOURCE_H_