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"
18 #include "support/lyxalgo.h"
19 #include "support/filetools.h"
20 #include "support/lstrings.h"
24 using namespace lyx::support;
31 using std::lower_bound;
37 // used by lower_bound, sort and sorted
39 int operator()(keyword_item const & a, keyword_item const & b) const {
40 // we use the ascii version, because in turkish, 'i'
41 // is not the lowercase version of 'I', and thus
42 // turkish locale breaks parsing of tags.
43 return compare_ascii_no_case(a.tag, b.tag) < 0;
46 // } // end of anon namespace
49 LyXLex::Pimpl::Pimpl(keyword_item * tab, int num)
50 : is(&fb__), table(tab), no_items(num),
51 status(0), lineno(0), commentChar('#')
57 string const LyXLex::Pimpl::getString() const
59 return string(buff.begin(), buff.end());
63 void LyXLex::Pimpl::printError(string const & message) const
65 string const tmpmsg = subst(message, "$$Token", getString());
66 lyxerr << "LyX: " << tmpmsg << " [around line " << lineno
67 << " of file " << MakeDisplayPath(name) << ']' << endl;
71 void LyXLex::Pimpl::printTable(ostream & os)
73 os << "\nNumber of tags: " << no_items << endl;
74 for (int i= 0; i < no_items; ++i)
76 << "]: tag: `" << table[i].tag
77 << "' code:" << table[i].code << '\n';
82 void LyXLex::Pimpl::verifyTable()
84 // Check if the table is sorted and if not, sort it.
86 && !lyx::sorted(table, table + no_items, compare_tags())) {
87 lyxerr << "The table passed to LyXLex is not sorted!\n"
88 << "Tell the developers to fix it!" << endl;
89 // We sort it anyway to avoid problems.
90 lyxerr << "\nUnsorted:" << endl;
93 sort(table, table + no_items, compare_tags());
94 lyxerr << "\nSorted:" << endl;
100 void LyXLex::Pimpl::pushTable(keyword_item * tab, int num)
102 pushed_table tmppu(table, no_items);
112 void LyXLex::Pimpl::popTable()
114 if (pushed.empty()) {
115 lyxerr << "LyXLex error: nothing to pop!" << endl;
119 pushed_table tmp = pushed.top();
121 table = tmp.table_elem;
122 no_items = tmp.table_siz;
126 bool LyXLex::Pimpl::setFile(string const & filename)
129 // Check the format of the file.
130 string const format = getExtFromContents(filename);
132 if (format == "gzip" || format == "zip" || format == "compress") {
133 lyxerr << "lyxlex: compressed" << endl;
135 // The check only outputs a debug message, because it triggers
136 // a bug in compaq cxx 6.2, where is_open() returns 'true' for
137 // a fresh new filebuf. (JMarc)
138 if (gz__.is_open() || is.tellg() > 0)
139 lyxerr[Debug::LYXLEX] << "Error in LyXLex::setFile: "
140 "file or stream already set." << endl;
141 gz__.open(filename.c_str(), ios::in);
145 return gz__.is_open() && is.good();
147 lyxerr << "lyxlex: UNcompressed" << endl;
149 // The check only outputs a debug message, because it triggers
150 // a bug in compaq cxx 6.2, where is_open() returns 'true' for
151 // a fresh new filebuf. (JMarc)
152 if (fb__.is_open() || is.tellg() > 0)
153 lyxerr[Debug::LYXLEX] << "Error in LyXLex::setFile: "
154 "file or stream already set." << endl;
155 fb__.open(filename.c_str(), ios::in);
159 return fb__.is_open() && is.good();
164 void LyXLex::Pimpl::setStream(istream & i)
166 if (fb__.is_open() || is.tellg() > 0)
167 lyxerr[Debug::LYXLEX] << "Error in LyXLex::setStream: "
168 "file or stream already set." << endl;
174 void LyXLex::Pimpl::setCommentChar(char c)
180 bool LyXLex::Pimpl::next(bool esc /* = false */)
182 if (!pushTok.empty()) {
183 // There can have been a whole line pushed so
184 // we extract the first word and leaves the rest
186 if (pushTok.find(' ') != string::npos && pushTok[0] == '\\') {
188 pushTok = split(pushTok, tmp, ' ');
189 buff.assign(tmp.begin(), tmp.end());
192 buff.assign(pushTok.begin(), pushTok.end());
198 unsigned char c = 0; // getc() returns an int
201 while (is && !status) {
204 if (c == commentChar) {
205 // Read rest of line (fast :-)
207 // That is not fast... (Lgb)
211 lyxerr[Debug::LYXLEX] << "Comment read: `" << c
212 << dummy << '\'' << endl;
214 // unfortunately ignore is buggy (Lgb)
215 is.ignore(100, '\n');
229 } while (c != '\"' && c != '\n' && is);
232 printError("Missing quote");
243 continue; /* Skip ','s */
245 // using relational operators with chars other
246 // than == and != is not safe. And if it is done
247 // the type _have_ to be unsigned. It usually a
248 // lot better to use the functions from cctype
256 } while (c > ' ' && c != ',' && is);
261 if (c == '\r' && is) {
262 // The Windows support has lead to the
263 // possibility of "\r\n" at the end of
264 // a line. This will stop LyX choking
265 // when it expected to find a '\n'
274 if (status) return true;
276 status = is.eof() ? LEX_FEOF: LEX_UNDEF;
280 unsigned char c = 0; // getc() returns an int
284 while (is && !status) {
289 if (c == ',') continue;
297 // escape the next char
304 } while (c > ' ' && c != ',' && is);
310 if (c == commentChar) {
311 // Read rest of line (fast :-)
313 // That is still not fast... (Lgb)
317 lyxerr[Debug::LYXLEX] << "Comment read: `" << c
318 << dummy << '\'' << endl;
320 // but ignore is also still buggy (Lgb)
321 // This is fast (Lgb)
322 is.ignore(100, '\n');
332 bool escaped = false;
337 if (c == '\r') continue;
339 // escape the next char
342 if (c == '\"' || c == '\\')
345 buff.push_back('\\');
349 if (!escaped && c == '\"') break;
350 } while (c != '\n' && is);
353 printError("Missing quote");
368 // escape the next char
376 } while (c > ' ' && c != ',' && is);
385 if (status) return true;
387 status = is.eof() ? LEX_FEOF : LEX_UNDEF;
394 int LyXLex::Pimpl::search_kw(char const * const tag) const
396 keyword_item search_tag = { tag, 0 };
398 lower_bound(table, table + no_items,
399 search_tag, compare_tags());
400 // use the compare_ascii_no_case instead of compare_no_case,
401 // because in turkish, 'i' is not the lowercase version of 'I',
402 // and thus turkish locale breaks parsing of tags.
403 if (res != table + no_items
404 && !compare_ascii_no_case(res->tag, tag))
410 int LyXLex::Pimpl::lex()
412 //NOTE: possible bug.
413 if (next() && status == LEX_TOKEN) {
414 return search_kw(getString().c_str());
420 bool LyXLex::Pimpl::eatLine()
424 unsigned char c = '\0';
426 while (is && c != '\n') {
429 //lyxerr[Debug::LYXLEX] << "LyXLex::EatLine read char: `"
430 // << c << '\'' << endl;
446 bool LyXLex::Pimpl::nextToken()
448 if (!pushTok.empty()) {
449 // There can have been a whole line pushed so
450 // we extract the first word and leaves the rest
452 if (pushTok.find(' ') != string::npos && pushTok[0] == '\\') {
454 pushTok = split(pushTok, tmp, ' ');
455 buff.assign(tmp.begin(), tmp.end());
458 buff.assign(pushTok.begin(), pushTok.end());
465 while (is && !status) {
470 if (c >= ' ' && is) {
473 if (c == '\\') { // first char == '\\'
478 } while (c > ' ' && c != '\\' && is);
484 } while (c >= ' ' && c != '\\' && is);
487 if (c == '\\') is.putback(c); // put it back
495 if (status) return true;
497 status = is.eof() ? LEX_FEOF: LEX_UNDEF;
503 void LyXLex::Pimpl::pushToken(string const & pt)