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"
25 using lyx::support::compare_ascii_no_case;
26 using lyx::support::getFormatFromContents;
27 using lyx::support::MakeDisplayPath;
28 using lyx::support::split;
29 using lyx::support::subst;
33 using std::lower_bound;
43 : public std::binary_function<keyword_item, keyword_item, bool> {
44 // used by lower_bound, sort and sorted
45 bool operator()(keyword_item const & a, keyword_item const & b) const
47 // we use the ascii version, because in turkish, 'i'
48 // is not the lowercase version of 'I', and thus
49 // turkish locale breaks parsing of tags.
50 return compare_ascii_no_case(a.tag, b.tag) < 0;
54 } // end of anon namespace
57 LyXLex::Pimpl::Pimpl(keyword_item * tab, int num)
58 : is(&fb_), table(tab), no_items(num),
59 status(0), lineno(0), commentChar('#')
65 string const LyXLex::Pimpl::getString() const
67 return string(buff.begin(), buff.end());
71 void LyXLex::Pimpl::printError(string const & message) const
73 string const tmpmsg = subst(message, "$$Token", getString());
74 lyxerr << "LyX: " << tmpmsg << " [around line " << lineno
75 << " of file " << MakeDisplayPath(name) << ']' << endl;
79 void LyXLex::Pimpl::printTable(ostream & os)
81 os << "\nNumber of tags: " << no_items << endl;
82 for (int i= 0; i < no_items; ++i)
84 << "]: tag: `" << table[i].tag
85 << "' code:" << table[i].code << '\n';
90 void LyXLex::Pimpl::verifyTable()
92 // Check if the table is sorted and if not, sort it.
94 && !lyx::sorted(table, table + no_items, compare_tags())) {
95 lyxerr << "The table passed to LyXLex is not sorted!\n"
96 << "Tell the developers to fix it!" << endl;
97 // We sort it anyway to avoid problems.
98 lyxerr << "\nUnsorted:" << endl;
101 sort(table, table + no_items, compare_tags());
102 lyxerr << "\nSorted:" << endl;
108 void LyXLex::Pimpl::pushTable(keyword_item * tab, int num)
110 pushed_table tmppu(table, no_items);
120 void LyXLex::Pimpl::popTable()
122 if (pushed.empty()) {
123 lyxerr << "LyXLex error: nothing to pop!" << endl;
127 pushed_table tmp = pushed.top();
129 table = tmp.table_elem;
130 no_items = tmp.table_siz;
134 bool LyXLex::Pimpl::setFile(string const & filename)
136 // Check the format of the file.
137 string const format = getFormatFromContents(filename);
139 if (format == "gzip" || format == "zip" || format == "compress") {
140 lyxerr[Debug::LYXLEX] << "lyxlex: compressed" << endl;
142 // The check only outputs a debug message, because it triggers
143 // a bug in compaq cxx 6.2, where is_open() returns 'true' for
144 // a fresh new filebuf. (JMarc)
145 if (gz_.is_open() || istream::off_type(is.tellg()) > -1)
146 lyxerr[Debug::LYXLEX] << "Error in LyXLex::setFile: "
147 "file or stream already set." << endl;
148 gz_.open(filename.c_str(), ios::in);
152 return gz_.is_open() && is.good();
154 lyxerr[Debug::LYXLEX] << "lyxlex: UNcompressed" << endl;
156 // The check only outputs a debug message, because it triggers
157 // a bug in compaq cxx 6.2, where is_open() returns 'true' for
158 // a fresh new filebuf. (JMarc)
159 if (fb_.is_open() || istream::off_type(is.tellg()) > 0)
160 lyxerr[Debug::LYXLEX] << "Error in LyXLex::setFile: "
161 "file or stream already set." << endl;
162 fb_.open(filename.c_str(), ios::in);
166 return fb_.is_open() && is.good();
171 void LyXLex::Pimpl::setStream(istream & i)
173 if (fb_.is_open() || istream::off_type(is.tellg()) > 0)
174 lyxerr[Debug::LYXLEX] << "Error in LyXLex::setStream: "
175 "file or stream already set." << endl;
181 void LyXLex::Pimpl::setCommentChar(char c)
187 bool LyXLex::Pimpl::next(bool esc /* = false */)
189 if (!pushTok.empty()) {
190 // There can have been a whole line pushed so
191 // we extract the first word and leaves the rest
193 if (pushTok.find(' ') != string::npos && pushTok[0] == '\\') {
195 pushTok = split(pushTok, tmp, ' ');
196 buff.assign(tmp.begin(), tmp.end());
199 buff.assign(pushTok.begin(), pushTok.end());
205 unsigned char c = 0; // getc() returns an int
208 while (is && !status) {
211 if (c == commentChar) {
212 // Read rest of line (fast :-)
214 // That is not fast... (Lgb)
218 lyxerr[Debug::LYXLEX] << "Comment read: `" << c
219 << dummy << '\'' << endl;
221 // unfortunately ignore is buggy (Lgb)
222 is.ignore(100, '\n');
236 } while (c != '\"' && c != '\n' && is);
239 printError("Missing quote");
250 continue; /* Skip ','s */
252 // using relational operators with chars other
253 // than == and != is not safe. And if it is done
254 // the type _have_ to be unsigned. It usually a
255 // lot better to use the functions from cctype
263 } while (c > ' ' && c != ',' && is);
268 if (c == '\r' && is) {
269 // The Windows support has lead to the
270 // possibility of "\r\n" at the end of
271 // a line. This will stop LyX choking
272 // when it expected to find a '\n'
281 if (status) return true;
283 status = is.eof() ? LEX_FEOF: LEX_UNDEF;
287 unsigned char c = 0; // getc() returns an int
291 while (is && !status) {
296 if (c == ',') continue;
304 // escape the next char
311 } while (c > ' ' && c != ',' && is);
317 if (c == commentChar) {
318 // Read rest of line (fast :-)
320 // That is still not fast... (Lgb)
324 lyxerr[Debug::LYXLEX] << "Comment read: `" << c
325 << dummy << '\'' << endl;
327 // but ignore is also still buggy (Lgb)
328 // This is fast (Lgb)
329 is.ignore(100, '\n');
339 bool escaped = false;
344 if (c == '\r') continue;
346 // escape the next char
349 if (c == '\"' || c == '\\')
352 buff.push_back('\\');
356 if (!escaped && c == '\"') break;
357 } while (c != '\n' && is);
360 printError("Missing quote");
375 // escape the next char
383 } while (c > ' ' && c != ',' && is);
392 if (status) return true;
394 status = is.eof() ? LEX_FEOF : LEX_UNDEF;
401 int LyXLex::Pimpl::search_kw(char const * const tag) const
403 keyword_item search_tag = { tag, 0 };
405 lower_bound(table, table + no_items,
406 search_tag, compare_tags());
407 // use the compare_ascii_no_case instead of compare_no_case,
408 // because in turkish, 'i' is not the lowercase version of 'I',
409 // and thus turkish locale breaks parsing of tags.
410 if (res != table + no_items
411 && !compare_ascii_no_case(res->tag, tag))
417 int LyXLex::Pimpl::lex()
419 //NOTE: possible bug.
420 if (next() && status == LEX_TOKEN) {
421 return search_kw(getString().c_str());
427 bool LyXLex::Pimpl::eatLine()
431 unsigned char c = '\0';
433 while (is && c != '\n') {
436 //lyxerr[Debug::LYXLEX] << "LyXLex::EatLine read char: `"
437 // << c << '\'' << endl;
453 bool LyXLex::Pimpl::nextToken()
455 if (!pushTok.empty()) {
456 // There can have been a whole line pushed so
457 // we extract the first word and leaves the rest
459 if (pushTok.find(' ') != string::npos && pushTok[0] == '\\') {
461 pushTok = split(pushTok, tmp, ' ');
462 buff.assign(tmp.begin(), tmp.end());
465 buff.assign(pushTok.begin(), pushTok.end());
472 while (is && !status) {
477 if (c >= ' ' && is) {
480 if (c == '\\') { // first char == '\\'
485 } while (c > ' ' && c != '\\' && is);
491 } while (c >= ' ' && c != '\\' && is);
494 if (c == '\\') is.putback(c); // put it back
502 if (status) return true;
504 status = is.eof() ? LEX_FEOF: LEX_UNDEF;
510 void LyXLex::Pimpl::pushToken(string const & pt)