1m4_divert(-1)
2
3# C++ skeleton for Bison
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 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, write to the Free Software
19# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20# 02110-1301  USA
21
22# We do want M4 expansion after # for CPP macros.
23m4_changecom()
24m4_divert(0)dnl
25@output b4_dir_prefix[]position.hh
26b4_copyright([Positions for Bison parsers in C++],
27  [2002, 2003, 2004, 2005, 2006])[
28
29/**
30 ** \file position.hh
31 ** Define the ]b4_namespace[::position class.
32 */
33
34#ifndef BISON_POSITION_HH
35# define BISON_POSITION_HH
36
37# include <iostream>
38# include <string>
39
40namespace ]b4_namespace[
41{
42  /// Abstract a position.
43  class position
44  {
45  public:
46]m4_ifdef([b4_location_constructors], [
47    /// Construct a position.
48    position ()
49      : filename (0), line (1), column (0)
50    {
51    }
52
53])[
54    /// Initialization.
55    inline void initialize (]b4_filename_type[* fn)
56    {
57      filename = fn;
58      line = 1;
59      column = 0;
60    }
61
62    /** \name Line and Column related manipulators
63     ** \{ */
64  public:
65    /// (line related) Advance to the COUNT next lines.
66    inline void lines (int count = 1)
67    {
68      column = 0;
69      line += count;
70    }
71
72    /// (column related) Advance to the COUNT next columns.
73    inline void columns (int count = 1)
74    {
75      int leftmost = 0;
76      int current  = column;
77      if (leftmost <= current + count)
78	column += count;
79      else
80	column = 0;
81    }
82    /** \} */
83
84  public:
85    /// File name to which this position refers.
86    ]b4_filename_type[* filename;
87    /// Current line number.
88    unsigned int line;
89    /// Current column number.
90    unsigned int column;
91  };
92
93  /// Add and assign a position.
94  inline const position&
95  operator+= (position& res, const int width)
96  {
97    res.columns (width);
98    return res;
99  }
100
101  /// Add two position objects.
102  inline const position
103  operator+ (const position& begin, const int width)
104  {
105    position res = begin;
106    return res += width;
107  }
108
109  /// Add and assign a position.
110  inline const position&
111  operator-= (position& res, const int width)
112  {
113    return res += -width;
114  }
115
116  /// Add two position objects.
117  inline const position
118  operator- (const position& begin, const int width)
119  {
120    return begin + -width;
121  }
122
123  /** \brief Intercept output stream redirection.
124   ** \param ostr the destination output stream
125   ** \param pos a reference to the position to redirect
126   */
127  inline std::ostream&
128  operator<< (std::ostream& ostr, const position& pos)
129  {
130    if (pos.filename)
131      ostr << *pos.filename << ':';
132    return ostr << pos.line << '.' << pos.column;
133  }
134
135}
136#endif // not BISON_POSITION_HH]
137@output b4_dir_prefix[]location.hh
138b4_copyright([Locations for Bison parsers in C++],
139  [2002, 2003, 2004, 2005, 2006])[
140
141/**
142 ** \file location.hh
143 ** Define the ]b4_namespace[::location class.
144 */
145
146#ifndef BISON_LOCATION_HH
147# define BISON_LOCATION_HH
148
149# include <iostream>
150# include <string>
151# include "position.hh"
152
153namespace ]b4_namespace[
154{
155
156  /// Abstract a location.
157  class location
158  {
159  public:
160]m4_ifdef([b4_location_constructors], [
161    /// Construct a location.
162    location ()
163      : begin (), end ()
164    {
165    }
166
167])[
168    /// Initialization.
169    inline void initialize (]b4_filename_type[* fn)
170    {
171      begin.initialize (fn);
172      end = begin;
173    }
174
175    /** \name Line and Column related manipulators
176     ** \{ */
177  public:
178    /// Reset initial location to final location.
179    inline void step ()
180    {
181      begin = end;
182    }
183
184    /// Extend the current location to the COUNT next columns.
185    inline void columns (unsigned int count = 1)
186    {
187      end += count;
188    }
189
190    /// Extend the current location to the COUNT next lines.
191    inline void lines (unsigned int count = 1)
192    {
193      end.lines (count);
194    }
195    /** \} */
196
197
198  public:
199    /// Beginning of the located region.
200    position begin;
201    /// End of the located region.
202    position end;
203  };
204
205  /// Join two location objects to create a location.
206  inline const location operator+ (const location& begin, const location& end)
207  {
208    location res = begin;
209    res.end = end.end;
210    return res;
211  }
212
213  /// Add two location objects.
214  inline const location operator+ (const location& begin, unsigned int width)
215  {
216    location res = begin;
217    res.columns (width);
218    return res;
219  }
220
221  /// Add and assign a location.
222  inline location& operator+= (location& res, unsigned int width)
223  {
224    res.columns (width);
225    return res;
226  }
227
228  /** \brief Intercept output stream redirection.
229   ** \param ostr the destination output stream
230   ** \param loc a reference to the location to redirect
231   **
232   ** Avoid duplicate information.
233   */
234  inline std::ostream& operator<< (std::ostream& ostr, const location& loc)
235  {
236    position last = loc.end - 1;
237    ostr << loc.begin;
238    if (last.filename
239	&& (!loc.begin.filename
240	    || *loc.begin.filename != *last.filename))
241      ostr << '-' << last;
242    else if (loc.begin.line != last.line)
243      ostr << '-' << last.line  << '.' << last.column;
244    else if (loc.begin.column != last.column)
245      ostr << '-' << last.column;
246    return ostr;
247  }
248
249}
250
251#endif // not BISON_LOCATION_HH]
252m4_divert(-1)
253m4_changecom([#])
254