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