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> {
45 // used by lower_bound, sort and sorted
46 bool operator()(keyword_item const & a, keyword_item const & b) const
48 // we use the ascii version, because in turkish, 'i'
49 // is not the lowercase version of 'I', and thus
50 // turkish locale breaks parsing of tags.
51 return compare_ascii_no_case(a.tag, b.tag) < 0;
55 } // end of anon namespace
58 LyXLex::Pimpl::Pimpl(keyword_item * tab, int num)
59 : is(&fb_), table(tab), no_items(num),
60 status(0), lineno(0), commentChar('#')
66 string const LyXLex::Pimpl::getString() const
68 return string(buff.begin(), buff.end());
72 void LyXLex::Pimpl::printError(string const & message) const
74 string const tmpmsg = subst(message, "$$Token", getString());
75 lyxerr << "LyX: " << tmpmsg << " [around line " << lineno
76 << " of file " << makeDisplayPath(name) << ']' << endl;
80 void LyXLex::Pimpl::printTable(ostream & os)
82 os << "\nNumber of tags: " << no_items << endl;
83 for (int i= 0; i < no_items; ++i)
85 << "]: tag: `" << table[i].tag
86 << "' code:" << table[i].code << '\n';
91 void LyXLex::Pimpl::verifyTable()
93 // Check if the table is sorted and if not, sort it.
95 && !lyx::sorted(table, table + no_items, compare_tags())) {
96 lyxerr << "The table passed to LyXLex is not sorted!\n"
97 << "Tell the developers to fix it!" << endl;
98 // We sort it anyway to avoid problems.
99 lyxerr << "\nUnsorted:" << endl;
102 sort(table, table + no_items, compare_tags());
103 lyxerr << "\nSorted:" << endl;
109 void LyXLex::Pimpl::pushTable(keyword_item * tab, int num)
111 pushed_table tmppu(table, no_items);
121 void LyXLex::Pimpl::popTable()
123 if (pushed.empty()) {
124 lyxerr << "LyXLex error: nothing to pop!" << endl;
128 pushed_table tmp = pushed.top();
130 table = tmp.table_elem;
131 no_items = tmp.table_siz;
135 bool LyXLex::Pimpl::setFile(string const & filename)
137 // Check the format of the file.
138 string const format = getFormatFromContents(filename);
140 if (format == "gzip" || format == "zip" || format == "compress") {
141 lyxerr[Debug::LYXLEX] << "lyxlex: compressed" << endl;
143 #ifdef USE_COMPRESSION
144 // The check only outputs a debug message, because it triggers
145 // a bug in compaq cxx 6.2, where is_open() returns 'true' for
146 // a fresh new filebuf. (JMarc)
147 if (!gz_.empty() || istream::off_type(is.tellg()) > -1)
148 lyxerr[Debug::LYXLEX] << "Error in LyXLex::setFile: "
149 "file or stream already set." << endl;
150 gz_.push(io::gzip_decompressor());
151 gz_.push(io::file_source(filename));
155 return gz_.component<io::file_source>(1)->is_open() && is.good();
160 lyxerr[Debug::LYXLEX] << "lyxlex: UNcompressed" << endl;
162 // The check only outputs a debug message, because it triggers
163 // a bug in compaq cxx 6.2, where is_open() returns 'true' for
164 // a fresh new filebuf. (JMarc)
165 if (fb_.is_open() || istream::off_type(is.tellg()) > 0)
166 lyxerr[Debug::LYXLEX] << "Error in LyXLex::setFile: "
167 "file or stream already set." << endl;
168 fb_.open(filename.c_str(), ios::in);
172 return fb_.is_open() && is.good();
177 void LyXLex::Pimpl::setStream(istream & i)
179 if (fb_.is_open() || istream::off_type(is.tellg()) > 0)
180 lyxerr[Debug::LYXLEX] << "Error in LyXLex::setStream: "
181 "file or stream already set." << endl;
187 void LyXLex::Pimpl::setCommentChar(char c)
193 bool LyXLex::Pimpl::next(bool esc /* = false */)
195 if (!pushTok.empty()) {
196 // There can have been a whole line pushed so
197 // we extract the first word and leaves the rest
199 if (pushTok.find(' ') != string::npos && pushTok[0] == '\\') {
201 pushTok = split(pushTok, tmp, ' ');
202 buff.assign(tmp.begin(), tmp.end());
205 buff.assign(pushTok.begin(), pushTok.end());
211 unsigned char c = 0; // getc() returns an int
214 while (is && !status) {
217 if (c == commentChar) {
218 // Read rest of line (fast :-)
220 // That is not fast... (Lgb)
224 lyxerr[Debug::LYXLEX] << "Comment read: `" << c
225 << dummy << '\'' << endl;
227 // unfortunately ignore is buggy (Lgb)
228 is.ignore(100, '\n');
242 } while (c != '\"' && c != '\n' && is);
245 printError("Missing quote");
256 continue; /* Skip ','s */
258 // using relational operators with chars other
259 // than == and != is not safe. And if it is done
260 // the type _have_ to be unsigned. It usually a
261 // lot better to use the functions from cctype
269 } while (c > ' ' && c != ',' && is);
274 if (c == '\r' && is) {
275 // The Windows support has lead to the
276 // possibility of "\r\n" at the end of
277 // a line. This will stop LyX choking
278 // when it expected to find a '\n'
287 if (status) return true;
289 status = is.eof() ? LEX_FEOF: LEX_UNDEF;
293 unsigned char c = 0; // getc() returns an int
297 while (is && !status) {
302 if (c == ',') continue;
310 // escape the next char
317 } while (c > ' ' && c != ',' && is);
323 if (c == commentChar) {
324 // Read rest of line (fast :-)
326 // That is still not fast... (Lgb)
330 lyxerr[Debug::LYXLEX] << "Comment read: `" << c
331 << dummy << '\'' << endl;
333 // but ignore is also still buggy (Lgb)
334 // This is fast (Lgb)
335 is.ignore(100, '\n');
345 bool escaped = false;
350 if (c == '\r') continue;
352 // escape the next char
355 if (c == '\"' || c == '\\')
358 buff.push_back('\\');
362 if (!escaped && c == '\"') break;
363 } while (c != '\n' && is);
366 printError("Missing quote");
381 // escape the next char
389 } while (c > ' ' && c != ',' && is);
398 if (status) return true;
400 status = is.eof() ? LEX_FEOF : LEX_UNDEF;
407 int LyXLex::Pimpl::search_kw(char const * const tag) const
409 keyword_item search_tag = { tag, 0 };
411 lower_bound(table, table + no_items,
412 search_tag, compare_tags());
413 // use the compare_ascii_no_case instead of compare_no_case,
414 // because in turkish, 'i' is not the lowercase version of 'I',
415 // and thus turkish locale breaks parsing of tags.
416 if (res != table + no_items
417 && !compare_ascii_no_case(res->tag, tag))
423 int LyXLex::Pimpl::lex()
425 //NOTE: possible bug.
426 if (next() && status == LEX_TOKEN) {
427 return search_kw(getString().c_str());
433 bool LyXLex::Pimpl::eatLine()
437 unsigned char c = '\0';
439 while (is && c != '\n') {
442 //lyxerr[Debug::LYXLEX] << "LyXLex::EatLine read char: `"
443 // << c << '\'' << endl;
459 bool LyXLex::Pimpl::nextToken()
461 if (!pushTok.empty()) {
462 // There can have been a whole line pushed so
463 // we extract the first word and leaves the rest
465 if (pushTok.find(' ') != string::npos && pushTok[0] == '\\') {
467 pushTok = split(pushTok, tmp, ' ');
468 buff.assign(tmp.begin(), tmp.end());
471 buff.assign(pushTok.begin(), pushTok.end());
478 while (is && !status) {
483 if (c >= ' ' && is) {
486 if (c == '\\') { // first char == '\\'
491 } while (c > ' ' && c != '\\' && is);
497 } while (c >= ' ' && c != '\\' && is);
500 if (c == '\\') is.putback(c); // put it back
508 if (status) return true;
510 status = is.eof() ? LEX_FEOF: LEX_UNDEF;
516 void LyXLex::Pimpl::pushToken(string const & pt)