C-Breeze
C Compiler Infrastructure

[ Project home page]
Main Page   Modules   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

coord.cc

Go to the documentation of this file.
00001 // $Id: coord.cc,v 1.6 2003/08/07 23:13:02 pnav Exp $
00002 // ----------------------------------------------------------------------
00003 //
00004 //  C-Breeze
00005 //  C Compiler Framework
00006 // 
00007 //  Copyright (c) 2000 University of Texas at Austin
00008 // 
00009 //  Samuel Z. Guyer
00010 //  Daniel A. Jimenez
00011 //  Calvin Lin
00012 // 
00013 //  Permission is hereby granted, free of charge, to any person
00014 //  obtaining a copy of this software and associated documentation
00015 //  files (the "Software"), to deal in the Software without
00016 //  restriction, including without limitation the rights to use, copy,
00017 //  modify, merge, publish, distribute, sublicense, and/or sell copies
00018 //  of the Software, and to permit persons to whom the Software is
00019 //  furnished to do so, subject to the following conditions:
00020 //  
00021 //  The above copyright notice and this permission notice shall be
00022 //  included in all copies or substantial portions of the Software.
00023 //  
00024 //  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00025 //  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00026 //  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00027 //  NONINFRINGEMENT.  IN NO EVENT SHALL THE UNIVERSITY OF TEXAS AT
00028 //  AUSTIN BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
00029 //  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
00030 //  OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00031 //  THE SOFTWARE.
00032 //
00033 //  We acknowledge the C-to-C Translator from MIT Laboratory for
00034 //  Computer Science for inspiring parts of the C-Breeze design.
00035 //
00036 // ----------------------------------------------------------------------
00037 
00038 #include "c_breeze.h"
00039 
00040 extern char * cbtext;
00041 extern char * lexer_line_start;
00042 
00043 #define TAB_WIDTH 8
00044 
00045 Coord Coord::Unknown(-1, -1, -1);
00046 
00047 // ------------------------------------------------------------
00048 // Convert simpleCoord to Coord
00049 // ------------------------------------------------------------
00050 
00051 Coord::Coord(simpleCoord s)
00052   : _line(s._line),
00053     _offset(s._offset),
00054     _file(CBZ::current_file)
00055 {}
00056 
00057 // ------------------------------------------------------------
00058 // simpleCoord get_pos
00059 // ------------------------------------------------------------
00060 
00061 // Used only in the lexer
00062 
00063 simpleCoord get_pos()
00064 {
00065   simpleCoord sc;
00066   char * s;
00067   short int the_offset;
00068 
00069   the_offset = 0;
00070   for (s = lexer_line_start; s < cbtext; ++s) {
00071     if (*s == '\t')
00072       the_offset = ((the_offset / TAB_WIDTH) + 1) * TAB_WIDTH;
00073     else 
00074       ++ the_offset;
00075   }
00076 
00077   sc._offset = the_offset;
00078   sc._line = CBZ::current_line;
00079 
00080   return sc;
00081 }
00082 
00083 // ------------------------------------------------------------
00084 // Get
00085 // ------------------------------------------------------------
00086 
00087 // Get the current file, line and offset from the lexer.
00088 
00089 void Coord::get()
00090 {
00091   char * s;
00092   short int the_offset;
00093 
00094   the_offset = 0;
00095   for (s = lexer_line_start; s < cbtext; ++s) {
00096     if (*s == '\t')
00097       the_offset = ((the_offset / TAB_WIDTH) + 1) * TAB_WIDTH;
00098     else 
00099       ++ the_offset;
00100   }
00101 
00102   file(CBZ::current_file);
00103   line(CBZ::current_line);
00104   offset(the_offset);
00105 }
00106 
00107 // ------------------------------------------------------------
00108 // ------------------------------------------------------------
00109 
00110 ostream& operator<< (ostream& lhs, const Coord & rhs) 
00111 {
00112   if (rhs.file() == -1)
00113     lhs << "(unknown)";
00114   else {
00115 
00116     if (CBZ::PrintLineOffset)
00117       lhs << CBZ::Files[rhs.file()] << ":" << rhs.line() << ":" << rhs.offset();
00118     else
00119       lhs << CBZ::Files[rhs.file()] << ":" << rhs.line();
00120   }
00121 
00122   return lhs;
00123 }
00124 
00125 string Coord::to_string()
00126 {
00127   ostringstream ost;
00128 
00129   if (file() == -1)
00130     ost << "(unknown)";
00131   else {
00132 
00133     if (CBZ::PrintLineOffset)
00134       ost << CBZ::Files[file()] << ":" << line() << ":" << offset();
00135     else
00136       ost << CBZ::Files[file()] << ":" << line();
00137   }
00138 
00139   return ost.str();
00140 }
00141 
00142 string & Coord::filename()
00143 {
00144         return CBZ::Files[_file];
00145 }

Generated on August 27, 2003
Back to the C-Breeze home page