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"
27 using lyx::support::compare_ascii_no_case;
28 using lyx::support::getFormatFromContents;
29 using lyx::support::makeDisplayPath;
30 using lyx::support::split;
31 using lyx::support::subst;
35 using std::lower_bound;
45 : public std::binary_function<keyword_item, keyword_item, bool> {
47 // used by lower_bound, sort and sorted
48 bool operator()(keyword_item const & a, keyword_item const & b) const
50 // we use the ascii version, because in turkish, 'i'
51 // is not the lowercase version of 'I', and thus
52 // turkish locale breaks parsing of tags.
53 return compare_ascii_no_case(a.tag, b.tag) < 0;
57 } // end of anon namespace
60 LyXLex::Pimpl::Pimpl(keyword_item * tab, int num)
61 : is(&fb_), table(tab), no_items(num),
62 status(0), lineno(0), commentChar('#')
68 string const LyXLex::Pimpl::getString() const
70 return string(buff.begin(), buff.end());
74 lyx::docstring const LyXLex::Pimpl::getDocString() const
76 std::vector<boost::uint32_t> res = utf8_to_ucs4(buff);
77 lyx::docstring dstr(res.begin(), res.end());
82 void LyXLex::Pimpl::printError(string const & message) const
84 string const tmpmsg = subst(message, "$$Token", getString());
85 lyxerr << "LyX: " << tmpmsg << " [around line " << lineno
86 << " of file " << lyx::to_utf8(makeDisplayPath(name)) << ']' << endl;
90 void LyXLex::Pimpl::printTable(ostream & os)
92 os << "\nNumber of tags: " << no_items << endl;
93 for (int i= 0; i < no_items; ++i)
95 << "]: tag: `" << table[i].tag
96 << "' code:" << table[i].code << '\n';
101 void LyXLex::Pimpl::verifyTable()
103 // Check if the table is sorted and if not, sort it.
105 && !lyx::sorted(table, table + no_items, compare_tags())) {
106 lyxerr << "The table passed to LyXLex is not sorted!\n"
107 << "Tell the developers to fix it!" << endl;
108 // We sort it anyway to avoid problems.
109 lyxerr << "\nUnsorted:" << endl;
112 sort(table, table + no_items, compare_tags());
113 lyxerr << "\nSorted:" << endl;
119 void LyXLex::Pimpl::pushTable(keyword_item * tab, int num)
121 pushed_table tmppu(table, no_items);
131 void LyXLex::Pimpl::popTable()
133 if (pushed.empty()) {
134 lyxerr << "LyXLex error: nothing to pop!" << endl;
138 pushed_table tmp = pushed.top();
140 table = tmp.table_elem;
141 no_items = tmp.table_siz;
145 bool LyXLex::Pimpl::setFile(string const & filename)
147 // Check the format of the file.
148 string const format = getFormatFromContents(filename);
150 if (format == "gzip" || format == "zip" || format == "compress") {
151 lyxerr[Debug::LYXLEX] << "lyxlex: compressed" << endl;
153 // The check only outputs a debug message, because it triggers
154 // a bug in compaq cxx 6.2, where is_open() returns 'true' for
155 // a fresh new filebuf. (JMarc)
156 if (!gz_.empty() || istream::off_type(is.tellg()) > -1)
157 lyxerr[Debug::LYXLEX] << "Error in LyXLex::setFile: "
158 "file or stream already set." << endl;
159 gz_.push(io::gzip_decompressor());
160 gz_.push(io::file_source(filename));
164 return gz_.component<io::file_source>(1)->is_open() && is.good();
166 lyxerr[Debug::LYXLEX] << "lyxlex: UNcompressed" << endl;
168 // The check only outputs a debug message, because it triggers
169 // a bug in compaq cxx 6.2, where is_open() returns 'true' for
170 // a fresh new filebuf. (JMarc)
171 if (fb_.is_open() || istream::off_type(is.tellg()) > 0)
172 lyxerr[Debug::LYXLEX] << "Error in LyXLex::setFile: "
173 "file or stream already set." << endl;
174 fb_.open(filename.c_str(), ios::in);
178 return fb_.is_open() && is.good();
183 void LyXLex::Pimpl::setStream(istream & i)
185 if (fb_.is_open() || istream::off_type(is.tellg()) > 0)
186 lyxerr[Debug::LYXLEX] << "Error in LyXLex::setStream: "
187 "file or stream already set." << endl;
193 void LyXLex::Pimpl::setCommentChar(char c)
199 bool LyXLex::Pimpl::next(bool esc /* = false */)
201 if (!pushTok.empty()) {
202 // There can have been a whole line pushed so
203 // we extract the first word and leaves the rest
205 if (pushTok.find(' ') != string::npos && pushTok[0] == '\\') {
207 pushTok = split(pushTok, tmp, ' ');
208 buff.assign(tmp.begin(), tmp.end());
211 buff.assign(pushTok.begin(), pushTok.end());
217 unsigned char c = 0; // getc() returns an int
220 while (is && !status) {
223 if (c == commentChar) {
224 // Read rest of line (fast :-)
226 // That is not fast... (Lgb)
230 lyxerr[Debug::LYXLEX] << "Comment read: `" << c
231 << dummy << '\'' << endl;
233 // unfortunately ignore is buggy (Lgb)
234 is.ignore(100, '\n');
248 } while (c != '\"' && c != '\n' && is);
251 printError("Missing quote");
262 continue; /* Skip ','s */
264 // using relational operators with chars other
265 // than == and != is not safe. And if it is done
266 // the type _have_ to be unsigned. It usually a
267 // lot better to use the functions from cctype
275 } while (c > ' ' && c != ',' && is);
280 if (c == '\r' && is) {
281 // The Windows support has lead to the
282 // possibility of "\r\n" at the end of
283 // a line. This will stop LyX choking
284 // when it expected to find a '\n'
293 if (status) return true;
295 status = is.eof() ? LEX_FEOF: LEX_UNDEF;
299 unsigned char c = 0; // getc() returns an int
303 while (is && !status) {
308 if (c == ',') continue;
316 // escape the next char
323 } while (c > ' ' && c != ',' && is);
329 if (c == commentChar) {
330 // Read rest of line (fast :-)
332 // That is still not fast... (Lgb)
336 lyxerr[Debug::LYXLEX] << "Comment read: `" << c
337 << dummy << '\'' << endl;
339 // but ignore is also still buggy (Lgb)
340 // This is fast (Lgb)
341 is.ignore(100, '\n');
351 bool escaped = false;
356 if (c == '\r') continue;
358 // escape the next char
361 if (c == '\"' || c == '\\')
364 buff.push_back('\\');
368 if (!escaped && c == '\"') break;
369 } while (c != '\n' && is);
372 printError("Missing quote");
387 // escape the next char
395 } while (c > ' ' && c != ',' && is);
404 if (status) return true;
406 status = is.eof() ? LEX_FEOF : LEX_UNDEF;
413 int LyXLex::Pimpl::search_kw(char const * const tag) const
415 keyword_item search_tag = { tag, 0 };
417 lower_bound(table, table + no_items,
418 search_tag, compare_tags());
419 // use the compare_ascii_no_case instead of compare_no_case,
420 // because in turkish, 'i' is not the lowercase version of 'I',
421 // and thus turkish locale breaks parsing of tags.
422 if (res != table + no_items
423 && !compare_ascii_no_case(res->tag, tag))
429 int LyXLex::Pimpl::lex()
431 //NOTE: possible bug.
432 if (next() && status == LEX_TOKEN) {
433 return search_kw(getString().c_str());
439 bool LyXLex::Pimpl::eatLine()
443 unsigned char c = '\0';
445 while (is && c != '\n') {
448 //lyxerr[Debug::LYXLEX] << "LyXLex::EatLine read char: `"
449 // << c << '\'' << endl;
465 bool LyXLex::Pimpl::nextToken()
467 if (!pushTok.empty()) {
468 // There can have been a whole line pushed so
469 // we extract the first word and leaves the rest
471 if (pushTok.find(' ') != string::npos && pushTok[0] == '\\') {
473 pushTok = split(pushTok, tmp, ' ');
474 buff.assign(tmp.begin(), tmp.end());
477 buff.assign(pushTok.begin(), pushTok.end());
484 while (is && !status) {
489 if (c >= ' ' && is) {
492 if (c == '\\') { // first char == '\\'
497 } while (c > ' ' && c != '\\' && is);
503 } while (c >= ' ' && c != '\\' && is);
506 if (c == '\\') is.putback(c); // put it back
514 if (status) return true;
516 status = is.eof() ? LEX_FEOF: LEX_UNDEF;
522 void LyXLex::Pimpl::pushToken(string const & pt)