blob: 2e99ab859a27b3783dc1829019047adff3cef40b [file] [log] [blame]
Yifan Honga4b53d02016-10-31 17:29:10 -07001/*
2 * Copyright (C) 2016 The Android Open Source Project
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
17#ifndef LOCATION_H_
18#define LOCATION_H_
19
Steven Moreland6f2f2c02017-08-05 00:32:14 +000020#include <stdint.h>
Timur Iskhakove8ee6a02017-09-06 11:42:10 -070021#include <ostream>
Yifan Honga4b53d02016-10-31 17:29:10 -070022#include <string>
23
24// Mimics for yy::location and yy::position
25namespace android {
26
27struct Position {
Timur Iskhakov505316c2017-08-05 03:38:59 +000028 Position() = default;
Timur Iskhakov0e7c2a92017-09-06 11:40:59 -070029 Position(std::string filename, size_t line, size_t column);
30
31 const std::string& filename() const;
32
33 size_t line() const;
34 size_t column() const;
35
36 static bool inSameFile(const Position& lhs, const Position& rhs);
37
Timur Iskhakov0079fd52017-09-08 23:45:01 -070038 // Precondition: inSameFile()
Timur Iskhakov0e7c2a92017-09-06 11:40:59 -070039 bool operator<(const Position& pos) const;
Yifan Honga4b53d02016-10-31 17:29:10 -070040
Timur Iskhakov505316c2017-08-05 03:38:59 +000041 private:
Yifan Honga4b53d02016-10-31 17:29:10 -070042 // File name to which this position refers.
Timur Iskhakov505316c2017-08-05 03:38:59 +000043 std::string mFilename;
Yifan Honga4b53d02016-10-31 17:29:10 -070044 // Current line number.
Timur Iskhakov505316c2017-08-05 03:38:59 +000045 size_t mLine;
Yifan Honga4b53d02016-10-31 17:29:10 -070046 // Current column number.
Timur Iskhakov505316c2017-08-05 03:38:59 +000047 size_t mColumn;
Yifan Honga4b53d02016-10-31 17:29:10 -070048};
49
Timur Iskhakov0e7c2a92017-09-06 11:40:59 -070050std::ostream& operator<<(std::ostream& ostr, const Position& pos);
Yifan Honga4b53d02016-10-31 17:29:10 -070051
52struct Location {
Timur Iskhakov505316c2017-08-05 03:38:59 +000053 Location() = default;
Timur Iskhakov0e7c2a92017-09-06 11:40:59 -070054 Location(const Position& begin, const Position& end);
Yifan Honga4b53d02016-10-31 17:29:10 -070055
Timur Iskhakov0e7c2a92017-09-06 11:40:59 -070056 void setLocation(const Position& begin, const Position& end);
Timur Iskhakov505316c2017-08-05 03:38:59 +000057
Timur Iskhakov0e7c2a92017-09-06 11:40:59 -070058 bool isValid() const;
59 const Position& begin() const;
60 const Position& end() const;
Timur Iskhakov505316c2017-08-05 03:38:59 +000061
Timur Iskhakov0e7c2a92017-09-06 11:40:59 -070062 static Location startOf(const std::string& path);
63
64 static bool inSameFile(const Location& lhs, const Location& rhs);
65 static bool intersect(const Location& lhs, const Location& rhs);
66
Timur Iskhakov0079fd52017-09-08 23:45:01 -070067 // Precondition: inSameFile() && !intersect()
Timur Iskhakov0e7c2a92017-09-06 11:40:59 -070068 bool operator<(const Location& loc) const;
Yifan Honga4b53d02016-10-31 17:29:10 -070069
Timur Iskhakov505316c2017-08-05 03:38:59 +000070 private:
71 bool mIsValid = false;
72
Yifan Honga4b53d02016-10-31 17:29:10 -070073 // Beginning of the located region.
Timur Iskhakov505316c2017-08-05 03:38:59 +000074 Position mBegin;
Yifan Honga4b53d02016-10-31 17:29:10 -070075 // End of the located region.
Timur Iskhakov505316c2017-08-05 03:38:59 +000076 Position mEnd;
Yifan Honga4b53d02016-10-31 17:29:10 -070077};
78
Timur Iskhakov0e7c2a92017-09-06 11:40:59 -070079std::ostream& operator<<(std::ostream& ostr, const Location& loc);
Yifan Honga4b53d02016-10-31 17:29:10 -070080
81} // namespace android
82
Timur Iskhakov0e7c2a92017-09-06 11:40:59 -070083#endif // LOCATION_H_