LexLoc.cxx

Go to the documentation of this file.
00001 /**************************************************************************
00002  *
00003  * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, The EROS
00004  *   Group, LLC.
00005  * Copyright (C) 2004, 2005, 2006, Johns Hopkins University.
00006  * All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or
00009  * without modification, are permitted provided that the following
00010  * conditions are met:
00011  *
00012  *   - Redistributions of source code must contain the above
00013  *     copyright notice, this list of conditions, and the following
00014  *     disclaimer.
00015  *
00016  *   - Redistributions in binary form must reproduce the above
00017  *     copyright notice, this list of conditions, and the following
00018  *     disclaimer in the documentation and/or other materials
00019  *     provided with the distribution.
00020  *
00021  *   - Neither the names of the copyright holders nor the names of any
00022  *     of any contributors may be used to endorse or promote products
00023  *     derived from this software without specific prior written
00024  *     permission.
00025  *
00026  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00027  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00028  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00029  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00030  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00031  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00032  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00033  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00034  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00035  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00036  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00037  *
00038  **************************************************************************/
00039 
00040 #include <assert.h>
00041 #include <dirent.h>
00042 
00043 #include <string>
00044 #include <sstream>
00045 
00046 #include <stdlib.h>  // for strtoul()
00047 
00048 #include <libsherpa/LexLoc.hxx>
00049 #include <libsherpa/utf8.hxx>
00050 
00051 namespace sherpa {
00052   std::string
00053   LexLoc::asString() const
00054   {
00055     if (line) {
00056       std::ostringstream msg;
00057       msg << origin << ":"
00058           << line << ":" << offset;
00059       return msg.str();
00060     }
00061     else
00062       return "<internal>";
00063   }
00064 
00065   const char *
00066   LexLoc::c_str() const
00067   {
00068     return asString().c_str();
00069   }
00070 
00071   void
00072   LexLoc::updateWith(const std::string& str)
00073   {
00074     const char *s = str.c_str();
00075     const char *snext = s;
00076 
00077     while (*s) {
00078       uint32_t c = utf8_decode(s, &snext);
00079       s = snext;
00080 
00081       switch(c) {
00082       case '\n':
00083         {
00084           line++;
00085           offset = 0;
00086           break;
00087         }
00088       case '\r':
00089         {
00090           line++;
00091           offset = 0;
00092           // Check for CR LF:
00093           if (*snext && *snext == '\n') {
00094             uint32_t c = utf8_decode(s, &snext);
00095             s = snext;
00096           }
00097           break;
00098         }
00099       default:
00100         {
00101           offset++;
00102           break;
00103         }
00104       }
00105     }
00106   }
00107 
00108   LexLoc
00109   LexLoc::with(const std::string& str)
00110   {
00111     const char *s = str.c_str();
00112     const char *snext = s;
00113 
00114     unsigned theLine = line;
00115     unsigned theOffset = offset;
00116 
00117     while (*s) {
00118       uint32_t c = utf8_decode(s, &snext);
00119       s = snext;
00120 
00121       switch(c) {
00122       case '\n':
00123         {
00124           theLine++;
00125           theOffset = 0;
00126           break;
00127         }
00128       case '\r':
00129         {
00130           theLine++;
00131           theOffset = 0;
00132           // Check for CR LF:
00133           if (*snext && *snext == '\n') {
00134             uint32_t c = utf8_decode(s, &snext);
00135             s = snext;
00136           }
00137           break;
00138         }
00139       default:
00140         {
00141           theOffset++;
00142         }
00143       }
00144     }
00145 
00146     return LexLoc(origin, theLine, theOffset);
00147   }
00148 } /* namespace sherpa */

Generated on Sat Feb 4 23:59:28 2012 for BitC Compiler by  doxygen 1.4.7