8 #include "lyxlex_pimpl.h"
9 #include "support/lyxalgo.h"
10 #include "support/filetools.h"
18 using std::lower_bound;
22 // used by lower_bound, sort and sorted
24 int operator()(keyword_item const & a, keyword_item const & b) const {
25 // we use the ascii version, because in turkish, 'i'
26 // is not the lowercase version of 'I', and thus
27 // turkish locale breaks parsing of tags.
28 return compare_ascii_no_case(a.tag, b.tag) < 0;
31 // } // end of anon namespace
34 LyXLex::Pimpl::Pimpl(keyword_item * tab, int num)
35 : is(&fb__), table(tab), no_items(num),
36 status(0), lineno(0), commentChar('#')
42 string const LyXLex::Pimpl::GetString() const
48 void LyXLex::Pimpl::printError(string const & message) const
50 string const tmpmsg = subst(message, "$$Token", GetString());
51 lyxerr << "LyX: " << tmpmsg << " [around line " << lineno
52 << " of file " << MakeDisplayPath(name) << ']' << endl;
56 void LyXLex::Pimpl::printTable(ostream & os)
58 os << "\nNumber of tags: " << no_items << '\n';
59 for (int i= 0; i < no_items; ++i)
61 << "]: tag: `" << table[i].tag
62 << "' code:" << table[i].code << '\n';
67 void LyXLex::Pimpl::verifyTable()
69 // Check if the table is sorted and if not, sort it.
71 && !lyx::sorted(table, table + no_items, compare_tags())) {
72 lyxerr << "The table passed to LyXLex is not sorted!\n"
73 << "Tell the developers to fix it!" << endl;
74 // We sort it anyway to avoid problems.
75 lyxerr << "\nUnsorted:\n";
78 sort(table, table + no_items, compare_tags());
79 lyxerr << "\nSorted:\n";
85 void LyXLex::Pimpl::pushTable(keyword_item * tab, int num)
87 pushed_table tmppu(table, no_items);
97 void LyXLex::Pimpl::popTable()
100 lyxerr << "LyXLex error: nothing to pop!" << endl;
104 pushed_table tmp = pushed.top();
106 table = tmp.table_elem;
107 no_items = tmp.table_siz;
111 bool LyXLex::Pimpl::setFile(string const & filename)
113 // The check only outputs a debug message, because it triggers
114 // a bug in compaq cxx 6.2, where is_open() returns 'true' for a
115 // fresh new filebuf. (JMarc)
116 if (fb__.is_open() || is.tellg() > 0)
117 lyxerr[Debug::LYXLEX] << "Error in LyXLex::setFile: "
118 "file or stream already set." << endl;
119 fb__.open(filename.c_str(), ios::in);
123 return fb__.is_open() && is.good();
127 void LyXLex::Pimpl::setStream(istream & i)
129 if (fb__.is_open() || is.tellg() > 0)
130 lyxerr[Debug::LYXLEX] << "Error in LyXLex::setStream: "
131 "file or stream already set." << endl;
136 void LyXLex::Pimpl::setCommentChar(char c)
142 bool LyXLex::Pimpl::next(bool esc /* = false */)
144 if (!pushTok.empty()) {
145 // There can have been a whole line pushed so
146 // we extract the first word and leaves the rest
148 if (pushTok.find(' ') != string::npos) {
150 pushTok = split(pushTok, tmp, ' ');
151 tmp.copy(buff, string::npos);
152 buff[tmp.length()] = '\0';
155 pushTok.copy(buff, string::npos);
156 buff[pushTok.length()] = '\0';
162 unsigned char c = 0; // getc() returns an int
165 while (is && !status) {
168 if (c == commentChar) {
169 // Read rest of line (fast :-)
170 // That is not fast... (Lgb)
172 is.getline(buff, sizeof(buff));
173 lyxerr[Debug::LYXLEX] << "Comment read: `" << c
174 << buff << "'" << endl;
176 // unfortunately ignore is buggy (Lgb)
177 is.ignore(100, '\n');
190 } while (c != '\"' && c != '\n' && is &&
191 i != (LEX_MAX_BUFF - 2));
193 if (i == (LEX_MAX_BUFF - 2)) {
194 printError("Line too long");
195 c = '\"'; // Pretend we got a "
200 printError("Missing quote");
211 continue; /* Skip ','s */
213 // using relational operators with chars other
214 // than == and != is not safe. And if it is done
215 // the type _have_ to be unsigned. It usually a
216 // lot better to use the functions from cctype
223 } while (c > ' ' && c != ',' && is
224 && (i != LEX_MAX_BUFF - 1) );
225 if (i == LEX_MAX_BUFF - 1) {
226 printError("Line too long");
232 if (c == '\r' && is) {
233 // The Windows support has lead to the
234 // possibility of "\r\n" at the end of
235 // a line. This will stop LyX choking
236 // when it expected to find a '\n'
245 if (status) return true;
247 status = is.eof() ? LEX_FEOF: LEX_UNDEF;
251 unsigned char c = 0; // getc() returns an int
255 while (is && !status) {
260 if (c == ',') continue;
267 // escape the next char
274 } while (c > ' ' && c != ',' && is
275 && (i != LEX_MAX_BUFF - 1) );
276 if (i == LEX_MAX_BUFF - 1) {
277 printError("Line too long");
284 if (c == commentChar) {
285 // Read rest of line (fast :-)
286 // That is still not fast... (Lgb)
288 is.getline(buff, sizeof(buff));
289 lyxerr[Debug::LYXLEX] << "Comment read: `" << c
290 << buff << "'" << endl;
292 // but ignore is also still buggy (Lgb)
293 // This is fast (Lgb)
294 is.ignore(100, '\n');
303 bool escaped = false;
308 if (c == '\r') continue;
310 // escape the next char
317 if (!escaped && c == '\"') break;
318 } while (c != '\n' && is &&
319 i != (LEX_MAX_BUFF - 2));
321 if (i == (LEX_MAX_BUFF - 2)) {
322 printError("Line too long");
323 c = '\"'; // Pretend we got a "
328 printError("Missing quote");
342 // escape the next char
350 } while (c > ' ' && c != ',' && is
351 && (i != LEX_MAX_BUFF-1) );
352 if (i == LEX_MAX_BUFF-1) {
353 printError("Line too long");
363 if (status) return true;
365 status = is.eof() ? LEX_FEOF : LEX_UNDEF;
372 int LyXLex::Pimpl::search_kw(char const * const tag) const
374 keyword_item search_tag = { tag, 0 };
376 lower_bound(table, table + no_items,
377 search_tag, compare_tags());
378 if (res != table + no_items
379 && !compare_no_case(res->tag, tag))
385 int LyXLex::Pimpl::lex()
387 //NOTE: possible bug.
388 if (next() && status == LEX_TOKEN)
389 return search_kw(buff);
395 bool LyXLex::Pimpl::EatLine()
398 unsigned char c = '\0';
400 while(is && c != '\n' && i != (LEX_MAX_BUFF - 1)) {
403 //lyxerr[Debug::LYXLEX] << "LyXLex::EatLine read char: `"
404 // << c << "'" << endl;
408 if (i == (LEX_MAX_BUFF - 1) && c != '\n') {
409 printError("Line too long");
410 c = '\n'; // Pretend we had an end of line
411 --lineno; // but don't increase line counter (netto effect)
412 ++i; // and preserve last character read.
416 buff[--i] = '\0'; // i can never be 0 here, so no danger
426 bool LyXLex::Pimpl::nextToken()
428 if (!pushTok.empty()) {
429 // There can have been a whole line pushed so
430 // we extract the first word and leaves the rest
432 if (pushTok.find(' ') != string::npos) {
434 pushTok = split(pushTok, tmp, ' ');
435 tmp.copy(buff, string::npos);
436 buff[tmp.length()] = '\0';
439 pushTok.copy(buff, string::npos);
440 buff[pushTok.length()] = '\0';
447 while (is && !status) {
452 if (c >= ' ' && is) {
454 if (c == '\\') { // first char == '\\'
459 } while (c > ' ' && c != '\\' && is
460 && i != (LEX_MAX_BUFF-1));
466 } while (c >= ' ' && c != '\\' && is
467 && i != (LEX_MAX_BUFF-1));
470 if (i == (LEX_MAX_BUFF - 1)) {
471 printError("Line too long");
474 if (c == '\\') is.putback(c); // put it back
483 if (status) return true;
485 status = is.eof() ? LEX_FEOF: LEX_UNDEF;
491 void LyXLex::Pimpl::pushToken(string const & pt)