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"
23 using namespace lyx::support;
27 using std::lower_bound;
36 // used by lower_bound, sort and sorted
38 int operator()(keyword_item const & a, keyword_item const & b) const {
39 // we use the ascii version, because in turkish, 'i'
40 // is not the lowercase version of 'I', and thus
41 // turkish locale breaks parsing of tags.
42 return compare_ascii_no_case(a.tag, b.tag) < 0;
45 // } // end of anon namespace
48 LyXLex::Pimpl::Pimpl(keyword_item * tab, int num)
49 : is(&fb__), table(tab), no_items(num),
50 status(0), lineno(0), commentChar('#')
56 string const LyXLex::Pimpl::getString() const
58 return string(buff.begin(), buff.end());
62 void LyXLex::Pimpl::printError(string const & message) const
64 string const tmpmsg = subst(message, "$$Token", getString());
65 lyxerr << "LyX: " << tmpmsg << " [around line " << lineno
66 << " of file " << MakeDisplayPath(name) << ']' << endl;
70 void LyXLex::Pimpl::printTable(ostream & os)
72 os << "\nNumber of tags: " << no_items << endl;
73 for (int i= 0; i < no_items; ++i)
75 << "]: tag: `" << table[i].tag
76 << "' code:" << table[i].code << '\n';
81 void LyXLex::Pimpl::verifyTable()
83 // Check if the table is sorted and if not, sort it.
85 && !lyx::sorted(table, table + no_items, compare_tags())) {
86 lyxerr << "The table passed to LyXLex is not sorted!\n"
87 << "Tell the developers to fix it!" << endl;
88 // We sort it anyway to avoid problems.
89 lyxerr << "\nUnsorted:" << endl;
92 sort(table, table + no_items, compare_tags());
93 lyxerr << "\nSorted:" << endl;
99 void LyXLex::Pimpl::pushTable(keyword_item * tab, int num)
101 pushed_table tmppu(table, no_items);
111 void LyXLex::Pimpl::popTable()
113 if (pushed.empty()) {
114 lyxerr << "LyXLex error: nothing to pop!" << endl;
118 pushed_table tmp = pushed.top();
120 table = tmp.table_elem;
121 no_items = tmp.table_siz;
125 bool LyXLex::Pimpl::setFile(string const & filename)
128 // Check the format of the file.
129 string const format = getExtFromContents(filename);
131 if (format == "gzip" || format == "zip" || format == "compress") {
132 lyxerr << "lyxlex: compressed" << endl;
134 // The check only outputs a debug message, because it triggers
135 // a bug in compaq cxx 6.2, where is_open() returns 'true' for
136 // a fresh new filebuf. (JMarc)
137 if (gz__.is_open() || is.tellg() > 0)
138 lyxerr[Debug::LYXLEX] << "Error in LyXLex::setFile: "
139 "file or stream already set." << endl;
140 gz__.open(filename.c_str(), ios::in);
144 return gz__.is_open() && is.good();
146 lyxerr << "lyxlex: UNcompressed" << endl;
148 // The check only outputs a debug message, because it triggers
149 // a bug in compaq cxx 6.2, where is_open() returns 'true' for
150 // a fresh new filebuf. (JMarc)
151 if (fb__.is_open() || is.tellg() > 0)
152 lyxerr[Debug::LYXLEX] << "Error in LyXLex::setFile: "
153 "file or stream already set." << endl;
154 fb__.open(filename.c_str(), ios::in);
158 return fb__.is_open() && is.good();
163 void LyXLex::Pimpl::setStream(istream & i)
165 if (fb__.is_open() || is.tellg() > 0)
166 lyxerr[Debug::LYXLEX] << "Error in LyXLex::setStream: "
167 "file or stream already set." << endl;
173 void LyXLex::Pimpl::setCommentChar(char c)
179 bool LyXLex::Pimpl::next(bool esc /* = false */)
181 if (!pushTok.empty()) {
182 // There can have been a whole line pushed so
183 // we extract the first word and leaves the rest
185 if (pushTok.find(' ') != string::npos && pushTok[0] == '\\') {
187 pushTok = split(pushTok, tmp, ' ');
188 buff.assign(tmp.begin(), tmp.end());
191 buff.assign(pushTok.begin(), pushTok.end());
197 unsigned char c = 0; // getc() returns an int
200 while (is && !status) {
203 if (c == commentChar) {
204 // Read rest of line (fast :-)
206 // That is not fast... (Lgb)
210 lyxerr[Debug::LYXLEX] << "Comment read: `" << c
211 << dummy << '\'' << endl;
213 // unfortunately ignore is buggy (Lgb)
214 is.ignore(100, '\n');
228 } while (c != '\"' && c != '\n' && is);
231 printError("Missing quote");
242 continue; /* Skip ','s */
244 // using relational operators with chars other
245 // than == and != is not safe. And if it is done
246 // the type _have_ to be unsigned. It usually a
247 // lot better to use the functions from cctype
255 } while (c > ' ' && c != ',' && is);
260 if (c == '\r' && is) {
261 // The Windows support has lead to the
262 // possibility of "\r\n" at the end of
263 // a line. This will stop LyX choking
264 // when it expected to find a '\n'
273 if (status) return true;
275 status = is.eof() ? LEX_FEOF: LEX_UNDEF;
279 unsigned char c = 0; // getc() returns an int
283 while (is && !status) {
288 if (c == ',') continue;
296 // escape the next char
303 } while (c > ' ' && c != ',' && is);
309 if (c == commentChar) {
310 // Read rest of line (fast :-)
312 // That is still not fast... (Lgb)
316 lyxerr[Debug::LYXLEX] << "Comment read: `" << c
317 << dummy << '\'' << endl;
319 // but ignore is also still buggy (Lgb)
320 // This is fast (Lgb)
321 is.ignore(100, '\n');
331 bool escaped = false;
336 if (c == '\r') continue;
338 // escape the next char
341 if (c == '\"' || c == '\\')
344 buff.push_back('\\');
348 if (!escaped && c == '\"') break;
349 } while (c != '\n' && is);
352 printError("Missing quote");
367 // escape the next char
375 } while (c > ' ' && c != ',' && is);
384 if (status) return true;
386 status = is.eof() ? LEX_FEOF : LEX_UNDEF;
393 int LyXLex::Pimpl::search_kw(char const * const tag) const
395 keyword_item search_tag = { tag, 0 };
397 lower_bound(table, table + no_items,
398 search_tag, compare_tags());
399 // use the compare_ascii_no_case instead of compare_no_case,
400 // because in turkish, 'i' is not the lowercase version of 'I',
401 // and thus turkish locale breaks parsing of tags.
402 if (res != table + no_items
403 && !compare_ascii_no_case(res->tag, tag))
409 int LyXLex::Pimpl::lex()
411 //NOTE: possible bug.
412 if (next() && status == LEX_TOKEN) {
413 return search_kw(getString().c_str());
419 bool LyXLex::Pimpl::eatLine()
423 unsigned char c = '\0';
425 while (is && c != '\n') {
428 //lyxerr[Debug::LYXLEX] << "LyXLex::EatLine read char: `"
429 // << c << '\'' << endl;
445 bool LyXLex::Pimpl::nextToken()
447 if (!pushTok.empty()) {
448 // There can have been a whole line pushed so
449 // we extract the first word and leaves the rest
451 if (pushTok.find(' ') != string::npos && pushTok[0] == '\\') {
453 pushTok = split(pushTok, tmp, ' ');
454 buff.assign(tmp.begin(), tmp.end());
457 buff.assign(pushTok.begin(), pushTok.end());
464 while (is && !status) {
469 if (c >= ' ' && is) {
472 if (c == '\\') { // first char == '\\'
477 } while (c > ' ' && c != '\\' && is);
483 } while (c >= ' ' && c != '\\' && is);
486 if (c == '\\') is.putback(c); // put it back
494 if (status) return true;
496 status = is.eof() ? LEX_FEOF: LEX_UNDEF;
502 void LyXLex::Pimpl::pushToken(string const & pt)