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), pushed(0), lineno(0)
35 if (table && !sorted(table, table + no_items, compare_tags())) {
36 lyxerr << "The table passed to LyXLex is not sorted!!\n"
37 << "Tell the developers to fix it!" << endl;
38 // We sort it anyway to avoid problems.
39 lyxerr << "\nUnsorted:\n";
42 sort(table, table + no_items,
44 lyxerr << "\nSorted:\n";
50 string LyXLex::Pimpl::GetString() const
56 void LyXLex::Pimpl::printError(string const & message) const
58 string tmpmsg = subst(message, "$$Token", GetString());
59 lyxerr << "LyX: " << tmpmsg << " [around line " << lineno
60 << " of file " << MakeDisplayPath(name) << ']' << endl;
64 void LyXLex::Pimpl::printTable(ostream & os)
66 os << "\nNumber of tags: " << no_items << '\n';
67 for(int i= 0; i < no_items; ++i)
69 << "]: tag: `" << table[i].tag
70 << "' code:" << table[i].code << '\n';
75 void LyXLex::Pimpl::pushTable(keyword_item * tab, int num)
77 pushed_table * tmppu = new pushed_table;
79 tmppu->table_elem = table;
80 tmppu->table_siz = no_items;
84 // Check if the table is sorted and if not, sort it.
86 && !sorted(table, table + no_items, compare_tags())) {
87 lyxerr << "The table passed to LyXLex is not sorted!!\n"
88 << "Tell the developers to fix it!" << endl;
89 // We sort it anyway to avoid problems.
90 lyxerr << "\nUnsorted:\n";
93 sort(table, table + no_items, compare_tags());
94 lyxerr << "\nSorted:\n";
100 void LyXLex::Pimpl::popTable()
103 lyxerr << "LyXLex error: nothing to pop!" << endl;
107 table = tmp->table_elem;
108 no_items = tmp->table_siz;
115 bool LyXLex::Pimpl::setFile(string const & filename)
117 //if (fb__.is_open())
118 // lyxerr << "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.rdbuf()->in_avail())
131 lyxerr << "Error in LyXLex::setStream: "
132 "file or stream already set." << endl;
138 bool LyXLex::Pimpl::next(bool esc /* = false */)
140 if (!pushTok.empty()) {
141 pushTok.copy(buff, string::npos);
142 buff[pushTok.length()] = '\0';
147 unsigned char c = 0; // getc() returns an int
150 while (is && !status) {
154 // Read rest of line (fast :-)
155 is.getline(buff, sizeof(buff));
156 lyxerr[Debug::LYXLEX] << "Comment read: `" << c
157 << buff << "'" << endl;
169 } while (c != '\"' && c != '\n' && is &&
170 i != (LEX_MAX_BUFF - 2));
172 if (i == (LEX_MAX_BUFF - 2)) {
173 printError("Line too long");
174 c = '\"'; // Pretend we got a "
179 printError("Missing quote");
190 continue; /* Skip ','s */
192 // using relational operators with chars other
193 // than == and != is not safe. And if it is done
194 // the type _have_ to be unsigned. It usually a
195 // lot better to use the functions from cctype
202 } while (c > ' ' && c != ',' && is
203 && (i != LEX_MAX_BUFF - 1) );
204 if (i == LEX_MAX_BUFF - 1) {
205 printError("Line too long");
211 if (c == '\r' && is) {
212 // The Windows support has lead to the
213 // possibility of "\r\n" at the end of
214 // a line. This will stop LyX choking
215 // when it expected to find a '\n'
224 if (status) return true;
226 status = is.eof() ? LEX_FEOF: LEX_UNDEF;
230 unsigned char c = 0; // getc() returns an int
234 while (is && !status) {
239 if (c == ',') continue;
246 // escape the next char
253 } while (c > ' ' && c != ',' && is
254 && (i != LEX_MAX_BUFF - 1) );
255 if (i == LEX_MAX_BUFF - 1) {
256 printError("Line too long");
264 // Read rest of line (fast :-)
265 is.getline(buff, sizeof(buff));
266 lyxerr[Debug::LYXLEX] << "Comment read: `" << c
267 << buff << "'" << endl;
275 bool escaped = false;
280 if (c == '\r') continue;
282 // escape the next char
289 if (!escaped && c == '\"') break;
290 } while (c != '\n' && is &&
291 i != (LEX_MAX_BUFF - 2));
293 if (i == (LEX_MAX_BUFF - 2)) {
294 printError("Line too long");
295 c = '\"'; // Pretend we got a "
300 printError("Missing quote");
314 // escape the next char
322 } while (c > ' ' && c != ',' && is
323 && (i != LEX_MAX_BUFF-1) );
324 if (i == LEX_MAX_BUFF-1) {
325 printError("Line too long");
335 if (status) return true;
337 status = is.eof() ? LEX_FEOF : LEX_UNDEF;
344 int LyXLex::Pimpl::search_kw(char const * const tag) const
346 keyword_item search_tag = { tag, 0 };
348 lower_bound(table, table + no_items,
349 search_tag, compare_tags());
350 if (res != table + no_items
351 && !compare_no_case(res->tag, tag))
357 int LyXLex::Pimpl::lex()
359 //NOTE: possible bug.
360 if (next() && status == LEX_TOKEN)
361 return search_kw(buff);
367 bool LyXLex::Pimpl::EatLine()
370 unsigned char c = '\0';
372 while(is && c != '\n' && i != (LEX_MAX_BUFF - 1)) {
375 lyxerr[Debug::LYXLEX] << "LyXLex::EatLine read char: `"
380 if (i == (LEX_MAX_BUFF - 1) && c != '\n') {
381 printError("Line too long");
382 c = '\n'; // Pretend we had an end of line
383 --lineno; // but don't increase line counter (netto effect)
384 ++i; // and preserve last character read.
388 buff[--i] = '\0'; // i can never be 0 here, so no danger
398 bool LyXLex::Pimpl::nextToken()
400 if (!pushTok.empty()) {
401 pushTok.copy(buff, string::npos);
402 buff[pushTok.length()] = '\0';
408 while (is && !status) {
413 if (c >= ' ' && is) {
415 if (c == '\\') { // first char == '\\'
420 } while (c > ' ' && c != '\\' && is
421 && i != (LEX_MAX_BUFF-1));
427 } while (c >= ' ' && c != '\\' && is
428 && i != (LEX_MAX_BUFF-1));
431 if (i == (LEX_MAX_BUFF - 1)) {
432 printError("Line too long");
435 if (c == '\\') is.putback(c); // put it back
444 if (status) return true;
446 status = is.eof() ? LEX_FEOF: LEX_UNDEF;
452 void LyXLex::Pimpl::pushToken(string const & pt)