ztsdb
location.hpp
Go to the documentation of this file.
1 // A Bison parser, made by GNU Bison 3.0.2.
2 
3 // Locations for Bison parsers in C++
4 
5 // Copyright (C) 2002-2013 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 <http://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 // This version for ztsdb is lightly modified from the original
34 // Bison-generated location/position files. Mainly, we add a
35 // shared_ptr to the buffer being parsed in the 'position' class, and
36 // make sure the addition of this element if properly handled by the
37 // constructors.
38 
44 #ifndef YY_YY_LOCATION_HH_INCLUDED
45 # define YY_YY_LOCATION_HH_INCLUDED
46 
47 #include <memory>
48 #include <string>
49 #include "position.hpp"
50 
51 
52 namespace yy {
54  class location
55  {
56  public:
57 
59  location (const position& b,
60  const position& e)
61  : begin(b)
62  , end(e)
63  {
64  }
65 
67  explicit location (const position& p = position ())
68  : begin(p)
69  , end(p)
70  {
71  }
72 
74  explicit location (const std::string& filename_p,
75  unsigned int l = 1u,
76  unsigned int c = 1u,
77  std::shared_ptr<const std::string> s_p =
78  std::make_shared<const std::string>())
79  : begin(filename_p, l, c, s_p)
80  , end(filename_p, l, c, s_p)
81  {
82  }
83 
85  void initialize(const std::string& filename_p,
86  unsigned int l,
87  unsigned int c,
88  std::shared_ptr<const std::string> s_p)
89  {
90  begin.initialize(filename_p, l, c, s_p);
91  end = begin;
92  }
93 
96  public:
98  void step ()
99  {
100  begin = end;
101  }
102 
104  void columns (int count = 1)
105  {
106  end += count;
107  }
108 
110  void lines (int count = 1)
111  {
112  end.lines (count);
113  }
117  public:
122  };
123 
125  inline location operator+ (location res, const location& end)
126  {
127  res.end = end.end;
128  return res;
129  }
130 
132  inline location& operator+= (location& res, int width)
133  {
134  res.columns (width);
135  return res;
136  }
137 
139  inline location operator+ (location res, int width)
140  {
141  return res += width;
142  }
143 
145  inline location& operator-= (location& res, int width)
146  {
147  return res += -width;
148  }
149 
151  inline location operator- (const location& begin, int width)
152  {
153  return begin + -width;
154  }
155 
157  inline bool
158  operator== (const location& loc1, const location& loc2)
159  {
160  return loc1.begin == loc2.begin && loc1.end == loc2.end;
161  }
162 
164  inline bool
165  operator!= (const location& loc1, const location& loc2)
166  {
167  return !(loc1 == loc2);
168  }
169 
176  template <typename YYChar>
177  inline std::basic_ostream<YYChar>&
178  operator<< (std::basic_ostream<YYChar>& ostr, const location& loc)
179  {
180  unsigned int end_col = 0 < loc.end.column ? loc.end.column - 1 : 0;
181  ostr << loc.begin// << "(" << loc.end << ") "
182 ;
183  if (!loc.end.filename.empty()
184  && (loc.begin.filename.empty()
185  || loc.begin.filename != loc.end.filename))
186  ostr << '-' << loc.end.filename << ':' << loc.end.line << '.' << end_col;
187  else if (loc.begin.line < loc.end.line)
188  ostr << '-' << loc.end.line << '.' << end_col;
189  else if (loc.begin.column < end_col)
190  ostr << '-' << end_col;
191  return ostr;
192  }
193 
197  return location("", 0, 0, std::make_shared<const std::string>());
198  }
199  inline location span_loc(const location& b, const location& e) {
200  return location(b.begin, e.end);
201  }
202 } // yy
203 #endif // !YY_YY_LOCATION_HH_INCLUDED
yy::location::columns
void columns(int count=1)
Extend the current location to the COUNT next columns.
Definition: location.hpp:104
yy::position::line
unsigned int line
Current line number.
Definition: position.hpp:113
yy::location::location
location(const position &b, const position &e)
Construct a location from b to e.
Definition: location.hpp:59
yy::location::location
location(const std::string &filename_p, unsigned int l=1u, unsigned int c=1u, std::shared_ptr< const std::string > s_p=std::make_shared< const std::string >())
Construct a 0-width location in f, l, c.
Definition: location.hpp:74
position.hpp
yy::location
Abstract a location.
Definition: location.hpp:54
yy::missing_loc
location missing_loc()
Definition: location.hpp:196
yy::location::lines
void lines(int count=1)
Extend the current location to the COUNT next lines.
Definition: location.hpp:110
yy::position
Abstract a position.
Definition: position.hpp:62
yy::position::filename
std::string filename
File name to which this location refers.
Definition: position.hpp:111
yy::position::column
unsigned int column
Current column number.
Definition: position.hpp:115
yy::location::begin
position begin
Beginning of the located region.
Definition: location.hpp:119
yy::location::step
void step()
Reset initial location to final location.
Definition: location.hpp:98
yy::location::location
location(const position &p=position())
Construct a 0-width location in p.
Definition: location.hpp:67
yy::position::lines
void lines(int count=1)
(line related) Advance to the COUNT next lines.
Definition: position.hpp:94
yy::location::initialize
void initialize(const std::string &filename_p, unsigned int l, unsigned int c, std::shared_ptr< const std::string > s_p)
Initialization.
Definition: location.hpp:85
yy::position::initialize
void initialize(const std::string &filename_p, unsigned int l, unsigned int c, std::shared_ptr< const std::string > s_p)
Initialization.
Definition: position.hpp:80
yy::location::end
position end
End of the located region.
Definition: location.hpp:121