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