Yifan Hong | a4b53d0 | 2016-10-31 17:29:10 -0700 | [diff] [blame] | 1 | /* |
| 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 Moreland | 6f2f2c0 | 2017-08-05 00:32:14 +0000 | [diff] [blame] | 20 | #include <stdint.h> |
Timur Iskhakov | e8ee6a0 | 2017-09-06 11:42:10 -0700 | [diff] [blame] | 21 | #include <ostream> |
Yifan Hong | a4b53d0 | 2016-10-31 17:29:10 -0700 | [diff] [blame] | 22 | #include <string> |
| 23 | |
| 24 | // Mimics for yy::location and yy::position |
| 25 | namespace android { |
| 26 | |
| 27 | struct Position { |
Timur Iskhakov | 505316c | 2017-08-05 03:38:59 +0000 | [diff] [blame] | 28 | Position() = default; |
Timur Iskhakov | 0e7c2a9 | 2017-09-06 11:40:59 -0700 | [diff] [blame] | 29 | 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 Iskhakov | 0079fd5 | 2017-09-08 23:45:01 -0700 | [diff] [blame] | 38 | // Precondition: inSameFile() |
Timur Iskhakov | 0e7c2a9 | 2017-09-06 11:40:59 -0700 | [diff] [blame] | 39 | bool operator<(const Position& pos) const; |
Yifan Hong | a4b53d0 | 2016-10-31 17:29:10 -0700 | [diff] [blame] | 40 | |
Timur Iskhakov | 505316c | 2017-08-05 03:38:59 +0000 | [diff] [blame] | 41 | private: |
Yifan Hong | a4b53d0 | 2016-10-31 17:29:10 -0700 | [diff] [blame] | 42 | // File name to which this position refers. |
Timur Iskhakov | 505316c | 2017-08-05 03:38:59 +0000 | [diff] [blame] | 43 | std::string mFilename; |
Yifan Hong | a4b53d0 | 2016-10-31 17:29:10 -0700 | [diff] [blame] | 44 | // Current line number. |
Timur Iskhakov | 505316c | 2017-08-05 03:38:59 +0000 | [diff] [blame] | 45 | size_t mLine; |
Yifan Hong | a4b53d0 | 2016-10-31 17:29:10 -0700 | [diff] [blame] | 46 | // Current column number. |
Timur Iskhakov | 505316c | 2017-08-05 03:38:59 +0000 | [diff] [blame] | 47 | size_t mColumn; |
Yifan Hong | a4b53d0 | 2016-10-31 17:29:10 -0700 | [diff] [blame] | 48 | }; |
| 49 | |
Timur Iskhakov | 0e7c2a9 | 2017-09-06 11:40:59 -0700 | [diff] [blame] | 50 | std::ostream& operator<<(std::ostream& ostr, const Position& pos); |
Yifan Hong | a4b53d0 | 2016-10-31 17:29:10 -0700 | [diff] [blame] | 51 | |
| 52 | struct Location { |
Timur Iskhakov | 505316c | 2017-08-05 03:38:59 +0000 | [diff] [blame] | 53 | Location() = default; |
Timur Iskhakov | 0e7c2a9 | 2017-09-06 11:40:59 -0700 | [diff] [blame] | 54 | Location(const Position& begin, const Position& end); |
Yifan Hong | a4b53d0 | 2016-10-31 17:29:10 -0700 | [diff] [blame] | 55 | |
Timur Iskhakov | 0e7c2a9 | 2017-09-06 11:40:59 -0700 | [diff] [blame] | 56 | void setLocation(const Position& begin, const Position& end); |
Timur Iskhakov | 505316c | 2017-08-05 03:38:59 +0000 | [diff] [blame] | 57 | |
Timur Iskhakov | 0e7c2a9 | 2017-09-06 11:40:59 -0700 | [diff] [blame] | 58 | bool isValid() const; |
| 59 | const Position& begin() const; |
| 60 | const Position& end() const; |
Timur Iskhakov | 505316c | 2017-08-05 03:38:59 +0000 | [diff] [blame] | 61 | |
Timur Iskhakov | 0e7c2a9 | 2017-09-06 11:40:59 -0700 | [diff] [blame] | 62 | 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 Iskhakov | 0079fd5 | 2017-09-08 23:45:01 -0700 | [diff] [blame] | 67 | // Precondition: inSameFile() && !intersect() |
Timur Iskhakov | 0e7c2a9 | 2017-09-06 11:40:59 -0700 | [diff] [blame] | 68 | bool operator<(const Location& loc) const; |
Yifan Hong | a4b53d0 | 2016-10-31 17:29:10 -0700 | [diff] [blame] | 69 | |
Timur Iskhakov | 505316c | 2017-08-05 03:38:59 +0000 | [diff] [blame] | 70 | private: |
| 71 | bool mIsValid = false; |
| 72 | |
Yifan Hong | a4b53d0 | 2016-10-31 17:29:10 -0700 | [diff] [blame] | 73 | // Beginning of the located region. |
Timur Iskhakov | 505316c | 2017-08-05 03:38:59 +0000 | [diff] [blame] | 74 | Position mBegin; |
Yifan Hong | a4b53d0 | 2016-10-31 17:29:10 -0700 | [diff] [blame] | 75 | // End of the located region. |
Timur Iskhakov | 505316c | 2017-08-05 03:38:59 +0000 | [diff] [blame] | 76 | Position mEnd; |
Yifan Hong | a4b53d0 | 2016-10-31 17:29:10 -0700 | [diff] [blame] | 77 | }; |
| 78 | |
Timur Iskhakov | 0e7c2a9 | 2017-09-06 11:40:59 -0700 | [diff] [blame] | 79 | std::ostream& operator<<(std::ostream& ostr, const Location& loc); |
Yifan Hong | a4b53d0 | 2016-10-31 17:29:10 -0700 | [diff] [blame] | 80 | |
| 81 | } // namespace android |
| 82 | |
Timur Iskhakov | 0e7c2a9 | 2017-09-06 11:40:59 -0700 | [diff] [blame] | 83 | #endif // LOCATION_H_ |