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