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