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 <stdint.h>
21 #include <ostream>
22 #include <string>
23 
24 // Mimics for yy::location and yy::position
25 namespace android {
26 
27 struct Position {
28     Position() = default;
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 
38     // Precondition: inSameFile()
39     bool operator<(const Position& pos) const;
40 
41    private:
42     // File name to which this position refers.
43     std::string mFilename;
44     // Current line number.
45     size_t mLine;
46     // Current column number.
47     size_t mColumn;
48 };
49 
50 std::ostream& operator<<(std::ostream& ostr, const Position& pos);
51 
52 struct Location {
53     Location() = default;
54     Location(const Position& begin, const Position& end);
55 
56     void setLocation(const Position& begin, const Position& end);
57 
58     bool isValid() const;
59     const Position& begin() const;
60     const Position& end() const;
61 
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 
67     // Precondition: inSameFile() && !intersect()
68     bool operator<(const Location& loc) const;
69 
70    private:
71     bool mIsValid = false;
72 
73     // Beginning of the located region.
74     Position mBegin;
75     // End of the located region.
76     Position mEnd;
77 };
78 
79 std::ostream& operator<<(std::ostream& ostr, const Location& loc);
80 
81 } // namespace android
82 
83 #endif  // LOCATION_H_
84