c++: expose the type used to store line and column numbers

* data/skeletons/location.cc (position::counter_type)
(location::counter_type): New.
Use them.
* doc/bison.texi (C++ position, C++ location): Adjust.
This commit is contained in:
Akim Demaille
2019-11-06 08:33:44 +01:00
parent 583c193ffa
commit 7bdf7246fb
3 changed files with 68 additions and 51 deletions

View File

@@ -62,11 +62,14 @@ m4_define([b4_location_define],
[[ /// A point in a source file.
class position
{
public:]m4_ifdef([b4_location_constructors], [[
public:
/// Type for line and column numbers.
typedef int counter_type;
]m4_ifdef([b4_location_constructors], [[
/// Construct a position.
explicit position (]b4_percent_define_get([[filename_type]])[* f = YY_NULLPTR,
int l = ]b4_location_initial_line[,
int c = ]b4_location_initial_column[)
counter_type l = ]b4_location_initial_line[,
counter_type c = ]b4_location_initial_column[)
: filename (f)
, line (l)
, column (c)
@@ -75,8 +78,8 @@ m4_define([b4_location_define],
]])[
/// Initialization.
void initialize (]b4_percent_define_get([[filename_type]])[* fn = YY_NULLPTR,
int l = ]b4_location_initial_line[,
int c = ]b4_location_initial_column[)
counter_type l = ]b4_location_initial_line[,
counter_type c = ]b4_location_initial_column[)
{
filename = fn;
line = l;
@@ -86,7 +89,7 @@ m4_define([b4_location_define],
/** \name Line and Column related manipulators
** \{ */
/// (line related) Advance to the COUNT next lines.
void lines (int count = 1)
void lines (counter_type count = 1)
{
if (count)
{
@@ -96,7 +99,7 @@ m4_define([b4_location_define],
}
/// (column related) Advance to the COUNT next columns.
void columns (int count = 1)
void columns (counter_type count = 1)
{
column = add_ (column, count, ]b4_location_initial_column[);
}
@@ -105,13 +108,13 @@ m4_define([b4_location_define],
/// File name to which this position refers.
]b4_percent_define_get([[filename_type]])[* filename;
/// Current line number.
int line;
counter_type line;
/// Current column number.
int column;
counter_type column;
private:
/// Compute max (min, lhs+rhs).
static int add_ (int lhs, int rhs, int min)
static counter_type add_ (counter_type lhs, counter_type rhs, counter_type min)
{
return lhs + rhs < min ? min : lhs + rhs;
}
@@ -119,7 +122,7 @@ m4_define([b4_location_define],
/// Add \a width columns, in place.
inline position&
operator+= (position& res, int width)
operator+= (position& res, position::counter_type width)
{
res.columns (width);
return res;
@@ -127,21 +130,21 @@ m4_define([b4_location_define],
/// Add \a width columns.
inline position
operator+ (position res, int width)
operator+ (position res, position::counter_type width)
{
return res += width;
}
/// Subtract \a width columns, in place.
inline position&
operator-= (position& res, int width)
operator-= (position& res, position::counter_type width)
{
return res += -width;
}
/// Subtract \a width columns.
inline position
operator- (position res, int width)
operator- (position res, position::counter_type width)
{
return res -= width;
}
@@ -181,6 +184,8 @@ m4_define([b4_location_define],
class location
{
public:
/// Type for line and column numbers.
typedef position::counter_type counter_type;
]m4_ifdef([b4_location_constructors], [
/// Construct a location from \a b to \a e.
location (const position& b, const position& e)
@@ -196,8 +201,8 @@ m4_define([b4_location_define],
/// Construct a 0-width location in \a f, \a l, \a c.
explicit location (]b4_percent_define_get([[filename_type]])[* f,
int l = ]b4_location_initial_line[,
int c = ]b4_location_initial_column[)
counter_type l = ]b4_location_initial_line[,
counter_type c = ]b4_location_initial_column[)
: begin (f, l, c)
, end (f, l, c)
{}
@@ -205,8 +210,8 @@ m4_define([b4_location_define],
])[
/// Initialization.
void initialize (]b4_percent_define_get([[filename_type]])[* f = YY_NULLPTR,
int l = ]b4_location_initial_line[,
int c = ]b4_location_initial_column[)
counter_type l = ]b4_location_initial_line[,
counter_type c = ]b4_location_initial_column[)
{
begin.initialize (f, l, c);
end = begin;
@@ -222,13 +227,13 @@ m4_define([b4_location_define],
}
/// Extend the current location to the COUNT next columns.
void columns (int count = 1)
void columns (counter_type count = 1)
{
end += count;
}
/// Extend the current location to the COUNT next lines.
void lines (int count = 1)
void lines (counter_type count = 1)
{
end.lines (count);
}
@@ -243,39 +248,45 @@ m4_define([b4_location_define],
};
/// Join two locations, in place.
inline location& operator+= (location& res, const location& end)
inline location&
operator+= (location& res, const location& end)
{
res.end = end.end;
return res;
}
/// Join two locations.
inline location operator+ (location res, const location& end)
inline location
operator+ (location res, const location& end)
{
return res += end;
}
/// Add \a width columns to the end position, in place.
inline location& operator+= (location& res, int width)
inline location&
operator+= (location& res, location::counter_type width)
{
res.columns (width);
return res;
}
/// Add \a width columns to the end position.
inline location operator+ (location res, int width)
inline location
operator+ (location res, location::counter_type width)
{
return res += width;
}
/// Subtract \a width columns to the end position, in place.
inline location& operator-= (location& res, int width)
inline location&
operator-= (location& res, location::counter_type width)
{
return res += -width;
}
/// Subtract \a width columns to the end position.
inline location operator- (location res, int width)
inline location
operator- (location res, location::counter_type width)
{
return res -= width;
}
@@ -304,7 +315,8 @@ m4_define([b4_location_define],
std::basic_ostream<YYChar>&
operator<< (std::basic_ostream<YYChar>& ostr, const location& loc)
{
int end_col = 0 < loc.end.column ? loc.end.column - 1 : 0;
location::counter_type end_col
= 0 < loc.end.column ? loc.end.column - 1 : 0;
ostr << loc.begin;
if (loc.end.filename
&& (!loc.begin.filename