duke@435: /* twisti@1038: * Copyright 1997-2009 Sun Microsystems, Inc. All Rights Reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * duke@435: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, duke@435: * CA 95054 USA or visit www.sun.com if you need additional information or duke@435: * have any questions. duke@435: * duke@435: */ duke@435: duke@435: // FILEBUFF.CPP - Routines for handling a parser file buffer duke@435: #include "adlc.hpp" duke@435: twisti@1038: using namespace std; twisti@1038: duke@435: //------------------------------FileBuff--------------------------------------- duke@435: // Create a new parsing buffer duke@435: FileBuff::FileBuff( BufferedFile *fptr, ArchDesc& archDesc) : _fp(fptr), _AD(archDesc) { duke@435: _err = fseek(_fp->_fp, 0, SEEK_END); // Seek to end of file duke@435: if (_err) { duke@435: file_error(SEMERR, 0, "File seek error reading input file"); duke@435: exit(1); // Exit on seek error duke@435: } duke@435: _filepos = ftell(_fp->_fp); // Find offset of end of file duke@435: _bufferSize = _filepos + 5; // Filepos points to last char, so add padding duke@435: _err = fseek(_fp->_fp, 0, SEEK_SET); // Reset to beginning of file duke@435: if (_err) { duke@435: file_error(SEMERR, 0, "File seek error reading input file\n"); duke@435: exit(1); // Exit on seek error duke@435: } duke@435: _filepos = ftell(_fp->_fp); // Reset current file position never@850: _linenum = 0; duke@435: duke@435: _bigbuf = new char[_bufferSize]; // Create buffer to hold text for parser duke@435: if( !_bigbuf ) { duke@435: file_error(SEMERR, 0, "Buffer allocation failed\n"); duke@435: exit(1); // Exit on allocation failure duke@435: } twisti@1040: *_bigbuf = '\n'; // Lead with a sentinel newline twisti@1040: _buf = _bigbuf+1; // Skip sentinel duke@435: _bufmax = _buf; // Buffer is empty twisti@1040: _bufeol = _bigbuf; // _bufeol points at sentinel duke@435: _filepos = -1; // filepos is in sync with _bufeol duke@435: _bufoff = _offset = 0L; // Offset at file start duke@435: duke@435: _bufmax += fread(_buf, 1, _bufferSize-2, _fp->_fp); // Fill buffer & set end value duke@435: if (_bufmax == _buf) { duke@435: file_error(SEMERR, 0, "File read error, no input read\n"); duke@435: exit(1); // Exit on read error duke@435: } twisti@1040: *_bufmax = '\n'; // End with a sentinel new-line twisti@1040: *(_bufmax+1) = '\0'; // Then end with a sentinel NULL duke@435: } duke@435: duke@435: //------------------------------~FileBuff-------------------------------------- duke@435: // Nuke the FileBuff duke@435: FileBuff::~FileBuff() { duke@435: delete _bigbuf; duke@435: } duke@435: duke@435: //------------------------------get_line---------------------------------------- duke@435: char *FileBuff::get_line(void) { duke@435: char *retval; duke@435: duke@435: // Check for end of file & return NULL duke@435: if (_bufeol >= _bufmax) return NULL; duke@435: never@850: _linenum++; duke@435: retval = ++_bufeol; // return character following end of previous line twisti@1040: if (*retval == '\0') return NULL; // Check for EOF sentinel duke@435: // Search for newline character which must end each line duke@435: for(_filepos++; *_bufeol != '\n'; _bufeol++) duke@435: _filepos++; // keep filepos in sync with _bufeol duke@435: // _bufeol & filepos point at end of current line, so return pointer to start duke@435: return retval; duke@435: } duke@435: duke@435: //------------------------------FileBuffRegion--------------------------------- duke@435: // Create a new region in a FileBuff. duke@435: FileBuffRegion::FileBuffRegion( FileBuff* bufr, int soln, int ln, duke@435: int off, int len) duke@435: : _bfr(bufr), _sol(soln), _line(ln), _offset(off), _length(len) { duke@435: _next = NULL; // No chained regions duke@435: } duke@435: duke@435: //------------------------------~FileBuffRegion-------------------------------- duke@435: // Delete the entire linked list of buffer regions. duke@435: FileBuffRegion::~FileBuffRegion() { duke@435: if( _next ) delete _next; duke@435: } duke@435: duke@435: //------------------------------copy------------------------------------------- duke@435: // Deep copy a FileBuffRegion duke@435: FileBuffRegion *FileBuffRegion::copy() { duke@435: if( !this ) return NULL; // The empty buffer region duke@435: FileBuffRegion *br = new FileBuffRegion(_bfr,_sol,_line,_offset,_length); duke@435: if( _next ) br->_next = _next->copy(); duke@435: return br; duke@435: } duke@435: duke@435: //------------------------------merge------------------------------------------ duke@435: // Merge another buffer region into this buffer region. Make overlapping areas duke@435: // become a single region. Remove (delete) the input FileBuffRegion. duke@435: // Since the buffer regions are sorted by file offset, this is a varient of a duke@435: // "sorted-merge" running in linear time. duke@435: FileBuffRegion *FileBuffRegion::merge( FileBuffRegion *br ) { duke@435: if( !br ) return this; // Merging nothing duke@435: if( !this ) return br; // Merging into nothing duke@435: duke@435: assert( _bfr == br->_bfr, "" ); // Check for pointer-equivalent buffers duke@435: duke@435: if( _offset < br->_offset ) { // "this" starts before "br" duke@435: if( _offset+_length < br->_offset ) { // "this" ends before "br" duke@435: if( _next ) _next->merge( br ); // Merge with remainder of list duke@435: else _next = br; // No more in this list; just append. duke@435: } else { // Regions overlap. duke@435: int l = br->_offset + br->_length - _offset; duke@435: if( l > _length ) _length = l; // Pick larger region duke@435: FileBuffRegion *nr = br->_next; // Get rest of region duke@435: br->_next = NULL; // Remove indication of rest of region duke@435: delete br; // Delete this region (it's been subsumed). duke@435: if( nr ) merge( nr ); // Merge with rest of region duke@435: } // End of if regions overlap or not. duke@435: } else { // "this" starts after "br" duke@435: if( br->_offset+br->_length < _offset ) { // "br" ends before "this" duke@435: FileBuffRegion *nr = new FileBuffRegion(_bfr,_sol,_line,_offset,_length); duke@435: nr->_next = _next; // Structure copy "this" guy to "nr" duke@435: *this = *br; // Structure copy "br" over "this". duke@435: br->_next = NULL; // Remove indication of rest of region duke@435: delete br; // Delete this region (it's been copied) duke@435: merge( nr ); // Finish merging duke@435: } else { // Regions overlap. duke@435: int l = _offset + _length - br->_offset; duke@435: if( l > _length ) _length = l; // Pick larger region duke@435: _offset = br->_offset; // Start with earlier region duke@435: _sol = br->_sol; // Also use earlier line start duke@435: _line = br->_line; // Also use earlier line duke@435: FileBuffRegion *nr = br->_next; // Get rest of region duke@435: br->_next = NULL; // Remove indication of rest of region duke@435: delete br; // Delete this region (it's been subsumed). duke@435: if( nr ) merge( nr ); // Merge with rest of region duke@435: } // End of if regions overlap or not. duke@435: } duke@435: return this; duke@435: } duke@435: duke@435: //------------------------------expandtab-------------------------------------- duke@435: static int expandtab( ostream &os, int off, char c, char fill1, char fill2 ) { duke@435: if( c == '\t' ) { // Tab? duke@435: do os << fill1; // Expand the tab; Output space duke@435: while( (++off) & 7 ); // Expand to tab stop duke@435: } else { // Normal character duke@435: os << fill2; // Display normal character duke@435: off++; // Increment "cursor" offset duke@435: } duke@435: return off; duke@435: } duke@435: duke@435: //------------------------------printline-------------------------------------- duke@435: // Print and highlite a region of a line. Return the amount of highliting left duke@435: // to do (i.e. highlite length minus length of line). duke@435: static int printline( ostream& os, const char *fname, int line, duke@435: const char *_sol, int skip, int len ) { duke@435: duke@435: // Display the entire tab-expanded line duke@435: os << fname << ":" << line << ": "; duke@435: const char *t = strchr(_sol,'\n')+1; // End of line duke@435: int off = 0; // Cursor offset for tab expansion duke@435: const char *s = _sol; // Nice string pointer duke@435: while( t-s ) { // Display whole line duke@435: char c = *s++; // Get next character to display duke@435: off = expandtab(os,off,c,' ',c); duke@435: } duke@435: duke@435: // Display the tab-expanded skippings before underlining. duke@435: os << fname << ":" << line << ": "; duke@435: off = 0; // Cursor offset for tab expansion duke@435: s = _sol; // Restart string pointer duke@435: duke@435: // Start underlining. duke@435: if( skip != -1 ) { // The no-start-indicating flag duke@435: const char *u = _sol+skip; // Amount to skip duke@435: while( u-s ) // Display skipped part duke@435: off = expandtab(os,off,*s++,' ',' '); duke@435: os << '^'; // Start region duke@435: off++; // Moved cursor duke@435: len--; // 1 less char to do duke@435: if( *s++ == '\t' ) // Starting character is a tab? duke@435: off = expandtab(os,off,'\t','-','^'); duke@435: } duke@435: duke@435: // Long region doesn't end on this line duke@435: int llen = (int)(t-s); // Length of line, minus what's already done duke@435: if( len > llen ) { // Doing entire rest of line? duke@435: while( t-s ) // Display rest of line duke@435: off = expandtab(os,off,*s++,'-','-'); duke@435: os << '\n'; // EOL duke@435: return len-llen; // Return what's not yet done. duke@435: } duke@435: duke@435: // Region does end on this line. This code fails subtly if the region ends duke@435: // in a tab character. duke@435: int i; duke@435: for( i=1; iprint(os); // Print region duke@435: brp = brp->_next; // Chain to next duke@435: } duke@435: return os; // Return final stream duke@435: } duke@435: duke@435: //------------------------------print------------------------------------------ duke@435: // Print the FileBuffRegion to a stream. FileBuffRegions are printed with the duke@435: // filename and line number to the left, and complete text lines to the right. duke@435: // Selected portions (portions of a line actually in the FileBuffRegion are duke@435: // underlined. Ellipses are used for long multi-line regions. duke@435: //void FileBuffRegion::print( std::ostream& os ) { duke@435: void FileBuffRegion::print( ostream& os ) { duke@435: if( !this ) return; // Nothing to print duke@435: char *s = _bfr->get_line(); duke@435: int skip = (int)(_offset - _sol); // Amount to skip to start of data duke@435: int len = printline( os, _bfr->_fp->_name, _line, s, skip, _length ); duke@435: duke@435: if( !len ) return; // All done; exit duke@435: duke@435: // Here we require at least 2 lines duke@435: int off1 = _length - len + skip; // Length of line 1 duke@435: int off2 = off1 + _sol; // Offset to start of line 2 duke@435: char *s2 = _bfr->get_line(); // Start of line 2 duke@435: char *s3 = strchr( s2, '\n' )+1; // Start of line 3 (unread) duke@435: if( len <= (s3-s2) ) { // It all fits on the next line duke@435: printline( os, _bfr->_fp->_name, _line+1, s2, -1, len ); // Print&underline duke@435: return; duke@435: } duke@435: duke@435: // Here we require at least 3 lines duke@435: int off3 = off2 + (int)(s3-s2); // Offset to start of line 3 duke@435: s3 = _bfr->get_line(); // Start of line 3 (read) duke@435: const char *s4 = strchr( s3, '\n' )+1;// Start of line 4 (unread) duke@435: if( len < (s4-s3) ) { // It all fits on the next 2 lines duke@435: s2 = _bfr->get_line(); duke@435: len = printline( os, _bfr->_fp->_name, _line+1, s2, -1, len ); // Line 2 duke@435: s3 = _bfr->get_line(); duke@435: printline( os, _bfr->_fp->_name, _line+2, s3, -1, len ); // Line 3 duke@435: return; duke@435: } duke@435: duke@435: // Here we require at least 4 lines. duke@435: // Print only the 1st and last line, with ellipses in middle. duke@435: os << "...\n"; // The ellipses duke@435: int cline = _line+1; // Skipped 2 lines duke@435: do { // Do until find last line duke@435: len -= (int)(s3-s2); // Remove length of line duke@435: cline++; // Next line duke@435: s2 = _bfr->get_line(); // Get next line from end of this line duke@435: s3 = strchr( s2, '\n' ) + 1;// Get end of next line duke@435: } while( len > (s3-s2) ); // Repeat until last line duke@435: printline( os, _bfr->_fp->_name, cline, s2, -1, len ); // Print & underline duke@435: } duke@435: duke@435: //------------------------------file_error------------------------------------- duke@435: void FileBuff::file_error(int flag, int linenum, const char *fmt, ...) duke@435: { duke@435: va_list args; duke@435: duke@435: va_start(args, fmt); duke@435: switch (flag) { duke@435: case 0: _AD._warnings += _AD.emit_msg(0, flag, linenum, fmt, args); duke@435: case 1: _AD._syntax_errs += _AD.emit_msg(0, flag, linenum, fmt, args); duke@435: case 2: _AD._semantic_errs += _AD.emit_msg(0, flag, linenum, fmt, args); duke@435: default: assert(0, ""); break; duke@435: } duke@435: va_end(args); duke@435: _AD._no_output = 1; duke@435: }