8 #include "lyxlex_pimpl.h"
9 #include "support/lyxalgo.h"
10 #include "support/filetools.h"
12 #if 1 // to get NEW_INSETS
13 #include "lyxparagraph.h"
21 using std::lower_bound;
25 // used by lower_bound, sort and sorted
27 int operator()(keyword_item const & a, keyword_item const & b) const {
28 return compare_no_case(a.tag, b.tag) < 0;
31 // } // end of anon namespace
34 LyXLex::Pimpl::Pimpl(keyword_item * tab, int num)
35 : is(&fb__), table(tab), no_items(num),
36 status(0), lineno(0), commentChar('#')
42 string const LyXLex::Pimpl::GetString() const
48 void LyXLex::Pimpl::printError(string const & message) const
50 string tmpmsg = subst(message, "$$Token", GetString());
51 lyxerr << "LyX: " << tmpmsg << " [around line " << lineno
52 << " of file " << MakeDisplayPath(name) << ']' << endl;
56 void LyXLex::Pimpl::printTable(ostream & os)
58 os << "\nNumber of tags: " << no_items << '\n';
59 for (int i= 0; i < no_items; ++i)
61 << "]: tag: `" << table[i].tag
62 << "' code:" << table[i].code << '\n';
67 void LyXLex::Pimpl::verifyTable()
69 // Check if the table is sorted and if not, sort it.
71 && !lyx::sorted(table, table + no_items, compare_tags())) {
72 lyxerr << "The table passed to LyXLex is not sorted!\n"
73 << "Tell the developers to fix it!" << endl;
74 // We sort it anyway to avoid problems.
75 lyxerr << "\nUnsorted:\n";
78 sort(table, table + no_items, compare_tags());
79 lyxerr << "\nSorted:\n";
85 void LyXLex::Pimpl::pushTable(keyword_item * tab, int num)
87 pushed_table tmppu(table, no_items);
97 void LyXLex::Pimpl::popTable()
100 lyxerr << "LyXLex error: nothing to pop!" << endl;
104 pushed_table tmp = pushed.top();
106 table = tmp.table_elem;
107 no_items = tmp.table_siz;
111 bool LyXLex::Pimpl::setFile(string const & filename)
113 // The check only outputs a debug message, because it triggers
114 // a bug in compaq cxx 6.2, where is_open() returns 'true' for a
115 // fresh new filebuf. (JMarc)
116 if (fb__.is_open() || is.tellg() > 0)
117 lyxerr[Debug::LYXLEX] << "Error in LyXLex::setFile: "
118 "file or stream already set." << endl;
119 fb__.open(filename.c_str(), ios::in);
123 return fb__.is_open() && is.good();
127 void LyXLex::Pimpl::setStream(istream & i)
129 if (fb__.is_open() || is.tellg() > 0)
130 lyxerr[Debug::LYXLEX] << "Error in LyXLex::setStream: "
131 "file or stream already set." << endl;
136 void LyXLex::Pimpl::setCommentChar(char c)
142 bool LyXLex::Pimpl::next(bool esc /* = false */)
144 if (!pushTok.empty()) {
146 pushTok.copy(buff, string::npos);
147 buff[pushTok.length()] = '\0';
151 // There can have been a whole line pushed so
152 // we extract the first word and leaves the rest
154 if (pushTok.find(' ') != string::npos) {
156 pushTok = split(pushTok, tmp, ' ');
157 tmp.copy(buff, string::npos);
158 buff[tmp.length()] = '\0';
161 pushTok.copy(buff, string::npos);
162 buff[pushTok.length()] = '\0';
169 unsigned char c = 0; // getc() returns an int
172 while (is && !status) {
175 if (c == commentChar) {
176 // Read rest of line (fast :-)
177 // That is not fast... (Lgb)
179 is.getline(buff, sizeof(buff));
180 lyxerr[Debug::LYXLEX] << "Comment read: `" << c
181 << buff << "'" << endl;
183 // unfortunately is ignore buggy (Lgb)
184 is.ignore(100, '\n');
197 } while (c != '\"' && c != '\n' && is &&
198 i != (LEX_MAX_BUFF - 2));
200 if (i == (LEX_MAX_BUFF - 2)) {
201 printError("Line too long");
202 c = '\"'; // Pretend we got a "
207 printError("Missing quote");
218 continue; /* Skip ','s */
220 // using relational operators with chars other
221 // than == and != is not safe. And if it is done
222 // the type _have_ to be unsigned. It usually a
223 // lot better to use the functions from cctype
230 } while (c > ' ' && c != ',' && is
231 && (i != LEX_MAX_BUFF - 1) );
232 if (i == LEX_MAX_BUFF - 1) {
233 printError("Line too long");
239 if (c == '\r' && is) {
240 // The Windows support has lead to the
241 // possibility of "\r\n" at the end of
242 // a line. This will stop LyX choking
243 // when it expected to find a '\n'
252 if (status) return true;
254 status = is.eof() ? LEX_FEOF: LEX_UNDEF;
258 unsigned char c = 0; // getc() returns an int
262 while (is && !status) {
267 if (c == ',') continue;
274 // escape the next char
281 } while (c > ' ' && c != ',' && is
282 && (i != LEX_MAX_BUFF - 1) );
283 if (i == LEX_MAX_BUFF - 1) {
284 printError("Line too long");
291 if (c == commentChar) {
292 // Read rest of line (fast :-)
293 // That is still not fast... (Lgb)
295 is.getline(buff, sizeof(buff));
296 lyxerr[Debug::LYXLEX] << "Comment read: `" << c
297 << buff << "'" << endl;
299 // but ignore is also still buggy (Lgb)
300 // This is fast (Lgb)
301 is.ignore(100, '\n');
310 bool escaped = false;
315 if (c == '\r') continue;
317 // escape the next char
324 if (!escaped && c == '\"') break;
325 } while (c != '\n' && is &&
326 i != (LEX_MAX_BUFF - 2));
328 if (i == (LEX_MAX_BUFF - 2)) {
329 printError("Line too long");
330 c = '\"'; // Pretend we got a "
335 printError("Missing quote");
349 // escape the next char
357 } while (c > ' ' && c != ',' && is
358 && (i != LEX_MAX_BUFF-1) );
359 if (i == LEX_MAX_BUFF-1) {
360 printError("Line too long");
370 if (status) return true;
372 status = is.eof() ? LEX_FEOF : LEX_UNDEF;
379 int LyXLex::Pimpl::search_kw(char const * const tag) const
381 keyword_item search_tag = { tag, 0 };
383 lower_bound(table, table + no_items,
384 search_tag, compare_tags());
385 if (res != table + no_items
386 && !compare_no_case(res->tag, tag))
392 int LyXLex::Pimpl::lex()
394 //NOTE: possible bug.
395 if (next() && status == LEX_TOKEN)
396 return search_kw(buff);
402 bool LyXLex::Pimpl::EatLine()
405 // This is not handling the pushed token
406 if (!pushTok.empty()) {
407 pushTok.copy(buff, string::npos);
408 buff[pushTok.length()] = '\0';
414 unsigned char c = '\0';
416 while(is && c != '\n' && i != (LEX_MAX_BUFF - 1)) {
419 lyxerr[Debug::LYXLEX] << "LyXLex::EatLine read char: `"
424 if (i == (LEX_MAX_BUFF - 1) && c != '\n') {
425 printError("Line too long");
426 c = '\n'; // Pretend we had an end of line
427 --lineno; // but don't increase line counter (netto effect)
428 ++i; // and preserve last character read.
432 buff[--i] = '\0'; // i can never be 0 here, so no danger
442 bool LyXLex::Pimpl::nextToken()
444 if (!pushTok.empty()) {
446 pushTok.copy(buff, string::npos);
447 buff[pushTok.length()] = '\0';
451 // There can have been a whole line pushed so
452 // we extract the first word and leaves the rest
454 if (pushTok.find(' ') != string::npos) {
456 pushTok = split(pushTok, tmp, ' ');
457 tmp.copy(buff, string::npos);
458 buff[tmp.length()] = '\0';
461 pushTok.copy(buff, string::npos);
462 buff[pushTok.length()] = '\0';
470 while (is && !status) {
475 if (c >= ' ' && is) {
477 if (c == '\\') { // first char == '\\'
482 } while (c > ' ' && c != '\\' && is
483 && i != (LEX_MAX_BUFF-1));
489 } while (c >= ' ' && c != '\\' && is
490 && i != (LEX_MAX_BUFF-1));
493 if (i == (LEX_MAX_BUFF - 1)) {
494 printError("Line too long");
497 if (c == '\\') is.putback(c); // put it back
506 if (status) return true;
508 status = is.eof() ? LEX_FEOF: LEX_UNDEF;
514 void LyXLex::Pimpl::pushToken(string const & pt)