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