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