]> git.lyx.org Git - lyx.git/blob - src/lyxlex_pimpl.C
hopefully fix tex2lyx linking.
[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 buff;
74 }
75
76
77 docstring const LyXLex::Pimpl::getDocString() const
78 {
79         return from_utf8(buff);
80 }
81
82
83 void LyXLex::Pimpl::printError(string const & message) const
84 {
85         string const tmpmsg = subst(message, "$$Token", getString());
86         lyxerr << "LyX: " << tmpmsg << " [around line " << lineno
87                 << " of file " << to_utf8(makeDisplayPath(name)) << ']' << endl;
88 }
89
90
91 void LyXLex::Pimpl::printTable(ostream & os)
92 {
93         os << "\nNumber of tags: " << no_items << endl;
94         for (int i= 0; i < no_items; ++i)
95                 os << "table[" << i
96                    << "]:  tag: `" << table[i].tag
97                    << "'  code:" << table[i].code << '\n';
98         os.flush();
99 }
100
101
102 void LyXLex::Pimpl::verifyTable()
103 {
104         // Check if the table is sorted and if not, sort it.
105         if (table
106             && !lyx::sorted(table, table + no_items, compare_tags())) {
107                 lyxerr << "The table passed to LyXLex is not sorted!\n"
108                        << "Tell the developers to fix it!" << endl;
109                 // We sort it anyway to avoid problems.
110                 lyxerr << "\nUnsorted:" << endl;
111                 printTable(lyxerr);
112
113                 sort(table, table + no_items, compare_tags());
114                 lyxerr << "\nSorted:" << endl;
115                 printTable(lyxerr);
116         }
117 }
118
119
120 void LyXLex::Pimpl::pushTable(keyword_item * tab, int num)
121 {
122         pushed_table tmppu(table, no_items);
123         pushed.push(tmppu);
124
125         table = tab;
126         no_items = num;
127
128         verifyTable();
129 }
130
131
132 void LyXLex::Pimpl::popTable()
133 {
134         if (pushed.empty()) {
135                 lyxerr << "LyXLex error: nothing to pop!" << endl;
136                 return;
137         }
138
139         pushed_table tmp = pushed.top();
140         pushed.pop();
141         table = tmp.table_elem;
142         no_items = tmp.table_siz;
143 }
144
145
146 bool LyXLex::Pimpl::setFile(string const & filename)
147 {
148         // Check the format of the file.
149         string const format = getFormatFromContents(filename);
150
151         if (format == "gzip" || format == "zip" || format == "compress") {
152                 lyxerr[Debug::LYXLEX] << "lyxlex: compressed" << endl;
153
154                 // The check only outputs a debug message, because it triggers
155                 // a bug in compaq cxx 6.2, where is_open() returns 'true' for
156                 // a fresh new filebuf.  (JMarc)
157                 if (!gz_.empty() || istream::off_type(is.tellg()) > -1)
158                         lyxerr[Debug::LYXLEX] << "Error in LyXLex::setFile: "
159                                 "file or stream already set." << endl;
160                 gz_.push(io::gzip_decompressor());
161                 gz_.push(io::file_source(filename));
162                 is.rdbuf(&gz_);
163                 name = filename;
164                 lineno = 0;
165                 return gz_.component<io::file_source>(1)->is_open() && is.good();
166         } else {
167                 lyxerr[Debug::LYXLEX] << "lyxlex: UNcompressed" << endl;
168
169                 // The check only outputs a debug message, because it triggers
170                 // a bug in compaq cxx 6.2, where is_open() returns 'true' for
171                 // a fresh new filebuf.  (JMarc)
172                 if (fb_.is_open() || istream::off_type(is.tellg()) > 0)
173                         lyxerr[Debug::LYXLEX] << "Error in LyXLex::setFile: "
174                                 "file or stream already set." << endl;
175                 fb_.open(filename.c_str(), ios::in);
176                 is.rdbuf(&fb_);
177                 name = filename;
178                 lineno = 0;
179                 return fb_.is_open() && is.good();
180         }
181 }
182
183
184 void LyXLex::Pimpl::setStream(istream & i)
185 {
186         if (fb_.is_open() || istream::off_type(is.tellg()) > 0)
187                 lyxerr[Debug::LYXLEX]  << "Error in LyXLex::setStream: "
188                         "file or stream already set." << endl;
189         is.rdbuf(i.rdbuf());
190         lineno = 0;
191 }
192
193
194 void LyXLex::Pimpl::setCommentChar(char c)
195 {
196         commentChar = c;
197 }
198
199
200 bool LyXLex::Pimpl::next(bool esc /* = false */)
201 {
202         if (!pushTok.empty()) {
203                 // There can have been a whole line pushed so
204                 // we extract the first word and leaves the rest
205                 // in pushTok. (Lgb)
206                 if (pushTok.find(' ') != string::npos && pushTok[0] == '\\') {
207                         buff.clear();
208                         pushTok = split(pushTok, buff, ' ');
209                         return true;
210                 } else {
211                         buff = pushTok;
212                         pushTok.clear();
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.resize(buff.size()-1);
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.resize(buff.size() -1);
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.resize(buff.size() - 1);
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                         buff.clear();
473                         pushTok = split(pushTok, buff, ' ');
474                         return true;
475                 } else {
476                         buff = pushTok;
477                         pushTok.clear();
478                         return true;
479                 }
480         }
481
482         status = 0;
483         while (is && !status) {
484                 unsigned char c = 0;
485                 char cc = 0;
486                 is.get(cc);
487                 c = cc;
488                 if (c >= ' ' && is) {
489                         buff.clear();
490
491                         if (c == '\\') { // first char == '\\'
492                                 do {
493                                         buff.push_back(c);
494                                         is.get(cc);
495                                         c = cc;
496                                 } while (c > ' ' && c != '\\' && is);
497                         } else {
498                                 do {
499                                         buff.push_back(c);
500                                         is.get(cc);
501                                         c = cc;
502                                 } while (c >= ' ' && c != '\\' && is);
503                         }
504
505                         if (c == '\\') is.putback(c); // put it back
506                         status = LEX_TOKEN;
507                 }
508
509                 if (c == '\n')
510                         ++lineno;
511
512         }
513         if (status)  return true;
514
515         status = is.eof() ? LEX_FEOF: LEX_UNDEF;
516         buff.clear();
517         return false;
518 }
519
520
521 void LyXLex::Pimpl::pushToken(string const & pt)
522 {
523         pushTok = pt;
524 }
525
526
527 } // namespace lyx