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_.empty() || istream::off_type(is.tellg()) > -1)
147 lyxerr[Debug::LYXLEX] << "Error in LyXLex::setFile: "
148 "file or stream already set." << endl;
149 gz_.push(io::gzip_decompressor());
150 gz_.push(io::file_source(filename));
154 return gz_.component<io::file_source>(1)->is_open() && is.good();
156 lyxerr[Debug::LYXLEX] << "lyxlex: UNcompressed" << endl;
158 // The check only outputs a debug message, because it triggers
159 // a bug in compaq cxx 6.2, where is_open() returns 'true' for
160 // a fresh new filebuf. (JMarc)
161 if (fb_.is_open() || istream::off_type(is.tellg()) > 0)
162 lyxerr[Debug::LYXLEX] << "Error in LyXLex::setFile: "
163 "file or stream already set." << endl;
164 fb_.open(filename.c_str(), ios::in);
168 return fb_.is_open() && is.good();
173 void LyXLex::Pimpl::setStream(istream & i)
175 if (fb_.is_open() || istream::off_type(is.tellg()) > 0)
176 lyxerr[Debug::LYXLEX] << "Error in LyXLex::setStream: "
177 "file or stream already set." << endl;
183 void LyXLex::Pimpl::setCommentChar(char c)
189 bool LyXLex::Pimpl::next(bool esc /* = false */)
191 if (!pushTok.empty()) {
192 // There can have been a whole line pushed so
193 // we extract the first word and leaves the rest
195 if (pushTok.find(' ') != string::npos && pushTok[0] == '\\') {
197 pushTok = split(pushTok, tmp, ' ');
198 buff.assign(tmp.begin(), tmp.end());
201 buff.assign(pushTok.begin(), pushTok.end());
207 unsigned char c = 0; // getc() returns an int
210 while (is && !status) {
213 if (c == commentChar) {
214 // Read rest of line (fast :-)
216 // That is not fast... (Lgb)
220 lyxerr[Debug::LYXLEX] << "Comment read: `" << c
221 << dummy << '\'' << endl;
223 // unfortunately ignore is buggy (Lgb)
224 is.ignore(100, '\n');
238 } while (c != '\"' && c != '\n' && is);
241 printError("Missing quote");
252 continue; /* Skip ','s */
254 // using relational operators with chars other
255 // than == and != is not safe. And if it is done
256 // the type _have_ to be unsigned. It usually a
257 // lot better to use the functions from cctype
265 } while (c > ' ' && c != ',' && is);
270 if (c == '\r' && is) {
271 // The Windows support has lead to the
272 // possibility of "\r\n" at the end of
273 // a line. This will stop LyX choking
274 // when it expected to find a '\n'
283 if (status) return true;
285 status = is.eof() ? LEX_FEOF: LEX_UNDEF;
289 unsigned char c = 0; // getc() returns an int
293 while (is && !status) {
298 if (c == ',') continue;
306 // escape the next char
313 } while (c > ' ' && c != ',' && is);
319 if (c == commentChar) {
320 // Read rest of line (fast :-)
322 // That is still not fast... (Lgb)
326 lyxerr[Debug::LYXLEX] << "Comment read: `" << c
327 << dummy << '\'' << endl;
329 // but ignore is also still buggy (Lgb)
330 // This is fast (Lgb)
331 is.ignore(100, '\n');
341 bool escaped = false;
346 if (c == '\r') continue;
348 // escape the next char
351 if (c == '\"' || c == '\\')
354 buff.push_back('\\');
358 if (!escaped && c == '\"') break;
359 } while (c != '\n' && is);
362 printError("Missing quote");
377 // escape the next char
385 } while (c > ' ' && c != ',' && is);
394 if (status) return true;
396 status = is.eof() ? LEX_FEOF : LEX_UNDEF;
403 int LyXLex::Pimpl::search_kw(char const * const tag) const
405 keyword_item search_tag = { tag, 0 };
407 lower_bound(table, table + no_items,
408 search_tag, compare_tags());
409 // use the compare_ascii_no_case instead of compare_no_case,
410 // because in turkish, 'i' is not the lowercase version of 'I',
411 // and thus turkish locale breaks parsing of tags.
412 if (res != table + no_items
413 && !compare_ascii_no_case(res->tag, tag))
419 int LyXLex::Pimpl::lex()
421 //NOTE: possible bug.
422 if (next() && status == LEX_TOKEN) {
423 return search_kw(getString().c_str());
429 bool LyXLex::Pimpl::eatLine()
433 unsigned char c = '\0';
435 while (is && c != '\n') {
438 //lyxerr[Debug::LYXLEX] << "LyXLex::EatLine read char: `"
439 // << c << '\'' << endl;
455 bool LyXLex::Pimpl::nextToken()
457 if (!pushTok.empty()) {
458 // There can have been a whole line pushed so
459 // we extract the first word and leaves the rest
461 if (pushTok.find(' ') != string::npos && pushTok[0] == '\\') {
463 pushTok = split(pushTok, tmp, ' ');
464 buff.assign(tmp.begin(), tmp.end());
467 buff.assign(pushTok.begin(), pushTok.end());
474 while (is && !status) {
479 if (c >= ' ' && is) {
482 if (c == '\\') { // first char == '\\'
487 } while (c > ' ' && c != '\\' && is);
493 } while (c >= ' ' && c != '\\' && is);
496 if (c == '\\') is.putback(c); // put it back
504 if (status) return true;
506 status = is.eof() ? LEX_FEOF: LEX_UNDEF;
512 void LyXLex::Pimpl::pushToken(string const & pt)