1 // A Bison parser, made by GNU Bison 3.8.2. 2 3 // Locations for Bison parsers in C++ 4 5 // Copyright (C) 2002-2015, 2018-2021 Free Software Foundation, Inc. 6 7 // This program is free software: you can redistribute it and/or modify 8 // it under the terms of the GNU General Public License as published by 9 // the Free Software Foundation, either version 3 of the License, or 10 // (at your option) any later version. 11 12 // This program is distributed in the hope that it will be useful, 13 // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 // GNU General Public License for more details. 16 17 // You should have received a copy of the GNU General Public License 18 // along with this program. If not, see <https://www.gnu.org/licenses/>. 19 20 // As a special exception, you may create a larger work that contains 21 // part or all of the Bison parser skeleton and distribute that work 22 // under terms of your choice, so long as that work isn't itself a 23 // parser generator using the skeleton or a modified version thereof 24 // as a parser skeleton. Alternatively, if you modify or redistribute 25 // the parser skeleton itself, you may (at your option) remove this 26 // special exception, which will cause the skeleton and the resulting 27 // Bison output files to be licensed under the GNU General Public 28 // License without this special exception. 29 30 // This special exception was added by the Free Software Foundation in 31 // version 2.2 of Bison. 32 33 /** 34 ** \file location.hh 35 ** Define the isc::d2::location class. 36 */ 37 38 #ifndef YY_D2_PARSER_LOCATION_HH_INCLUDED 39 # define YY_D2_PARSER_LOCATION_HH_INCLUDED 40 41 # include <iostream> 42 # include <string> 43 44 # ifndef YY_NULLPTR 45 # if defined __cplusplus 46 # if 201103L <= __cplusplus 47 # define YY_NULLPTR nullptr 48 # else 49 # define YY_NULLPTR 0 50 # endif 51 # else 52 # define YY_NULLPTR ((void*)0) 53 # endif 54 # endif 55 56 #line 14 "d2_parser.yy" 57 namespace isc { namespace d2 { 58 #line 59 "location.hh" 59 60 /// A point in a source file. 61 class position 62 { 63 public: 64 /// Type for file name. 65 typedef const std::string filename_type; 66 /// Type for line and column numbers. 67 typedef int counter_type; 68 69 /// Construct a position. position(filename_type * f=YY_NULLPTR,counter_type l=1,counter_type c=1)70 explicit position (filename_type* f = YY_NULLPTR, 71 counter_type l = 1, 72 counter_type c = 1) 73 : filename (f) 74 , line (l) 75 , column (c) 76 {} 77 78 79 /// Initialization. initialize(filename_type * fn=YY_NULLPTR,counter_type l=1,counter_type c=1)80 void initialize (filename_type* fn = YY_NULLPTR, 81 counter_type l = 1, 82 counter_type c = 1) 83 { 84 filename = fn; 85 line = l; 86 column = c; 87 } 88 89 /** \name Line and Column related manipulators 90 ** \{ */ 91 /// (line related) Advance to the COUNT next lines. lines(counter_type count=1)92 void lines (counter_type count = 1) 93 { 94 if (count) 95 { 96 column = 1; 97 line = add_ (line, count, 1); 98 } 99 } 100 101 /// (column related) Advance to the COUNT next columns. columns(counter_type count=1)102 void columns (counter_type count = 1) 103 { 104 column = add_ (column, count, 1); 105 } 106 /** \} */ 107 108 /// File name to which this position refers. 109 filename_type* filename; 110 /// Current line number. 111 counter_type line; 112 /// Current column number. 113 counter_type column; 114 115 private: 116 /// Compute max (min, lhs+rhs). add_(counter_type lhs,counter_type rhs,counter_type min)117 static counter_type add_ (counter_type lhs, counter_type rhs, counter_type min) 118 { 119 return lhs + rhs < min ? min : lhs + rhs; 120 } 121 }; 122 123 /// Add \a width columns, in place. 124 inline position& operator +=(position & res,position::counter_type width)125 operator+= (position& res, position::counter_type width) 126 { 127 res.columns (width); 128 return res; 129 } 130 131 /// Add \a width columns. 132 inline position operator +(position res,position::counter_type width)133 operator+ (position res, position::counter_type width) 134 { 135 return res += width; 136 } 137 138 /// Subtract \a width columns, in place. 139 inline position& operator -=(position & res,position::counter_type width)140 operator-= (position& res, position::counter_type width) 141 { 142 return res += -width; 143 } 144 145 /// Subtract \a width columns. 146 inline position operator -(position res,position::counter_type width)147 operator- (position res, position::counter_type width) 148 { 149 return res -= width; 150 } 151 152 /** \brief Intercept output stream redirection. 153 ** \param ostr the destination output stream 154 ** \param pos a reference to the position to redirect 155 */ 156 template <typename YYChar> 157 std::basic_ostream<YYChar>& operator <<(std::basic_ostream<YYChar> & ostr,const position & pos)158 operator<< (std::basic_ostream<YYChar>& ostr, const position& pos) 159 { 160 if (pos.filename) 161 ostr << *pos.filename << ':'; 162 return ostr << pos.line << '.' << pos.column; 163 } 164 165 /// Two points in a source file. 166 class location 167 { 168 public: 169 /// Type for file name. 170 typedef position::filename_type filename_type; 171 /// Type for line and column numbers. 172 typedef position::counter_type counter_type; 173 174 /// Construct a location from \a b to \a e. location(const position & b,const position & e)175 location (const position& b, const position& e) 176 : begin (b) 177 , end (e) 178 {} 179 180 /// Construct a 0-width location in \a p. location(const position & p=position ())181 explicit location (const position& p = position ()) 182 : begin (p) 183 , end (p) 184 {} 185 186 /// Construct a 0-width location in \a f, \a l, \a c. location(filename_type * f,counter_type l=1,counter_type c=1)187 explicit location (filename_type* f, 188 counter_type l = 1, 189 counter_type c = 1) 190 : begin (f, l, c) 191 , end (f, l, c) 192 {} 193 194 195 /// Initialization. initialize(filename_type * f=YY_NULLPTR,counter_type l=1,counter_type c=1)196 void initialize (filename_type* f = YY_NULLPTR, 197 counter_type l = 1, 198 counter_type c = 1) 199 { 200 begin.initialize (f, l, c); 201 end = begin; 202 } 203 204 /** \name Line and Column related manipulators 205 ** \{ */ 206 public: 207 /// Reset initial location to final location. step()208 void step () 209 { 210 begin = end; 211 } 212 213 /// Extend the current location to the COUNT next columns. columns(counter_type count=1)214 void columns (counter_type count = 1) 215 { 216 end += count; 217 } 218 219 /// Extend the current location to the COUNT next lines. lines(counter_type count=1)220 void lines (counter_type count = 1) 221 { 222 end.lines (count); 223 } 224 /** \} */ 225 226 227 public: 228 /// Beginning of the located region. 229 position begin; 230 /// End of the located region. 231 position end; 232 }; 233 234 /// Join two locations, in place. 235 inline location& operator +=(location & res,const location & end)236 operator+= (location& res, const location& end) 237 { 238 res.end = end.end; 239 return res; 240 } 241 242 /// Join two locations. 243 inline location operator +(location res,const location & end)244 operator+ (location res, const location& end) 245 { 246 return res += end; 247 } 248 249 /// Add \a width columns to the end position, in place. 250 inline location& operator +=(location & res,location::counter_type width)251 operator+= (location& res, location::counter_type width) 252 { 253 res.columns (width); 254 return res; 255 } 256 257 /// Add \a width columns to the end position. 258 inline location operator +(location res,location::counter_type width)259 operator+ (location res, location::counter_type width) 260 { 261 return res += width; 262 } 263 264 /// Subtract \a width columns to the end position, in place. 265 inline location& operator -=(location & res,location::counter_type width)266 operator-= (location& res, location::counter_type width) 267 { 268 return res += -width; 269 } 270 271 /// Subtract \a width columns to the end position. 272 inline location operator -(location res,location::counter_type width)273 operator- (location res, location::counter_type width) 274 { 275 return res -= width; 276 } 277 278 /** \brief Intercept output stream redirection. 279 ** \param ostr the destination output stream 280 ** \param loc a reference to the location to redirect 281 ** 282 ** Avoid duplicate information. 283 */ 284 template <typename YYChar> 285 std::basic_ostream<YYChar>& operator <<(std::basic_ostream<YYChar> & ostr,const location & loc)286 operator<< (std::basic_ostream<YYChar>& ostr, const location& loc) 287 { 288 location::counter_type end_col 289 = 0 < loc.end.column ? loc.end.column - 1 : 0; 290 ostr << loc.begin; 291 if (loc.end.filename 292 && (!loc.begin.filename 293 || *loc.begin.filename != *loc.end.filename)) 294 ostr << '-' << loc.end.filename << ':' << loc.end.line << '.' << end_col; 295 else if (loc.begin.line < loc.end.line) 296 ostr << '-' << loc.end.line << '.' << end_col; 297 else if (loc.begin.column < end_col) 298 ostr << '-' << end_col; 299 return ostr; 300 } 301 302 #line 14 "d2_parser.yy" 303 } } // isc::d2 304 #line 305 "location.hh" 305 306 #endif // !YY_D2_PARSER_LOCATION_HH_INCLUDED 307