3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
7 * \author Jean-Marc Lasgouttes
10 * Full author contact details are available in file CREDITS.
15 #include "lyxlex_pimpl.h"
19 #include "support/filetools.h"
20 #include "support/lyxalgo.h"
21 #include "support/lstrings.h"
23 using lyx::support::compare_ascii_no_case;
24 using lyx::support::getExtFromContents;
25 using lyx::support::MakeDisplayPath;
26 using lyx::support::split;
27 using lyx::support::subst;
31 using std::lower_bound;
41 : public std::binary_function<keyword_item, keyword_item, bool> {
42 // used by lower_bound, sort and sorted
43 bool operator()(keyword_item const & a, keyword_item const & b) const
45 // we use the ascii version, because in turkish, 'i'
46 // is not the lowercase version of 'I', and thus
47 // turkish locale breaks parsing of tags.
48 return compare_ascii_no_case(a.tag, b.tag) < 0;
52 } // end of anon namespace
55 LyXLex::Pimpl::Pimpl(keyword_item * tab, int num)
56 : is(&fb_), table(tab), no_items(num),
57 status(0), lineno(0), commentChar('#')
63 string const LyXLex::Pimpl::getString() const
65 return string(buff.begin(), buff.end());
69 void LyXLex::Pimpl::printError(string const & message) const
71 string const tmpmsg = subst(message, "$$Token", getString());
72 lyxerr << "LyX: " << tmpmsg << " [around line " << lineno
73 << " of file " << MakeDisplayPath(name) << ']' << endl;
77 void LyXLex::Pimpl::printTable(ostream & os)
79 os << "\nNumber of tags: " << no_items << endl;
80 for (int i= 0; i < no_items; ++i)
82 << "]: tag: `" << table[i].tag
83 << "' code:" << table[i].code << '\n';
88 void LyXLex::Pimpl::verifyTable()
90 // Check if the table is sorted and if not, sort it.
92 && !lyx::sorted(table, table + no_items, compare_tags())) {
93 lyxerr << "The table passed to LyXLex is not sorted!\n"
94 << "Tell the developers to fix it!" << endl;
95 // We sort it anyway to avoid problems.
96 lyxerr << "\nUnsorted:" << endl;
99 sort(table, table + no_items, compare_tags());
100 lyxerr << "\nSorted:" << endl;
106 void LyXLex::Pimpl::pushTable(keyword_item * tab, int num)
108 pushed_table tmppu(table, no_items);
118 void LyXLex::Pimpl::popTable()
120 if (pushed.empty()) {
121 lyxerr << "LyXLex error: nothing to pop!" << endl;
125 pushed_table tmp = pushed.top();
127 table = tmp.table_elem;
128 no_items = tmp.table_siz;
132 bool LyXLex::Pimpl::setFile(string const & filename)
135 // Check the format of the file.
136 string const format = getExtFromContents(filename);
138 if (format == "gzip" || format == "zip" || format == "compress") {
139 lyxerr[Debug::LYXLEX] << "lyxlex: compressed" << endl;
141 // The check only outputs a debug message, because it triggers
142 // a bug in compaq cxx 6.2, where is_open() returns 'true' for
143 // a fresh new filebuf. (JMarc)
144 if (gz_.is_open() || istream::off_type(is.tellg()) > -1)
145 lyxerr[Debug::LYXLEX] << "Error in LyXLex::setFile: "
146 "file or stream already set." << endl;
147 gz_.open(filename.c_str(), ios::in);
151 return gz_.is_open() && is.good();
153 lyxerr[Debug::LYXLEX] << "lyxlex: UNcompressed" << endl;
155 // The check only outputs a debug message, because it triggers
156 // a bug in compaq cxx 6.2, where is_open() returns 'true' for
157 // a fresh new filebuf. (JMarc)
158 if (fb_.is_open() || istream::off_type(is.tellg()) > 0)
159 lyxerr[Debug::LYXLEX] << "Error in LyXLex::setFile: "
160 "file or stream already set." << endl;
161 fb_.open(filename.c_str(), ios::in);
165 return fb_.is_open() && is.good();
170 void LyXLex::Pimpl::setStream(istream & i)
172 if (fb_.is_open() || istream::off_type(is.tellg()) > 0)
173 lyxerr[Debug::LYXLEX] << "Error in LyXLex::setStream: "
174 "file or stream already set." << endl;
180 void LyXLex::Pimpl::setCommentChar(char c)
186 bool LyXLex::Pimpl::next(bool esc /* = false */)
188 if (!pushTok.empty()) {
189 // There can have been a whole line pushed so
190 // we extract the first word and leaves the rest
192 if (pushTok.find(' ') != string::npos && pushTok[0] == '\\') {
194 pushTok = split(pushTok, tmp, ' ');
195 buff.assign(tmp.begin(), tmp.end());
198 buff.assign(pushTok.begin(), pushTok.end());
204 unsigned char c = 0; // getc() returns an int
207 while (is && !status) {
210 if (c == commentChar) {
211 // Read rest of line (fast :-)
213 // That is not fast... (Lgb)
217 lyxerr[Debug::LYXLEX] << "Comment read: `" << c
218 << dummy << '\'' << endl;
220 // unfortunately ignore is buggy (Lgb)
221 is.ignore(100, '\n');
235 } while (c != '\"' && c != '\n' && is);
238 printError("Missing quote");
249 continue; /* Skip ','s */
251 // using relational operators with chars other
252 // than == and != is not safe. And if it is done
253 // the type _have_ to be unsigned. It usually a
254 // lot better to use the functions from cctype
262 } while (c > ' ' && c != ',' && is);
267 if (c == '\r' && is) {
268 // The Windows support has lead to the
269 // possibility of "\r\n" at the end of
270 // a line. This will stop LyX choking
271 // when it expected to find a '\n'
280 if (status) return true;
282 status = is.eof() ? LEX_FEOF: LEX_UNDEF;
286 unsigned char c = 0; // getc() returns an int
290 while (is && !status) {
295 if (c == ',') continue;
303 // escape the next char
310 } while (c > ' ' && c != ',' && is);
316 if (c == commentChar) {
317 // Read rest of line (fast :-)
319 // That is still not fast... (Lgb)
323 lyxerr[Debug::LYXLEX] << "Comment read: `" << c
324 << dummy << '\'' << endl;
326 // but ignore is also still buggy (Lgb)
327 // This is fast (Lgb)
328 is.ignore(100, '\n');
338 bool escaped = false;
343 if (c == '\r') continue;
345 // escape the next char
348 if (c == '\"' || c == '\\')
351 buff.push_back('\\');
355 if (!escaped && c == '\"') break;
356 } while (c != '\n' && is);
359 printError("Missing quote");
374 // escape the next char
382 } while (c > ' ' && c != ',' && is);
391 if (status) return true;
393 status = is.eof() ? LEX_FEOF : LEX_UNDEF;
400 int LyXLex::Pimpl::search_kw(char const * const tag) const
402 keyword_item search_tag = { tag, 0 };
404 lower_bound(table, table + no_items,
405 search_tag, compare_tags());
406 // use the compare_ascii_no_case instead of compare_no_case,
407 // because in turkish, 'i' is not the lowercase version of 'I',
408 // and thus turkish locale breaks parsing of tags.
409 if (res != table + no_items
410 && !compare_ascii_no_case(res->tag, tag))
416 int LyXLex::Pimpl::lex()
418 //NOTE: possible bug.
419 if (next() && status == LEX_TOKEN) {
420 return search_kw(getString().c_str());
426 bool LyXLex::Pimpl::eatLine()
430 unsigned char c = '\0';
432 while (is && c != '\n') {
435 //lyxerr[Debug::LYXLEX] << "LyXLex::EatLine read char: `"
436 // << c << '\'' << endl;
452 bool LyXLex::Pimpl::nextToken()
454 if (!pushTok.empty()) {
455 // There can have been a whole line pushed so
456 // we extract the first word and leaves the rest
458 if (pushTok.find(' ') != string::npos && pushTok[0] == '\\') {
460 pushTok = split(pushTok, tmp, ' ');
461 buff.assign(tmp.begin(), tmp.end());
464 buff.assign(pushTok.begin(), pushTok.end());
471 while (is && !status) {
476 if (c >= ' ' && is) {
479 if (c == '\\') { // first char == '\\'
484 } while (c > ' ' && c != '\\' && is);
490 } while (c >= ' ' && c != '\\' && is);
493 if (c == '\\') is.putback(c); // put it back
501 if (status) return true;
503 status = is.eof() ? LEX_FEOF: LEX_UNDEF;
509 void LyXLex::Pimpl::pushToken(string const & pt)