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 */)
145 if (!pushTok.empty()) {
146 pushTok.copy(buff, string::npos);
147 buff[pushTok.length()] = '\0';
152 unsigned char c = 0; // getc() returns an int
155 while (is && !status) {
159 // Read rest of line (fast :-)
160 is.getline(buff, sizeof(buff));
161 lyxerr[Debug::LYXLEX] << "Comment read: `" << c
162 << buff << "'" << endl;
174 } while (c != '\"' && c != '\n' && is &&
175 i != (LEX_MAX_BUFF - 2));
177 if (i == (LEX_MAX_BUFF - 2)) {
178 printError("Line too long");
179 c = '\"'; // Pretend we got a "
184 printError("Missing quote");
195 continue; /* Skip ','s */
197 // using relational operators with chars other
198 // than == and != is not safe. And if it is done
199 // the type _have_ to be unsigned. It usually a
200 // lot better to use the functions from cctype
207 } while (c > ' ' && c != ',' && is
208 && (i != LEX_MAX_BUFF - 1) );
209 if (i == LEX_MAX_BUFF - 1) {
210 printError("Line too long");
216 if (c == '\r' && is) {
217 // The Windows support has lead to the
218 // possibility of "\r\n" at the end of
219 // a line. This will stop LyX choking
220 // when it expected to find a '\n'
229 if (status) return true;
231 status = is.eof() ? LEX_FEOF: LEX_UNDEF;
235 unsigned char c = 0; // getc() returns an int
239 while (is && !status) {
244 if (c == ',') continue;
251 // escape the next char
258 } while (c > ' ' && c != ',' && is
259 && (i != LEX_MAX_BUFF - 1) );
260 if (i == LEX_MAX_BUFF - 1) {
261 printError("Line too long");
269 // Read rest of line (fast :-)
270 is.getline(buff, sizeof(buff));
271 lyxerr[Debug::LYXLEX] << "Comment read: `" << c
272 << buff << "'" << endl;
280 bool escaped = false;
285 if (c == '\r') continue;
287 // escape the next char
294 if (!escaped && c == '\"') break;
295 } while (c != '\n' && is &&
296 i != (LEX_MAX_BUFF - 2));
298 if (i == (LEX_MAX_BUFF - 2)) {
299 printError("Line too long");
300 c = '\"'; // Pretend we got a "
305 printError("Missing quote");
319 // escape the next char
327 } while (c > ' ' && c != ',' && is
328 && (i != LEX_MAX_BUFF-1) );
329 if (i == LEX_MAX_BUFF-1) {
330 printError("Line too long");
340 if (status) return true;
342 status = is.eof() ? LEX_FEOF : LEX_UNDEF;
349 int LyXLex::Pimpl::search_kw(char const * const tag) const
352 lower_bound(table, table + no_items,
353 tag, compare_tags());
354 if (res != table + no_items
355 && !compare_no_case(res->tag, tag))
361 int LyXLex::Pimpl::lex()
363 //NOTE: possible bug.
364 if (next() && status == LEX_TOKEN)
365 return search_kw(buff);
371 bool LyXLex::Pimpl::EatLine()
374 unsigned char c = '\0';
376 while(is && c != '\n' && i != (LEX_MAX_BUFF - 1)) {
379 lyxerr[Debug::LYXLEX] << "LyXLex::EatLine read char: `"
384 if (i == (LEX_MAX_BUFF - 1) && c != '\n') {
385 printError("Line too long");
386 c = '\n'; // Pretend we had an end of line
387 --lineno; // but don't increase line counter (netto effect)
388 ++i; // and preserve last character read.
392 buff[--i] = '\0'; // i can never be 0 here, so no danger
402 bool LyXLex::Pimpl::nextToken()
404 if (!pushTok.empty()) {
405 pushTok.copy(buff, string::npos);
406 buff[pushTok.length()] = '\0';
412 while (is && !status) {
417 if (c >= ' ' && is) {
419 if (c == '\\') { // first char == '\\'
424 } while (c > ' ' && c != '\\' && is
425 && i != (LEX_MAX_BUFF-1));
431 } while (c >= ' ' && c != '\\' && is
432 && i != (LEX_MAX_BUFF-1));
435 if (i == (LEX_MAX_BUFF - 1)) {
436 printError("Line too long");
439 if (c == '\\') is.putback(c); // put it back
448 if (status) return true;
450 status = is.eof() ? LEX_FEOF: LEX_UNDEF;
456 void LyXLex::Pimpl::pushToken(string const & pt)