00001 /* A Bison parser, made by GNU Bison 2.3. */ 00002 00003 /* Locations for Bison parsers in C++ 00004 00005 Copyright (C) 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc. 00006 00007 This program is free software; you can redistribute it and/or modify 00008 it under the terms of the GNU General Public License as published by 00009 the Free Software Foundation; either version 2, or (at your option) 00010 any later version. 00011 00012 This program is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 GNU General Public License for more details. 00016 00017 You should have received a copy of the GNU General Public License 00018 along with this program; if not, write to the Free Software 00019 Foundation, Inc., 51 Franklin Street, Fifth Floor, 00020 Boston, MA 02110-1301, USA. */ 00021 00022 /* As a special exception, you may create a larger work that contains 00023 part or all of the Bison parser skeleton and distribute that work 00024 under terms of your choice, so long as that work isn't itself a 00025 parser generator using the skeleton or a modified version thereof 00026 as a parser skeleton. Alternatively, if you modify or redistribute 00027 the parser skeleton itself, you may (at your option) remove this 00028 special exception, which will cause the skeleton and the resulting 00029 Bison output files to be licensed under the GNU General Public 00030 License without this special exception. 00031 00032 This special exception was added by the Free Software Foundation in 00033 version 2.2 of Bison. */ 00034 00035 /** 00036 ** \file location.hh 00037 ** Define the nmt::location class. 00038 */ 00039 00040 #ifndef BISON_LOCATION_HH 00041 # define BISON_LOCATION_HH 00042 00043 # include <iostream> 00044 # include <string> 00045 # include "position.hh" 00046 00047 namespace torc 00048 { 00049 00050 /// Abstract a location. 00051 class location 00052 { 00053 public: 00054 00055 /// Construct a location. 00056 location () 00057 : begin (), end () 00058 { 00059 } 00060 00061 00062 /// Initialization. 00063 inline void initialize (std::string* fn) 00064 { 00065 begin.initialize (fn); 00066 end = begin; 00067 } 00068 00069 /** \name Line and Column related manipulators 00070 ** \{ */ 00071 public: 00072 /// Reset initial location to final location. 00073 inline void step () 00074 { 00075 begin = end; 00076 } 00077 00078 /// Extend the current location to the COUNT next columns. 00079 inline void columns (unsigned int count = 1) 00080 { 00081 end += count; 00082 } 00083 00084 /// Extend the current location to the COUNT next lines. 00085 inline void lines (unsigned int count = 1) 00086 { 00087 end.lines (count); 00088 } 00089 /** \} */ 00090 00091 00092 public: 00093 /// Beginning of the located region. 00094 position begin; 00095 /// End of the located region. 00096 position end; 00097 }; 00098 00099 /// Join two location objects to create a location. 00100 inline const location operator+ (const location& begin, const location& end) 00101 { 00102 location res = begin; 00103 res.end = end.end; 00104 return res; 00105 } 00106 00107 /// Add two location objects. 00108 inline const location operator+ (const location& begin, unsigned int width) 00109 { 00110 location res = begin; 00111 res.columns (width); 00112 return res; 00113 } 00114 00115 /// Add and assign a location. 00116 inline location& operator+= (location& res, unsigned int width) 00117 { 00118 res.columns (width); 00119 return res; 00120 } 00121 00122 /** \brief Intercept output stream redirection. 00123 ** \param ostr the destination output stream 00124 ** \param loc a reference to the location to redirect 00125 ** 00126 ** Avoid duplicate information. 00127 */ 00128 inline std::ostream& operator<< (std::ostream& ostr, const location& loc) 00129 { 00130 position last = loc.end - 1; 00131 ostr << loc.begin; 00132 if (last.filename 00133 && (!loc.begin.filename 00134 || *loc.begin.filename != *last.filename)) 00135 ostr << '-' << last; 00136 else if (loc.begin.line != last.line) 00137 ostr << '-' << last.line << '.' << last.column; 00138 else if (loc.begin.column != last.column) 00139 ostr << '-' << last.column; 00140 return ostr; 00141 } 00142 00143 } 00144 00145 #endif // not BISON_LOCATION_HH