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