00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00040 #ifndef BISON_POSITION_HH
00041 # define BISON_POSITION_HH
00042
00043 # include <iostream>
00044 # include <string>
00045
00046 namespace example {
00047
00049 class position
00050 {
00051 public:
00052
00054 position ()
00055 : filename (0), line (1), column (0)
00056 {
00057 }
00058
00060 inline void initialize (std::string* fn)
00061 {
00062 filename = fn;
00063 line = 1;
00064 column = 0;
00065 }
00066
00069 public:
00071 inline void lines (int count = 1)
00072 {
00073 column = 0;
00074 line += count;
00075 }
00076
00078 inline void columns (int count = 1)
00079 {
00080 int leftmost = 0;
00081 int current = column;
00082 if (leftmost <= current + count)
00083 column += count;
00084 else
00085 column = 0;
00086 }
00089 public:
00091 std::string* filename;
00093 unsigned int line;
00095 unsigned int column;
00096 };
00097
00099 inline const position&
00100 operator+= (position& res, const int width)
00101 {
00102 res.columns (width);
00103 return res;
00104 }
00105
00107 inline const position
00108 operator+ (const position& begin, const int width)
00109 {
00110 position res = begin;
00111 return res += width;
00112 }
00113
00115 inline const position&
00116 operator-= (position& res, const int width)
00117 {
00118 return res += -width;
00119 }
00120
00122 inline const position
00123 operator- (const position& begin, const int width)
00124 {
00125 return begin + -width;
00126 }
00127
00132 inline std::ostream&
00133 operator<< (std::ostream& ostr, const position& pos)
00134 {
00135 if (pos.filename)
00136 ostr << *pos.filename << ':';
00137 return ostr << pos.line << '.' << pos.column;
00138 }
00139
00140 }
00141
00142 #endif // not BISON_POSITION_HH