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::getFormatFromContents;
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)
134 // Check the format of the file.
135 string const format = getFormatFromContents(filename);
137 if (format == "gzip" || format == "zip" || format == "compress") {
138 lyxerr[Debug::LYXLEX] << "lyxlex: compressed" << endl;
140 // The check only outputs a debug message, because it triggers
141 // a bug in compaq cxx 6.2, where is_open() returns 'true' for
142 // a fresh new filebuf. (JMarc)
143 if (gz_.is_open() || istream::off_type(is.tellg()) > -1)
144 lyxerr[Debug::LYXLEX] << "Error in LyXLex::setFile: "
145 "file or stream already set." << endl;
146 gz_.open(filename.c_str(), ios::in);
150 return gz_.is_open() && is.good();
152 lyxerr[Debug::LYXLEX] << "lyxlex: UNcompressed" << 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 (fb_.is_open() || istream::off_type(is.tellg()) > 0)
158 lyxerr[Debug::LYXLEX] << "Error in LyXLex::setFile: "
159 "file or stream already set." << endl;
160 fb_.open(filename.c_str(), ios::in);
164 return fb_.is_open() && is.good();
169 void LyXLex::Pimpl::setStream(istream & i)
171 if (fb_.is_open() || istream::off_type(is.tellg()) > 0)
172 lyxerr[Debug::LYXLEX] << "Error in LyXLex::setStream: "
173 "file or stream already set." << endl;
179 void LyXLex::Pimpl::setCommentChar(char c)
185 bool LyXLex::Pimpl::next(bool esc /* = false */)
187 if (!pushTok.empty()) {
188 // There can have been a whole line pushed so
189 // we extract the first word and leaves the rest
191 if (pushTok.find(' ') != string::npos && pushTok[0] == '\\') {
193 pushTok = split(pushTok, tmp, ' ');
194 buff.assign(tmp.begin(), tmp.end());
197 buff.assign(pushTok.begin(), pushTok.end());
203 unsigned char c = 0; // getc() returns an int
206 while (is && !status) {
209 if (c == commentChar) {
210 // Read rest of line (fast :-)
212 // That is not fast... (Lgb)
216 lyxerr[Debug::LYXLEX] << "Comment read: `" << c
217 << dummy << '\'' << endl;
219 // unfortunately ignore is buggy (Lgb)
220 is.ignore(100, '\n');
234 } while (c != '\"' && c != '\n' && is);
237 printError("Missing quote");
248 continue; /* Skip ','s */
250 // using relational operators with chars other
251 // than == and != is not safe. And if it is done
252 // the type _have_ to be unsigned. It usually a
253 // lot better to use the functions from cctype
261 } while (c > ' ' && c != ',' && is);
266 if (c == '\r' && is) {
267 // The Windows support has lead to the
268 // possibility of "\r\n" at the end of
269 // a line. This will stop LyX choking
270 // when it expected to find a '\n'
279 if (status) return true;
281 status = is.eof() ? LEX_FEOF: LEX_UNDEF;
285 unsigned char c = 0; // getc() returns an int
289 while (is && !status) {
294 if (c == ',') continue;
302 // escape the next char
309 } while (c > ' ' && c != ',' && is);
315 if (c == commentChar) {
316 // Read rest of line (fast :-)
318 // That is still not fast... (Lgb)
322 lyxerr[Debug::LYXLEX] << "Comment read: `" << c
323 << dummy << '\'' << endl;
325 // but ignore is also still buggy (Lgb)
326 // This is fast (Lgb)
327 is.ignore(100, '\n');
337 bool escaped = false;
342 if (c == '\r') continue;
344 // escape the next char
347 if (c == '\"' || c == '\\')
350 buff.push_back('\\');
354 if (!escaped && c == '\"') break;
355 } while (c != '\n' && is);
358 printError("Missing quote");
373 // escape the next char
381 } while (c > ' ' && c != ',' && is);
390 if (status) return true;
392 status = is.eof() ? LEX_FEOF : LEX_UNDEF;
399 int LyXLex::Pimpl::search_kw(char const * const tag) const
401 keyword_item search_tag = { tag, 0 };
403 lower_bound(table, table + no_items,
404 search_tag, compare_tags());
405 // use the compare_ascii_no_case instead of compare_no_case,
406 // because in turkish, 'i' is not the lowercase version of 'I',
407 // and thus turkish locale breaks parsing of tags.
408 if (res != table + no_items
409 && !compare_ascii_no_case(res->tag, tag))
415 int LyXLex::Pimpl::lex()
417 //NOTE: possible bug.
418 if (next() && status == LEX_TOKEN) {
419 return search_kw(getString().c_str());
425 bool LyXLex::Pimpl::eatLine()
429 unsigned char c = '\0';
431 while (is && c != '\n') {
434 //lyxerr[Debug::LYXLEX] << "LyXLex::EatLine read char: `"
435 // << c << '\'' << endl;
451 bool LyXLex::Pimpl::nextToken()
453 if (!pushTok.empty()) {
454 // There can have been a whole line pushed so
455 // we extract the first word and leaves the rest
457 if (pushTok.find(' ') != string::npos && pushTok[0] == '\\') {
459 pushTok = split(pushTok, tmp, ' ');
460 buff.assign(tmp.begin(), tmp.end());
463 buff.assign(pushTok.begin(), pushTok.end());
470 while (is && !status) {
475 if (c >= ' ' && is) {
478 if (c == '\\') { // first char == '\\'
483 } while (c > ' ' && c != '\\' && is);
489 } while (c >= ' ' && c != '\\' && is);
492 if (c == '\\') is.putback(c); // put it back
500 if (status) return true;
502 status = is.eof() ? LEX_FEOF: LEX_UNDEF;
508 void LyXLex::Pimpl::pushToken(string const & pt)