1 /* A Bison parser, made by GNU Bison 2.3.  */
2 
3 /* Positions for Bison parsers in C++
4 
5    Copyright (C) 2002, 2003, 2004, 2005, 2006 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 2, or (at your option)
10    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, write to the Free Software
19    Foundation, Inc., 51 Franklin Street, Fifth Floor,
20    Boston, MA 02110-1301, USA.  */
21 
22 /* As a special exception, you may create a larger work that contains
23    part or all of the Bison parser skeleton and distribute that work
24    under terms of your choice, so long as that work isn't itself a
25    parser generator using the skeleton or a modified version thereof
26    as a parser skeleton.  Alternatively, if you modify or redistribute
27    the parser skeleton itself, you may (at your option) remove this
28    special exception, which will cause the skeleton and the resulting
29    Bison output files to be licensed under the GNU General Public
30    License without this special exception.
31 
32    This special exception was added by the Free Software Foundation in
33    version 2.2 of Bison.  */
34 
35 /**
36  ** \file position.hh
37  ** Define the yy::position class.
38  */
39 
40 #ifndef BISON_POSITION_HH
41 # define BISON_POSITION_HH
42 
43 # include <iostream>
44 # include <string>
45 
46 namespace yy
47 {
48   /// Abstract a position.
49   class position
50   {
51   public:
52 
53     /// Construct a position.
position()54     position ()
55       : filename (0), line (1), column (0)
56     {
57     }
58 
59 
60     /// Initialization.
initialize(std::string * fn)61     inline void initialize (std::string* fn)
62     {
63       filename = fn;
64       line = 1;
65       column = 0;
66     }
67 
68     /** \name Line and Column related manipulators
69      ** \{ */
70   public:
71     /// (line related) Advance to the COUNT next lines.
lines(int count=1)72     inline void lines (int count = 1)
73     {
74       column = 0;
75       line += count;
76     }
77 
78     /// (column related) Advance to the COUNT next columns.
columns(int count=1)79     inline void columns (int count = 1)
80     {
81       int leftmost = 0;
82       int current  = column;
83       if (leftmost <= current + count)
84 	column += count;
85       else
86 	column = 0;
87     }
88     /** \} */
89 
90   public:
91     /// File name to which this position refers.
92     std::string* filename;
93     /// Current line number.
94     unsigned int line;
95     /// Current column number.
96     unsigned int column;
97   };
98 
99   /// Add and assign a position.
100   inline const position&
operator +=(position & res,const int width)101   operator+= (position& res, const int width)
102   {
103     res.columns (width);
104     return res;
105   }
106 
107   /// Add two position objects.
108   inline const position
operator +(const position & begin,const int width)109   operator+ (const position& begin, const int width)
110   {
111     position res = begin;
112     return res += width;
113   }
114 
115   /// Add and assign a position.
116   inline const position&
operator -=(position & res,const int width)117   operator-= (position& res, const int width)
118   {
119     return res += -width;
120   }
121 
122   /// Add two position objects.
123   inline const position
operator -(const position & begin,const int width)124   operator- (const position& begin, const int width)
125   {
126     return begin + -width;
127   }
128 
129   /** \brief Intercept output stream redirection.
130    ** \param ostr the destination output stream
131    ** \param pos a reference to the position to redirect
132    */
operator <<(std::ostream & ostr,const position & pos)133   inline std::ostream&  operator<< (std::ostream& ostr, const position& pos)
134   {
135     if (pos.filename) {
136       ostr << (*pos.filename).c_str();
137       ostr << ':';
138     }
139     return ostr << pos.line << '.' << pos.column;
140   }
141 }
142 
143 #endif // not BISON_POSITION_HH
144