]> git.lyx.org Git - lyx.git/blob - src/lyxlex_pimpl.C
292778761a44a3bd87fc7d77c3a8b60e4b91fc6e
[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_.is_open() || istream::off_type(is.tellg()) > -1)
148                         lyxerr[Debug::LYXLEX] << "Error in LyXLex::setFile: "
149                                 "file or stream already set." << endl;
150                 gz_.open(filename.c_str(), ios::in);
151                 is.rdbuf(&gz_);
152                 name = filename;
153                 lineno = 0;
154                 return gz_.is_open() && is.good();
155 #else
156                 return false;
157 #endif
158         } else {
159                 lyxerr[Debug::LYXLEX] << "lyxlex: UNcompressed" << endl;
160
161                 // The check only outputs a debug message, because it triggers
162                 // a bug in compaq cxx 6.2, where is_open() returns 'true' for
163                 // a fresh new filebuf.  (JMarc)
164                 if (fb_.is_open() || istream::off_type(is.tellg()) > 0)
165                         lyxerr[Debug::LYXLEX] << "Error in LyXLex::setFile: "
166                                 "file or stream already set." << endl;
167                 fb_.open(filename.c_str(), ios::in);
168                 is.rdbuf(&fb_);
169                 name = filename;
170                 lineno = 0;
171                 return fb_.is_open() && is.good();
172         }
173 }
174
175
176 void LyXLex::Pimpl::setStream(istream & i)
177 {
178         if (fb_.is_open() || istream::off_type(is.tellg()) > 0)
179                 lyxerr[Debug::LYXLEX]  << "Error in LyXLex::setStream: "
180                         "file or stream already set." << endl;
181         is.rdbuf(i.rdbuf());
182         lineno = 0;
183 }
184
185
186 void LyXLex::Pimpl::setCommentChar(char c)
187 {
188         commentChar = c;
189 }
190
191
192 bool LyXLex::Pimpl::next(bool esc /* = false */)
193 {
194         if (!pushTok.empty()) {
195                 // There can have been a whole line pushed so
196                 // we extract the first word and leaves the rest
197                 // in pushTok. (Lgb)
198                 if (pushTok.find(' ') != string::npos && pushTok[0] == '\\') {
199                         string tmp;
200                         pushTok = split(pushTok, tmp, ' ');
201                         buff.assign(tmp.begin(), tmp.end());
202                         return true;
203                 } else {
204                         buff.assign(pushTok.begin(), pushTok.end());
205                         pushTok.erase();
206                         return true;
207                 }
208         }
209         if (!esc) {
210                 unsigned char c = 0; // getc() returns an int
211                 char cc = 0;
212                 status = 0;
213                 while (is && !status) {
214                         is.get(cc);
215                         c = cc;
216                         if (c == commentChar) {
217                                 // Read rest of line (fast :-)
218 #if 1
219                                 // That is not fast... (Lgb)
220                                 string dummy;
221                                 getline(is, dummy);
222
223                                 lyxerr[Debug::LYXLEX] << "Comment read: `" << c
224                                                       << dummy << '\'' << endl;
225 #else
226                                 // unfortunately ignore is buggy (Lgb)
227                                 is.ignore(100, '\n');
228 #endif
229                                 ++lineno;
230                                 continue;
231                         }
232
233                         if (c == '\"') {
234                                 buff.clear();
235
236                                 do {
237                                         is.get(cc);
238                                         c = cc;
239                                         if (c != '\r')
240                                                 buff.push_back(c);
241                                 } while (c != '\"' && c != '\n' && is);
242
243                                 if (c != '\"') {
244                                         printError("Missing quote");
245                                         if (c == '\n')
246                                                 ++lineno;
247                                 }
248
249                                 buff.pop_back();
250                                 status = LEX_DATA;
251                                 break;
252                         }
253
254                         if (c == ',')
255                                 continue;              /* Skip ','s */
256
257                                 // using relational operators with chars other
258                                 // than == and != is not safe. And if it is done
259                                 // the type _have_ to be unsigned. It usually a
260                                 // lot better to use the functions from cctype
261                         if (c > ' ' && is)  {
262                                 buff.clear();
263
264                                 do {
265                                         buff.push_back(c);
266                                         is.get(cc);
267                                         c = cc;
268                                 } while (c > ' ' && c != ',' && is);
269
270                                 status = LEX_TOKEN;
271                         }
272
273                         if (c == '\r' && is) {
274                                 // The Windows support has lead to the
275                                 // possibility of "\r\n" at the end of
276                                 // a line.  This will stop LyX choking
277                                 // when it expected to find a '\n'
278                                 is.get(cc);
279                                 c = cc;
280                         }
281
282                         if (c == '\n')
283                                 ++lineno;
284
285                 }
286                 if (status) return true;
287
288                 status = is.eof() ? LEX_FEOF: LEX_UNDEF;
289                 buff.clear();
290                 return false;
291         } else {
292                 unsigned char c = 0; // getc() returns an int
293                 char cc = 0;
294
295                 status = 0;
296                 while (is && !status) {
297                         is.get(cc);
298                         c = cc;
299
300                         // skip ','s
301                         if (c == ',') continue;
302
303                         if (c == '\\') {
304                                 // escape
305                                 buff.clear();
306
307                                 do {
308                                         if (c == '\\') {
309                                                 // escape the next char
310                                                 is.get(cc);
311                                                 c = cc;
312                                         }
313                                         buff.push_back(c);
314                                         is.get(cc);
315                                         c = cc;
316                                 } while (c > ' ' && c != ',' && is);
317
318                                 status = LEX_TOKEN;
319                                 continue;
320                         }
321
322                         if (c == commentChar) {
323                                 // Read rest of line (fast :-)
324 #if 1
325                                 // That is still not fast... (Lgb)
326                                 string dummy;
327                                 getline(is, dummy);
328
329                                 lyxerr[Debug::LYXLEX] << "Comment read: `" << c
330                                                       << dummy << '\'' << endl;
331 #else
332                                 // but ignore is also still buggy (Lgb)
333                                 // This is fast (Lgb)
334                                 is.ignore(100, '\n');
335 #endif
336                                 ++lineno;
337                                 continue;
338                         }
339
340                         // string
341                         if (c == '\"') {
342                                 buff.clear();
343
344                                 bool escaped = false;
345                                 do {
346                                         escaped = false;
347                                         is.get(cc);
348                                         c = cc;
349                                         if (c == '\r') continue;
350                                         if (c == '\\') {
351                                                 // escape the next char
352                                                 is.get(cc);
353                                                 c = cc;
354                                                 if (c == '\"' || c == '\\')
355                                                         escaped = true;
356                                                 else
357                                                         buff.push_back('\\');
358                                         }
359                                         buff.push_back(c);
360
361                                         if (!escaped && c == '\"') break;
362                                 } while (c != '\n' && is);
363
364                                 if (c != '\"') {
365                                         printError("Missing quote");
366                                         if (c == '\n')
367                                                 ++lineno;
368                                 }
369
370                                 buff.pop_back();
371                                 status = LEX_DATA;
372                                 break;
373                         }
374
375                         if (c > ' ' && is) {
376                                 buff.clear();
377
378                                 do {
379                                         if (c == '\\') {
380                                                 // escape the next char
381                                                 is.get(cc);
382                                                 c = cc;
383                                                 //escaped = true;
384                                         }
385                                         buff.push_back(c);
386                                         is.get(cc);
387                                         c = cc;
388                                 } while (c > ' ' && c != ',' && is);
389
390                                 status = LEX_TOKEN;
391                         }
392                         // new line
393                         if (c == '\n')
394                                 ++lineno;
395                 }
396
397                 if (status) return true;
398
399                 status = is.eof() ? LEX_FEOF : LEX_UNDEF;
400                 buff.clear();
401                 return false;
402         }
403 }
404
405
406 int LyXLex::Pimpl::search_kw(char const * const tag) const
407 {
408         keyword_item search_tag = { tag, 0 };
409         keyword_item * res =
410                 lower_bound(table, table + no_items,
411                             search_tag, compare_tags());
412         // use the compare_ascii_no_case instead of compare_no_case,
413         // because in turkish, 'i' is not the lowercase version of 'I',
414         // and thus turkish locale breaks parsing of tags.
415         if (res != table + no_items
416             && !compare_ascii_no_case(res->tag, tag))
417                 return res->code;
418         return LEX_UNDEF;
419 }
420
421
422 int LyXLex::Pimpl::lex()
423 {
424         //NOTE: possible bug.
425         if (next() && status == LEX_TOKEN) {
426                 return search_kw(getString().c_str());
427         } else
428                 return status;
429 }
430
431
432 bool LyXLex::Pimpl::eatLine()
433 {
434         buff.clear();
435
436         unsigned char c = '\0';
437         char cc = 0;
438         while (is && c != '\n') {
439                 is.get(cc);
440                 c = cc;
441                 //lyxerr[Debug::LYXLEX] << "LyXLex::EatLine read char: `"
442                 //                    << c << '\'' << endl;
443                 if (c != '\r')
444                         buff.push_back(c);
445         }
446
447         if (c == '\n') {
448                 ++lineno;
449                 buff.pop_back();
450                 status = LEX_DATA;
451                 return true;
452         } else {
453                 return false;
454         }
455 }
456
457
458 bool LyXLex::Pimpl::nextToken()
459 {
460         if (!pushTok.empty()) {
461                 // There can have been a whole line pushed so
462                 // we extract the first word and leaves the rest
463                 // in pushTok. (Lgb)
464                 if (pushTok.find(' ') != string::npos && pushTok[0] == '\\') {
465                         string tmp;
466                         pushTok = split(pushTok, tmp, ' ');
467                         buff.assign(tmp.begin(), tmp.end());
468                         return true;
469                 } else {
470                         buff.assign(pushTok.begin(), pushTok.end());
471                         pushTok.erase();
472                         return true;
473                 }
474         }
475
476         status = 0;
477         while (is && !status) {
478                 unsigned char c = 0;
479                 char cc = 0;
480                 is.get(cc);
481                 c = cc;
482                 if (c >= ' ' && is) {
483                         buff.clear();
484
485                         if (c == '\\') { // first char == '\\'
486                                 do {
487                                         buff.push_back(c);
488                                         is.get(cc);
489                                         c = cc;
490                                 } while (c > ' ' && c != '\\' && is);
491                         } else {
492                                 do {
493                                         buff.push_back(c);
494                                         is.get(cc);
495                                         c = cc;
496                                 } while (c >= ' ' && c != '\\' && is);
497                         }
498
499                         if (c == '\\') is.putback(c); // put it back
500                         status = LEX_TOKEN;
501                 }
502
503                 if (c == '\n')
504                         ++lineno;
505
506         }
507         if (status)  return true;
508
509         status = is.eof() ? LEX_FEOF: LEX_UNDEF;
510         buff.clear();
511         return false;
512 }
513
514
515 void LyXLex::Pimpl::pushToken(string const & pt)
516 {
517         pushTok = pt;
518 }