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;
40 struct compare_tags : public std::binary_function<keyword_item, keyword_item, int> {
41 // used by lower_bound, sort and sorted
42 int operator()(keyword_item const & a, keyword_item const & b) const {
43 // we use the ascii version, because in turkish, 'i'
44 // is not the lowercase version of 'I', and thus
45 // turkish locale breaks parsing of tags.
46 return compare_ascii_no_case(a.tag, b.tag) < 0;
50 } // end of anon namespace
53 LyXLex::Pimpl::Pimpl(keyword_item * tab, int num)
54 : is(&fb_), table(tab), no_items(num),
55 status(0), lineno(0), commentChar('#')
61 string const LyXLex::Pimpl::getString() const
63 return string(buff.begin(), buff.end());
67 void LyXLex::Pimpl::printError(string const & message) const
69 string const tmpmsg = subst(message, "$$Token", getString());
70 lyxerr << "LyX: " << tmpmsg << " [around line " << lineno
71 << " of file " << MakeDisplayPath(name) << ']' << endl;
75 void LyXLex::Pimpl::printTable(ostream & os)
77 os << "\nNumber of tags: " << no_items << endl;
78 for (int i= 0; i < no_items; ++i)
80 << "]: tag: `" << table[i].tag
81 << "' code:" << table[i].code << '\n';
86 void LyXLex::Pimpl::verifyTable()
88 // Check if the table is sorted and if not, sort it.
90 && !lyx::sorted(table, table + no_items, compare_tags())) {
91 lyxerr << "The table passed to LyXLex is not sorted!\n"
92 << "Tell the developers to fix it!" << endl;
93 // We sort it anyway to avoid problems.
94 lyxerr << "\nUnsorted:" << endl;
97 sort(table, table + no_items, compare_tags());
98 lyxerr << "\nSorted:" << endl;
104 void LyXLex::Pimpl::pushTable(keyword_item * tab, int num)
106 pushed_table tmppu(table, no_items);
116 void LyXLex::Pimpl::popTable()
118 if (pushed.empty()) {
119 lyxerr << "LyXLex error: nothing to pop!" << endl;
123 pushed_table tmp = pushed.top();
125 table = tmp.table_elem;
126 no_items = tmp.table_siz;
130 bool LyXLex::Pimpl::setFile(string const & filename)
133 // Check the format of the file.
134 string const format = getExtFromContents(filename);
136 if (format == "gzip" || format == "zip" || format == "compress") {
137 lyxerr[Debug::LYXLEX] << "lyxlex: compressed" << endl;
139 // The check only outputs a debug message, because it triggers
140 // a bug in compaq cxx 6.2, where is_open() returns 'true' for
141 // a fresh new filebuf. (JMarc)
142 if (gz_.is_open() || istream::off_type(is.tellg()) > -1)
143 lyxerr[Debug::LYXLEX] << "Error in LyXLex::setFile: "
144 "file or stream already set." << endl;
145 gz_.open(filename.c_str(), ios::in);
149 return gz_.is_open() && is.good();
151 lyxerr[Debug::LYXLEX] << "lyxlex: UNcompressed" << endl;
153 // The check only outputs a debug message, because it triggers
154 // a bug in compaq cxx 6.2, where is_open() returns 'true' for
155 // a fresh new filebuf. (JMarc)
156 if (fb_.is_open() || istream::off_type(is.tellg()) > 0)
157 lyxerr[Debug::LYXLEX] << "Error in LyXLex::setFile: "
158 "file or stream already set." << endl;
159 fb_.open(filename.c_str(), ios::in);
163 return fb_.is_open() && is.good();
168 void LyXLex::Pimpl::setStream(istream & i)
170 if (fb_.is_open() || istream::off_type(is.tellg()) > 0)
171 lyxerr[Debug::LYXLEX] << "Error in LyXLex::setStream: "
172 "file or stream already set." << endl;
178 void LyXLex::Pimpl::setCommentChar(char c)
184 bool LyXLex::Pimpl::next(bool esc /* = false */)
186 if (!pushTok.empty()) {
187 // There can have been a whole line pushed so
188 // we extract the first word and leaves the rest
190 if (pushTok.find(' ') != string::npos && pushTok[0] == '\\') {
192 pushTok = split(pushTok, tmp, ' ');
193 buff.assign(tmp.begin(), tmp.end());
196 buff.assign(pushTok.begin(), pushTok.end());
202 unsigned char c = 0; // getc() returns an int
205 while (is && !status) {
208 if (c == commentChar) {
209 // Read rest of line (fast :-)
211 // That is not fast... (Lgb)
215 lyxerr[Debug::LYXLEX] << "Comment read: `" << c
216 << dummy << '\'' << endl;
218 // unfortunately ignore is buggy (Lgb)
219 is.ignore(100, '\n');
233 } while (c != '\"' && c != '\n' && is);
236 printError("Missing quote");
247 continue; /* Skip ','s */
249 // using relational operators with chars other
250 // than == and != is not safe. And if it is done
251 // the type _have_ to be unsigned. It usually a
252 // lot better to use the functions from cctype
260 } while (c > ' ' && c != ',' && is);
265 if (c == '\r' && is) {
266 // The Windows support has lead to the
267 // possibility of "\r\n" at the end of
268 // a line. This will stop LyX choking
269 // when it expected to find a '\n'
278 if (status) return true;
280 status = is.eof() ? LEX_FEOF: LEX_UNDEF;
284 unsigned char c = 0; // getc() returns an int
288 while (is && !status) {
293 if (c == ',') continue;
301 // escape the next char
308 } while (c > ' ' && c != ',' && is);
314 if (c == commentChar) {
315 // Read rest of line (fast :-)
317 // That is still not fast... (Lgb)
321 lyxerr[Debug::LYXLEX] << "Comment read: `" << c
322 << dummy << '\'' << endl;
324 // but ignore is also still buggy (Lgb)
325 // This is fast (Lgb)
326 is.ignore(100, '\n');
336 bool escaped = false;
341 if (c == '\r') continue;
343 // escape the next char
346 if (c == '\"' || c == '\\')
349 buff.push_back('\\');
353 if (!escaped && c == '\"') break;
354 } while (c != '\n' && is);
357 printError("Missing quote");
372 // escape the next char
380 } while (c > ' ' && c != ',' && is);
389 if (status) return true;
391 status = is.eof() ? LEX_FEOF : LEX_UNDEF;
398 int LyXLex::Pimpl::search_kw(char const * const tag) const
400 keyword_item search_tag = { tag, 0 };
402 lower_bound(table, table + no_items,
403 search_tag, compare_tags());
404 // use the compare_ascii_no_case instead of compare_no_case,
405 // because in turkish, 'i' is not the lowercase version of 'I',
406 // and thus turkish locale breaks parsing of tags.
407 if (res != table + no_items
408 && !compare_ascii_no_case(res->tag, tag))
414 int LyXLex::Pimpl::lex()
416 //NOTE: possible bug.
417 if (next() && status == LEX_TOKEN) {
418 return search_kw(getString().c_str());
424 bool LyXLex::Pimpl::eatLine()
428 unsigned char c = '\0';
430 while (is && c != '\n') {
433 //lyxerr[Debug::LYXLEX] << "LyXLex::EatLine read char: `"
434 // << c << '\'' << endl;
450 bool LyXLex::Pimpl::nextToken()
452 if (!pushTok.empty()) {
453 // There can have been a whole line pushed so
454 // we extract the first word and leaves the rest
456 if (pushTok.find(' ') != string::npos && pushTok[0] == '\\') {
458 pushTok = split(pushTok, tmp, ' ');
459 buff.assign(tmp.begin(), tmp.end());
462 buff.assign(pushTok.begin(), pushTok.end());
469 while (is && !status) {
474 if (c >= ' ' && is) {
477 if (c == '\\') { // first char == '\\'
482 } while (c > ' ' && c != '\\' && is);
488 } while (c >= ' ' && c != '\\' && is);
491 if (c == '\\') is.putback(c); // put it back
499 if (status) return true;
501 status = is.eof() ? LEX_FEOF: LEX_UNDEF;
507 void LyXLex::Pimpl::pushToken(string const & pt)