ztsdb
position.hpp
Go to the documentation of this file.
1 // A Bison parser, made by GNU Bison 3.0.2.
2 
3 // Positions 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_POSITION_HH_INCLUDED
45 # define YY_YY_POSITION_HH_INCLUDED
46 
47 # include <algorithm> // std::max3
48 # include <iostream>
49 # include <string>
50 
51 # ifndef YY_NULLPTR
52 # if defined __cplusplus && 201103L <= __cplusplus
53 # define YY_NULLPTR nullptr
54 # else
55 # define YY_NULLPTR 0
56 # endif
57 # endif
58 
59 
60 namespace yy {
62  class position
63  {
64  public:
66  explicit position (const std::string& filename_p = std::string(),
67  unsigned int l = 1u,
68  unsigned int c = 1u,
69  std::shared_ptr<const std::string> s_p =
70  std::make_shared<const std::string>())
71  : filename(filename_p)
72  , line (l)
73  , column (c)
74  , s(s_p)
75  {
76  }
77 
78 
80  void initialize (const std::string& filename_p,
81  unsigned int l,
82  unsigned int c,
83  std::shared_ptr<const std::string> s_p)
84  {
85  filename = filename_p;
86  line = l;
87  column = c;
88  s = s_p;
89  }
90 
93  void lines (int count = 1)
95  {
96  if (count)
97  {
98  column = 1u;
99  line = add_ (line, count, 1);
100  }
101  }
102 
104  void columns (int count = 1)
105  {
106  column = add_ (column, count, 1);
107  }
110  std::string filename;
113  unsigned int line;
115  unsigned int column;
117  std::shared_ptr<const std::string> s;
118 
119  private:
121  static unsigned int add_ (unsigned int lhs, int rhs, unsigned int min)
122  {
123  return (0 < rhs || -static_cast<unsigned int>(rhs) < lhs
124  ? rhs + lhs
125  : min);
126  }
127  };
128 
130  inline position&
131  operator+= (position& res, int width)
132  {
133  res.columns (width);
134  return res;
135  }
136 
138  inline position
139  operator+ (position res, int width)
140  {
141  return res += width;
142  }
143 
145  inline position&
146  operator-= (position& res, int width)
147  {
148  return res += -width;
149  }
150 
152  inline position
153  operator- (position res, int width)
154  {
155  return res -= width;
156  }
157 
159  inline bool
160  operator== (const position& pos1, const position& pos2)
161  {
162  return (pos1.line == pos2.line
163  && pos1.column == pos2.column
164  && (pos1.filename == pos2.filename));
165  }
166 
168  inline bool
169  operator!= (const position& pos1, const position& pos2)
170  {
171  return !(pos1 == pos2);
172  }
173 
178  template <typename YYChar>
179  inline std::basic_ostream<YYChar>&
180  operator<< (std::basic_ostream<YYChar>& ostr, const position& pos)
181  {
182  if (!pos.filename.empty())
183  ostr << pos.filename << ':';
184  return ostr << pos.line << '.' << pos.column;
185  }
186 
187 
188 } // yy
189 #endif // !YY_YY_POSITION_HH_INCLUDED
yy::position::line
unsigned int line
Current line number.
Definition: position.hpp:113
yy::position::position
position(const std::string &filename_p=std::string(), unsigned int l=1u, unsigned int c=1u, std::shared_ptr< const std::string > s_p=std::make_shared< const std::string >())
Construct a position.
Definition: position.hpp:66
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::position::columns
void columns(int count=1)
(column related) Advance to the COUNT next columns.
Definition: position.hpp:104
yy::position::s
std::shared_ptr< const std::string > s
Shared pointer to the buffer being parsed;.
Definition: position.hpp:117
yy::position::lines
void lines(int count=1)
(line related) Advance to the COUNT next lines.
Definition: position.hpp:94
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