librostlab-blast  1.0.1
blast-parser-location.h
Go to the documentation of this file.
1 // A Bison parser, made by GNU Bison 3.3.2.
2 
3 // Locations for Bison parsers in C++
4 
5 // Copyright (C) 2002-2015, 2018-2019 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 
38 #ifndef YY_YY_ROSTLAB_BLAST_PARSER_LOCATION_H_INCLUDED
39 # define YY_YY_ROSTLAB_BLAST_PARSER_LOCATION_H_INCLUDED
40 
41 # include <algorithm> // std::max
42 # include <iostream>
43 # include <string>
44 
45 # ifndef YY_NULLPTR
46 # if defined __cplusplus
47 # if 201103L <= __cplusplus
48 # define YY_NULLPTR nullptr
49 # else
50 # define YY_NULLPTR 0
51 # endif
52 # else
53 # define YY_NULLPTR ((void*)0)
54 # endif
55 # endif
56 
57 #line 22 "blast-parser-parser.ypp" // location.cc:339
58 namespace rostlab { namespace blast {
59 #line 60 "rostlab/blast-parser-location.h" // location.cc:339
60  class position
62  {
63  public:
65  explicit position (std::string* f = YY_NULLPTR,
66  unsigned l = 1u,
67  unsigned c = 1u)
68  : filename (f)
69  , line (l)
70  , column (c)
71  {}
72 
73 
75  void initialize (std::string* fn = YY_NULLPTR,
76  unsigned l = 1u,
77  unsigned c = 1u)
78  {
79  filename = fn;
80  line = l;
81  column = c;
82  }
83 
86  void lines (int count = 1)
88  {
89  if (count)
90  {
91  column = 1u;
92  line = add_ (line, count, 1);
93  }
94  }
95 
97  void columns (int count = 1)
98  {
99  column = add_ (column, count, 1);
100  }
103  std::string* filename;
106  unsigned line;
108  unsigned column;
109 
110  private:
112  static unsigned add_ (unsigned lhs, int rhs, int min)
113  {
114  return static_cast<unsigned> (std::max (min,
115  static_cast<int> (lhs) + rhs));
116  }
117  };
118 
120  inline position&
121  operator+= (position& res, int width)
122  {
123  res.columns (width);
124  return res;
125  }
126 
128  inline position
129  operator+ (position res, int width)
130  {
131  return res += width;
132  }
133 
135  inline position&
136  operator-= (position& res, int width)
137  {
138  return res += -width;
139  }
140 
142  inline position
143  operator- (position res, int width)
144  {
145  return res -= width;
146  }
147 
149  inline bool
150  operator== (const position& pos1, const position& pos2)
151  {
152  return (pos1.line == pos2.line
153  && pos1.column == pos2.column
154  && (pos1.filename == pos2.filename
155  || (pos1.filename && pos2.filename
156  && *pos1.filename == *pos2.filename)));
157  }
158 
160  inline bool
161  operator!= (const position& pos1, const position& pos2)
162  {
163  return !(pos1 == pos2);
164  }
165 
170  template <typename YYChar>
171  std::basic_ostream<YYChar>&
172  operator<< (std::basic_ostream<YYChar>& ostr, const position& pos)
173  {
174  if (pos.filename)
175  ostr << *pos.filename << ':';
176  return ostr << pos.line << '.' << pos.column;
177  }
178 
180  class location
181  {
182  public:
183 
185  location (const position& b, const position& e)
186  : begin (b)
187  , end (e)
188  {}
189 
191  explicit location (const position& p = position ())
192  : begin (p)
193  , end (p)
194  {}
195 
197  explicit location (std::string* f,
198  unsigned l = 1u,
199  unsigned c = 1u)
200  : begin (f, l, c)
201  , end (f, l, c)
202  {}
203 
204 
206  void initialize (std::string* f = YY_NULLPTR,
207  unsigned l = 1u,
208  unsigned c = 1u)
209  {
210  begin.initialize (f, l, c);
211  end = begin;
212  }
213 
216  public:
218  void step ()
219  {
220  begin = end;
221  }
222 
224  void columns (int count = 1)
225  {
226  end += count;
227  }
228 
230  void lines (int count = 1)
231  {
232  end.lines (count);
233  }
237  public:
242  };
243 
245  inline location& operator+= (location& res, const location& end)
246  {
247  res.end = end.end;
248  return res;
249  }
250 
252  inline location operator+ (location res, const location& end)
253  {
254  return res += end;
255  }
256 
258  inline location& operator+= (location& res, int width)
259  {
260  res.columns (width);
261  return res;
262  }
263 
265  inline location operator+ (location res, int width)
266  {
267  return res += width;
268  }
269 
271  inline location& operator-= (location& res, int width)
272  {
273  return res += -width;
274  }
275 
277  inline location operator- (location res, int width)
278  {
279  return res -= width;
280  }
281 
283  inline bool
284  operator== (const location& loc1, const location& loc2)
285  {
286  return loc1.begin == loc2.begin && loc1.end == loc2.end;
287  }
288 
290  inline bool
291  operator!= (const location& loc1, const location& loc2)
292  {
293  return !(loc1 == loc2);
294  }
295 
302  template <typename YYChar>
303  std::basic_ostream<YYChar>&
304  operator<< (std::basic_ostream<YYChar>& ostr, const location& loc)
305  {
306  unsigned end_col = 0 < loc.end.column ? loc.end.column - 1 : 0;
307  ostr << loc.begin;
308  if (loc.end.filename
309  && (!loc.begin.filename
310  || *loc.begin.filename != *loc.end.filename))
311  ostr << '-' << loc.end.filename << ':' << loc.end.line << '.' << end_col;
312  else if (loc.begin.line < loc.end.line)
313  ostr << '-' << loc.end.line << '.' << end_col;
314  else if (loc.begin.column < end_col)
315  ostr << '-' << end_col;
316  return ostr;
317  }
318 
319 #line 22 "blast-parser-parser.ypp" // location.cc:339
320 } } // rostlab::blast
321 #line 322 "rostlab/blast-parser-location.h" // location.cc:339
322 #endif // !YY_YY_ROSTLAB_BLAST_PARSER_LOCATION_H_INCLUDED
bool operator!=(const position &pos1, const position &pos2)
Compare two position objects.
std::string * filename
File name to which this position refers.
location(std::string *f, unsigned l=1u, unsigned c=1u)
Construct a 0-width location in f, l, c.
location(const position &p=position())
Construct a 0-width location in p.
position & operator-=(position &res, int width)
Subtract width columns, in place.
void lines(int count=1)
(line related) Advance to the COUNT next lines.
bool operator==(const position &pos1, const position &pos2)
Compare two position objects.
void initialize(std::string *f=YY_NULLPTR, unsigned l=1u, unsigned c=1u)
Initialization.
A point in a source file.
position begin
Beginning of the located region.
Two points in a source file.
position end
End of the located region.
void initialize(std::string *fn=YY_NULLPTR, unsigned l=1u, unsigned c=1u)
Initialization.
position(std::string *f=YY_NULLPTR, unsigned l=1u, unsigned c=1u)
Construct a position.
position operator+(position res, int width)
Add width columns.
void lines(int count=1)
Extend the current location to the COUNT next lines.
location(const position &b, const position &e)
Construct a location from b to e.
void columns(int count=1)
Extend the current location to the COUNT next columns.
unsigned column
Current column number.
position operator-(position res, int width)
Subtract width columns.
#define YY_NULLPTR
void step()
Reset initial location to final location.
position & operator+=(position &res, int width)
Add width columns, in place.
void columns(int count=1)
(column related) Advance to the COUNT next columns.
unsigned line
Current line number.