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