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
24 int operator()(keyword_item const & a, char const * const tag) const {
25 return compare_no_case(a.tag, tag) < 0;
27 // used by sorted and sort
29 int operator()(keyword_item const & a, keyword_item const & b) const {
30 return compare_no_case(a.tag, b.tag) < 0;
33 // } // end of anon namespace
36 LyXLex::Pimpl::Pimpl(keyword_item * tab, int num)
37 : is(&fb__), table(tab), no_items(num),
38 status(0), pushed(0), lineno(0)
40 if (table && !sorted(table, table + no_items, compare_tags())) {
41 lyxerr << "The table passed to LyXLex is not sorted!!\n"
42 << "Tell the developers to fix it!" << endl;
43 // We sort it anyway to avoid problems.
44 lyxerr << "\nUnsorted:\n";
47 sort(table, table + no_items,
49 lyxerr << "\nSorted:\n";
55 string const LyXLex::Pimpl::GetString() const
61 void LyXLex::Pimpl::printError(string const & message) const
63 string tmpmsg = subst(message, "$$Token", GetString());
64 lyxerr << "LyX: " << tmpmsg << " [around line " << lineno
65 << " of file " << MakeDisplayPath(name) << ']' << endl;
69 void LyXLex::Pimpl::printTable(ostream & os)
71 os << "\nNumber of tags: " << no_items << '\n';
72 for(int i= 0; i < no_items; ++i)
74 << "]: tag: `" << table[i].tag
75 << "' code:" << table[i].code << '\n';
80 void LyXLex::Pimpl::pushTable(keyword_item * tab, int num)
82 pushed_table * tmppu = new pushed_table;
84 tmppu->table_elem = table;
85 tmppu->table_siz = no_items;
89 // Check if the table is sorted and if not, sort it.
91 && !sorted(table, table + no_items, compare_tags())) {
92 lyxerr << "The table passed to LyXLex is not sorted!!\n"
93 << "Tell the developers to fix it!" << endl;
94 // We sort it anyway to avoid problems.
95 lyxerr << "\nUnsorted:\n";
98 sort(table, table + no_items, compare_tags());
99 lyxerr << "\nSorted:\n";
105 void LyXLex::Pimpl::popTable()
108 lyxerr << "LyXLex error: nothing to pop!" << endl;
112 table = tmp->table_elem;
113 no_items = tmp->table_siz;
120 bool LyXLex::Pimpl::setFile(string const & filename)
123 lyxerr << "Error in LyXLex::setFile: "
124 "file or stream already set." << endl;
125 fb__.open(filename.c_str(), ios::in);
129 return fb__.is_open() && is.good();
133 void LyXLex::Pimpl::setStream(istream & i)
135 if (fb__.is_open() || is.rdbuf()->in_avail())
136 lyxerr << "Error in LyXLex::setStream: "
137 "file or stream already set." << endl;
143 bool LyXLex::Pimpl::next(bool esc /* = false */)
146 unsigned char c = 0; // getc() returns an int
149 while (is && !status) {
153 // Read rest of line (fast :-)
154 is.getline(buff, sizeof(buff));
155 lyxerr[Debug::LYXLEX] << "Comment read: `" << c
156 << buff << "'" << endl;
168 } while (c != '\"' && c != '\n' && is &&
169 i != (LEX_MAX_BUFF - 2));
171 if (i == (LEX_MAX_BUFF - 2)) {
172 printError("Line too long");
173 c = '\"'; // Pretend we got a "
178 printError("Missing quote");
189 continue; /* Skip ','s */
191 // using relational operators with chars other
192 // than == and != is not safe. And if it is done
193 // the type _have_ to be unsigned. It usually a
194 // lot better to use the functions from cctype
201 } while (c > ' ' && c != ',' && is
202 && (i != LEX_MAX_BUFF - 1) );
203 if (i == LEX_MAX_BUFF - 1) {
204 printError("Line too long");
210 if (c == '\r' && is) {
211 // The Windows support has lead to the
212 // possibility of "\r\n" at the end of
213 // a line. This will stop LyX choking
214 // when it expected to find a '\n'
223 if (status) return true;
225 status = is.eof() ? LEX_FEOF: LEX_UNDEF;
229 unsigned char c = 0; // getc() returns an int
233 while (is && !status) {
238 if (c == ',') continue;
245 // escape the next char
252 } while (c > ' ' && c != ',' && is
253 && (i != LEX_MAX_BUFF - 1) );
254 if (i == LEX_MAX_BUFF - 1) {
255 printError("Line too long");
263 // Read rest of line (fast :-)
264 is.getline(buff, sizeof(buff));
265 lyxerr[Debug::LYXLEX] << "Comment read: `" << c
266 << buff << "'" << endl;
274 bool escaped = false;
279 if (c == '\r') continue;
281 // escape the next char
288 if (!escaped && c == '\"') break;
289 } while (c != '\n' && is &&
290 i != (LEX_MAX_BUFF - 2));
292 if (i == (LEX_MAX_BUFF - 2)) {
293 printError("Line too long");
294 c = '\"'; // Pretend we got a "
299 printError("Missing quote");
313 // escape the next char
321 } while (c > ' ' && c != ',' && is
322 && (i != LEX_MAX_BUFF-1) );
323 if (i == LEX_MAX_BUFF-1) {
324 printError("Line too long");
334 if (status) return true;
336 status = is.eof() ? LEX_FEOF : LEX_UNDEF;
343 int LyXLex::Pimpl::search_kw(char const * const tag) const
346 lower_bound(table, table + no_items,
347 tag, compare_tags());
348 if (res != table + no_items
349 && !compare_no_case(res->tag, tag))
355 int LyXLex::Pimpl::lex()
357 //NOTE: possible bug.
358 if (next() && status == LEX_TOKEN)
359 return search_kw(buff);
365 bool LyXLex::Pimpl::EatLine()
368 unsigned char c = '\0';
370 while(is && c != '\n' && i != (LEX_MAX_BUFF - 1)) {
373 lyxerr[Debug::LYXLEX] << "LyXLex::EatLine read char: `"
378 if (i == (LEX_MAX_BUFF - 1) && c != '\n') {
379 printError("Line too long");
380 c = '\n'; // Pretend we had an end of line
381 --lineno; // but don't increase line counter (netto effect)
382 ++i; // and preserve last character read.
386 buff[--i] = '\0'; // i can never be 0 here, so no danger
396 bool LyXLex::Pimpl::nextToken()
399 while (is && !status) {
404 if (c >= ' ' && is) {
406 if (c == '\\') { // first char == '\\'
411 } while (c > ' ' && c != '\\' && is
412 && i != (LEX_MAX_BUFF-1));
418 } while (c >= ' ' && c != '\\' && is
419 && i != (LEX_MAX_BUFF-1));
422 if (i == (LEX_MAX_BUFF - 1)) {
423 printError("Line too long");
426 if (c == '\\') is.putback(c); // put it back
435 if (status) return true;
437 status = is.eof() ? LEX_FEOF: LEX_UNDEF;