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