8 #include "lyxlex_pimpl.h"
9 #include "support/lyxalgo.h"
10 #include "support/filetools.h"
18 using std::lower_bound;
22 // used by lower_bound, sort and sorted
24 int operator()(keyword_item const & a, keyword_item const & b) const {
25 return compare_no_case(a.tag, b.tag) < 0;
28 // } // end of anon namespace
31 LyXLex::Pimpl::Pimpl(keyword_item * tab, int num)
32 : is(&fb__), table(tab), no_items(num),
33 status(0), lineno(0), commentChar('#')
39 string const LyXLex::Pimpl::GetString() const
45 void LyXLex::Pimpl::printError(string const & message) const
47 string tmpmsg = subst(message, "$$Token", GetString());
48 lyxerr << "LyX: " << tmpmsg << " [around line " << lineno
49 << " of file " << MakeDisplayPath(name) << ']' << endl;
53 void LyXLex::Pimpl::printTable(ostream & os)
55 os << "\nNumber of tags: " << no_items << '\n';
56 for (int i= 0; i < no_items; ++i)
58 << "]: tag: `" << table[i].tag
59 << "' code:" << table[i].code << '\n';
64 void LyXLex::Pimpl::verifyTable()
66 // Check if the table is sorted and if not, sort it.
68 && !sorted(table, table + no_items, compare_tags())) {
69 lyxerr << "The table passed to LyXLex is not sorted!\n"
70 << "Tell the developers to fix it!" << endl;
71 // We sort it anyway to avoid problems.
72 lyxerr << "\nUnsorted:\n";
75 sort(table, table + no_items, compare_tags());
76 lyxerr << "\nSorted:\n";
82 void LyXLex::Pimpl::pushTable(keyword_item * tab, int num)
84 pushed_table tmppu(table, no_items);
94 void LyXLex::Pimpl::popTable()
97 lyxerr << "LyXLex error: nothing to pop!" << endl;
101 pushed_table tmp = pushed.top();
103 table = tmp.table_elem;
104 no_items = tmp.table_siz;
108 bool LyXLex::Pimpl::setFile(string const & filename)
110 // The check only outputs a debug message, because it triggers
111 // a bug in compaq cxx 6.2, where is_open() returns 'true' for a
112 // fresh new filebuf. (JMarc)
113 if (fb__.is_open() || is.tellg() > 0)
114 lyxerr[Debug::LYXLEX] << "Error in LyXLex::setFile: "
115 "file or stream already set." << endl;
116 fb__.open(filename.c_str(), ios::in);
120 return fb__.is_open() && is.good();
124 void LyXLex::Pimpl::setStream(istream & i)
126 if (fb__.is_open() || is.tellg() > 0)
127 lyxerr[Debug::LYXLEX] << "Error in LyXLex::setStream: "
128 "file or stream already set." << endl;
133 void LyXLex::Pimpl::setCommentChar(char c)
139 bool LyXLex::Pimpl::next(bool esc /* = false */)
141 if (!pushTok.empty()) {
142 pushTok.copy(buff, string::npos);
143 buff[pushTok.length()] = '\0';
148 unsigned char c = 0; // getc() returns an int
151 while (is && !status) {
154 if (c == commentChar) {
155 // Read rest of line (fast :-)
156 // That is not fast... (Lgb)
158 is.getline(buff, sizeof(buff));
159 lyxerr[Debug::LYXLEX] << "Comment read: `" << c
160 << buff << "'" << endl;
162 // unfortunately is ignore buggy (Lgb)
163 is.ignore(100, '\n');
176 } while (c != '\"' && c != '\n' && is &&
177 i != (LEX_MAX_BUFF - 2));
179 if (i == (LEX_MAX_BUFF - 2)) {
180 printError("Line too long");
181 c = '\"'; // Pretend we got a "
186 printError("Missing quote");
197 continue; /* Skip ','s */
199 // using relational operators with chars other
200 // than == and != is not safe. And if it is done
201 // the type _have_ to be unsigned. It usually a
202 // lot better to use the functions from cctype
209 } while (c > ' ' && c != ',' && is
210 && (i != LEX_MAX_BUFF - 1) );
211 if (i == LEX_MAX_BUFF - 1) {
212 printError("Line too long");
218 if (c == '\r' && is) {
219 // The Windows support has lead to the
220 // possibility of "\r\n" at the end of
221 // a line. This will stop LyX choking
222 // when it expected to find a '\n'
231 if (status) return true;
233 status = is.eof() ? LEX_FEOF: LEX_UNDEF;
237 unsigned char c = 0; // getc() returns an int
241 while (is && !status) {
246 if (c == ',') continue;
253 // escape the next char
260 } while (c > ' ' && c != ',' && is
261 && (i != LEX_MAX_BUFF - 1) );
262 if (i == LEX_MAX_BUFF - 1) {
263 printError("Line too long");
270 if (c == commentChar) {
271 // Read rest of line (fast :-)
272 // That is still not fast... (Lgb)
274 is.getline(buff, sizeof(buff));
275 lyxerr[Debug::LYXLEX] << "Comment read: `" << c
276 << buff << "'" << endl;
278 // but ignore is also still buggy (Lgb)
279 // This is fast (Lgb)
280 is.ignore(100, '\n');
289 bool escaped = false;
294 if (c == '\r') continue;
296 // escape the next char
303 if (!escaped && c == '\"') break;
304 } while (c != '\n' && is &&
305 i != (LEX_MAX_BUFF - 2));
307 if (i == (LEX_MAX_BUFF - 2)) {
308 printError("Line too long");
309 c = '\"'; // Pretend we got a "
314 printError("Missing quote");
328 // escape the next char
336 } while (c > ' ' && c != ',' && is
337 && (i != LEX_MAX_BUFF-1) );
338 if (i == LEX_MAX_BUFF-1) {
339 printError("Line too long");
349 if (status) return true;
351 status = is.eof() ? LEX_FEOF : LEX_UNDEF;
358 int LyXLex::Pimpl::search_kw(char const * const tag) const
360 keyword_item search_tag = { tag, 0 };
362 lower_bound(table, table + no_items,
363 search_tag, compare_tags());
364 if (res != table + no_items
365 && !compare_no_case(res->tag, tag))
371 int LyXLex::Pimpl::lex()
373 //NOTE: possible bug.
374 if (next() && status == LEX_TOKEN)
375 return search_kw(buff);
381 bool LyXLex::Pimpl::EatLine()
384 unsigned char c = '\0';
386 while(is && c != '\n' && i != (LEX_MAX_BUFF - 1)) {
389 lyxerr[Debug::LYXLEX] << "LyXLex::EatLine read char: `"
394 if (i == (LEX_MAX_BUFF - 1) && c != '\n') {
395 printError("Line too long");
396 c = '\n'; // Pretend we had an end of line
397 --lineno; // but don't increase line counter (netto effect)
398 ++i; // and preserve last character read.
402 buff[--i] = '\0'; // i can never be 0 here, so no danger
412 bool LyXLex::Pimpl::nextToken()
414 if (!pushTok.empty()) {
415 pushTok.copy(buff, string::npos);
416 buff[pushTok.length()] = '\0';
422 while (is && !status) {
427 if (c >= ' ' && is) {
429 if (c == '\\') { // first char == '\\'
434 } while (c > ' ' && c != '\\' && is
435 && i != (LEX_MAX_BUFF-1));
441 } while (c >= ' ' && c != '\\' && is
442 && i != (LEX_MAX_BUFF-1));
445 if (i == (LEX_MAX_BUFF - 1)) {
446 printError("Line too long");
449 if (c == '\\') is.putback(c); // put it back
458 if (status) return true;
460 status = is.eof() ? LEX_FEOF: LEX_UNDEF;
466 void LyXLex::Pimpl::pushToken(string const & pt)