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