blob: cdcbc7c32e6d27e98727c6c9c4d640f272e8a7c5 [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;
Yifan Honga4b53d02016-10-31 17:29:10 -070029 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 Iskhakov505316c2017-08-05 03:38:59 +000035 private:
Yifan Honga4b53d02016-10-31 17:29:10 -070036 // File name to which this position refers.
Timur Iskhakov505316c2017-08-05 03:38:59 +000037 std::string mFilename;
Yifan Honga4b53d02016-10-31 17:29:10 -070038 // Current line number.
Timur Iskhakov505316c2017-08-05 03:38:59 +000039 size_t mLine;
Yifan Honga4b53d02016-10-31 17:29:10 -070040 // Current column number.
Timur Iskhakov505316c2017-08-05 03:38:59 +000041 size_t mColumn;
Yifan Honga4b53d02016-10-31 17:29:10 -070042};
43
44inline 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
51struct Location {
Timur Iskhakov505316c2017-08-05 03:38:59 +000052 Location() = default;
53 Location(const Position& begin, const Position& end) { setLocation(begin, end); }
Yifan Honga4b53d02016-10-31 17:29:10 -070054
Timur Iskhakov505316c2017-08-05 03:38:59 +000055 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 Honga4b53d02016-10-31 17:29:10 -070066 return Location(Position(path, 1, 1), Position(path, 1, 1));
67 }
68
Timur Iskhakov505316c2017-08-05 03:38:59 +000069 private:
70 bool mIsValid = false;
71
Yifan Honga4b53d02016-10-31 17:29:10 -070072 // Beginning of the located region.
Timur Iskhakov505316c2017-08-05 03:38:59 +000073 Position mBegin;
Yifan Honga4b53d02016-10-31 17:29:10 -070074 // End of the located region.
Timur Iskhakov505316c2017-08-05 03:38:59 +000075 Position mEnd;
Yifan Honga4b53d02016-10-31 17:29:10 -070076};
77
78inline 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