filebuff.cpp revision 470:ad8c8ca4ab0f
1124758Semax/*
2124758Semax * Copyright 1997-2008 Sun Microsystems, Inc.  All Rights Reserved.
3124758Semax * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4124758Semax *
5124758Semax * This code is free software; you can redistribute it and/or modify it
6124758Semax * under the terms of the GNU General Public License version 2 only, as
7124758Semax * published by the Free Software Foundation.
8124758Semax *
9124758Semax * This code is distributed in the hope that it will be useful, but WITHOUT
10124758Semax * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11124758Semax * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12124758Semax * version 2 for more details (a copy is included in the LICENSE file that
13124758Semax * accompanied this code).
14124758Semax *
15124758Semax * You should have received a copy of the GNU General Public License version
16124758Semax * 2 along with this work; if not, write to the Free Software Foundation,
17124758Semax * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18124758Semax *
19124758Semax * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20124758Semax * CA 95054 USA or visit www.sun.com if you need additional information or
21124758Semax * have any questions.
22124758Semax *
23124758Semax */
24124758Semax
25124758Semax// FILEBUFF.CPP - Routines for handling a parser file buffer
26124758Semax#include "adlc.hpp"
27124758Semax
28124758Semax//------------------------------FileBuff---------------------------------------
29124758Semax// Create a new parsing buffer
30124758SemaxFileBuff::FileBuff( BufferedFile *fptr, ArchDesc& archDesc) : _fp(fptr), _AD(archDesc) {
31124758Semax  _err = fseek(_fp->_fp, 0, SEEK_END);  // Seek to end of file
32124758Semax  if (_err) {
33124758Semax    file_error(SEMERR, 0, "File seek error reading input file");
34124758Semax    exit(1);                    // Exit on seek error
35124758Semax  }
36124758Semax  _filepos = ftell(_fp->_fp);   // Find offset of end of file
37124758Semax  _bufferSize = _filepos + 5;   // Filepos points to last char, so add padding
38124758Semax  _err = fseek(_fp->_fp, 0, SEEK_SET);  // Reset to beginning of file
39124758Semax  if (_err) {
40124758Semax    file_error(SEMERR, 0, "File seek error reading input file\n");
41124758Semax    exit(1);                    // Exit on seek error
42124758Semax  }
43124758Semax  _filepos = ftell(_fp->_fp);      // Reset current file position
44124758Semax  _linenum = 0;
45124758Semax
46124758Semax  _bigbuf = new char[_bufferSize]; // Create buffer to hold text for parser
47124758Semax  if( !_bigbuf ) {
48124758Semax    file_error(SEMERR, 0, "Buffer allocation failed\n");
49124758Semax    exit(1);                    // Exit on allocation failure
50124758Semax  }
51124758Semax  *_bigbuf = '\n';               // Lead with a sentinal newline
52124758Semax  _buf = _bigbuf+1;                     // Skip sentinal
53124758Semax  _bufmax = _buf;               // Buffer is empty
54124758Semax  _bufeol = _bigbuf;              // _bufeol points at sentinal
55124758Semax  _filepos = -1;                 // filepos is in sync with _bufeol
56124758Semax  _bufoff = _offset = 0L;       // Offset at file start
57124758Semax
58124758Semax  _bufmax += fread(_buf, 1, _bufferSize-2, _fp->_fp); // Fill buffer & set end value
59124758Semax  if (_bufmax == _buf) {
60124758Semax    file_error(SEMERR, 0, "File read error, no input read\n");
61124758Semax    exit(1);                     // Exit on read error
62124758Semax  }
63124758Semax  *_bufmax = '\n';               // End with a sentinal new-line
64124758Semax  *(_bufmax+1) = '\0';           // Then end with a sentinal NULL
65124758Semax}
66124758Semax
67124758Semax//------------------------------~FileBuff--------------------------------------
68124758Semax// Nuke the FileBuff
69124758SemaxFileBuff::~FileBuff() {
70124758Semax  delete _bigbuf;
71124758Semax}
72124758Semax
73124758Semax//------------------------------get_line----------------------------------------
74124758Semaxchar *FileBuff::get_line(void) {
75124758Semax  char *retval;
76124758Semax
77124758Semax  // Check for end of file & return NULL
78124758Semax  if (_bufeol >= _bufmax) return NULL;
79124758Semax
80124758Semax  _linenum++;
81124758Semax  retval = ++_bufeol;      // return character following end of previous line
82124758Semax  if (*retval == '\0') return NULL; // Check for EOF sentinal
83124758Semax  // Search for newline character which must end each line
84124758Semax  for(_filepos++; *_bufeol != '\n'; _bufeol++)
85124758Semax    _filepos++;                    // keep filepos in sync with _bufeol
86124758Semax  // _bufeol & filepos point at end of current line, so return pointer to start
87124758Semax  return retval;
88124758Semax}
89124758Semax
90124758Semax//------------------------------FileBuffRegion---------------------------------
91124758Semax// Create a new region in a FileBuff.
92124758SemaxFileBuffRegion::FileBuffRegion( FileBuff* bufr, int soln, int ln,
93124758Semax                                int off, int len)
94124758Semax: _bfr(bufr), _sol(soln), _line(ln), _offset(off), _length(len) {
95124758Semax  _next = NULL;                 // No chained regions
96124758Semax}
97124758Semax
98124758Semax//------------------------------~FileBuffRegion--------------------------------
99124758Semax// Delete the entire linked list of buffer regions.
100124758SemaxFileBuffRegion::~FileBuffRegion() {
101124758Semax  if( _next ) delete _next;
102124758Semax}
103124758Semax
104124758Semax//------------------------------copy-------------------------------------------
105124758Semax// Deep copy a FileBuffRegion
106124758SemaxFileBuffRegion *FileBuffRegion::copy() {
107124758Semax  if( !this ) return NULL;      // The empty buffer region
108124758Semax  FileBuffRegion *br = new FileBuffRegion(_bfr,_sol,_line,_offset,_length);
109124758Semax  if( _next ) br->_next = _next->copy();
110124758Semax  return br;
111124758Semax}
112124758Semax
113124758Semax//------------------------------merge------------------------------------------
114124758Semax// Merge another buffer region into this buffer region.  Make overlapping areas
115124758Semax// become a single region.  Remove (delete) the input FileBuffRegion.
116124758Semax// Since the buffer regions are sorted by file offset, this is a varient of a
117124758Semax// "sorted-merge" running in linear time.
118124758SemaxFileBuffRegion *FileBuffRegion::merge( FileBuffRegion *br ) {
119124758Semax  if( !br ) return this;        // Merging nothing
120124758Semax  if( !this ) return br;        // Merging into nothing
121124758Semax
122124758Semax  assert( _bfr == br->_bfr, "" );     // Check for pointer-equivalent buffers
123124758Semax
124124758Semax  if( _offset < br->_offset ) { // "this" starts before "br"
125124758Semax    if( _offset+_length < br->_offset ) { // "this" ends before "br"
126124758Semax      if( _next ) _next->merge( br );    // Merge with remainder of list
127124758Semax      else _next = br;                 // No more in this list; just append.
128124758Semax    } else {                           // Regions overlap.
129124758Semax      int l = br->_offset + br->_length - _offset;
130124758Semax      if( l > _length ) _length = l;     // Pick larger region
131124758Semax      FileBuffRegion *nr = br->_next;     // Get rest of region
132124758Semax      br->_next = NULL;         // Remove indication of rest of region
133124758Semax      delete br;                // Delete this region (it's been subsumed).
134124758Semax      if( nr ) merge( nr );     // Merge with rest of region
135124758Semax    }                           // End of if regions overlap or not.
136124758Semax  } else {                      // "this" starts after "br"
137124758Semax    if( br->_offset+br->_length < _offset ) {    // "br" ends before "this"
138124758Semax      FileBuffRegion *nr = new FileBuffRegion(_bfr,_sol,_line,_offset,_length);
139124758Semax      nr->_next = _next;                // Structure copy "this" guy to "nr"
140124758Semax      *this = *br;              // Structure copy "br" over "this".
141124758Semax      br->_next = NULL;         // Remove indication of rest of region
142124758Semax      delete br;                // Delete this region (it's been copied)
143124758Semax      merge( nr );              // Finish merging
144124758Semax    } else {                    // Regions overlap.
145124758Semax      int l = _offset + _length - br->_offset;
146124758Semax      if( l > _length ) _length = l;    // Pick larger region
147124758Semax      _offset = br->_offset;            // Start with earlier region
148124758Semax      _sol = br->_sol;                  // Also use earlier line start
149124758Semax      _line = br->_line;                        // Also use earlier line
150124758Semax      FileBuffRegion *nr = br->_next;   // Get rest of region
151124758Semax      br->_next = NULL;         // Remove indication of rest of region
152124758Semax      delete br;                // Delete this region (it's been subsumed).
153124758Semax      if( nr ) merge( nr );     // Merge with rest of region
154124758Semax    }                           // End of if regions overlap or not.
155124758Semax  }
156124758Semax  return this;
157124758Semax}
158124758Semax
159124758Semax//------------------------------expandtab--------------------------------------
160124758Semaxstatic int expandtab( ostream &os, int off, char c, char fill1, char fill2 ) {
161124758Semax  if( c == '\t' ) {             // Tab?
162124758Semax    do os << fill1;             // Expand the tab; Output space
163124758Semax    while( (++off) & 7 );       // Expand to tab stop
164124758Semax  } else {                      // Normal character
165124758Semax    os << fill2;                // Display normal character
166124758Semax    off++;                      // Increment "cursor" offset
167124758Semax  }
168124758Semax  return off;
169124758Semax}
170124758Semax
171124758Semax//------------------------------printline--------------------------------------
172124758Semax// Print and highlite a region of a line.  Return the amount of highliting left
173124758Semax// to do (i.e. highlite length minus length of line).
174124758Semaxstatic int printline( ostream& os, const char *fname, int line,
175124758Semax                        const char *_sol, int skip, int len ) {
176124758Semax
177124758Semax  // Display the entire tab-expanded line
178124758Semax  os << fname << ":" << line << ": ";
179124758Semax  const char *t = strchr(_sol,'\n')+1; // End of line
180124758Semax  int off = 0;                  // Cursor offset for tab expansion
181124758Semax  const char *s = _sol;         // Nice string pointer
182124758Semax  while( t-s ) {                // Display whole line
183124758Semax    char c = *s++;              // Get next character to display
184124758Semax    off = expandtab(os,off,c,' ',c);
185124758Semax  }
186124758Semax
187124758Semax  // Display the tab-expanded skippings before underlining.
188124758Semax  os << fname << ":" << line << ": ";
189124758Semax  off = 0;                      // Cursor offset for tab expansion
190124758Semax  s = _sol;                     // Restart string pointer
191124758Semax
192124758Semax  // Start underlining.
193124758Semax  if( skip != -1 ) {            // The no-start-indicating flag
194124758Semax    const char *u = _sol+skip;  // Amount to skip
195124758Semax    while( u-s )                // Display skipped part
196124758Semax      off = expandtab(os,off,*s++,' ',' ');
197124758Semax    os << '^';                  // Start region
198124758Semax    off++;                      // Moved cursor
199124758Semax    len--;                      // 1 less char to do
200124758Semax    if( *s++ == '\t' )          // Starting character is a tab?
201124758Semax      off = expandtab(os,off,'\t','-','^');
202124758Semax  }
203124758Semax
204124758Semax  // Long region doesn't end on this line
205124758Semax  int llen = (int)(t-s);        // Length of line, minus what's already done
206124758Semax  if( len > llen ) {            // Doing entire rest of line?
207124758Semax    while( t-s )                // Display rest of line
208124758Semax      off = expandtab(os,off,*s++,'-','-');
209124758Semax    os << '\n';                 // EOL
210124758Semax    return len-llen;            // Return what's not yet done.
211124758Semax  }
212124758Semax
213124758Semax  // Region does end on this line.  This code fails subtly if the region ends
214124758Semax  // in a tab character.
215124758Semax  int i;
216124758Semax  for( i=1; i<len; i++ )        // Underline just what's needed
217124758Semax    off = expandtab(os,off,*s++,'-','-');
218124758Semax  if( i == len ) os << '^';     // Mark end of region
219124758Semax  os << '\n';                   // End of marked line
220124758Semax  return 0L;                    // All done
221124758Semax}
222124758Semax
223124758Semax//------------------------------print------------------------------------------
224124758Semax//std::ostream& operator<< ( std::ostream& os, FileBuffRegion &br ) {
225124758Semaxostream& operator<< ( ostream& os, FileBuffRegion &br ) {
226124758Semax  if( &br == NULL ) return os;  // The empty buffer region
227124758Semax  FileBuffRegion *brp = &br;    // Pointer to region
228124758Semax  while( brp ) {                // While have chained regions
229124758Semax    brp->print(os);             // Print region
230124758Semax    brp = brp->_next;           // Chain to next
231124758Semax  }
232124758Semax  return os;                    // Return final stream
233124758Semax}
234124758Semax
235124758Semax//------------------------------print------------------------------------------
236// Print the FileBuffRegion to a stream. FileBuffRegions are printed with the
237// filename and line number to the left, and complete text lines to the right.
238// Selected portions (portions of a line actually in the FileBuffRegion are
239// underlined.  Ellipses are used for long multi-line regions.
240//void FileBuffRegion::print( std::ostream& os ) {
241void FileBuffRegion::print( ostream& os ) {
242  if( !this ) return;           // Nothing to print
243  char *s = _bfr->get_line();
244  int skip = (int)(_offset - _sol);     // Amount to skip to start of data
245  int len = printline( os, _bfr->_fp->_name, _line, s, skip, _length );
246
247  if( !len ) return;                    // All done; exit
248
249  // Here we require at least 2 lines
250  int off1 = _length - len + skip;      // Length of line 1
251  int off2 = off1 + _sol;               // Offset to start of line 2
252  char *s2 = _bfr->get_line();           // Start of line 2
253  char *s3 = strchr( s2, '\n' )+1;      // Start of line 3 (unread)
254  if( len <= (s3-s2) ) {                // It all fits on the next line
255    printline( os, _bfr->_fp->_name, _line+1, s2, -1, len ); // Print&underline
256    return;
257  }
258
259  // Here we require at least 3 lines
260  int off3 = off2 + (int)(s3-s2);       // Offset to start of line 3
261  s3 = _bfr->get_line();                // Start of line 3 (read)
262  const char *s4 = strchr( s3, '\n' )+1;// Start of line 4 (unread)
263  if( len < (s4-s3) ) {                 // It all fits on the next 2 lines
264    s2 = _bfr->get_line();
265    len = printline( os, _bfr->_fp->_name, _line+1, s2, -1, len ); // Line 2
266    s3 = _bfr->get_line();
267    printline( os, _bfr->_fp->_name, _line+2, s3, -1, len );     // Line 3
268    return;
269  }
270
271  // Here we require at least 4 lines.
272  // Print only the 1st and last line, with ellipses in middle.
273  os << "...\n";                // The ellipses
274  int cline = _line+1;          // Skipped 2 lines
275  do {                          // Do until find last line
276    len -= (int)(s3-s2);        // Remove length of line
277    cline++;                    // Next line
278    s2 = _bfr->get_line();      // Get next line from end of this line
279    s3 = strchr( s2, '\n' ) + 1;// Get end of next line
280  } while( len > (s3-s2) );     // Repeat until last line
281  printline( os, _bfr->_fp->_name, cline, s2, -1, len ); // Print & underline
282}
283
284//------------------------------file_error-------------------------------------
285void FileBuff::file_error(int flag, int linenum, const char *fmt, ...)
286{
287  va_list args;
288
289  va_start(args, fmt);
290  switch (flag) {
291  case 0: _AD._warnings += _AD.emit_msg(0, flag, linenum, fmt, args);
292  case 1: _AD._syntax_errs += _AD.emit_msg(0, flag, linenum, fmt, args);
293  case 2: _AD._semantic_errs += _AD.emit_msg(0, flag, linenum, fmt, args);
294  default: assert(0, ""); break;
295  }
296  va_end(args);
297  _AD._no_output = 1;
298}
299