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"
22 #include "support/types.h"
23 #include "support/unicode.h"
30 using support::compare_ascii_no_case;
31 using support::getFormatFromContents;
32 using support::makeDisplayPath;
38 using std::lower_bound;
48 : public std::binary_function<keyword_item, keyword_item, bool> {
50 // used by lower_bound, sort and sorted
51 bool operator()(keyword_item const & a, keyword_item const & b) const
53 // we use the ascii version, because in turkish, 'i'
54 // is not the lowercase version of 'I', and thus
55 // turkish locale breaks parsing of tags.
56 return compare_ascii_no_case(a.tag, b.tag) < 0;
60 } // end of anon namespace
63 LyXLex::Pimpl::Pimpl(keyword_item * tab, int num)
64 : is(&fb_), table(tab), no_items(num),
65 status(0), lineno(0), commentChar('#')
71 string const LyXLex::Pimpl::getString() const
77 docstring const LyXLex::Pimpl::getDocString() const
79 return from_utf8(buff);
83 void LyXLex::Pimpl::printError(string const & message) const
85 string const tmpmsg = subst(message, "$$Token", getString());
86 lyxerr << "LyX: " << tmpmsg << " [around line " << lineno
87 << " of file " << to_utf8(makeDisplayPath(name)) << ']' << endl;
91 void LyXLex::Pimpl::printTable(ostream & os)
93 os << "\nNumber of tags: " << no_items << endl;
94 for (int i= 0; i < no_items; ++i)
96 << "]: tag: `" << table[i].tag
97 << "' code:" << table[i].code << '\n';
102 void LyXLex::Pimpl::verifyTable()
104 // Check if the table is sorted and if not, sort it.
106 && !lyx::sorted(table, table + no_items, compare_tags())) {
107 lyxerr << "The table passed to LyXLex is not sorted!\n"
108 << "Tell the developers to fix it!" << endl;
109 // We sort it anyway to avoid problems.
110 lyxerr << "\nUnsorted:" << endl;
113 sort(table, table + no_items, compare_tags());
114 lyxerr << "\nSorted:" << endl;
120 void LyXLex::Pimpl::pushTable(keyword_item * tab, int num)
122 pushed_table tmppu(table, no_items);
132 void LyXLex::Pimpl::popTable()
134 if (pushed.empty()) {
135 lyxerr << "LyXLex error: nothing to pop!" << endl;
139 pushed_table tmp = pushed.top();
141 table = tmp.table_elem;
142 no_items = tmp.table_siz;
146 bool LyXLex::Pimpl::setFile(string const & filename)
148 // Check the format of the file.
149 string const format = getFormatFromContents(filename);
151 if (format == "gzip" || format == "zip" || format == "compress") {
152 lyxerr[Debug::LYXLEX] << "lyxlex: compressed" << endl;
154 // The check only outputs a debug message, because it triggers
155 // a bug in compaq cxx 6.2, where is_open() returns 'true' for
156 // a fresh new filebuf. (JMarc)
157 if (!gz_.empty() || istream::off_type(is.tellg()) > -1)
158 lyxerr[Debug::LYXLEX] << "Error in LyXLex::setFile: "
159 "file or stream already set." << endl;
160 gz_.push(io::gzip_decompressor());
161 gz_.push(io::file_source(filename));
165 return gz_.component<io::file_source>(1)->is_open() && is.good();
167 lyxerr[Debug::LYXLEX] << "lyxlex: UNcompressed" << endl;
169 // The check only outputs a debug message, because it triggers
170 // a bug in compaq cxx 6.2, where is_open() returns 'true' for
171 // a fresh new filebuf. (JMarc)
172 if (fb_.is_open() || istream::off_type(is.tellg()) > 0)
173 lyxerr[Debug::LYXLEX] << "Error in LyXLex::setFile: "
174 "file or stream already set." << endl;
175 fb_.open(filename.c_str(), ios::in);
179 return fb_.is_open() && is.good();
184 void LyXLex::Pimpl::setStream(istream & i)
186 if (fb_.is_open() || istream::off_type(is.tellg()) > 0)
187 lyxerr[Debug::LYXLEX] << "Error in LyXLex::setStream: "
188 "file or stream already set." << endl;
194 void LyXLex::Pimpl::setCommentChar(char c)
200 bool LyXLex::Pimpl::next(bool esc /* = false */)
202 if (!pushTok.empty()) {
203 // There can have been a whole line pushed so
204 // we extract the first word and leaves the rest
206 if (pushTok.find(' ') != string::npos && pushTok[0] == '\\') {
208 pushTok = split(pushTok, buff, ' ');
217 unsigned char c = 0; // getc() returns an int
220 while (is && !status) {
223 if (c == commentChar) {
224 // Read rest of line (fast :-)
226 // That is not fast... (Lgb)
230 lyxerr[Debug::LYXLEX] << "Comment read: `" << c
231 << dummy << '\'' << endl;
233 // unfortunately ignore is buggy (Lgb)
234 is.ignore(100, '\n');
248 } while (c != '\"' && c != '\n' && is);
251 printError("Missing quote");
256 buff.resize(buff.size()-1);
262 continue; /* Skip ','s */
264 // using relational operators with chars other
265 // than == and != is not safe. And if it is done
266 // the type _have_ to be unsigned. It usually a
267 // lot better to use the functions from cctype
275 } while (c > ' ' && c != ',' && is);
280 if (c == '\r' && is) {
281 // The Windows support has lead to the
282 // possibility of "\r\n" at the end of
283 // a line. This will stop LyX choking
284 // when it expected to find a '\n'
293 if (status) return true;
295 status = is.eof() ? LEX_FEOF: LEX_UNDEF;
299 unsigned char c = 0; // getc() returns an int
303 while (is && !status) {
308 if (c == ',') continue;
316 // escape the next char
323 } while (c > ' ' && c != ',' && is);
329 if (c == commentChar) {
330 // Read rest of line (fast :-)
332 // That is still not fast... (Lgb)
336 lyxerr[Debug::LYXLEX] << "Comment read: `" << c
337 << dummy << '\'' << endl;
339 // but ignore is also still buggy (Lgb)
340 // This is fast (Lgb)
341 is.ignore(100, '\n');
351 bool escaped = false;
356 if (c == '\r') continue;
358 // escape the next char
361 if (c == '\"' || c == '\\')
364 buff.push_back('\\');
368 if (!escaped && c == '\"') break;
369 } while (c != '\n' && is);
372 printError("Missing quote");
377 buff.resize(buff.size() -1);
387 // escape the next char
395 } while (c > ' ' && c != ',' && is);
404 if (status) return true;
406 status = is.eof() ? LEX_FEOF : LEX_UNDEF;
413 int LyXLex::Pimpl::search_kw(char const * const tag) const
415 keyword_item search_tag = { tag, 0 };
417 lower_bound(table, table + no_items,
418 search_tag, compare_tags());
419 // use the compare_ascii_no_case instead of compare_no_case,
420 // because in turkish, 'i' is not the lowercase version of 'I',
421 // and thus turkish locale breaks parsing of tags.
422 if (res != table + no_items
423 && !compare_ascii_no_case(res->tag, tag))
429 int LyXLex::Pimpl::lex()
431 //NOTE: possible bug.
432 if (next() && status == LEX_TOKEN) {
433 return search_kw(getString().c_str());
439 bool LyXLex::Pimpl::eatLine()
443 unsigned char c = '\0';
445 while (is && c != '\n') {
448 //lyxerr[Debug::LYXLEX] << "LyXLex::EatLine read char: `"
449 // << c << '\'' << endl;
456 buff.resize(buff.size() - 1);
465 bool LyXLex::Pimpl::nextToken()
467 if (!pushTok.empty()) {
468 // There can have been a whole line pushed so
469 // we extract the first word and leaves the rest
471 if (pushTok.find(' ') != string::npos && pushTok[0] == '\\') {
473 pushTok = split(pushTok, buff, ' ');
483 while (is && !status) {
488 if (c >= ' ' && is) {
491 if (c == '\\') { // first char == '\\'
496 } while (c > ' ' && c != '\\' && is);
502 } while (c >= ' ' && c != '\\' && is);
505 if (c == '\\') is.putback(c); // put it back
513 if (status) return true;
515 status = is.eof() ? LEX_FEOF: LEX_UNDEF;
521 void LyXLex::Pimpl::pushToken(string const & pt)