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 // The check only outputs a debug message, because it triggers
144 // a bug in compaq cxx 6.2, where is_open() returns 'true' for
145 // a fresh new filebuf. (JMarc)
146 if (gz_.is_open() || istream::off_type(is.tellg()) > -1)
147 lyxerr[Debug::LYXLEX] << "Error in LyXLex::setFile: "
148 "file or stream already set." << endl;
149 gz_.open(filename.c_str(), ios::in);
153 return gz_.is_open() && is.good();
155 lyxerr[Debug::LYXLEX] << "lyxlex: UNcompressed" << endl;
157 // The check only outputs a debug message, because it triggers
158 // a bug in compaq cxx 6.2, where is_open() returns 'true' for
159 // a fresh new filebuf. (JMarc)
160 if (fb_.is_open() || istream::off_type(is.tellg()) > 0)
161 lyxerr[Debug::LYXLEX] << "Error in LyXLex::setFile: "
162 "file or stream already set." << endl;
163 fb_.open(filename.c_str(), ios::in);
167 return fb_.is_open() && is.good();
172 void LyXLex::Pimpl::setStream(istream & i)
174 if (fb_.is_open() || istream::off_type(is.tellg()) > 0)
175 lyxerr[Debug::LYXLEX] << "Error in LyXLex::setStream: "
176 "file or stream already set." << endl;
182 void LyXLex::Pimpl::setCommentChar(char c)
188 bool LyXLex::Pimpl::next(bool esc /* = false */)
190 if (!pushTok.empty()) {
191 // There can have been a whole line pushed so
192 // we extract the first word and leaves the rest
194 if (pushTok.find(' ') != string::npos && pushTok[0] == '\\') {
196 pushTok = split(pushTok, tmp, ' ');
197 buff.assign(tmp.begin(), tmp.end());
200 buff.assign(pushTok.begin(), pushTok.end());
206 unsigned char c = 0; // getc() returns an int
209 while (is && !status) {
212 if (c == commentChar) {
213 // Read rest of line (fast :-)
215 // That is not fast... (Lgb)
219 lyxerr[Debug::LYXLEX] << "Comment read: `" << c
220 << dummy << '\'' << endl;
222 // unfortunately ignore is buggy (Lgb)
223 is.ignore(100, '\n');
237 } while (c != '\"' && c != '\n' && is);
240 printError("Missing quote");
251 continue; /* Skip ','s */
253 // using relational operators with chars other
254 // than == and != is not safe. And if it is done
255 // the type _have_ to be unsigned. It usually a
256 // lot better to use the functions from cctype
264 } while (c > ' ' && c != ',' && is);
269 if (c == '\r' && is) {
270 // The Windows support has lead to the
271 // possibility of "\r\n" at the end of
272 // a line. This will stop LyX choking
273 // when it expected to find a '\n'
282 if (status) return true;
284 status = is.eof() ? LEX_FEOF: LEX_UNDEF;
288 unsigned char c = 0; // getc() returns an int
292 while (is && !status) {
297 if (c == ',') continue;
305 // escape the next char
312 } while (c > ' ' && c != ',' && is);
318 if (c == commentChar) {
319 // Read rest of line (fast :-)
321 // That is still not fast... (Lgb)
325 lyxerr[Debug::LYXLEX] << "Comment read: `" << c
326 << dummy << '\'' << endl;
328 // but ignore is also still buggy (Lgb)
329 // This is fast (Lgb)
330 is.ignore(100, '\n');
340 bool escaped = false;
345 if (c == '\r') continue;
347 // escape the next char
350 if (c == '\"' || c == '\\')
353 buff.push_back('\\');
357 if (!escaped && c == '\"') break;
358 } while (c != '\n' && is);
361 printError("Missing quote");
376 // escape the next char
384 } while (c > ' ' && c != ',' && is);
393 if (status) return true;
395 status = is.eof() ? LEX_FEOF : LEX_UNDEF;
402 int LyXLex::Pimpl::search_kw(char const * const tag) const
404 keyword_item search_tag = { tag, 0 };
406 lower_bound(table, table + no_items,
407 search_tag, compare_tags());
408 // use the compare_ascii_no_case instead of compare_no_case,
409 // because in turkish, 'i' is not the lowercase version of 'I',
410 // and thus turkish locale breaks parsing of tags.
411 if (res != table + no_items
412 && !compare_ascii_no_case(res->tag, tag))
418 int LyXLex::Pimpl::lex()
420 //NOTE: possible bug.
421 if (next() && status == LEX_TOKEN) {
422 return search_kw(getString().c_str());
428 bool LyXLex::Pimpl::eatLine()
432 unsigned char c = '\0';
434 while (is && c != '\n') {
437 //lyxerr[Debug::LYXLEX] << "LyXLex::EatLine read char: `"
438 // << c << '\'' << endl;
454 bool LyXLex::Pimpl::nextToken()
456 if (!pushTok.empty()) {
457 // There can have been a whole line pushed so
458 // we extract the first word and leaves the rest
460 if (pushTok.find(' ') != string::npos && pushTok[0] == '\\') {
462 pushTok = split(pushTok, tmp, ' ');
463 buff.assign(tmp.begin(), tmp.end());
466 buff.assign(pushTok.begin(), pushTok.end());
473 while (is && !status) {
478 if (c >= ' ' && is) {
481 if (c == '\\') { // first char == '\\'
486 } while (c > ' ' && c != '\\' && is);
492 } while (c >= ' ' && c != '\\' && is);
495 if (c == '\\') is.putback(c); // put it back
503 if (status) return true;
505 status = is.eof() ? LEX_FEOF: LEX_UNDEF;
511 void LyXLex::Pimpl::pushToken(string const & pt)