5 #define LINE_WIDTH 256 // set the line width to anything you want.
6 // The Only assumption is that a column number
7 // is lesser than LINE_WIDTH
9 #define POSITION(line,column) ( (line) * LINE_WIDTH + (column) )
11 //**************************************
12 //* Class Location : *
13 //* Class used for storing location *
15 //**************************************
17 // Constructor with two arguments : the markers are set equal each other
18 // Start mark = End mark.
20 Location::Location( void )
22 LineStart = LineEnd = 1;
23 ColumnStart = ColumnEnd = 1;
26 Location::Location( int Line, int Column = 1 )
28 if (Line < 0 || Column < 0 || Column > LINE_WIDTH)
29 error("Incoherent values for initialisation of Location object.");
30 LineStart = LineEnd = Line;
31 ColumnStart = ColumnEnd = Column;
34 // Constructor with four arguments : the two first are for Start mark
35 // and the two last are for the End mark.
37 Location::Location( int First_Line, int First_Column,
38 int Last_Line, int Last_Column )
40 if ((First_Line <= 0) || (First_Column <= 0) ||
41 (Last_Line <= 0) || (Last_Column <= 0) ||
42 (First_Column > LINE_WIDTH) || (Last_Column > LINE_WIDTH))
43 error("Incoherent values for initialisation of Location object.");
45 if ( POSITION(First_Line,First_Column) < POSITION(Last_Line,Last_Column) )
47 LineStart = First_Line;
49 ColumnStart = First_Column;
50 ColumnEnd = Last_Column;
54 LineStart = Last_Line;
56 ColumnStart = Last_Column;
57 ColumnEnd = First_Column;
61 Location::Location( const Location& TheLoc )
63 LineStart = TheLoc.LineStart;
64 LineEnd = TheLoc.LineEnd;
65 ColumnStart = TheLoc.ColumnStart;
66 ColumnEnd = TheLoc.ColumnEnd;
69 // SetEnd method : Set the End mark to line number Line
70 // and column number Column.
72 void Location::SetEnd( int Line, int Column )
75 if ( Line <= 0 || Column <= 0 || Column > LINE_WIDTH)
76 error("Incoherent values for initialisation of Location object.");
78 if ( POSITION(Line,Column) > POSITION(LineStart,ColumnStart) )
79 // The new End Mark is after the Start Mark so we just adjust the End Mark.
86 // The new End Mark is before the Start Mark so we have two swap the two marks.
88 ColumnEnd = ColumnStart;
94 // This function sets the beginning of the Location Object at
95 // the line Line and the column Column
97 void Location::Move( int Line , int Column )
100 ColumnStart = Column;
101 if ( POSITION(LineStart,ColumnStart) > POSITION(LineEnd,ColumnEnd) )
103 ColumnEnd = ColumnStart;
108 // This function simply advance the start position by 'distance' columns.
109 void Location::Tab( int distance )
111 ColumnStart += distance;
112 if (ColumnStart > LINE_WIDTH)
115 ColumnStart = ((ColumnStart - 1) % LINE_WIDTH) + 1;
117 if (POSITION(LineStart,ColumnStart) > POSITION(LineEnd,ColumnEnd) )
120 ColumnEnd = ColumnStart;
124 // This function return the start position to the first column of the next line.
125 void Location::Cr( void )
129 if ( POSITION(LineStart,ColumnStart) > POSITION(LineEnd,ColumnEnd) )
132 ColumnEnd = ColumnStart;
136 void Location::Select( int length )
139 ColumnStart = ColumnEnd;
140 ColumnEnd += length - 1;
141 if ( ColumnEnd >= LINE_WIDTH )
144 ColumnEnd = (ColumnEnd - 1 ) % LINE_WIDTH + 1;
148 void Location::Advance( void )
151 ColumnStart = ++ColumnEnd;
152 if (ColumnStart == LINE_WIDTH)
154 ColumnStart = ColumnEnd = 1;
155 LineStart = ++LineEnd;
159 void Location::CrSelect( void )
165 // Checking the properties of a location Object :
166 // Line numbers and Column numbers must be greater than 0 and
167 // Start Mark must be come before the End Mark.
169 int Location::OK( void )
171 if ( (LineStart < 0) || (LineEnd < 0) ||
172 (ColumnStart < 0) || (ColumnEnd < 0) ||
173 // here we compute the number of characters from the begining
174 // assuming the line is LINE_WIDTH characters wide .
175 ( POSITION( LineStart,ColumnStart ) > POSITION( LineEnd,ColumnEnd ) )
178 error(" This Location object is not in a coherent state.");
185 // Overloading semantic of + operator.
186 // this return a Location Object that include both objects Location.
187 Location operator + ( const Location& FirstLocation ,
188 const Location& SecondLocation )
190 if (POSITION( FirstLocation .LineStart,FirstLocation .ColumnStart ) <
191 POSITION( SecondLocation.LineEnd ,SecondLocation.ColumnEnd ) )
193 return Location( FirstLocation .LineStart ,
194 FirstLocation .ColumnStart,
195 SecondLocation.LineEnd,
196 SecondLocation.ColumnEnd );
200 return Location( SecondLocation.LineStart ,
201 SecondLocation.ColumnStart,
202 FirstLocation.LineEnd,
203 FirstLocation.ColumnEnd );
207 // Well, this print out the contents of a Location object.
209 ostream& operator << ( ostream& cout, Location there )
211 cout << '(' << there.LineStart << ':' << there.ColumnStart << ',';
212 return cout << there.LineEnd << ':' << there.ColumnEnd << ')';
215 // General fonction that force the program to abort.
216 void error( char * TheString )