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 | |
| 20 | #include <iostream> |
| 21 | #include <stdint.h> |
| 22 | #include <string> |
| 23 | |
| 24 | // Mimics for yy::location and yy::position |
| 25 | namespace android { |
| 26 | |
| 27 | struct Position { |
| 28 | Position(std::string f, size_t l, size_t c) |
| 29 | : mFilename(f), mLine(l), mColumn(c) {} |
| 30 | inline const std::string &filename() const { return mFilename; } |
| 31 | inline size_t line() const { return mLine; } |
| 32 | inline size_t column() const { return mColumn; } |
| 33 | |
| 34 | private: |
| 35 | // File name to which this position refers. |
| 36 | const std::string mFilename; |
| 37 | // Current line number. |
| 38 | const size_t mLine; |
| 39 | // Current column number. |
| 40 | const size_t mColumn; |
| 41 | }; |
| 42 | |
| 43 | inline std::ostream& operator<< (std::ostream& ostr, const Position& pos) { |
| 44 | if (!pos.filename().empty()) { |
| 45 | ostr << pos.filename() << ":"; |
| 46 | } |
| 47 | return ostr << pos.line() << "." << pos.column(); |
| 48 | } |
| 49 | |
| 50 | struct Location { |
| 51 | Location (Position begin, Position end) |
| 52 | : mBegin(begin), mEnd(end) {} |
| 53 | inline const Position &begin() const { return mBegin; } |
| 54 | inline const Position &end() const { return mEnd; } |
| 55 | |
| 56 | inline static Location startOf(const std::string &path) { |
| 57 | return Location(Position(path, 1, 1), Position(path, 1, 1)); |
| 58 | } |
| 59 | |
| 60 | private: |
| 61 | // Beginning of the located region. |
| 62 | const Position mBegin; |
| 63 | // End of the located region. |
| 64 | const Position mEnd; |
| 65 | }; |
| 66 | |
| 67 | inline std::ostream& operator<< (std::ostream& ostr, const Location& loc) { |
| 68 | Position last = Position(loc.end().filename(), |
| 69 | loc.end().line(), |
| 70 | std::max<size_t>(1u, loc.end().column() - 1)); |
| 71 | ostr << loc.begin(); |
| 72 | if (loc.begin().filename() != last.filename()) { |
| 73 | ostr << "-" << last; |
| 74 | } else if (loc.begin().line() != last.line()) { |
| 75 | ostr << "-" << last.line() << "." << last.column(); |
| 76 | } else if (loc.begin().column() != last.column()) { |
| 77 | ostr << "-" << last.column(); |
| 78 | } |
| 79 | return ostr; |
| 80 | } |
| 81 | |
| 82 | } // namespace android |
| 83 | |
| 84 | #endif |