3 #include "lyxlex_pimpl.h"
6 #include "support/lyxalgo.h"
7 #include "support/filetools.h"
8 #include "support/lstrings.h"
17 using std::lower_bound;
23 // used by lower_bound, sort and sorted
25 int operator()(keyword_item const & a, keyword_item const & b) const {
26 // we use the ascii version, because in turkish, 'i'
27 // is not the lowercase version of 'I', and thus
28 // turkish locale breaks parsing of tags.
29 return compare_ascii_no_case(a.tag, b.tag) < 0;
32 // } // end of anon namespace
35 LyXLex::Pimpl::Pimpl(keyword_item * tab, int num)
36 : is(&fb__), table(tab), no_items(num),
37 status(0), lineno(0), commentChar('#')
43 string const LyXLex::Pimpl::getString() const
45 return string(buff.begin(), buff.end());
49 void LyXLex::Pimpl::printError(string const & message) const
51 string const tmpmsg = subst(message, "$$Token", getString());
52 lyxerr << "LyX: " << tmpmsg << " [around line " << lineno
53 << " of file " << MakeDisplayPath(name) << ']' << endl;
57 void LyXLex::Pimpl::printTable(ostream & os)
59 os << "\nNumber of tags: " << no_items << '\n';
60 for (int i= 0; i < no_items; ++i)
62 << "]: tag: `" << table[i].tag
63 << "' code:" << table[i].code << '\n';
68 void LyXLex::Pimpl::verifyTable()
70 // Check if the table is sorted and if not, sort it.
72 && !lyx::sorted(table, table + no_items, compare_tags())) {
73 lyxerr << "The table passed to LyXLex is not sorted!\n"
74 << "Tell the developers to fix it!" << endl;
75 // We sort it anyway to avoid problems.
76 lyxerr << "\nUnsorted:\n";
79 sort(table, table + no_items, compare_tags());
80 lyxerr << "\nSorted:\n";
86 void LyXLex::Pimpl::pushTable(keyword_item * tab, int num)
88 pushed_table tmppu(table, no_items);
98 void LyXLex::Pimpl::popTable()
100 if (pushed.empty()) {
101 lyxerr << "LyXLex error: nothing to pop!" << endl;
105 pushed_table tmp = pushed.top();
107 table = tmp.table_elem;
108 no_items = tmp.table_siz;
112 bool LyXLex::Pimpl::setFile(string const & filename)
114 // The check only outputs a debug message, because it triggers
115 // a bug in compaq cxx 6.2, where is_open() returns 'true' for a
116 // fresh new filebuf. (JMarc)
117 if (fb__.is_open() || is.tellg() > 0)
118 lyxerr[Debug::LYXLEX] << "Error in LyXLex::setFile: "
119 "file or stream already set." << endl;
120 fb__.open(filename.c_str(), ios::in);
124 return fb__.is_open() && is.good();
128 void LyXLex::Pimpl::setStream(istream & i)
130 if (fb__.is_open() || is.tellg() > 0)
131 lyxerr[Debug::LYXLEX] << "Error in LyXLex::setStream: "
132 "file or stream already set." << endl;
138 void LyXLex::Pimpl::setCommentChar(char c)
144 bool LyXLex::Pimpl::next(bool esc /* = false */)
146 if (!pushTok.empty()) {
147 // There can have been a whole line pushed so
148 // we extract the first word and leaves the rest
150 if (pushTok.find(' ') != string::npos && pushTok[0] == '\\') {
152 pushTok = split(pushTok, tmp, ' ');
153 buff.assign(tmp.begin(), tmp.end());
156 buff.assign(pushTok.begin(), pushTok.end());
162 unsigned char c = 0; // getc() returns an int
165 while (is && !status) {
168 if (c == commentChar) {
169 // Read rest of line (fast :-)
171 // That is not fast... (Lgb)
175 lyxerr[Debug::LYXLEX] << "Comment read: `" << c
176 << dummy << '\'' << endl;
178 // unfortunately ignore is buggy (Lgb)
179 is.ignore(100, '\n');
193 } while (c != '\"' && c != '\n' && is);
196 printError("Missing quote");
207 continue; /* Skip ','s */
209 // using relational operators with chars other
210 // than == and != is not safe. And if it is done
211 // the type _have_ to be unsigned. It usually a
212 // lot better to use the functions from cctype
220 } while (c > ' ' && c != ',' && is);
225 if (c == '\r' && is) {
226 // The Windows support has lead to the
227 // possibility of "\r\n" at the end of
228 // a line. This will stop LyX choking
229 // when it expected to find a '\n'
238 if (status) return true;
240 status = is.eof() ? LEX_FEOF: LEX_UNDEF;
244 unsigned char c = 0; // getc() returns an int
248 while (is && !status) {
253 if (c == ',') continue;
261 // escape the next char
268 } while (c > ' ' && c != ',' && is);
274 if (c == commentChar) {
275 // Read rest of line (fast :-)
277 // That is still not fast... (Lgb)
281 lyxerr[Debug::LYXLEX] << "Comment read: `" << c
282 << dummy << '\'' << endl;
284 // but ignore is also still buggy (Lgb)
285 // This is fast (Lgb)
286 is.ignore(100, '\n');
296 bool escaped = false;
301 if (c == '\r') continue;
303 // escape the next char
306 if (c == '\"' || c == '\\')
309 buff.push_back('\\');
313 if (!escaped && c == '\"') break;
314 } while (c != '\n' && is);
317 printError("Missing quote");
332 // escape the next char
340 } while (c > ' ' && c != ',' && is);
349 if (status) return true;
351 status = is.eof() ? LEX_FEOF : LEX_UNDEF;
358 int LyXLex::Pimpl::search_kw(char const * const tag) const
360 keyword_item search_tag = { tag, 0 };
362 lower_bound(table, table + no_items,
363 search_tag, compare_tags());
364 // use the compare_ascii_no_case instead of compare_no_case,
365 // because in turkish, 'i' is not the lowercase version of 'I',
366 // and thus turkish locale breaks parsing of tags.
367 if (res != table + no_items
368 && !compare_ascii_no_case(res->tag, tag))
374 int LyXLex::Pimpl::lex()
376 //NOTE: possible bug.
377 if (next() && status == LEX_TOKEN) {
378 return search_kw(getString().c_str());
384 bool LyXLex::Pimpl::eatLine()
388 unsigned char c = '\0';
390 while (is && c != '\n') {
393 //lyxerr[Debug::LYXLEX] << "LyXLex::EatLine read char: `"
394 // << c << '\'' << endl;
410 bool LyXLex::Pimpl::nextToken()
412 if (!pushTok.empty()) {
413 // There can have been a whole line pushed so
414 // we extract the first word and leaves the rest
416 if (pushTok.find(' ') != string::npos && pushTok[0] == '\\') {
418 pushTok = split(pushTok, tmp, ' ');
419 buff.assign(tmp.begin(), tmp.end());
422 buff.assign(pushTok.begin(), pushTok.end());
429 while (is && !status) {
434 if (c >= ' ' && is) {
437 if (c == '\\') { // first char == '\\'
442 } while (c > ' ' && c != '\\' && is);
448 } while (c >= ' ' && c != '\\' && is);
451 if (c == '\\') is.putback(c); // put it back
459 if (status) return true;
461 status = is.eof() ? LEX_FEOF: LEX_UNDEF;
467 void LyXLex::Pimpl::pushToken(string const & pt)