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.5.1.
2 
3 // Locations for Bison parsers in C++
4 
5 // Copyright (C) 2002-2015, 2018-2020 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 <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 22 "blast-parser-parser.ypp"
57 namespace rostlab { namespace blast {
58 #line 59 "rostlab/blast-parser-location.h"
59 
61  class position
62  {
63  public:
65  typedef int counter_type;
66 
68  explicit position (std::string* f = YY_NULLPTR,
69  counter_type l = 1,
70  counter_type c = 1)
71  : filename (f)
72  , line (l)
73  , column (c)
74  {}
75 
76 
78  void initialize (std::string* fn = YY_NULLPTR,
79  counter_type l = 1,
80  counter_type c = 1)
81  {
82  filename = fn;
83  line = l;
84  column = c;
85  }
86 
89  void lines (counter_type count = 1)
91  {
92  if (count)
93  {
94  column = 1;
95  line = add_ (line, count, 1);
96  }
97  }
98 
100  void columns (counter_type count = 1)
101  {
102  column = add_ (column, count, 1);
103  }
106  std::string* filename;
112 
113  private:
115  static counter_type add_ (counter_type lhs, counter_type rhs, counter_type min)
116  {
117  return lhs + rhs < min ? min : lhs + rhs;
118  }
119  };
120 
122  inline position&
124  {
125  res.columns (width);
126  return res;
127  }
128 
130  inline position
132  {
133  return res += width;
134  }
135 
137  inline position&
139  {
140  return res += -width;
141  }
142 
144  inline position
146  {
147  return res -= width;
148  }
149 
151  inline bool
152  operator== (const position& pos1, const position& pos2)
153  {
154  return (pos1.line == pos2.line
155  && pos1.column == pos2.column
156  && (pos1.filename == pos2.filename
157  || (pos1.filename && pos2.filename
158  && *pos1.filename == *pos2.filename)));
159  }
160 
162  inline bool
163  operator!= (const position& pos1, const position& pos2)
164  {
165  return !(pos1 == pos2);
166  }
167 
172  template <typename YYChar>
173  std::basic_ostream<YYChar>&
174  operator<< (std::basic_ostream<YYChar>& ostr, const position& pos)
175  {
176  if (pos.filename)
177  ostr << *pos.filename << ':';
178  return ostr << pos.line << '.' << pos.column;
179  }
180 
182  class location
183  {
184  public:
187 
189  location (const position& b, const position& e)
190  : begin (b)
191  , end (e)
192  {}
193 
195  explicit location (const position& p = position ())
196  : begin (p)
197  , end (p)
198  {}
199 
201  explicit location (std::string* f,
202  counter_type l = 1,
203  counter_type c = 1)
204  : begin (f, l, c)
205  , end (f, l, c)
206  {}
207 
208 
210  void initialize (std::string* f = YY_NULLPTR,
211  counter_type l = 1,
212  counter_type c = 1)
213  {
214  begin.initialize (f, l, c);
215  end = begin;
216  }
217 
220  public:
222  void step ()
223  {
224  begin = end;
225  }
226 
228  void columns (counter_type count = 1)
229  {
230  end += count;
231  }
232 
234  void lines (counter_type count = 1)
235  {
236  end.lines (count);
237  }
241  public:
246  };
247 
249  inline location&
250  operator+= (location& res, const location& end)
251  {
252  res.end = end.end;
253  return res;
254  }
255 
257  inline location
258  operator+ (location res, const location& end)
259  {
260  return res += end;
261  }
262 
264  inline location&
266  {
267  res.columns (width);
268  return res;
269  }
270 
272  inline location
274  {
275  return res += width;
276  }
277 
279  inline location&
281  {
282  return res += -width;
283  }
284 
286  inline location
288  {
289  return res -= width;
290  }
291 
293  inline bool
294  operator== (const location& loc1, const location& loc2)
295  {
296  return loc1.begin == loc2.begin && loc1.end == loc2.end;
297  }
298 
300  inline bool
301  operator!= (const location& loc1, const location& loc2)
302  {
303  return !(loc1 == loc2);
304  }
305 
312  template <typename YYChar>
313  std::basic_ostream<YYChar>&
314  operator<< (std::basic_ostream<YYChar>& ostr, const location& loc)
315  {
316  location::counter_type end_col
317  = 0 < loc.end.column ? loc.end.column - 1 : 0;
318  ostr << loc.begin;
319  if (loc.end.filename
320  && (!loc.begin.filename
321  || *loc.begin.filename != *loc.end.filename))
322  ostr << '-' << loc.end.filename << ':' << loc.end.line << '.' << end_col;
323  else if (loc.begin.line < loc.end.line)
324  ostr << '-' << loc.end.line << '.' << end_col;
325  else if (loc.begin.column < end_col)
326  ostr << '-' << end_col;
327  return ostr;
328  }
329 
330 #line 22 "blast-parser-parser.ypp"
331 } } // rostlab::blast
332 #line 333 "rostlab/blast-parser-location.h"
333 
334 #endif // !YY_YY_ROSTLAB_BLAST_PARSER_LOCATION_H_INCLUDED
rostlab::blast::position::line
counter_type line
Current line number.
Definition: blast-parser-location.h:109
rostlab::blast::location::columns
void columns(counter_type count=1)
Extend the current location to the COUNT next columns.
Definition: blast-parser-location.h:228
rostlab::blast::operator!=
bool operator!=(const position &pos1, const position &pos2)
Compare two position objects.
Definition: blast-parser-location.h:163
rostlab::blast::position
A point in a source file.
Definition: blast-parser-location.h:61
rostlab::blast::location::end
position end
End of the located region.
Definition: blast-parser-location.h:245
rostlab::blast::position::columns
void columns(counter_type count=1)
(column related) Advance to the COUNT next columns.
Definition: blast-parser-location.h:100
rostlab::blast::location::location
location(std::string *f, counter_type l=1, counter_type c=1)
Construct a 0-width location in f, l, c.
Definition: blast-parser-location.h:201
rostlab::blast::operator+=
position & operator+=(position &res, position::counter_type width)
Add width columns, in place.
Definition: blast-parser-location.h:123
rostlab::blast::location::location
location(const position &b, const position &e)
Construct a location from b to e.
Definition: blast-parser-location.h:189
rostlab::blast::location
Two points in a source file.
Definition: blast-parser-location.h:182
rostlab::blast::position::counter_type
int counter_type
Type for line and column numbers.
Definition: blast-parser-location.h:65
rostlab::blast::position::lines
void lines(counter_type count=1)
(line related) Advance to the COUNT next lines.
Definition: blast-parser-location.h:90
rostlab::blast::operator-
position operator-(position res, position::counter_type width)
Subtract width columns.
Definition: blast-parser-location.h:145
rostlab::blast::location::lines
void lines(counter_type count=1)
Extend the current location to the COUNT next lines.
Definition: blast-parser-location.h:234
rostlab::blast::operator-=
position & operator-=(position &res, position::counter_type width)
Subtract width columns, in place.
Definition: blast-parser-location.h:138
rostlab
Definition: blast-parser-driver.h:45
rostlab::blast::location::location
location(const position &p=position())
Construct a 0-width location in p.
Definition: blast-parser-location.h:195
rostlab::blast::position::filename
std::string * filename
File name to which this position refers.
Definition: blast-parser-location.h:107
rostlab::blast::operator<<
std::basic_ostream< YYChar > & operator<<(std::basic_ostream< YYChar > &ostr, const position &pos)
Intercept output stream redirection.
Definition: blast-parser-location.h:174
rostlab::blast::location::initialize
void initialize(std::string *f=YY_NULLPTR, counter_type l=1, counter_type c=1)
Initialization.
Definition: blast-parser-location.h:210
rostlab::blast::location::step
void step()
Reset initial location to final location.
Definition: blast-parser-location.h:222
rostlab::blast::position::position
position(std::string *f=YY_NULLPTR, counter_type l=1, counter_type c=1)
Construct a position.
Definition: blast-parser-location.h:68
rostlab::blast::position::initialize
void initialize(std::string *fn=YY_NULLPTR, counter_type l=1, counter_type c=1)
Initialization.
Definition: blast-parser-location.h:78
YY_NULLPTR
#define YY_NULLPTR
Definition: blast-parser-location.h:52
rostlab::blast::operator+
position operator+(position res, position::counter_type width)
Add width columns.
Definition: blast-parser-location.h:131
rostlab::blast::operator==
bool operator==(const position &pos1, const position &pos2)
Compare two position objects.
Definition: blast-parser-location.h:152
rostlab::blast::position::column
counter_type column
Current column number.
Definition: blast-parser-location.h:111
rostlab::blast::location::begin
position begin
Beginning of the located region.
Definition: blast-parser-location.h:243
rostlab::blast::location::counter_type
position::counter_type counter_type
Type for line and column numbers.
Definition: blast-parser-location.h:186