]> git.lyx.org Git - lyx.git/blob - src/lyxlex_pimpl.C
fix mathed crash
[lyx.git] / src / lyxlex_pimpl.C
1 /**
2  * \file lyxlex_pimpl.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author Jean-Marc Lasgouttes
8  * \author Jürgen Vigna
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "lyxlex_pimpl.h"
16
17 #include "debug.h"
18
19 #include "support/filetools.h"
20 #include "support/lyxalgo.h"
21 #include "support/lstrings.h"
22
23 using lyx::support::compare_ascii_no_case;
24 using lyx::support::getExtFromContents;
25 using lyx::support::MakeDisplayPath;
26 using lyx::support::split;
27 using lyx::support::subst;
28
29 using std::endl;
30 using std::getline;
31 using std::lower_bound;
32 using std::sort;
33 using std::string;
34 using std::ios;
35 using std::istream;
36 using std::ostream;
37
38 namespace {
39
40 struct compare_tags : public std::binary_function<keyword_item, keyword_item, int> {
41         // used by lower_bound, sort and sorted
42         int operator()(keyword_item const & a, keyword_item const & b) const {
43                 // we use the ascii version, because in turkish, 'i'
44                 // is not the lowercase version of 'I', and thus
45                 // turkish locale breaks parsing of tags.
46                 return compare_ascii_no_case(a.tag, b.tag) < 0;
47         }
48 };
49
50 } // end of anon namespace
51
52
53 LyXLex::Pimpl::Pimpl(keyword_item * tab, int num)
54         : is(&fb_), table(tab), no_items(num),
55           status(0), lineno(0), commentChar('#')
56 {
57         verifyTable();
58 }
59
60
61 string const LyXLex::Pimpl::getString() const
62 {
63         return string(buff.begin(), buff.end());
64 }
65
66
67 void LyXLex::Pimpl::printError(string const & message) const
68 {
69         string const tmpmsg = subst(message, "$$Token", getString());
70         lyxerr << "LyX: " << tmpmsg << " [around line " << lineno
71                << " of file " << MakeDisplayPath(name) << ']' << endl;
72 }
73
74
75 void LyXLex::Pimpl::printTable(ostream & os)
76 {
77         os << "\nNumber of tags: " << no_items << endl;
78         for (int i= 0; i < no_items; ++i)
79                 os << "table[" << i
80                    << "]:  tag: `" << table[i].tag
81                    << "'  code:" << table[i].code << '\n';
82         os.flush();
83 }
84
85
86 void LyXLex::Pimpl::verifyTable()
87 {
88         // Check if the table is sorted and if not, sort it.
89         if (table
90             && !lyx::sorted(table, table + no_items, compare_tags())) {
91                 lyxerr << "The table passed to LyXLex is not sorted!\n"
92                        << "Tell the developers to fix it!" << endl;
93                 // We sort it anyway to avoid problems.
94                 lyxerr << "\nUnsorted:" << endl;
95                 printTable(lyxerr);
96
97                 sort(table, table + no_items, compare_tags());
98                 lyxerr << "\nSorted:" << endl;
99                 printTable(lyxerr);
100         }
101 }
102
103
104 void LyXLex::Pimpl::pushTable(keyword_item * tab, int num)
105 {
106         pushed_table tmppu(table, no_items);
107         pushed.push(tmppu);
108
109         table = tab;
110         no_items = num;
111
112         verifyTable();
113 }
114
115
116 void LyXLex::Pimpl::popTable()
117 {
118         if (pushed.empty()) {
119                 lyxerr << "LyXLex error: nothing to pop!" << endl;
120                 return;
121         }
122
123         pushed_table tmp = pushed.top();
124         pushed.pop();
125         table = tmp.table_elem;
126         no_items = tmp.table_siz;
127 }
128
129
130 bool LyXLex::Pimpl::setFile(string const & filename)
131 {
132
133         // Check the format of the file.
134         string const format = getExtFromContents(filename);
135
136         if (format == "gzip" || format == "zip" || format == "compress") {
137                 lyxerr[Debug::LYXLEX] << "lyxlex: compressed" << endl;
138
139                 // The check only outputs a debug message, because it triggers
140                 // a bug in compaq cxx 6.2, where is_open() returns 'true' for
141                 // a fresh new filebuf.  (JMarc)
142                 if (gz_.is_open() || istream::off_type(is.tellg()) > -1)
143                         lyxerr[Debug::LYXLEX] << "Error in LyXLex::setFile: "
144                                 "file or stream already set." << endl;
145                 gz_.open(filename.c_str(), ios::in);
146                 is.rdbuf(&gz_);
147                 name = filename;
148                 lineno = 0;
149                 return gz_.is_open() && is.good();
150         } else {
151                 lyxerr[Debug::LYXLEX] << "lyxlex: UNcompressed" << endl;
152
153                 // The check only outputs a debug message, because it triggers
154                 // a bug in compaq cxx 6.2, where is_open() returns 'true' for
155                 // a fresh new filebuf.  (JMarc)
156                 if (fb_.is_open() || istream::off_type(is.tellg()) > 0)
157                         lyxerr[Debug::LYXLEX] << "Error in LyXLex::setFile: "
158                                 "file or stream already set." << endl;
159                 fb_.open(filename.c_str(), ios::in);
160                 is.rdbuf(&fb_);
161                 name = filename;
162                 lineno = 0;
163                 return fb_.is_open() && is.good();
164         }
165 }
166
167
168 void LyXLex::Pimpl::setStream(istream & i)
169 {
170         if (fb_.is_open() || istream::off_type(is.tellg()) > 0)
171                 lyxerr[Debug::LYXLEX]  << "Error in LyXLex::setStream: "
172                         "file or stream already set." << endl;
173         is.rdbuf(i.rdbuf());
174         lineno = 0;
175 }
176
177
178 void LyXLex::Pimpl::setCommentChar(char c)
179 {
180         commentChar = c;
181 }
182
183
184 bool LyXLex::Pimpl::next(bool esc /* = false */)
185 {
186         if (!pushTok.empty()) {
187                 // There can have been a whole line pushed so
188                 // we extract the first word and leaves the rest
189                 // in pushTok. (Lgb)
190                 if (pushTok.find(' ') != string::npos && pushTok[0] == '\\') {
191                         string tmp;
192                         pushTok = split(pushTok, tmp, ' ');
193                         buff.assign(tmp.begin(), tmp.end());
194                         return true;
195                 } else {
196                         buff.assign(pushTok.begin(), pushTok.end());
197                         pushTok.erase();
198                         return true;
199                 }
200         }
201         if (!esc) {
202                 unsigned char c = 0; // getc() returns an int
203                 char cc = 0;
204                 status = 0;
205                 while (is && !status) {
206                         is.get(cc);
207                         c = cc;
208                         if (c == commentChar) {
209                                 // Read rest of line (fast :-)
210 #if 1
211                                 // That is not fast... (Lgb)
212                                 string dummy;
213                                 getline(is, dummy);
214
215                                 lyxerr[Debug::LYXLEX] << "Comment read: `" << c
216                                                       << dummy << '\'' << endl;
217 #else
218                                 // unfortunately ignore is buggy (Lgb)
219                                 is.ignore(100, '\n');
220 #endif
221                                 ++lineno;
222                                 continue;
223                         }
224
225                         if (c == '\"') {
226                                 buff.clear();
227
228                                 do {
229                                         is.get(cc);
230                                         c = cc;
231                                         if (c != '\r')
232                                                 buff.push_back(c);
233                                 } while (c != '\"' && c != '\n' && is);
234
235                                 if (c != '\"') {
236                                         printError("Missing quote");
237                                         if (c == '\n')
238                                                 ++lineno;
239                                 }
240
241                                 buff.pop_back();
242                                 status = LEX_DATA;
243                                 break;
244                         }
245
246                         if (c == ',')
247                                 continue;              /* Skip ','s */
248
249                                 // using relational operators with chars other
250                                 // than == and != is not safe. And if it is done
251                                 // the type _have_ to be unsigned. It usually a
252                                 // lot better to use the functions from cctype
253                         if (c > ' ' && is)  {
254                                 buff.clear();
255
256                                 do {
257                                         buff.push_back(c);
258                                         is.get(cc);
259                                         c = cc;
260                                 } while (c > ' ' && c != ',' && is);
261
262                                 status = LEX_TOKEN;
263                         }
264
265                         if (c == '\r' && is) {
266                                 // The Windows support has lead to the
267                                 // possibility of "\r\n" at the end of
268                                 // a line.  This will stop LyX choking
269                                 // when it expected to find a '\n'
270                                 is.get(cc);
271                                 c = cc;
272                         }
273
274                         if (c == '\n')
275                                 ++lineno;
276
277                 }
278                 if (status) return true;
279
280                 status = is.eof() ? LEX_FEOF: LEX_UNDEF;
281                 buff.clear();
282                 return false;
283         } else {
284                 unsigned char c = 0; // getc() returns an int
285                 char cc = 0;
286
287                 status = 0;
288                 while (is && !status) {
289                         is.get(cc);
290                         c = cc;
291
292                         // skip ','s
293                         if (c == ',') continue;
294
295                         if (c == '\\') {
296                                 // escape
297                                 buff.clear();
298
299                                 do {
300                                         if (c == '\\') {
301                                                 // escape the next char
302                                                 is.get(cc);
303                                                 c = cc;
304                                         }
305                                         buff.push_back(c);
306                                         is.get(cc);
307                                         c = cc;
308                                 } while (c > ' ' && c != ',' && is);
309
310                                 status = LEX_TOKEN;
311                                 continue;
312                         }
313
314                         if (c == commentChar) {
315                                 // Read rest of line (fast :-)
316 #if 1
317                                 // That is still not fast... (Lgb)
318                                 string dummy;
319                                 getline(is, dummy);
320
321                                 lyxerr[Debug::LYXLEX] << "Comment read: `" << c
322                                                       << dummy << '\'' << endl;
323 #else
324                                 // but ignore is also still buggy (Lgb)
325                                 // This is fast (Lgb)
326                                 is.ignore(100, '\n');
327 #endif
328                                 ++lineno;
329                                 continue;
330                         }
331
332                         // string
333                         if (c == '\"') {
334                                 buff.clear();
335
336                                 bool escaped = false;
337                                 do {
338                                         escaped = false;
339                                         is.get(cc);
340                                         c = cc;
341                                         if (c == '\r') continue;
342                                         if (c == '\\') {
343                                                 // escape the next char
344                                                 is.get(cc);
345                                                 c = cc;
346                                                 if (c == '\"' || c == '\\')
347                                                         escaped = true;
348                                                 else
349                                                         buff.push_back('\\');
350                                         }
351                                         buff.push_back(c);
352
353                                         if (!escaped && c == '\"') break;
354                                 } while (c != '\n' && is);
355
356                                 if (c != '\"') {
357                                         printError("Missing quote");
358                                         if (c == '\n')
359                                                 ++lineno;
360                                 }
361
362                                 buff.pop_back();
363                                 status = LEX_DATA;
364                                 break;
365                         }
366
367                         if (c > ' ' && is) {
368                                 buff.clear();
369
370                                 do {
371                                         if (c == '\\') {
372                                                 // escape the next char
373                                                 is.get(cc);
374                                                 c = cc;
375                                                 //escaped = true;
376                                         }
377                                         buff.push_back(c);
378                                         is.get(cc);
379                                         c = cc;
380                                 } while (c > ' ' && c != ',' && is);
381
382                                 status = LEX_TOKEN;
383                         }
384                         // new line
385                         if (c == '\n')
386                                 ++lineno;
387                 }
388
389                 if (status) return true;
390
391                 status = is.eof() ? LEX_FEOF : LEX_UNDEF;
392                 buff.clear();
393                 return false;
394         }
395 }
396
397
398 int LyXLex::Pimpl::search_kw(char const * const tag) const
399 {
400         keyword_item search_tag = { tag, 0 };
401         keyword_item * res =
402                 lower_bound(table, table + no_items,
403                             search_tag, compare_tags());
404         // use the compare_ascii_no_case instead of compare_no_case,
405         // because in turkish, 'i' is not the lowercase version of 'I',
406         // and thus turkish locale breaks parsing of tags.
407         if (res != table + no_items
408             && !compare_ascii_no_case(res->tag, tag))
409                 return res->code;
410         return LEX_UNDEF;
411 }
412
413
414 int LyXLex::Pimpl::lex()
415 {
416         //NOTE: possible bug.
417         if (next() && status == LEX_TOKEN) {
418                 return search_kw(getString().c_str());
419         } else
420                 return status;
421 }
422
423
424 bool LyXLex::Pimpl::eatLine()
425 {
426         buff.clear();
427
428         unsigned char c = '\0';
429         char cc = 0;
430         while (is && c != '\n') {
431                 is.get(cc);
432                 c = cc;
433                 //lyxerr[Debug::LYXLEX] << "LyXLex::EatLine read char: `"
434                 //                    << c << '\'' << endl;
435                 if (c != '\r')
436                         buff.push_back(c);
437         }
438
439         if (c == '\n') {
440                 ++lineno;
441                 buff.pop_back();
442                 status = LEX_DATA;
443                 return true;
444         } else {
445                 return false;
446         }
447 }
448
449
450 bool LyXLex::Pimpl::nextToken()
451 {
452         if (!pushTok.empty()) {
453                 // There can have been a whole line pushed so
454                 // we extract the first word and leaves the rest
455                 // in pushTok. (Lgb)
456                 if (pushTok.find(' ') != string::npos && pushTok[0] == '\\') {
457                         string tmp;
458                         pushTok = split(pushTok, tmp, ' ');
459                         buff.assign(tmp.begin(), tmp.end());
460                         return true;
461                 } else {
462                         buff.assign(pushTok.begin(), pushTok.end());
463                         pushTok.erase();
464                         return true;
465                 }
466         }
467
468         status = 0;
469         while (is && !status) {
470                 unsigned char c = 0;
471                 char cc = 0;
472                 is.get(cc);
473                 c = cc;
474                 if (c >= ' ' && is) {
475                         buff.clear();
476
477                         if (c == '\\') { // first char == '\\'
478                                 do {
479                                         buff.push_back(c);
480                                         is.get(cc);
481                                         c = cc;
482                                 } while (c > ' ' && c != '\\' && is);
483                         } else {
484                                 do {
485                                         buff.push_back(c);
486                                         is.get(cc);
487                                         c = cc;
488                                 } while (c >= ' ' && c != '\\' && is);
489                         }
490
491                         if (c == '\\') is.putback(c); // put it back
492                         status = LEX_TOKEN;
493                 }
494
495                 if (c == '\n')
496                         ++lineno;
497
498         }
499         if (status)  return true;
500
501         status = is.eof() ? LEX_FEOF: LEX_UNDEF;
502         buff.clear();
503         return false;
504 }
505
506
507 void LyXLex::Pimpl::pushToken(string const & pt)
508 {
509         pushTok = pt;
510 }