hotspot/src/share/vm/adlc/filebuff.cpp
author xdono
Mon, 15 Dec 2008 16:55:11 -0800
changeset 1623 a0dd9009e992
parent 1495 128fe18951ed
child 2129 e810a33b5c67
permissions -rw-r--r--
6785258: Update copyright year Summary: Update copyright for files that have been modified starting July 2008 to Dec 2008 Reviewed-by: katleman, ohair, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
1623
a0dd9009e992 6785258: Update copyright year
xdono
parents: 1495
diff changeset
     2
 * Copyright 1997-2008 Sun Microsystems, Inc.  All Rights Reserved.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     4
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
489c9b5090e2 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     8
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
489c9b5090e2 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    14
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
489c9b5090e2 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    18
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
489c9b5090e2 Initial load
duke
parents:
diff changeset
    25
// FILEBUFF.CPP - Routines for handling a parser file buffer
489c9b5090e2 Initial load
duke
parents:
diff changeset
    26
#include "adlc.hpp"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    27
489c9b5090e2 Initial load
duke
parents:
diff changeset
    28
//------------------------------FileBuff---------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
// Create a new parsing buffer
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
FileBuff::FileBuff( BufferedFile *fptr, ArchDesc& archDesc) : _fp(fptr), _AD(archDesc) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
  _err = fseek(_fp->_fp, 0, SEEK_END);  // Seek to end of file
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
  if (_err) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
    file_error(SEMERR, 0, "File seek error reading input file");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
    exit(1);                    // Exit on seek error
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
  _filepos = ftell(_fp->_fp);   // Find offset of end of file
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
  _bufferSize = _filepos + 5;   // Filepos points to last char, so add padding
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
  _err = fseek(_fp->_fp, 0, SEEK_SET);  // Reset to beginning of file
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
  if (_err) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
    file_error(SEMERR, 0, "File seek error reading input file\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
    exit(1);                    // Exit on seek error
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
  _filepos = ftell(_fp->_fp);      // Reset current file position
1495
128fe18951ed 6754519: don't emit flag fixup for NaN when condition being tested doesn't need it
never
parents: 1
diff changeset
    44
  _linenum = 0;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
  _bigbuf = new char[_bufferSize]; // Create buffer to hold text for parser
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
  if( !_bigbuf ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
    file_error(SEMERR, 0, "Buffer allocation failed\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
    exit(1);                    // Exit on allocation failure
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
  *_bigbuf = '\n';               // Lead with a sentinal newline
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
  _buf = _bigbuf+1;                     // Skip sentinal
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
  _bufmax = _buf;               // Buffer is empty
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
  _bufeol = _bigbuf;              // _bufeol points at sentinal
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
  _filepos = -1;                 // filepos is in sync with _bufeol
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
  _bufoff = _offset = 0L;       // Offset at file start
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
  _bufmax += fread(_buf, 1, _bufferSize-2, _fp->_fp); // Fill buffer & set end value
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
  if (_bufmax == _buf) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
    file_error(SEMERR, 0, "File read error, no input read\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
    exit(1);                     // Exit on read error
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
  *_bufmax = '\n';               // End with a sentinal new-line
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
  *(_bufmax+1) = '\0';           // Then end with a sentinal NULL
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
//------------------------------~FileBuff--------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
// Nuke the FileBuff
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
FileBuff::~FileBuff() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
  delete _bigbuf;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
//------------------------------get_line----------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
char *FileBuff::get_line(void) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
  char *retval;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
  // Check for end of file & return NULL
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
  if (_bufeol >= _bufmax) return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
1495
128fe18951ed 6754519: don't emit flag fixup for NaN when condition being tested doesn't need it
never
parents: 1
diff changeset
    80
  _linenum++;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
  retval = ++_bufeol;      // return character following end of previous line
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
  if (*retval == '\0') return NULL; // Check for EOF sentinal
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
  // Search for newline character which must end each line
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
  for(_filepos++; *_bufeol != '\n'; _bufeol++)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
    _filepos++;                    // keep filepos in sync with _bufeol
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
  // _bufeol & filepos point at end of current line, so return pointer to start
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
  return retval;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
//------------------------------FileBuffRegion---------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
// Create a new region in a FileBuff.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
FileBuffRegion::FileBuffRegion( FileBuff* bufr, int soln, int ln,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
                                int off, int len)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
: _bfr(bufr), _sol(soln), _line(ln), _offset(off), _length(len) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
  _next = NULL;                 // No chained regions
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
//------------------------------~FileBuffRegion--------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
// Delete the entire linked list of buffer regions.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
FileBuffRegion::~FileBuffRegion() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
  if( _next ) delete _next;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
//------------------------------copy-------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
// Deep copy a FileBuffRegion
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
FileBuffRegion *FileBuffRegion::copy() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
  if( !this ) return NULL;      // The empty buffer region
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
  FileBuffRegion *br = new FileBuffRegion(_bfr,_sol,_line,_offset,_length);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
  if( _next ) br->_next = _next->copy();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
  return br;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
//------------------------------merge------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
// Merge another buffer region into this buffer region.  Make overlapping areas
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
// become a single region.  Remove (delete) the input FileBuffRegion.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
// Since the buffer regions are sorted by file offset, this is a varient of a
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
// "sorted-merge" running in linear time.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
FileBuffRegion *FileBuffRegion::merge( FileBuffRegion *br ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
  if( !br ) return this;        // Merging nothing
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
  if( !this ) return br;        // Merging into nothing
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
  assert( _bfr == br->_bfr, "" );     // Check for pointer-equivalent buffers
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
  if( _offset < br->_offset ) { // "this" starts before "br"
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
    if( _offset+_length < br->_offset ) { // "this" ends before "br"
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
      if( _next ) _next->merge( br );    // Merge with remainder of list
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
      else _next = br;                 // No more in this list; just append.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
    } else {                           // Regions overlap.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
      int l = br->_offset + br->_length - _offset;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
      if( l > _length ) _length = l;     // Pick larger region
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
      FileBuffRegion *nr = br->_next;     // Get rest of region
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
      br->_next = NULL;         // Remove indication of rest of region
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
      delete br;                // Delete this region (it's been subsumed).
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
      if( nr ) merge( nr );     // Merge with rest of region
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
    }                           // End of if regions overlap or not.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
  } else {                      // "this" starts after "br"
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
    if( br->_offset+br->_length < _offset ) {    // "br" ends before "this"
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
      FileBuffRegion *nr = new FileBuffRegion(_bfr,_sol,_line,_offset,_length);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
      nr->_next = _next;                // Structure copy "this" guy to "nr"
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
      *this = *br;              // Structure copy "br" over "this".
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
      br->_next = NULL;         // Remove indication of rest of region
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
      delete br;                // Delete this region (it's been copied)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
      merge( nr );              // Finish merging
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
    } else {                    // Regions overlap.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
      int l = _offset + _length - br->_offset;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
      if( l > _length ) _length = l;    // Pick larger region
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
      _offset = br->_offset;            // Start with earlier region
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
      _sol = br->_sol;                  // Also use earlier line start
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
      _line = br->_line;                        // Also use earlier line
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
      FileBuffRegion *nr = br->_next;   // Get rest of region
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
      br->_next = NULL;         // Remove indication of rest of region
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
      delete br;                // Delete this region (it's been subsumed).
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
      if( nr ) merge( nr );     // Merge with rest of region
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
    }                           // End of if regions overlap or not.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
  return this;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
//------------------------------expandtab--------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
static int expandtab( ostream &os, int off, char c, char fill1, char fill2 ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
  if( c == '\t' ) {             // Tab?
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
    do os << fill1;             // Expand the tab; Output space
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
    while( (++off) & 7 );       // Expand to tab stop
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
  } else {                      // Normal character
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
    os << fill2;                // Display normal character
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
    off++;                      // Increment "cursor" offset
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
  return off;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
//------------------------------printline--------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
// Print and highlite a region of a line.  Return the amount of highliting left
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
// to do (i.e. highlite length minus length of line).
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
static int printline( ostream& os, const char *fname, int line,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
                        const char *_sol, int skip, int len ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
  // Display the entire tab-expanded line
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
  os << fname << ":" << line << ": ";
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
  const char *t = strchr(_sol,'\n')+1; // End of line
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
  int off = 0;                  // Cursor offset for tab expansion
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
  const char *s = _sol;         // Nice string pointer
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
  while( t-s ) {                // Display whole line
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
    char c = *s++;              // Get next character to display
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
    off = expandtab(os,off,c,' ',c);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
  // Display the tab-expanded skippings before underlining.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   188
  os << fname << ":" << line << ": ";
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
  off = 0;                      // Cursor offset for tab expansion
489c9b5090e2 Initial load
duke
parents:
diff changeset
   190
  s = _sol;                     // Restart string pointer
489c9b5090e2 Initial load
duke
parents:
diff changeset
   191
489c9b5090e2 Initial load
duke
parents:
diff changeset
   192
  // Start underlining.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   193
  if( skip != -1 ) {            // The no-start-indicating flag
489c9b5090e2 Initial load
duke
parents:
diff changeset
   194
    const char *u = _sol+skip;  // Amount to skip
489c9b5090e2 Initial load
duke
parents:
diff changeset
   195
    while( u-s )                // Display skipped part
489c9b5090e2 Initial load
duke
parents:
diff changeset
   196
      off = expandtab(os,off,*s++,' ',' ');
489c9b5090e2 Initial load
duke
parents:
diff changeset
   197
    os << '^';                  // Start region
489c9b5090e2 Initial load
duke
parents:
diff changeset
   198
    off++;                      // Moved cursor
489c9b5090e2 Initial load
duke
parents:
diff changeset
   199
    len--;                      // 1 less char to do
489c9b5090e2 Initial load
duke
parents:
diff changeset
   200
    if( *s++ == '\t' )          // Starting character is a tab?
489c9b5090e2 Initial load
duke
parents:
diff changeset
   201
      off = expandtab(os,off,'\t','-','^');
489c9b5090e2 Initial load
duke
parents:
diff changeset
   202
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   203
489c9b5090e2 Initial load
duke
parents:
diff changeset
   204
  // Long region doesn't end on this line
489c9b5090e2 Initial load
duke
parents:
diff changeset
   205
  int llen = (int)(t-s);        // Length of line, minus what's already done
489c9b5090e2 Initial load
duke
parents:
diff changeset
   206
  if( len > llen ) {            // Doing entire rest of line?
489c9b5090e2 Initial load
duke
parents:
diff changeset
   207
    while( t-s )                // Display rest of line
489c9b5090e2 Initial load
duke
parents:
diff changeset
   208
      off = expandtab(os,off,*s++,'-','-');
489c9b5090e2 Initial load
duke
parents:
diff changeset
   209
    os << '\n';                 // EOL
489c9b5090e2 Initial load
duke
parents:
diff changeset
   210
    return len-llen;            // Return what's not yet done.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   211
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   212
489c9b5090e2 Initial load
duke
parents:
diff changeset
   213
  // Region does end on this line.  This code fails subtly if the region ends
489c9b5090e2 Initial load
duke
parents:
diff changeset
   214
  // in a tab character.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   215
  int i;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   216
  for( i=1; i<len; i++ )        // Underline just what's needed
489c9b5090e2 Initial load
duke
parents:
diff changeset
   217
    off = expandtab(os,off,*s++,'-','-');
489c9b5090e2 Initial load
duke
parents:
diff changeset
   218
  if( i == len ) os << '^';     // Mark end of region
489c9b5090e2 Initial load
duke
parents:
diff changeset
   219
  os << '\n';                   // End of marked line
489c9b5090e2 Initial load
duke
parents:
diff changeset
   220
  return 0L;                    // All done
489c9b5090e2 Initial load
duke
parents:
diff changeset
   221
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   222
489c9b5090e2 Initial load
duke
parents:
diff changeset
   223
//------------------------------print------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   224
//std::ostream& operator<< ( std::ostream& os, FileBuffRegion &br ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   225
ostream& operator<< ( ostream& os, FileBuffRegion &br ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   226
  if( &br == NULL ) return os;  // The empty buffer region
489c9b5090e2 Initial load
duke
parents:
diff changeset
   227
  FileBuffRegion *brp = &br;    // Pointer to region
489c9b5090e2 Initial load
duke
parents:
diff changeset
   228
  while( brp ) {                // While have chained regions
489c9b5090e2 Initial load
duke
parents:
diff changeset
   229
    brp->print(os);             // Print region
489c9b5090e2 Initial load
duke
parents:
diff changeset
   230
    brp = brp->_next;           // Chain to next
489c9b5090e2 Initial load
duke
parents:
diff changeset
   231
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   232
  return os;                    // Return final stream
489c9b5090e2 Initial load
duke
parents:
diff changeset
   233
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   234
489c9b5090e2 Initial load
duke
parents:
diff changeset
   235
//------------------------------print------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   236
// Print the FileBuffRegion to a stream. FileBuffRegions are printed with the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   237
// filename and line number to the left, and complete text lines to the right.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   238
// Selected portions (portions of a line actually in the FileBuffRegion are
489c9b5090e2 Initial load
duke
parents:
diff changeset
   239
// underlined.  Ellipses are used for long multi-line regions.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   240
//void FileBuffRegion::print( std::ostream& os ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   241
void FileBuffRegion::print( ostream& os ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   242
  if( !this ) return;           // Nothing to print
489c9b5090e2 Initial load
duke
parents:
diff changeset
   243
  char *s = _bfr->get_line();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   244
  int skip = (int)(_offset - _sol);     // Amount to skip to start of data
489c9b5090e2 Initial load
duke
parents:
diff changeset
   245
  int len = printline( os, _bfr->_fp->_name, _line, s, skip, _length );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   246
489c9b5090e2 Initial load
duke
parents:
diff changeset
   247
  if( !len ) return;                    // All done; exit
489c9b5090e2 Initial load
duke
parents:
diff changeset
   248
489c9b5090e2 Initial load
duke
parents:
diff changeset
   249
  // Here we require at least 2 lines
489c9b5090e2 Initial load
duke
parents:
diff changeset
   250
  int off1 = _length - len + skip;      // Length of line 1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   251
  int off2 = off1 + _sol;               // Offset to start of line 2
489c9b5090e2 Initial load
duke
parents:
diff changeset
   252
  char *s2 = _bfr->get_line();           // Start of line 2
489c9b5090e2 Initial load
duke
parents:
diff changeset
   253
  char *s3 = strchr( s2, '\n' )+1;      // Start of line 3 (unread)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   254
  if( len <= (s3-s2) ) {                // It all fits on the next line
489c9b5090e2 Initial load
duke
parents:
diff changeset
   255
    printline( os, _bfr->_fp->_name, _line+1, s2, -1, len ); // Print&underline
489c9b5090e2 Initial load
duke
parents:
diff changeset
   256
    return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   257
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   258
489c9b5090e2 Initial load
duke
parents:
diff changeset
   259
  // Here we require at least 3 lines
489c9b5090e2 Initial load
duke
parents:
diff changeset
   260
  int off3 = off2 + (int)(s3-s2);       // Offset to start of line 3
489c9b5090e2 Initial load
duke
parents:
diff changeset
   261
  s3 = _bfr->get_line();                // Start of line 3 (read)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   262
  const char *s4 = strchr( s3, '\n' )+1;// Start of line 4 (unread)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   263
  if( len < (s4-s3) ) {                 // It all fits on the next 2 lines
489c9b5090e2 Initial load
duke
parents:
diff changeset
   264
    s2 = _bfr->get_line();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   265
    len = printline( os, _bfr->_fp->_name, _line+1, s2, -1, len ); // Line 2
489c9b5090e2 Initial load
duke
parents:
diff changeset
   266
    s3 = _bfr->get_line();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   267
    printline( os, _bfr->_fp->_name, _line+2, s3, -1, len );     // Line 3
489c9b5090e2 Initial load
duke
parents:
diff changeset
   268
    return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   269
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   270
489c9b5090e2 Initial load
duke
parents:
diff changeset
   271
  // Here we require at least 4 lines.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   272
  // Print only the 1st and last line, with ellipses in middle.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   273
  os << "...\n";                // The ellipses
489c9b5090e2 Initial load
duke
parents:
diff changeset
   274
  int cline = _line+1;          // Skipped 2 lines
489c9b5090e2 Initial load
duke
parents:
diff changeset
   275
  do {                          // Do until find last line
489c9b5090e2 Initial load
duke
parents:
diff changeset
   276
    len -= (int)(s3-s2);        // Remove length of line
489c9b5090e2 Initial load
duke
parents:
diff changeset
   277
    cline++;                    // Next line
489c9b5090e2 Initial load
duke
parents:
diff changeset
   278
    s2 = _bfr->get_line();      // Get next line from end of this line
489c9b5090e2 Initial load
duke
parents:
diff changeset
   279
    s3 = strchr( s2, '\n' ) + 1;// Get end of next line
489c9b5090e2 Initial load
duke
parents:
diff changeset
   280
  } while( len > (s3-s2) );     // Repeat until last line
489c9b5090e2 Initial load
duke
parents:
diff changeset
   281
  printline( os, _bfr->_fp->_name, cline, s2, -1, len ); // Print & underline
489c9b5090e2 Initial load
duke
parents:
diff changeset
   282
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   283
489c9b5090e2 Initial load
duke
parents:
diff changeset
   284
//------------------------------file_error-------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   285
void FileBuff::file_error(int flag, int linenum, const char *fmt, ...)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   286
{
489c9b5090e2 Initial load
duke
parents:
diff changeset
   287
  va_list args;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   288
489c9b5090e2 Initial load
duke
parents:
diff changeset
   289
  va_start(args, fmt);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   290
  switch (flag) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   291
  case 0: _AD._warnings += _AD.emit_msg(0, flag, linenum, fmt, args);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   292
  case 1: _AD._syntax_errs += _AD.emit_msg(0, flag, linenum, fmt, args);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   293
  case 2: _AD._semantic_errs += _AD.emit_msg(0, flag, linenum, fmt, args);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   294
  default: assert(0, ""); break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   295
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   296
  va_end(args);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   297
  _AD._no_output = 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   298
}