8 #include "lyxlex_pimpl.h"
9 #include "support/lyxalgo.h"
10 #include "support/filetools.h"
11 #include "support/lstrings.h"
19 using std::lower_bound;
23 // used by lower_bound, sort and sorted
25 int operator()(keyword_item const & a, keyword_item const & b) const {
26 // we use the ascii version, because in turkish, 'i'
27 // is not the lowercase version of 'I', and thus
28 // turkish locale breaks parsing of tags.
29 return compare_ascii_no_case(a.tag, b.tag) < 0;
32 // } // end of anon namespace
35 LyXLex::Pimpl::Pimpl(keyword_item * tab, int num)
36 : is(&fb__), table(tab), no_items(num),
37 status(0), lineno(0), commentChar('#')
43 string const LyXLex::Pimpl::getString() const
49 void LyXLex::Pimpl::printError(string const & message) const
51 string const tmpmsg = subst(message, "$$Token", getString());
52 lyxerr << "LyX: " << tmpmsg << " [around line " << lineno
53 << " of file " << MakeDisplayPath(name) << ']' << endl;
57 void LyXLex::Pimpl::printTable(ostream & os)
59 os << "\nNumber of tags: " << no_items << '\n';
60 for (int i= 0; i < no_items; ++i)
62 << "]: tag: `" << table[i].tag
63 << "' code:" << table[i].code << '\n';
68 void LyXLex::Pimpl::verifyTable()
70 // Check if the table is sorted and if not, sort it.
72 && !lyx::sorted(table, table + no_items, compare_tags())) {
73 lyxerr << "The table passed to LyXLex is not sorted!\n"
74 << "Tell the developers to fix it!" << endl;
75 // We sort it anyway to avoid problems.
76 lyxerr << "\nUnsorted:\n";
79 sort(table, table + no_items, compare_tags());
80 lyxerr << "\nSorted:\n";
86 void LyXLex::Pimpl::pushTable(keyword_item * tab, int num)
88 pushed_table tmppu(table, no_items);
98 void LyXLex::Pimpl::popTable()
100 if (pushed.empty()) {
101 lyxerr << "LyXLex error: nothing to pop!" << endl;
105 pushed_table tmp = pushed.top();
107 table = tmp.table_elem;
108 no_items = tmp.table_siz;
112 bool LyXLex::Pimpl::setFile(string const & filename)
114 // The check only outputs a debug message, because it triggers
115 // a bug in compaq cxx 6.2, where is_open() returns 'true' for a
116 // fresh new filebuf. (JMarc)
117 if (fb__.is_open() || is.tellg() > 0)
118 lyxerr[Debug::LYXLEX] << "Error in LyXLex::setFile: "
119 "file or stream already set." << endl;
120 fb__.open(filename.c_str(), ios::in);
124 return fb__.is_open() && is.good();
128 void LyXLex::Pimpl::setStream(istream & i)
130 if (fb__.is_open() || is.tellg() > 0)
131 lyxerr[Debug::LYXLEX] << "Error in LyXLex::setStream: "
132 "file or stream already set." << endl;
137 void LyXLex::Pimpl::setCommentChar(char c)
143 bool LyXLex::Pimpl::next(bool esc /* = false */)
145 if (!pushTok.empty()) {
146 // There can have been a whole line pushed so
147 // we extract the first word and leaves the rest
149 if (pushTok.find(' ') != string::npos) {
151 pushTok = split(pushTok, tmp, ' ');
152 tmp.copy(buff, string::npos);
153 buff[tmp.length()] = '\0';
156 pushTok.copy(buff, string::npos);
157 buff[pushTok.length()] = '\0';
163 unsigned char c = 0; // getc() returns an int
166 while (is && !status) {
169 if (c == commentChar) {
170 // Read rest of line (fast :-)
171 // That is not fast... (Lgb)
173 is.getline(buff, sizeof(buff));
174 lyxerr[Debug::LYXLEX] << "Comment read: `" << c
175 << buff << "'" << endl;
177 // unfortunately ignore is buggy (Lgb)
178 is.ignore(100, '\n');
191 } while (c != '\"' && c != '\n' && is &&
192 i != (LEX_MAX_BUFF - 2));
194 if (i == (LEX_MAX_BUFF - 2)) {
195 printError("Line too long");
196 c = '\"'; // Pretend we got a "
201 printError("Missing quote");
212 continue; /* Skip ','s */
214 // using relational operators with chars other
215 // than == and != is not safe. And if it is done
216 // the type _have_ to be unsigned. It usually a
217 // lot better to use the functions from cctype
224 } while (c > ' ' && c != ',' && is
225 && (i != LEX_MAX_BUFF - 1) );
226 if (i == LEX_MAX_BUFF - 1) {
227 printError("Line too long");
233 if (c == '\r' && is) {
234 // The Windows support has lead to the
235 // possibility of "\r\n" at the end of
236 // a line. This will stop LyX choking
237 // when it expected to find a '\n'
246 if (status) return true;
248 status = is.eof() ? LEX_FEOF: LEX_UNDEF;
252 unsigned char c = 0; // getc() returns an int
256 while (is && !status) {
261 if (c == ',') continue;
268 // escape the next char
275 } while (c > ' ' && c != ',' && is
276 && (i != LEX_MAX_BUFF - 1) );
277 if (i == LEX_MAX_BUFF - 1) {
278 printError("Line too long");
285 if (c == commentChar) {
286 // Read rest of line (fast :-)
287 // That is still not fast... (Lgb)
289 is.getline(buff, sizeof(buff));
290 lyxerr[Debug::LYXLEX] << "Comment read: `" << c
291 << buff << "'" << endl;
293 // but ignore is also still buggy (Lgb)
294 // This is fast (Lgb)
295 is.ignore(100, '\n');
304 bool escaped = false;
309 if (c == '\r') continue;
311 // escape the next char
314 if (c == '\"' || c == '\\')
321 if (!escaped && c == '\"') break;
322 } while (c != '\n' && is &&
323 i != (LEX_MAX_BUFF - 2));
325 if (i == (LEX_MAX_BUFF - 2)) {
326 printError("Line too long");
327 c = '\"'; // Pretend we got a "
332 printError("Missing quote");
346 // escape the next char
354 } while (c > ' ' && c != ',' && is
355 && (i != LEX_MAX_BUFF-1) );
356 if (i == LEX_MAX_BUFF-1) {
357 printError("Line too long");
367 if (status) return true;
369 status = is.eof() ? LEX_FEOF : LEX_UNDEF;
376 int LyXLex::Pimpl::search_kw(char const * const tag) const
378 keyword_item search_tag = { tag, 0 };
380 lower_bound(table, table + no_items,
381 search_tag, compare_tags());
382 if (res != table + no_items
383 && !compare_no_case(res->tag, tag))
389 int LyXLex::Pimpl::lex()
391 //NOTE: possible bug.
392 if (next() && status == LEX_TOKEN)
393 return search_kw(buff);
399 bool LyXLex::Pimpl::eatLine()
402 unsigned char c = '\0';
404 while(is && c != '\n' && i != (LEX_MAX_BUFF - 1)) {
407 //lyxerr[Debug::LYXLEX] << "LyXLex::EatLine read char: `"
408 // << c << "'" << endl;
412 if (i == (LEX_MAX_BUFF - 1) && c != '\n') {
413 printError("Line too long");
414 c = '\n'; // Pretend we had an end of line
415 --lineno; // but don't increase line counter (netto effect)
416 ++i; // and preserve last character read.
420 buff[--i] = '\0'; // i can never be 0 here, so no danger
430 bool LyXLex::Pimpl::nextToken()
432 if (!pushTok.empty()) {
433 // There can have been a whole line pushed so
434 // we extract the first word and leaves the rest
436 if (pushTok.find(' ') != string::npos) {
438 pushTok = split(pushTok, tmp, ' ');
439 tmp.copy(buff, string::npos);
440 buff[tmp.length()] = '\0';
443 pushTok.copy(buff, string::npos);
444 buff[pushTok.length()] = '\0';
451 while (is && !status) {
456 if (c >= ' ' && is) {
458 if (c == '\\') { // first char == '\\'
463 } while (c > ' ' && c != '\\' && is
464 && i != (LEX_MAX_BUFF-1));
470 } while (c >= ' ' && c != '\\' && is
471 && i != (LEX_MAX_BUFF-1));
474 if (i == (LEX_MAX_BUFF - 1)) {
475 printError("Line too long");
478 if (c == '\\') is.putback(c); // put it back
487 if (status) return true;
489 status = is.eof() ? LEX_FEOF: LEX_UNDEF;
495 void LyXLex::Pimpl::pushToken(string const & pt)