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 && !lyx::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 // There can have been a whole line pushed so
143 // we extract the first word and leaves the rest
145 if (pushTok.find(' ') != string::npos) {
147 pushTok = split(pushTok, tmp, ' ');
148 tmp.copy(buff, string::npos);
149 buff[tmp.length()] = '\0';
152 pushTok.copy(buff, string::npos);
153 buff[pushTok.length()] = '\0';
159 unsigned char c = 0; // getc() returns an int
162 while (is && !status) {
165 if (c == commentChar) {
166 // Read rest of line (fast :-)
167 // That is not fast... (Lgb)
169 is.getline(buff, sizeof(buff));
170 lyxerr[Debug::LYXLEX] << "Comment read: `" << c
171 << buff << "'" << endl;
173 // unfortunately is ignore buggy (Lgb)
174 is.ignore(100, '\n');
187 } while (c != '\"' && c != '\n' && is &&
188 i != (LEX_MAX_BUFF - 2));
190 if (i == (LEX_MAX_BUFF - 2)) {
191 printError("Line too long");
192 c = '\"'; // Pretend we got a "
197 printError("Missing quote");
208 continue; /* Skip ','s */
210 // using relational operators with chars other
211 // than == and != is not safe. And if it is done
212 // the type _have_ to be unsigned. It usually a
213 // lot better to use the functions from cctype
220 } while (c > ' ' && c != ',' && is
221 && (i != LEX_MAX_BUFF - 1) );
222 if (i == LEX_MAX_BUFF - 1) {
223 printError("Line too long");
229 if (c == '\r' && is) {
230 // The Windows support has lead to the
231 // possibility of "\r\n" at the end of
232 // a line. This will stop LyX choking
233 // when it expected to find a '\n'
242 if (status) return true;
244 status = is.eof() ? LEX_FEOF: LEX_UNDEF;
248 unsigned char c = 0; // getc() returns an int
252 while (is && !status) {
257 if (c == ',') continue;
264 // escape the next char
271 } while (c > ' ' && c != ',' && is
272 && (i != LEX_MAX_BUFF - 1) );
273 if (i == LEX_MAX_BUFF - 1) {
274 printError("Line too long");
281 if (c == commentChar) {
282 // Read rest of line (fast :-)
283 // That is still not fast... (Lgb)
285 is.getline(buff, sizeof(buff));
286 lyxerr[Debug::LYXLEX] << "Comment read: `" << c
287 << buff << "'" << endl;
289 // but ignore is also still buggy (Lgb)
290 // This is fast (Lgb)
291 is.ignore(100, '\n');
300 bool escaped = false;
305 if (c == '\r') continue;
307 // escape the next char
314 if (!escaped && c == '\"') break;
315 } while (c != '\n' && is &&
316 i != (LEX_MAX_BUFF - 2));
318 if (i == (LEX_MAX_BUFF - 2)) {
319 printError("Line too long");
320 c = '\"'; // Pretend we got a "
325 printError("Missing quote");
339 // escape the next char
347 } while (c > ' ' && c != ',' && is
348 && (i != LEX_MAX_BUFF-1) );
349 if (i == LEX_MAX_BUFF-1) {
350 printError("Line too long");
360 if (status) return true;
362 status = is.eof() ? LEX_FEOF : LEX_UNDEF;
369 int LyXLex::Pimpl::search_kw(char const * const tag) const
371 keyword_item search_tag = { tag, 0 };
373 lower_bound(table, table + no_items,
374 search_tag, compare_tags());
375 if (res != table + no_items
376 && !compare_no_case(res->tag, tag))
382 int LyXLex::Pimpl::lex()
384 //NOTE: possible bug.
385 if (next() && status == LEX_TOKEN)
386 return search_kw(buff);
392 bool LyXLex::Pimpl::EatLine()
395 unsigned char c = '\0';
397 while(is && c != '\n' && i != (LEX_MAX_BUFF - 1)) {
400 lyxerr[Debug::LYXLEX] << "LyXLex::EatLine read char: `"
405 if (i == (LEX_MAX_BUFF - 1) && c != '\n') {
406 printError("Line too long");
407 c = '\n'; // Pretend we had an end of line
408 --lineno; // but don't increase line counter (netto effect)
409 ++i; // and preserve last character read.
413 buff[--i] = '\0'; // i can never be 0 here, so no danger
423 bool LyXLex::Pimpl::nextToken()
425 if (!pushTok.empty()) {
426 // There can have been a whole line pushed so
427 // we extract the first word and leaves the rest
429 if (pushTok.find(' ') != string::npos) {
431 pushTok = split(pushTok, tmp, ' ');
432 tmp.copy(buff, string::npos);
433 buff[tmp.length()] = '\0';
436 pushTok.copy(buff, string::npos);
437 buff[pushTok.length()] = '\0';
444 while (is && !status) {
449 if (c >= ' ' && is) {
451 if (c == '\\') { // first char == '\\'
456 } while (c > ' ' && c != '\\' && is
457 && i != (LEX_MAX_BUFF-1));
463 } while (c >= ' ' && c != '\\' && is
464 && i != (LEX_MAX_BUFF-1));
467 if (i == (LEX_MAX_BUFF - 1)) {
468 printError("Line too long");
471 if (c == '\\') is.putback(c); // put it back
480 if (status) return true;
482 status = is.eof() ? LEX_FEOF: LEX_UNDEF;
488 void LyXLex::Pimpl::pushToken(string const & pt)