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),
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;
134 bool LyXLex::Pimpl::next(bool esc /* = false */)
136 if (!pushTok.empty()) {
137 pushTok.copy(buff, string::npos);
138 buff[pushTok.length()] = '\0';
143 unsigned char c = 0; // getc() returns an int
146 while (is && !status) {
150 // Read rest of line (fast :-)
151 is.getline(buff, sizeof(buff));
152 lyxerr[Debug::LYXLEX] << "Comment read: `" << c
153 << buff << "'" << endl;
165 } while (c != '\"' && c != '\n' && is &&
166 i != (LEX_MAX_BUFF - 2));
168 if (i == (LEX_MAX_BUFF - 2)) {
169 printError("Line too long");
170 c = '\"'; // Pretend we got a "
175 printError("Missing quote");
186 continue; /* Skip ','s */
188 // using relational operators with chars other
189 // than == and != is not safe. And if it is done
190 // the type _have_ to be unsigned. It usually a
191 // lot better to use the functions from cctype
198 } while (c > ' ' && c != ',' && is
199 && (i != LEX_MAX_BUFF - 1) );
200 if (i == LEX_MAX_BUFF - 1) {
201 printError("Line too long");
207 if (c == '\r' && is) {
208 // The Windows support has lead to the
209 // possibility of "\r\n" at the end of
210 // a line. This will stop LyX choking
211 // when it expected to find a '\n'
220 if (status) return true;
222 status = is.eof() ? LEX_FEOF: LEX_UNDEF;
226 unsigned char c = 0; // getc() returns an int
230 while (is && !status) {
235 if (c == ',') continue;
242 // escape the next char
249 } while (c > ' ' && c != ',' && is
250 && (i != LEX_MAX_BUFF - 1) );
251 if (i == LEX_MAX_BUFF - 1) {
252 printError("Line too long");
260 // Read rest of line (fast :-)
261 is.getline(buff, sizeof(buff));
262 lyxerr[Debug::LYXLEX] << "Comment read: `" << c
263 << buff << "'" << endl;
271 bool escaped = false;
276 if (c == '\r') continue;
278 // escape the next char
285 if (!escaped && c == '\"') break;
286 } while (c != '\n' && is &&
287 i != (LEX_MAX_BUFF - 2));
289 if (i == (LEX_MAX_BUFF - 2)) {
290 printError("Line too long");
291 c = '\"'; // Pretend we got a "
296 printError("Missing quote");
310 // escape the next char
318 } while (c > ' ' && c != ',' && is
319 && (i != LEX_MAX_BUFF-1) );
320 if (i == LEX_MAX_BUFF-1) {
321 printError("Line too long");
331 if (status) return true;
333 status = is.eof() ? LEX_FEOF : LEX_UNDEF;
340 int LyXLex::Pimpl::search_kw(char const * const tag) const
342 keyword_item search_tag = { tag, 0 };
344 lower_bound(table, table + no_items,
345 search_tag, compare_tags());
346 if (res != table + no_items
347 && !compare_no_case(res->tag, tag))
353 int LyXLex::Pimpl::lex()
355 //NOTE: possible bug.
356 if (next() && status == LEX_TOKEN)
357 return search_kw(buff);
363 bool LyXLex::Pimpl::EatLine()
366 unsigned char c = '\0';
368 while(is && c != '\n' && i != (LEX_MAX_BUFF - 1)) {
371 lyxerr[Debug::LYXLEX] << "LyXLex::EatLine read char: `"
376 if (i == (LEX_MAX_BUFF - 1) && c != '\n') {
377 printError("Line too long");
378 c = '\n'; // Pretend we had an end of line
379 --lineno; // but don't increase line counter (netto effect)
380 ++i; // and preserve last character read.
384 buff[--i] = '\0'; // i can never be 0 here, so no danger
394 bool LyXLex::Pimpl::nextToken()
396 if (!pushTok.empty()) {
397 pushTok.copy(buff, string::npos);
398 buff[pushTok.length()] = '\0';
404 while (is && !status) {
409 if (c >= ' ' && is) {
411 if (c == '\\') { // first char == '\\'
416 } while (c > ' ' && c != '\\' && is
417 && i != (LEX_MAX_BUFF-1));
423 } while (c >= ' ' && c != '\\' && is
424 && i != (LEX_MAX_BUFF-1));
427 if (i == (LEX_MAX_BUFF - 1)) {
428 printError("Line too long");
431 if (c == '\\') is.putback(c); // put it back
440 if (status) return true;
442 status = is.eof() ? LEX_FEOF: LEX_UNDEF;
448 void LyXLex::Pimpl::pushToken(string const & pt)