\end_document replaces \the_end.
[lyx.git] / src / lyxlex_pimpl.C
blob8ad57be9d0b9bf41e5cda4544278e75c4f450d4b
1 #include <config.h>
3 #include "lyxlex_pimpl.h"
4 #include "debug.h"
6 #include "support/lyxalgo.h"
7 #include "support/filetools.h"
8 #include "support/lstrings.h"
10 #include <algorithm>
12 using namespace lyx::support;
14 using std::sort;
15 using std::ostream;
16 using std::ios;
17 using std::istream;
18 using std::endl;
19 using std::lower_bound;
20 using std::vector;
21 using std::getline;
23 // namespace {
24 struct compare_tags {
25         // used by lower_bound, sort and sorted
26         inline
27         int operator()(keyword_item const & a, keyword_item const & b) const {
28                 // we use the ascii version, because in turkish, 'i'
29                 // is not the lowercase version of 'I', and thus
30                 // turkish locale breaks parsing of tags.
31                 return compare_ascii_no_case(a.tag, b.tag) < 0;
32         }
34 // } // end of anon namespace
37 LyXLex::Pimpl::Pimpl(keyword_item * tab, int num)
38         : is(&fb__), table(tab), no_items(num),
39           status(0), lineno(0), commentChar('#')
41         verifyTable();
45 string const LyXLex::Pimpl::getString() const
47         return string(buff.begin(), buff.end());
51 void LyXLex::Pimpl::printError(string const & message) const
53         string const tmpmsg = subst(message, "$$Token", getString());
54         lyxerr << "LyX: " << tmpmsg << " [around line " << lineno
55                << " of file " << MakeDisplayPath(name) << ']' << endl;
59 void LyXLex::Pimpl::printTable(ostream & os)
61         os << "\nNumber of tags: " << no_items << '\n';
62         for (int i= 0; i < no_items; ++i)
63                 os << "table[" << i
64                    << "]:  tag: `" << table[i].tag
65                    << "'  code:" << table[i].code << '\n';
66         os.flush();
70 void LyXLex::Pimpl::verifyTable()
72         // Check if the table is sorted and if not, sort it.
73         if (table
74             && !lyx::sorted(table, table + no_items, compare_tags())) {
75                 lyxerr << "The table passed to LyXLex is not sorted!\n"
76                        << "Tell the developers to fix it!" << endl;
77                 // We sort it anyway to avoid problems.
78                 lyxerr << "\nUnsorted:\n";
79                 printTable(lyxerr);
81                 sort(table, table + no_items, compare_tags());
82                 lyxerr << "\nSorted:\n";
83                 printTable(lyxerr);
84         }
88 void LyXLex::Pimpl::pushTable(keyword_item * tab, int num)
90         pushed_table tmppu(table, no_items);
91         pushed.push(tmppu);
93         table = tab;
94         no_items = num;
96         verifyTable();
100 void LyXLex::Pimpl::popTable()
102         if (pushed.empty()) {
103                 lyxerr << "LyXLex error: nothing to pop!" << endl;
104                 return;
105         }
107         pushed_table tmp = pushed.top();
108         pushed.pop();
109         table = tmp.table_elem;
110         no_items = tmp.table_siz;
114 bool LyXLex::Pimpl::setFile(string const & filename)
117         // Check the format of the file.
118         string const format = getExtFromContents(filename);
120         if (format == "gzip" || format == "zip" || format == "compress") {
121                 lyxerr << "lyxlex: compressed" << endl;
123                 // The check only outputs a debug message, because it triggers
124                 // a bug in compaq cxx 6.2, where is_open() returns 'true' for
125                 // a fresh new filebuf.  (JMarc)
126                 if (gz__.is_open() || is.tellg() > 0)
127                         lyxerr[Debug::LYXLEX] << "Error in LyXLex::setFile: "
128                                 "file or stream already set." << endl;
129                 gz__.open(filename.c_str(), ios::in);
130                 is.rdbuf(&gz__);
131                 name = filename;
132                 lineno = 0;
133                 return gz__.is_open() && is.good();
134         } else {
135                 lyxerr << "lyxlex: UNcompressed" << endl;
137                 // The check only outputs a debug message, because it triggers
138                 // a bug in compaq cxx 6.2, where is_open() returns 'true' for
139                 // a fresh new filebuf.  (JMarc)
140                 if (fb__.is_open() || is.tellg() > 0)
141                         lyxerr[Debug::LYXLEX] << "Error in LyXLex::setFile: "
142                                 "file or stream already set." << endl;
143                 fb__.open(filename.c_str(), ios::in);
144                 is.rdbuf(&fb__);
145                 name = filename;
146                 lineno = 0;
147                 return fb__.is_open() && is.good();
148         }
152 void LyXLex::Pimpl::setStream(istream & i)
154         if (fb__.is_open() || is.tellg() > 0)
155                 lyxerr[Debug::LYXLEX]  << "Error in LyXLex::setStream: "
156                         "file or stream already set." << endl;
157         is.rdbuf(i.rdbuf());
158         lineno = 0;
162 void LyXLex::Pimpl::setCommentChar(char c)
164         commentChar = c;
168 bool LyXLex::Pimpl::next(bool esc /* = false */)
170         if (!pushTok.empty()) {
171                 // There can have been a whole line pushed so
172                 // we extract the first word and leaves the rest
173                 // in pushTok. (Lgb)
174                 if (pushTok.find(' ') != string::npos && pushTok[0] == '\\') {
175                         string tmp;
176                         pushTok = split(pushTok, tmp, ' ');
177                         buff.assign(tmp.begin(), tmp.end());
178                         return true;
179                 } else {
180                         buff.assign(pushTok.begin(), pushTok.end());
181                         pushTok.erase();
182                         return true;
183                 }
184         }
185         if (!esc) {
186                 unsigned char c = 0; // getc() returns an int
187                 char cc = 0;
188                 status = 0;
189                 while (is && !status) {
190                         is.get(cc);
191                         c = cc;
192                         if (c == commentChar) {
193                                 // Read rest of line (fast :-)
194 #if 1
195                                 // That is not fast... (Lgb)
196                                 string dummy;
197                                 getline(is, dummy);
199                                 lyxerr[Debug::LYXLEX] << "Comment read: `" << c
200                                                       << dummy << '\'' << endl;
201 #else
202                                 // unfortunately ignore is buggy (Lgb)
203                                 is.ignore(100, '\n');
204 #endif
205                                 ++lineno;
206                                 continue;
207                         }
209                         if (c == '\"') {
210                                 buff.clear();
212                                 do {
213                                         is.get(cc);
214                                         c = cc;
215                                         if (c != '\r')
216                                                 buff.push_back(c);
217                                 } while (c != '\"' && c != '\n' && is);
219                                 if (c != '\"') {
220                                         printError("Missing quote");
221                                         if (c == '\n')
222                                                 ++lineno;
223                                 }
225                                 buff.pop_back();
226                                 status = LEX_DATA;
227                                 break;
228                         }
230                         if (c == ',')
231                                 continue;              /* Skip ','s */
233                                 // using relational operators with chars other
234                                 // than == and != is not safe. And if it is done
235                                 // the type _have_ to be unsigned. It usually a
236                                 // lot better to use the functions from cctype
237                         if (c > ' ' && is)  {
238                                 buff.clear();
240                                 do {
241                                         buff.push_back(c);
242                                         is.get(cc);
243                                         c = cc;
244                                 } while (c > ' ' && c != ',' && is);
246                                 status = LEX_TOKEN;
247                         }
249                         if (c == '\r' && is) {
250                                 // The Windows support has lead to the
251                                 // possibility of "\r\n" at the end of
252                                 // a line.  This will stop LyX choking
253                                 // when it expected to find a '\n'
254                                 is.get(cc);
255                                 c = cc;
256                         }
258                         if (c == '\n')
259                                 ++lineno;
261                 }
262                 if (status) return true;
264                 status = is.eof() ? LEX_FEOF: LEX_UNDEF;
265                 buff.clear();
266                 return false;
267         } else {
268                 unsigned char c = 0; // getc() returns an int
269                 char cc = 0;
271                 status = 0;
272                 while (is && !status) {
273                         is.get(cc);
274                         c = cc;
276                         // skip ','s
277                         if (c == ',') continue;
279                         if (c == '\\') {
280                                 // escape
281                                 buff.clear();
283                                 do {
284                                         if (c == '\\') {
285                                                 // escape the next char
286                                                 is.get(cc);
287                                                 c = cc;
288                                         }
289                                         buff.push_back(c);
290                                         is.get(cc);
291                                         c = cc;
292                                 } while (c > ' ' && c != ',' && is);
294                                 status = LEX_TOKEN;
295                                 continue;
296                         }
298                         if (c == commentChar) {
299                                 // Read rest of line (fast :-)
300 #if 1
301                                 // That is still not fast... (Lgb)
302                                 string dummy;
303                                 getline(is, dummy);
305                                 lyxerr[Debug::LYXLEX] << "Comment read: `" << c
306                                                       << dummy << '\'' << endl;
307 #else
308                                 // but ignore is also still buggy (Lgb)
309                                 // This is fast (Lgb)
310                                 is.ignore(100, '\n');
311 #endif
312                                 ++lineno;
313                                 continue;
314                         }
316                         // string
317                         if (c == '\"') {
318                                 buff.clear();
320                                 bool escaped = false;
321                                 do {
322                                         escaped = false;
323                                         is.get(cc);
324                                         c = cc;
325                                         if (c == '\r') continue;
326                                         if (c == '\\') {
327                                                 // escape the next char
328                                                 is.get(cc);
329                                                 c = cc;
330                                                 if (c == '\"' || c == '\\')
331                                                         escaped = true;
332                                                 else
333                                                         buff.push_back('\\');
334                                         }
335                                         buff.push_back(c);
337                                         if (!escaped && c == '\"') break;
338                                 } while (c != '\n' && is);
340                                 if (c != '\"') {
341                                         printError("Missing quote");
342                                         if (c == '\n')
343                                                 ++lineno;
344                                 }
346                                 buff.pop_back();
347                                 status = LEX_DATA;
348                                 break;
349                         }
351                         if (c > ' ' && is) {
352                                 buff.clear();
354                                 do {
355                                         if (c == '\\') {
356                                                 // escape the next char
357                                                 is.get(cc);
358                                                 c = cc;
359                                                 //escaped = true;
360                                         }
361                                         buff.push_back(c);
362                                         is.get(cc);
363                                         c = cc;
364                                 } while (c > ' ' && c != ',' && is);
366                                 status = LEX_TOKEN;
367                         }
368                         // new line
369                         if (c == '\n')
370                                 ++lineno;
371                 }
373                 if (status) return true;
375                 status = is.eof() ? LEX_FEOF : LEX_UNDEF;
376                 buff.clear();
377                 return false;
378         }
382 int LyXLex::Pimpl::search_kw(char const * const tag) const
384         keyword_item search_tag = { tag, 0 };
385         keyword_item * res =
386                 lower_bound(table, table + no_items,
387                             search_tag, compare_tags());
388         // use the compare_ascii_no_case instead of compare_no_case,
389         // because in turkish, 'i' is not the lowercase version of 'I',
390         // and thus turkish locale breaks parsing of tags.
391         if (res != table + no_items
392             && !compare_ascii_no_case(res->tag, tag))
393                 return res->code;
394         return LEX_UNDEF;
398 int LyXLex::Pimpl::lex()
400         //NOTE: possible bug.
401         if (next() && status == LEX_TOKEN) {
402                 return search_kw(getString().c_str());
403         } else
404                 return status;
408 bool LyXLex::Pimpl::eatLine()
410         buff.clear();
412         unsigned char c = '\0';
413         char cc = 0;
414         while (is && c != '\n') {
415                 is.get(cc);
416                 c = cc;
417                 //lyxerr[Debug::LYXLEX] << "LyXLex::EatLine read char: `"
418                 //                    << c << '\'' << endl;
419                 if (c != '\r')
420                         buff.push_back(c);
421         }
423         if (c == '\n') {
424                 ++lineno;
425                 buff.pop_back();
426                 status = LEX_DATA;
427                 return true;
428         } else {
429                 return false;
430         }
434 bool LyXLex::Pimpl::nextToken()
436         if (!pushTok.empty()) {
437                 // There can have been a whole line pushed so
438                 // we extract the first word and leaves the rest
439                 // in pushTok. (Lgb)
440                 if (pushTok.find(' ') != string::npos && pushTok[0] == '\\') {
441                         string tmp;
442                         pushTok = split(pushTok, tmp, ' ');
443                         buff.assign(tmp.begin(), tmp.end());
444                         return true;
445                 } else {
446                         buff.assign(pushTok.begin(), pushTok.end());
447                         pushTok.erase();
448                         return true;
449                 }
450         }
452         status = 0;
453         while (is && !status) {
454                 unsigned char c = 0;
455                 char cc = 0;
456                 is.get(cc);
457                 c = cc;
458                 if (c >= ' ' && is) {
459                         buff.clear();
461                         if (c == '\\') { // first char == '\\'
462                                 do {
463                                         buff.push_back(c);
464                                         is.get(cc);
465                                         c = cc;
466                                 } while (c > ' ' && c != '\\' && is);
467                         } else {
468                                 do {
469                                         buff.push_back(c);
470                                         is.get(cc);
471                                         c = cc;
472                                 } while (c >= ' ' && c != '\\' && is);
473                         }
475                         if (c == '\\') is.putback(c); // put it back
476                         status = LEX_TOKEN;
477                 }
479                 if (c == '\n')
480                         ++lineno;
482         }
483         if (status)  return true;
485         status = is.eof() ? LEX_FEOF: LEX_UNDEF;
486         buff.clear();
487         return false;
491 void LyXLex::Pimpl::pushToken(string const & pt)
493         pushTok = pt;