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