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