blob: 7407b60a29e4344051cb2ff69770ebc6cdd74ace [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
38 bool operator<(const Position& pos) const;
Yifan Honga4b53d02016-10-31 17:29:10 -070039
Timur Iskhakov505316c2017-08-05 03:38:59 +000040 private:
Yifan Honga4b53d02016-10-31 17:29:10 -070041 // File name to which this position refers.
Timur Iskhakov505316c2017-08-05 03:38:59 +000042 std::string mFilename;
Yifan Honga4b53d02016-10-31 17:29:10 -070043 // Current line number.
Timur Iskhakov505316c2017-08-05 03:38:59 +000044 size_t mLine;
Yifan Honga4b53d02016-10-31 17:29:10 -070045 // Current column number.
Timur Iskhakov505316c2017-08-05 03:38:59 +000046 size_t mColumn;
Yifan Honga4b53d02016-10-31 17:29:10 -070047};
48
Timur Iskhakov0e7c2a92017-09-06 11:40:59 -070049std::ostream& operator<<(std::ostream& ostr, const Position& pos);
Yifan Honga4b53d02016-10-31 17:29:10 -070050
51struct Location {
Timur Iskhakov505316c2017-08-05 03:38:59 +000052 Location() = default;
Timur Iskhakov0e7c2a92017-09-06 11:40:59 -070053 Location(const Position& begin, const Position& end);
Yifan Honga4b53d02016-10-31 17:29:10 -070054
Timur Iskhakov0e7c2a92017-09-06 11:40:59 -070055 void setLocation(const Position& begin, const Position& end);
Timur Iskhakov505316c2017-08-05 03:38:59 +000056
Timur Iskhakov0e7c2a92017-09-06 11:40:59 -070057 bool isValid() const;
58 const Position& begin() const;
59 const Position& end() const;
Timur Iskhakov505316c2017-08-05 03:38:59 +000060
Timur Iskhakov0e7c2a92017-09-06 11:40:59 -070061 static Location startOf(const std::string& path);
62
63 static bool inSameFile(const Location& lhs, const Location& rhs);
64 static bool intersect(const Location& lhs, const Location& rhs);
65
66 bool operator<(const Location& loc) const;
Yifan Honga4b53d02016-10-31 17:29:10 -070067
Timur Iskhakov505316c2017-08-05 03:38:59 +000068 private:
69 bool mIsValid = false;
70
Yifan Honga4b53d02016-10-31 17:29:10 -070071 // Beginning of the located region.
Timur Iskhakov505316c2017-08-05 03:38:59 +000072 Position mBegin;
Yifan Honga4b53d02016-10-31 17:29:10 -070073 // End of the located region.
Timur Iskhakov505316c2017-08-05 03:38:59 +000074 Position mEnd;
Yifan Honga4b53d02016-10-31 17:29:10 -070075};
76
Timur Iskhakov0e7c2a92017-09-06 11:40:59 -070077std::ostream& operator<<(std::ostream& ostr, const Location& loc);
Yifan Honga4b53d02016-10-31 17:29:10 -070078
79} // namespace android
80
Timur Iskhakov0e7c2a92017-09-06 11:40:59 -070081#endif // LOCATION_H_