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