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