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 | 505316c | 2017-08-05 03:38:59 +0000 | [diff] [blame] | 21 | #include <iostream> |
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; |
Yifan Hong | a4b53d0 | 2016-10-31 17:29:10 -0700 | [diff] [blame] | 29 | Position(std::string f, size_t l, size_t c) |
| 30 | : mFilename(f), mLine(l), mColumn(c) {} |
| 31 | inline const std::string &filename() const { return mFilename; } |
| 32 | inline size_t line() const { return mLine; } |
| 33 | inline size_t column() const { return mColumn; } |
| 34 | |
Timur Iskhakov | 505316c | 2017-08-05 03:38:59 +0000 | [diff] [blame] | 35 | private: |
Yifan Hong | a4b53d0 | 2016-10-31 17:29:10 -0700 | [diff] [blame] | 36 | // File name to which this position refers. |
Timur Iskhakov | 505316c | 2017-08-05 03:38:59 +0000 | [diff] [blame] | 37 | std::string mFilename; |
Yifan Hong | a4b53d0 | 2016-10-31 17:29:10 -0700 | [diff] [blame] | 38 | // Current line number. |
Timur Iskhakov | 505316c | 2017-08-05 03:38:59 +0000 | [diff] [blame] | 39 | size_t mLine; |
Yifan Hong | a4b53d0 | 2016-10-31 17:29:10 -0700 | [diff] [blame] | 40 | // Current column number. |
Timur Iskhakov | 505316c | 2017-08-05 03:38:59 +0000 | [diff] [blame] | 41 | size_t mColumn; |
Yifan Hong | a4b53d0 | 2016-10-31 17:29:10 -0700 | [diff] [blame] | 42 | }; |
| 43 | |
| 44 | inline std::ostream& operator<< (std::ostream& ostr, const Position& pos) { |
| 45 | if (!pos.filename().empty()) { |
| 46 | ostr << pos.filename() << ":"; |
| 47 | } |
| 48 | return ostr << pos.line() << "." << pos.column(); |
| 49 | } |
| 50 | |
| 51 | struct Location { |
Timur Iskhakov | 505316c | 2017-08-05 03:38:59 +0000 | [diff] [blame] | 52 | Location() = default; |
| 53 | Location(const Position& begin, const Position& end) { setLocation(begin, end); } |
Yifan Hong | a4b53d0 | 2016-10-31 17:29:10 -0700 | [diff] [blame] | 54 | |
Timur Iskhakov | 505316c | 2017-08-05 03:38:59 +0000 | [diff] [blame] | 55 | void setLocation(const Position& begin, const Position& end) { |
| 56 | mIsValid = true; |
| 57 | mBegin = begin; |
| 58 | mEnd = end; |
| 59 | } |
| 60 | |
| 61 | bool isValid() const { return mIsValid; } |
| 62 | const Position& begin() const { return mBegin; } |
| 63 | const Position& end() const { return mEnd; } |
| 64 | |
| 65 | static Location startOf(const std::string& path) { |
Yifan Hong | a4b53d0 | 2016-10-31 17:29:10 -0700 | [diff] [blame] | 66 | return Location(Position(path, 1, 1), Position(path, 1, 1)); |
| 67 | } |
| 68 | |
Timur Iskhakov | 505316c | 2017-08-05 03:38:59 +0000 | [diff] [blame] | 69 | private: |
| 70 | bool mIsValid = false; |
| 71 | |
Yifan Hong | a4b53d0 | 2016-10-31 17:29:10 -0700 | [diff] [blame] | 72 | // Beginning of the located region. |
Timur Iskhakov | 505316c | 2017-08-05 03:38:59 +0000 | [diff] [blame] | 73 | Position mBegin; |
Yifan Hong | a4b53d0 | 2016-10-31 17:29:10 -0700 | [diff] [blame] | 74 | // End of the located region. |
Timur Iskhakov | 505316c | 2017-08-05 03:38:59 +0000 | [diff] [blame] | 75 | Position mEnd; |
Yifan Hong | a4b53d0 | 2016-10-31 17:29:10 -0700 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | inline std::ostream& operator<< (std::ostream& ostr, const Location& loc) { |
| 79 | Position last = Position(loc.end().filename(), |
| 80 | loc.end().line(), |
| 81 | std::max<size_t>(1u, loc.end().column() - 1)); |
| 82 | ostr << loc.begin(); |
| 83 | if (loc.begin().filename() != last.filename()) { |
| 84 | ostr << "-" << last; |
| 85 | } else if (loc.begin().line() != last.line()) { |
| 86 | ostr << "-" << last.line() << "." << last.column(); |
| 87 | } else if (loc.begin().column() != last.column()) { |
| 88 | ostr << "-" << last.column(); |
| 89 | } |
| 90 | return ostr; |
| 91 | } |
| 92 | |
| 93 | } // namespace android |
| 94 | |
| 95 | #endif |