/Users/deen/code/yugabyte-db/src/yb/yql/cql/ql/ptree/yb_location.h
Line | Count | Source (jump to first uncovered line) |
1 | | //-------------------------------------------------------------------------------------------------- |
2 | | // Copyright (c) YugaByte, Inc. |
3 | | // |
4 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
5 | | // in compliance with the License. You may obtain a copy of the License at |
6 | | // |
7 | | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
10 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
11 | | // or implied. See the License for the specific language governing permissions and limitations |
12 | | // under the License. |
13 | | // |
14 | | // |
15 | | // This module defines an abstract interface for location(line and column) of a token in a SQL |
16 | | // statement. The location value is to be kept in tree nodes, and when reporting an execution error |
17 | | // or warning, SQL engine will use this value to indicate where the error occurs. |
18 | | //-------------------------------------------------------------------------------------------------- |
19 | | |
20 | | #ifndef YB_YQL_CQL_QL_PTREE_YB_LOCATION_H_ |
21 | | #define YB_YQL_CQL_QL_PTREE_YB_LOCATION_H_ |
22 | | |
23 | | #include <stdio.h> |
24 | | |
25 | | #include "yb/util/memory/mc_types.h" |
26 | | |
27 | | namespace yb { |
28 | | namespace ql { |
29 | | |
30 | | class YBLocation : public MCBase { |
31 | | public: |
32 | | //------------------------------------------------------------------------------------------------ |
33 | | // Public types. |
34 | | typedef MCSharedPtr<YBLocation> SharedPtr; |
35 | | typedef MCSharedPtr<const YBLocation> SharedPtrConst; |
36 | | |
37 | | typedef MCUniPtr<YBLocation> UniPtr; |
38 | | typedef MCUniPtr<const YBLocation> UniPtrConst; |
39 | | |
40 | | //------------------------------------------------------------------------------------------------ |
41 | | // Constructor and destructor. |
42 | 4.42M | YBLocation() { |
43 | 4.42M | } |
44 | 4.42M | ~YBLocation() { |
45 | 4.42M | } |
46 | | |
47 | | //------------------------------------------------------------------------------------------------ |
48 | | // Abstract interface for location of a token. |
49 | | |
50 | | // Line and column at the beginning character of a token. |
51 | | virtual int BeginLine() const = 0; |
52 | | virtual int BeginColumn() const = 0; |
53 | | |
54 | | // Line and column at the ending character of a token. |
55 | | virtual int EndLine() const = 0; |
56 | | virtual int EndColumn() const = 0; |
57 | | |
58 | | // Convert to string for printing messages. |
59 | | // In general, we only need to record the starting location of a token where error occurs. |
60 | | template<typename StringType> |
61 | 15 | void ToString(StringType *msg, bool starting_location_only = true) const { |
62 | 15 | char temp[4096]; |
63 | 15 | if (starting_location_only) { |
64 | 0 | snprintf(temp, sizeof(temp), "%d.%d", BeginLine(), BeginColumn()); |
65 | 15 | } else if (BeginLine() == EndLine()) { |
66 | 15 | if (BeginColumn() == EndColumn()) { |
67 | 6 | snprintf(temp, sizeof(temp), "%d.%d", BeginLine(), BeginColumn()); |
68 | 9 | } else { |
69 | 9 | snprintf(temp, sizeof(temp), "%d.%d-%d", BeginLine(), BeginColumn(), EndColumn()); |
70 | 9 | } |
71 | 0 | } else { |
72 | 0 | snprintf(temp, sizeof(temp), "%d.%d-%d.%d", |
73 | 0 | BeginLine(), BeginColumn(), EndLine(), EndColumn()); |
74 | 0 | } |
75 | 15 | *msg += temp; |
76 | 15 | } |
77 | | }; |
78 | | |
79 | | //-------------------------------------------------------------------------------------------------- |
80 | | |
81 | | // Template to print the location as "line#.column#" such as "1.32" for line #1 and column #32. |
82 | | template <typename YYChar> |
83 | | inline std::basic_ostream<YYChar>& |
84 | 0 | operator<< (std::basic_ostream<YYChar>& ostr, const YBLocation& loc) { |
85 | | // In general, we only need to record the starting location of a token where error occurs. |
86 | 0 | constexpr bool kRecordStartingLocationOnly = true; |
87 | |
|
88 | 0 | if (kRecordStartingLocationOnly) { |
89 | 0 | ostr << loc.BeginLine() << "." << loc.BeginColumn(); |
90 | 0 | } else if (loc.BeginLine() == loc.EndLine() && loc.BeginColumn() == loc.EndColumn()) { |
91 | 0 | ostr << loc.BeginLine() << "." << loc.BeginColumn(); |
92 | 0 | } else { |
93 | 0 | ostr << loc.BeginLine() << "." << loc.BeginColumn() |
94 | 0 | << " - " << loc.EndLine() << "." << loc.EndColumn(); |
95 | 0 | } |
96 | 0 | return ostr; |
97 | 0 | } |
98 | | |
99 | | } // namespace ql |
100 | | } // namespace yb |
101 | | |
102 | | #endif // YB_YQL_CQL_QL_PTREE_YB_LOCATION_H_ |