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"
23 using lyx::support::compare_ascii_no_case;
24 using lyx::support::getExtFromContents;
25 using lyx::support::MakeDisplayPath;
26 using lyx::support::split;
27 using lyx::support::subst;
31 using std::lower_bound;
40 // used by lower_bound, sort and sorted
42 int operator()(keyword_item const & a, keyword_item const & b) const {
43 // we use the ascii version, because in turkish, 'i'
44 // is not the lowercase version of 'I', and thus
45 // turkish locale breaks parsing of tags.
46 return compare_ascii_no_case(a.tag, b.tag) < 0;
49 // } // end of anon namespace
52 LyXLex::Pimpl::Pimpl(keyword_item * tab, int num)
53 : is(&fb_), table(tab), no_items(num),
54 status(0), lineno(0), commentChar('#')
60 string const LyXLex::Pimpl::getString() const
62 return string(buff.begin(), buff.end());
66 void LyXLex::Pimpl::printError(string const & message) const
68 string const tmpmsg = subst(message, "$$Token", getString());
69 lyxerr << "LyX: " << tmpmsg << " [around line " << lineno
70 << " of file " << MakeDisplayPath(name) << ']' << endl;
74 void LyXLex::Pimpl::printTable(ostream & os)
76 os << "\nNumber of tags: " << no_items << endl;
77 for (int i= 0; i < no_items; ++i)
79 << "]: tag: `" << table[i].tag
80 << "' code:" << table[i].code << '\n';
85 void LyXLex::Pimpl::verifyTable()
87 // Check if the table is sorted and if not, sort it.
89 && !lyx::sorted(table, table + no_items, compare_tags())) {
90 lyxerr << "The table passed to LyXLex is not sorted!\n"
91 << "Tell the developers to fix it!" << endl;
92 // We sort it anyway to avoid problems.
93 lyxerr << "\nUnsorted:" << endl;
96 sort(table, table + no_items, compare_tags());
97 lyxerr << "\nSorted:" << endl;
103 void LyXLex::Pimpl::pushTable(keyword_item * tab, int num)
105 pushed_table tmppu(table, no_items);
115 void LyXLex::Pimpl::popTable()
117 if (pushed.empty()) {
118 lyxerr << "LyXLex error: nothing to pop!" << endl;
122 pushed_table tmp = pushed.top();
124 table = tmp.table_elem;
125 no_items = tmp.table_siz;
129 bool LyXLex::Pimpl::setFile(string const & filename)
132 // Check the format of the file.
133 string const format = getExtFromContents(filename);
135 if (format == "gzip" || format == "zip" || format == "compress") {
136 lyxerr[Debug::LYXLEX] << "lyxlex: compressed" << endl;
138 // The check only outputs a debug message, because it triggers
139 // a bug in compaq cxx 6.2, where is_open() returns 'true' for
140 // a fresh new filebuf. (JMarc)
141 if (gz_.is_open() || istream::off_type(is.tellg()) > -1)
142 lyxerr[Debug::LYXLEX] << "Error in LyXLex::setFile: "
143 "file or stream already set." << endl;
144 gz_.open(filename.c_str(), ios::in);
148 return gz_.is_open() && is.good();
150 lyxerr[Debug::LYXLEX] << "lyxlex: UNcompressed" << endl;
152 // The check only outputs a debug message, because it triggers
153 // a bug in compaq cxx 6.2, where is_open() returns 'true' for
154 // a fresh new filebuf. (JMarc)
155 if (fb_.is_open() || istream::off_type(is.tellg()) > 0)
156 lyxerr[Debug::LYXLEX] << "Error in LyXLex::setFile: "
157 "file or stream already set." << endl;
158 fb_.open(filename.c_str(), ios::in);
162 return fb_.is_open() && is.good();
167 void LyXLex::Pimpl::setStream(istream & i)
169 if (fb_.is_open() || istream::off_type(is.tellg()) > 0)
170 lyxerr[Debug::LYXLEX] << "Error in LyXLex::setStream: "
171 "file or stream already set." << endl;
177 void LyXLex::Pimpl::setCommentChar(char c)
183 bool LyXLex::Pimpl::next(bool esc /* = false */)
185 if (!pushTok.empty()) {
186 // There can have been a whole line pushed so
187 // we extract the first word and leaves the rest
189 if (pushTok.find(' ') != string::npos && pushTok[0] == '\\') {
191 pushTok = split(pushTok, tmp, ' ');
192 buff.assign(tmp.begin(), tmp.end());
195 buff.assign(pushTok.begin(), pushTok.end());
201 unsigned char c = 0; // getc() returns an int
204 while (is && !status) {
207 if (c == commentChar) {
208 // Read rest of line (fast :-)
210 // That is not fast... (Lgb)
214 lyxerr[Debug::LYXLEX] << "Comment read: `" << c
215 << dummy << '\'' << endl;
217 // unfortunately ignore is buggy (Lgb)
218 is.ignore(100, '\n');
232 } while (c != '\"' && c != '\n' && is);
235 printError("Missing quote");
246 continue; /* Skip ','s */
248 // using relational operators with chars other
249 // than == and != is not safe. And if it is done
250 // the type _have_ to be unsigned. It usually a
251 // lot better to use the functions from cctype
259 } while (c > ' ' && c != ',' && is);
264 if (c == '\r' && is) {
265 // The Windows support has lead to the
266 // possibility of "\r\n" at the end of
267 // a line. This will stop LyX choking
268 // when it expected to find a '\n'
277 if (status) return true;
279 status = is.eof() ? LEX_FEOF: LEX_UNDEF;
283 unsigned char c = 0; // getc() returns an int
287 while (is && !status) {
292 if (c == ',') continue;
300 // escape the next char
307 } while (c > ' ' && c != ',' && is);
313 if (c == commentChar) {
314 // Read rest of line (fast :-)
316 // That is still not fast... (Lgb)
320 lyxerr[Debug::LYXLEX] << "Comment read: `" << c
321 << dummy << '\'' << endl;
323 // but ignore is also still buggy (Lgb)
324 // This is fast (Lgb)
325 is.ignore(100, '\n');
335 bool escaped = false;
340 if (c == '\r') continue;
342 // escape the next char
345 if (c == '\"' || c == '\\')
348 buff.push_back('\\');
352 if (!escaped && c == '\"') break;
353 } while (c != '\n' && is);
356 printError("Missing quote");
371 // escape the next char
379 } while (c > ' ' && c != ',' && is);
388 if (status) return true;
390 status = is.eof() ? LEX_FEOF : LEX_UNDEF;
397 int LyXLex::Pimpl::search_kw(char const * const tag) const
399 keyword_item search_tag = { tag, 0 };
401 lower_bound(table, table + no_items,
402 search_tag, compare_tags());
403 // use the compare_ascii_no_case instead of compare_no_case,
404 // because in turkish, 'i' is not the lowercase version of 'I',
405 // and thus turkish locale breaks parsing of tags.
406 if (res != table + no_items
407 && !compare_ascii_no_case(res->tag, tag))
413 int LyXLex::Pimpl::lex()
415 //NOTE: possible bug.
416 if (next() && status == LEX_TOKEN) {
417 return search_kw(getString().c_str());
423 bool LyXLex::Pimpl::eatLine()
427 unsigned char c = '\0';
429 while (is && c != '\n') {
432 //lyxerr[Debug::LYXLEX] << "LyXLex::EatLine read char: `"
433 // << c << '\'' << endl;
449 bool LyXLex::Pimpl::nextToken()
451 if (!pushTok.empty()) {
452 // There can have been a whole line pushed so
453 // we extract the first word and leaves the rest
455 if (pushTok.find(' ') != string::npos && pushTok[0] == '\\') {
457 pushTok = split(pushTok, tmp, ' ');
458 buff.assign(tmp.begin(), tmp.end());
461 buff.assign(pushTok.begin(), pushTok.end());
468 while (is && !status) {
473 if (c >= ' ' && is) {
476 if (c == '\\') { // first char == '\\'
481 } while (c > ' ' && c != '\\' && is);
487 } while (c >= ' ' && c != '\\' && is);
490 if (c == '\\') is.putback(c); // put it back
498 if (status) return true;
500 status = is.eof() ? LEX_FEOF: LEX_UNDEF;
506 void LyXLex::Pimpl::pushToken(string const & pt)