00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 #include <assert.h>
00041 #include <dirent.h>
00042
00043 #include <string>
00044 #include <sstream>
00045
00046 #include <stdlib.h>
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
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
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 }