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 if (fb__.is_open() || is.tellg() > 0)
111 lyxerr << "Error in LyXLex::setFile: "
112 "file or stream already set." << endl;
113 fb__.open(filename.c_str(), ios::in);
117 return fb__.is_open() && is.good();
121 void LyXLex::Pimpl::setStream(istream & i)
123 if (fb__.is_open() || is.tellg() > 0)
124 lyxerr << "Error in LyXLex::setStream: "
125 "file or stream already set." << endl;
131 bool LyXLex::Pimpl::next(bool esc /* = false */)
133 if (!pushTok.empty()) {
134 pushTok.copy(buff, string::npos);
135 buff[pushTok.length()] = '\0';
140 unsigned char c = 0; // getc() returns an int
143 while (is && !status) {
147 // Read rest of line (fast :-)
148 is.getline(buff, sizeof(buff));
149 lyxerr[Debug::LYXLEX] << "Comment read: `" << c
150 << buff << "'" << endl;
162 } while (c != '\"' && c != '\n' && is &&
163 i != (LEX_MAX_BUFF - 2));
165 if (i == (LEX_MAX_BUFF - 2)) {
166 printError("Line too long");
167 c = '\"'; // Pretend we got a "
172 printError("Missing quote");
183 continue; /* Skip ','s */
185 // using relational operators with chars other
186 // than == and != is not safe. And if it is done
187 // the type _have_ to be unsigned. It usually a
188 // lot better to use the functions from cctype
195 } while (c > ' ' && c != ',' && is
196 && (i != LEX_MAX_BUFF - 1) );
197 if (i == LEX_MAX_BUFF - 1) {
198 printError("Line too long");
204 if (c == '\r' && is) {
205 // The Windows support has lead to the
206 // possibility of "\r\n" at the end of
207 // a line. This will stop LyX choking
208 // when it expected to find a '\n'
217 if (status) return true;
219 status = is.eof() ? LEX_FEOF: LEX_UNDEF;
223 unsigned char c = 0; // getc() returns an int
227 while (is && !status) {
232 if (c == ',') continue;
239 // escape the next char
246 } while (c > ' ' && c != ',' && is
247 && (i != LEX_MAX_BUFF - 1) );
248 if (i == LEX_MAX_BUFF - 1) {
249 printError("Line too long");
257 // Read rest of line (fast :-)
258 is.getline(buff, sizeof(buff));
259 lyxerr[Debug::LYXLEX] << "Comment read: `" << c
260 << buff << "'" << endl;
268 bool escaped = false;
273 if (c == '\r') continue;
275 // escape the next char
282 if (!escaped && c == '\"') break;
283 } while (c != '\n' && is &&
284 i != (LEX_MAX_BUFF - 2));
286 if (i == (LEX_MAX_BUFF - 2)) {
287 printError("Line too long");
288 c = '\"'; // Pretend we got a "
293 printError("Missing quote");
307 // escape the next char
315 } while (c > ' ' && c != ',' && is
316 && (i != LEX_MAX_BUFF-1) );
317 if (i == LEX_MAX_BUFF-1) {
318 printError("Line too long");
328 if (status) return true;
330 status = is.eof() ? LEX_FEOF : LEX_UNDEF;
337 int LyXLex::Pimpl::search_kw(char const * const tag) const
339 keyword_item search_tag = { tag, 0 };
341 lower_bound(table, table + no_items,
342 search_tag, compare_tags());
343 if (res != table + no_items
344 && !compare_no_case(res->tag, tag))
350 int LyXLex::Pimpl::lex()
352 //NOTE: possible bug.
353 if (next() && status == LEX_TOKEN)
354 return search_kw(buff);
360 bool LyXLex::Pimpl::EatLine()
363 unsigned char c = '\0';
365 while(is && c != '\n' && i != (LEX_MAX_BUFF - 1)) {
368 lyxerr[Debug::LYXLEX] << "LyXLex::EatLine read char: `"
373 if (i == (LEX_MAX_BUFF - 1) && c != '\n') {
374 printError("Line too long");
375 c = '\n'; // Pretend we had an end of line
376 --lineno; // but don't increase line counter (netto effect)
377 ++i; // and preserve last character read.
381 buff[--i] = '\0'; // i can never be 0 here, so no danger
391 bool LyXLex::Pimpl::nextToken()
393 if (!pushTok.empty()) {
394 pushTok.copy(buff, string::npos);
395 buff[pushTok.length()] = '\0';
401 while (is && !status) {
406 if (c >= ' ' && is) {
408 if (c == '\\') { // first char == '\\'
413 } while (c > ' ' && c != '\\' && is
414 && i != (LEX_MAX_BUFF-1));
420 } while (c >= ' ' && c != '\\' && is
421 && i != (LEX_MAX_BUFF-1));
424 if (i == (LEX_MAX_BUFF - 1)) {
425 printError("Line too long");
428 if (c == '\\') is.putback(c); // put it back
437 if (status) return true;
439 status = is.eof() ? LEX_FEOF: LEX_UNDEF;
445 void LyXLex::Pimpl::pushToken(string const & pt)