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"
22 #include "support/types.h"
23 #include "support/unicode.h"
30 using support::compare_ascii_no_case;
31 using support::getFormatFromContents;
32 using support::makeDisplayPath;
38 using std::lower_bound;
48 : public std::binary_function<keyword_item, keyword_item, bool> {
50 // used by lower_bound, sort and sorted
51 bool operator()(keyword_item const & a, keyword_item const & b) const
53 // we use the ascii version, because in turkish, 'i'
54 // is not the lowercase version of 'I', and thus
55 // turkish locale breaks parsing of tags.
56 return compare_ascii_no_case(a.tag, b.tag) < 0;
60 } // end of anon namespace
63 LyXLex::Pimpl::Pimpl(keyword_item * tab, int num)
64 : is(&fb_), table(tab), no_items(num),
65 status(0), lineno(0), commentChar('#')
71 string const LyXLex::Pimpl::getString() const
73 return string(buff.begin(), buff.end());
77 docstring const LyXLex::Pimpl::getDocString() const
79 std::vector<char_type> res = utf8_to_ucs4(buff);
80 docstring dstr(res.begin(), res.end());
85 void LyXLex::Pimpl::printError(string const & message) const
87 string const tmpmsg = subst(message, "$$Token", getString());
88 lyxerr << "LyX: " << tmpmsg << " [around line " << lineno
89 << " of file " << to_utf8(makeDisplayPath(name)) << ']' << endl;
93 void LyXLex::Pimpl::printTable(ostream & os)
95 os << "\nNumber of tags: " << no_items << endl;
96 for (int i= 0; i < no_items; ++i)
98 << "]: tag: `" << table[i].tag
99 << "' code:" << table[i].code << '\n';
104 void LyXLex::Pimpl::verifyTable()
106 // Check if the table is sorted and if not, sort it.
108 && !lyx::sorted(table, table + no_items, compare_tags())) {
109 lyxerr << "The table passed to LyXLex is not sorted!\n"
110 << "Tell the developers to fix it!" << endl;
111 // We sort it anyway to avoid problems.
112 lyxerr << "\nUnsorted:" << endl;
115 sort(table, table + no_items, compare_tags());
116 lyxerr << "\nSorted:" << endl;
122 void LyXLex::Pimpl::pushTable(keyword_item * tab, int num)
124 pushed_table tmppu(table, no_items);
134 void LyXLex::Pimpl::popTable()
136 if (pushed.empty()) {
137 lyxerr << "LyXLex error: nothing to pop!" << endl;
141 pushed_table tmp = pushed.top();
143 table = tmp.table_elem;
144 no_items = tmp.table_siz;
148 bool LyXLex::Pimpl::setFile(string const & filename)
150 // Check the format of the file.
151 string const format = getFormatFromContents(filename);
153 if (format == "gzip" || format == "zip" || format == "compress") {
154 lyxerr[Debug::LYXLEX] << "lyxlex: compressed" << endl;
156 // The check only outputs a debug message, because it triggers
157 // a bug in compaq cxx 6.2, where is_open() returns 'true' for
158 // a fresh new filebuf. (JMarc)
159 if (!gz_.empty() || istream::off_type(is.tellg()) > -1)
160 lyxerr[Debug::LYXLEX] << "Error in LyXLex::setFile: "
161 "file or stream already set." << endl;
162 gz_.push(io::gzip_decompressor());
163 gz_.push(io::file_source(filename));
167 return gz_.component<io::file_source>(1)->is_open() && is.good();
169 lyxerr[Debug::LYXLEX] << "lyxlex: UNcompressed" << endl;
171 // The check only outputs a debug message, because it triggers
172 // a bug in compaq cxx 6.2, where is_open() returns 'true' for
173 // a fresh new filebuf. (JMarc)
174 if (fb_.is_open() || istream::off_type(is.tellg()) > 0)
175 lyxerr[Debug::LYXLEX] << "Error in LyXLex::setFile: "
176 "file or stream already set." << endl;
177 fb_.open(filename.c_str(), ios::in);
181 return fb_.is_open() && is.good();
186 void LyXLex::Pimpl::setStream(istream & i)
188 if (fb_.is_open() || istream::off_type(is.tellg()) > 0)
189 lyxerr[Debug::LYXLEX] << "Error in LyXLex::setStream: "
190 "file or stream already set." << endl;
196 void LyXLex::Pimpl::setCommentChar(char c)
202 bool LyXLex::Pimpl::next(bool esc /* = false */)
204 if (!pushTok.empty()) {
205 // There can have been a whole line pushed so
206 // we extract the first word and leaves the rest
208 if (pushTok.find(' ') != string::npos && pushTok[0] == '\\') {
210 pushTok = split(pushTok, tmp, ' ');
211 buff.assign(tmp.begin(), tmp.end());
214 buff.assign(pushTok.begin(), pushTok.end());
220 unsigned char c = 0; // getc() returns an int
223 while (is && !status) {
226 if (c == commentChar) {
227 // Read rest of line (fast :-)
229 // That is not fast... (Lgb)
233 lyxerr[Debug::LYXLEX] << "Comment read: `" << c
234 << dummy << '\'' << endl;
236 // unfortunately ignore is buggy (Lgb)
237 is.ignore(100, '\n');
251 } while (c != '\"' && c != '\n' && is);
254 printError("Missing quote");
265 continue; /* Skip ','s */
267 // using relational operators with chars other
268 // than == and != is not safe. And if it is done
269 // the type _have_ to be unsigned. It usually a
270 // lot better to use the functions from cctype
278 } while (c > ' ' && c != ',' && is);
283 if (c == '\r' && is) {
284 // The Windows support has lead to the
285 // possibility of "\r\n" at the end of
286 // a line. This will stop LyX choking
287 // when it expected to find a '\n'
296 if (status) return true;
298 status = is.eof() ? LEX_FEOF: LEX_UNDEF;
302 unsigned char c = 0; // getc() returns an int
306 while (is && !status) {
311 if (c == ',') continue;
319 // escape the next char
326 } while (c > ' ' && c != ',' && is);
332 if (c == commentChar) {
333 // Read rest of line (fast :-)
335 // That is still not fast... (Lgb)
339 lyxerr[Debug::LYXLEX] << "Comment read: `" << c
340 << dummy << '\'' << endl;
342 // but ignore is also still buggy (Lgb)
343 // This is fast (Lgb)
344 is.ignore(100, '\n');
354 bool escaped = false;
359 if (c == '\r') continue;
361 // escape the next char
364 if (c == '\"' || c == '\\')
367 buff.push_back('\\');
371 if (!escaped && c == '\"') break;
372 } while (c != '\n' && is);
375 printError("Missing quote");
390 // escape the next char
398 } while (c > ' ' && c != ',' && is);
407 if (status) return true;
409 status = is.eof() ? LEX_FEOF : LEX_UNDEF;
416 int LyXLex::Pimpl::search_kw(char const * const tag) const
418 keyword_item search_tag = { tag, 0 };
420 lower_bound(table, table + no_items,
421 search_tag, compare_tags());
422 // use the compare_ascii_no_case instead of compare_no_case,
423 // because in turkish, 'i' is not the lowercase version of 'I',
424 // and thus turkish locale breaks parsing of tags.
425 if (res != table + no_items
426 && !compare_ascii_no_case(res->tag, tag))
432 int LyXLex::Pimpl::lex()
434 //NOTE: possible bug.
435 if (next() && status == LEX_TOKEN) {
436 return search_kw(getString().c_str());
442 bool LyXLex::Pimpl::eatLine()
446 unsigned char c = '\0';
448 while (is && c != '\n') {
451 //lyxerr[Debug::LYXLEX] << "LyXLex::EatLine read char: `"
452 // << c << '\'' << endl;
468 bool LyXLex::Pimpl::nextToken()
470 if (!pushTok.empty()) {
471 // There can have been a whole line pushed so
472 // we extract the first word and leaves the rest
474 if (pushTok.find(' ') != string::npos && pushTok[0] == '\\') {
476 pushTok = split(pushTok, tmp, ' ');
477 buff.assign(tmp.begin(), tmp.end());
480 buff.assign(pushTok.begin(), pushTok.end());
487 while (is && !status) {
492 if (c >= ' ' && is) {
495 if (c == '\\') { // first char == '\\'
500 } while (c > ' ' && c != '\\' && is);
506 } while (c >= ' ' && c != '\\' && is);
509 if (c == '\\') is.putback(c); // put it back
517 if (status) return true;
519 status = is.eof() ? LEX_FEOF: LEX_UNDEF;
525 void LyXLex::Pimpl::pushToken(string const & pt)