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_.is_open() || istream::off_type(is.tellg()) > -1)
148 lyxerr[Debug::LYXLEX] << "Error in LyXLex::setFile: "
149 "file or stream already set." << endl;
150 gz_.open(filename.c_str(), ios::in);
154 return gz_.is_open() && is.good();
159 lyxerr[Debug::LYXLEX] << "lyxlex: UNcompressed" << endl;
161 // The check only outputs a debug message, because it triggers
162 // a bug in compaq cxx 6.2, where is_open() returns 'true' for
163 // a fresh new filebuf. (JMarc)
164 if (fb_.is_open() || istream::off_type(is.tellg()) > 0)
165 lyxerr[Debug::LYXLEX] << "Error in LyXLex::setFile: "
166 "file or stream already set." << endl;
167 fb_.open(filename.c_str(), ios::in);
171 return fb_.is_open() && is.good();
176 void LyXLex::Pimpl::setStream(istream & i)
178 if (fb_.is_open() || istream::off_type(is.tellg()) > 0)
179 lyxerr[Debug::LYXLEX] << "Error in LyXLex::setStream: "
180 "file or stream already set." << endl;
186 void LyXLex::Pimpl::setCommentChar(char c)
192 bool LyXLex::Pimpl::next(bool esc /* = false */)
194 if (!pushTok.empty()) {
195 // There can have been a whole line pushed so
196 // we extract the first word and leaves the rest
198 if (pushTok.find(' ') != string::npos && pushTok[0] == '\\') {
200 pushTok = split(pushTok, tmp, ' ');
201 buff.assign(tmp.begin(), tmp.end());
204 buff.assign(pushTok.begin(), pushTok.end());
210 unsigned char c = 0; // getc() returns an int
213 while (is && !status) {
216 if (c == commentChar) {
217 // Read rest of line (fast :-)
219 // That is not fast... (Lgb)
223 lyxerr[Debug::LYXLEX] << "Comment read: `" << c
224 << dummy << '\'' << endl;
226 // unfortunately ignore is buggy (Lgb)
227 is.ignore(100, '\n');
241 } while (c != '\"' && c != '\n' && is);
244 printError("Missing quote");
255 continue; /* Skip ','s */
257 // using relational operators with chars other
258 // than == and != is not safe. And if it is done
259 // the type _have_ to be unsigned. It usually a
260 // lot better to use the functions from cctype
268 } while (c > ' ' && c != ',' && is);
273 if (c == '\r' && is) {
274 // The Windows support has lead to the
275 // possibility of "\r\n" at the end of
276 // a line. This will stop LyX choking
277 // when it expected to find a '\n'
286 if (status) return true;
288 status = is.eof() ? LEX_FEOF: LEX_UNDEF;
292 unsigned char c = 0; // getc() returns an int
296 while (is && !status) {
301 if (c == ',') continue;
309 // escape the next char
316 } while (c > ' ' && c != ',' && is);
322 if (c == commentChar) {
323 // Read rest of line (fast :-)
325 // That is still not fast... (Lgb)
329 lyxerr[Debug::LYXLEX] << "Comment read: `" << c
330 << dummy << '\'' << endl;
332 // but ignore is also still buggy (Lgb)
333 // This is fast (Lgb)
334 is.ignore(100, '\n');
344 bool escaped = false;
349 if (c == '\r') continue;
351 // escape the next char
354 if (c == '\"' || c == '\\')
357 buff.push_back('\\');
361 if (!escaped && c == '\"') break;
362 } while (c != '\n' && is);
365 printError("Missing quote");
380 // escape the next char
388 } while (c > ' ' && c != ',' && is);
397 if (status) return true;
399 status = is.eof() ? LEX_FEOF : LEX_UNDEF;
406 int LyXLex::Pimpl::search_kw(char const * const tag) const
408 keyword_item search_tag = { tag, 0 };
410 lower_bound(table, table + no_items,
411 search_tag, compare_tags());
412 // use the compare_ascii_no_case instead of compare_no_case,
413 // because in turkish, 'i' is not the lowercase version of 'I',
414 // and thus turkish locale breaks parsing of tags.
415 if (res != table + no_items
416 && !compare_ascii_no_case(res->tag, tag))
422 int LyXLex::Pimpl::lex()
424 //NOTE: possible bug.
425 if (next() && status == LEX_TOKEN) {
426 return search_kw(getString().c_str());
432 bool LyXLex::Pimpl::eatLine()
436 unsigned char c = '\0';
438 while (is && c != '\n') {
441 //lyxerr[Debug::LYXLEX] << "LyXLex::EatLine read char: `"
442 // << c << '\'' << endl;
458 bool LyXLex::Pimpl::nextToken()
460 if (!pushTok.empty()) {
461 // There can have been a whole line pushed so
462 // we extract the first word and leaves the rest
464 if (pushTok.find(' ') != string::npos && pushTok[0] == '\\') {
466 pushTok = split(pushTok, tmp, ' ');
467 buff.assign(tmp.begin(), tmp.end());
470 buff.assign(pushTok.begin(), pushTok.end());
477 while (is && !status) {
482 if (c >= ' ' && is) {
485 if (c == '\\') { // first char == '\\'
490 } while (c > ' ' && c != '\\' && is);
496 } while (c >= ' ' && c != '\\' && is);
499 if (c == '\\') is.putback(c); // put it back
507 if (status) return true;
509 status = is.eof() ? LEX_FEOF: LEX_UNDEF;
515 void LyXLex::Pimpl::pushToken(string const & pt)