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 // That is not fast... (Lgb)
153 is.getline(buff, sizeof(buff));
154 lyxerr[Debug::LYXLEX] << "Comment read: `" << c
155 << buff << "'" << endl;
157 // unfortunately is ignore buggy (Lgb)
158 is.ignore(100, '\n');
171 } while (c != '\"' && c != '\n' && is &&
172 i != (LEX_MAX_BUFF - 2));
174 if (i == (LEX_MAX_BUFF - 2)) {
175 printError("Line too long");
176 c = '\"'; // Pretend we got a "
181 printError("Missing quote");
192 continue; /* Skip ','s */
194 // using relational operators with chars other
195 // than == and != is not safe. And if it is done
196 // the type _have_ to be unsigned. It usually a
197 // lot better to use the functions from cctype
204 } while (c > ' ' && c != ',' && is
205 && (i != LEX_MAX_BUFF - 1) );
206 if (i == LEX_MAX_BUFF - 1) {
207 printError("Line too long");
213 if (c == '\r' && is) {
214 // The Windows support has lead to the
215 // possibility of "\r\n" at the end of
216 // a line. This will stop LyX choking
217 // when it expected to find a '\n'
226 if (status) return true;
228 status = is.eof() ? LEX_FEOF: LEX_UNDEF;
232 unsigned char c = 0; // getc() returns an int
236 while (is && !status) {
241 if (c == ',') continue;
248 // escape the next char
255 } while (c > ' ' && c != ',' && is
256 && (i != LEX_MAX_BUFF - 1) );
257 if (i == LEX_MAX_BUFF - 1) {
258 printError("Line too long");
266 // Read rest of line (fast :-)
267 // That is still not fast... (Lgb)
269 is.getline(buff, sizeof(buff));
270 lyxerr[Debug::LYXLEX] << "Comment read: `" << c
271 << buff << "'" << endl;
273 // but ignore is also still buggy (Lgb)
274 // This is fast (Lgb)
275 is.ignore(100, '\n');
284 bool escaped = false;
289 if (c == '\r') continue;
291 // escape the next char
298 if (!escaped && c == '\"') break;
299 } while (c != '\n' && is &&
300 i != (LEX_MAX_BUFF - 2));
302 if (i == (LEX_MAX_BUFF - 2)) {
303 printError("Line too long");
304 c = '\"'; // Pretend we got a "
309 printError("Missing quote");
323 // escape the next char
331 } while (c > ' ' && c != ',' && is
332 && (i != LEX_MAX_BUFF-1) );
333 if (i == LEX_MAX_BUFF-1) {
334 printError("Line too long");
344 if (status) return true;
346 status = is.eof() ? LEX_FEOF : LEX_UNDEF;
353 int LyXLex::Pimpl::search_kw(char const * const tag) const
355 keyword_item search_tag = { tag, 0 };
357 lower_bound(table, table + no_items,
358 search_tag, compare_tags());
359 if (res != table + no_items
360 && !compare_no_case(res->tag, tag))
366 int LyXLex::Pimpl::lex()
368 //NOTE: possible bug.
369 if (next() && status == LEX_TOKEN)
370 return search_kw(buff);
376 bool LyXLex::Pimpl::EatLine()
379 unsigned char c = '\0';
381 while(is && c != '\n' && i != (LEX_MAX_BUFF - 1)) {
384 lyxerr[Debug::LYXLEX] << "LyXLex::EatLine read char: `"
389 if (i == (LEX_MAX_BUFF - 1) && c != '\n') {
390 printError("Line too long");
391 c = '\n'; // Pretend we had an end of line
392 --lineno; // but don't increase line counter (netto effect)
393 ++i; // and preserve last character read.
397 buff[--i] = '\0'; // i can never be 0 here, so no danger
407 bool LyXLex::Pimpl::nextToken()
409 if (!pushTok.empty()) {
410 pushTok.copy(buff, string::npos);
411 buff[pushTok.length()] = '\0';
417 while (is && !status) {
422 if (c >= ' ' && is) {
424 if (c == '\\') { // first char == '\\'
429 } while (c > ' ' && c != '\\' && is
430 && i != (LEX_MAX_BUFF-1));
436 } while (c >= ' ' && c != '\\' && is
437 && i != (LEX_MAX_BUFF-1));
440 if (i == (LEX_MAX_BUFF - 1)) {
441 printError("Line too long");
444 if (c == '\\') is.putback(c); // put it back
453 if (status) return true;
455 status = is.eof() ? LEX_FEOF: LEX_UNDEF;
461 void LyXLex::Pimpl::pushToken(string const & pt)