]> git.lyx.org Git - lyx.git/blob - src/lyxlex_pimpl.C
remove Last when NEW_INSETS is defined
[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), commentChar('#')
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 void LyXLex::Pimpl::setCommentChar(char c)
134 {
135         commentChar = c;
136 }
137
138
139 bool LyXLex::Pimpl::next(bool esc /* = false */)
140 {
141         if (!pushTok.empty()) {
142                 pushTok.copy(buff, string::npos);
143                 buff[pushTok.length()] = '\0';
144                 pushTok.erase();
145                 return true;
146         }
147         if (!esc) {
148                 unsigned char c = 0; // getc() returns an int
149                 char cc = 0;
150                 status = 0;
151                 while (is && !status) {
152                         is.get(cc);
153                         c = cc;
154                         if (c == commentChar) {
155                                 // Read rest of line (fast :-)
156                                 // That is not fast... (Lgb)
157 #if 1
158                                 is.getline(buff, sizeof(buff));
159                                 lyxerr[Debug::LYXLEX] << "Comment read: `" << c
160                                                       << buff << "'" << endl;
161 #else
162                                 // unfortunately is ignore buggy (Lgb)
163                                 is.ignore(100, '\n');
164 #endif
165                                 ++lineno;
166                                 continue;
167                         }
168                         
169                         if (c == '\"') {
170                                 int i = -1;
171                                 do {
172                                         is.get(cc);
173                                         c = cc;
174                                         if (c != '\r')
175                                                 buff[++i] = c;
176                                 } while (c != '\"' && c != '\n' && is &&
177                                          i != (LEX_MAX_BUFF - 2));
178                                 
179                                 if (i == (LEX_MAX_BUFF - 2)) {
180                                         printError("Line too long");
181                                         c = '\"'; // Pretend we got a "
182                                         ++i;
183                                 }
184                                 
185                                 if (c != '\"') {
186                                         printError("Missing quote");
187                                         if (c == '\n')
188                                                 ++lineno;
189                                 }
190                                 
191                                 buff[i] = '\0';
192                                 status = LEX_DATA;
193                                 break; 
194                         }
195                         
196                         if (c == ',')
197                                 continue;              /* Skip ','s */
198                         
199                                 // using relational operators with chars other
200                                 // than == and != is not safe. And if it is done
201                                 // the type _have_ to be unsigned. It usually a
202                                 // lot better to use the functions from cctype
203                         if (c > ' ' && is)  {
204                                 int i = 0;
205                                 do {
206                                         buff[i++] = c;
207                                         is.get(cc);
208                                         c = cc;
209                                 } while (c > ' ' && c != ',' && is
210                                          && (i != LEX_MAX_BUFF - 1) );
211                                 if (i == LEX_MAX_BUFF - 1) {
212                                         printError("Line too long");
213                                 }
214                                 buff[i] = '\0';
215                                 status = LEX_TOKEN;
216                         }
217                         
218                         if (c == '\r' && is) {
219                                 // The Windows support has lead to the
220                                 // possibility of "\r\n" at the end of
221                                 // a line.  This will stop LyX choking
222                                 // when it expected to find a '\n'
223                                 is.get(cc);
224                                 c = cc;
225                         }
226                         
227                         if (c == '\n')
228                                 ++lineno;
229                         
230                 }
231                 if (status) return true;
232                 
233                 status = is.eof() ? LEX_FEOF: LEX_UNDEF;
234                 buff[0] = '\0';
235                 return false;
236         } else {
237                 unsigned char c = 0; // getc() returns an int
238                 char cc = 0;
239                 
240                 status = 0;
241                 while (is && !status) {
242                         is.get(cc);
243                         c = cc;
244                         
245                         // skip ','s
246                         if (c == ',') continue;
247                         
248                         if (c == '\\') {
249                                 // escape
250                                 int i = 0;
251                                 do {
252                                         if (c == '\\') {
253                                                 // escape the next char
254                                                 is.get(cc);
255                                                 c = cc;
256                                         }
257                                         buff[i++] = c;
258                                         is.get(cc);
259                                         c = cc;
260                                 } while (c > ' ' && c != ',' && is
261                                          && (i != LEX_MAX_BUFF - 1) );
262                                 if (i == LEX_MAX_BUFF - 1) {
263                                         printError("Line too long");
264                                 }
265                                 buff[i] = '\0';
266                                 status = LEX_TOKEN;
267                                 continue;
268                         }
269                         
270                         if (c == commentChar) {
271                                 // Read rest of line (fast :-)
272                                 // That is still not fast... (Lgb)
273 #if 1
274                                 is.getline(buff, sizeof(buff));
275                                 lyxerr[Debug::LYXLEX] << "Comment read: `" << c
276                                                       << buff << "'" << endl;
277 #else
278                                 // but ignore is also still buggy (Lgb)
279                                 // This is fast (Lgb)
280                                 is.ignore(100, '\n');
281 #endif
282                                 ++lineno;
283                                 continue;
284                         }
285                         
286                         // string
287                         if (c == '\"') {
288                                 int i = -1;
289                                 bool escaped = false;
290                                 do {
291                                         escaped = false;
292                                         is.get(cc);
293                                         c = cc;
294                                         if (c == '\r') continue;
295                                         if (c == '\\') {
296                                                 // escape the next char
297                                                 is.get(cc);
298                                                 c = cc;
299                                                 escaped = true;
300                                         }
301                                         buff[++i] = c;
302                                         
303                                         if (!escaped && c == '\"') break;
304                                 } while (c != '\n' && is &&
305                                          i != (LEX_MAX_BUFF - 2));
306                                 
307                                 if (i == (LEX_MAX_BUFF - 2)) {
308                                         printError("Line too long");
309                                         c = '\"'; // Pretend we got a "
310                                         ++i;
311                                 }
312                                 
313                                 if (c != '\"') {
314                                         printError("Missing quote");
315                                         if (c == '\n')
316                                                 ++lineno;
317                                 }
318                                 
319                                 buff[i] = '\0';
320                                 status = LEX_DATA;
321                                 break; 
322                         }
323                         
324                         if (c > ' ' && is) {
325                                 int i = 0;
326                                 do {
327                                         if (c == '\\') {
328                                                 // escape the next char
329                                                 is.get(cc);
330                                                 c = cc;
331                                                 //escaped = true;
332                                         }
333                                         buff[i++] = c;
334                                         is.get(cc);
335                                         c = cc;
336                                 } while (c > ' ' && c != ',' && is
337                                          && (i != LEX_MAX_BUFF-1) );
338                                 if (i == LEX_MAX_BUFF-1) {
339                                         printError("Line too long");
340                                 }
341                                 buff[i] = '\0';
342                                 status = LEX_TOKEN;
343                         }
344                         // new line
345                         if (c == '\n')
346                                 ++lineno;
347                 }
348                 
349                 if (status) return true;
350                 
351                 status = is.eof() ? LEX_FEOF : LEX_UNDEF;
352                 buff[0] = '\0';
353                 return false;
354         }
355 }
356
357
358 int LyXLex::Pimpl::search_kw(char const * const tag) const
359 {
360         keyword_item search_tag = { tag, 0 };
361         keyword_item * res =
362                 lower_bound(table, table + no_items,
363                             search_tag, compare_tags());
364         if (res != table + no_items
365             && !compare_no_case(res->tag, tag))
366                 return res->code;
367         return LEX_UNDEF;
368 }
369
370
371 int LyXLex::Pimpl::lex()
372 {
373         //NOTE: possible bug.
374         if (next() && status == LEX_TOKEN)
375                 return search_kw(buff);
376         else
377                 return status;
378 }
379
380         
381 bool LyXLex::Pimpl::EatLine()
382 {
383         int i = 0;
384         unsigned char c = '\0';
385         char cc = 0;
386         while(is && c != '\n' && i != (LEX_MAX_BUFF - 1)) {
387                 is.get(cc);
388                 c = cc;
389                 lyxerr[Debug::LYXLEX] << "LyXLex::EatLine read char: `"
390                                       << c << "'" << endl;
391                 if (c != '\r')
392                         buff[i++] = c;
393         }
394         if (i == (LEX_MAX_BUFF - 1) && c != '\n') {
395                 printError("Line too long");
396                 c = '\n'; // Pretend we had an end of line
397                 --lineno; // but don't increase line counter (netto effect)
398                 ++i; // and preserve last character read.
399         }
400         if (c == '\n') {
401                 ++lineno;
402                 buff[--i] = '\0'; // i can never be 0 here, so no danger
403                 status = LEX_DATA;
404                 return true;
405         } else {
406                 buff[i] = '\0';
407                 return false;
408         }
409 }
410
411
412 bool LyXLex::Pimpl::nextToken()
413 {
414         if (!pushTok.empty()) {
415                 pushTok.copy(buff, string::npos);
416                 buff[pushTok.length()] = '\0';
417                 pushTok.erase();
418                 return true;
419         }
420
421         status = 0;
422         while (is && !status) {
423                 unsigned char c = 0;
424                 char cc = 0;
425                 is.get(cc);
426                 c = cc;
427                 if (c >= ' ' && is) {
428                         int i = 0;
429                         if (c == '\\') { // first char == '\\'
430                                 do {
431                                         buff[i++] = c;
432                                         is.get(cc);
433                                         c = cc;
434                                 } while (c > ' ' && c != '\\' && is
435                                          && i != (LEX_MAX_BUFF-1));
436                         } else {
437                                 do {
438                                         buff[i++] = c;
439                                         is.get(cc);
440                                         c = cc;
441                                 } while (c >= ' ' && c != '\\' && is
442                                          && i != (LEX_MAX_BUFF-1));
443                         }
444                         
445                         if (i == (LEX_MAX_BUFF - 1)) {
446                                 printError("Line too long");
447                         }
448                         
449                         if (c == '\\') is.putback(c); // put it back
450                         buff[i] = '\0';
451                         status = LEX_TOKEN;
452                 }
453                 
454                 if (c == '\n')
455                         ++lineno;
456                 
457         }
458         if (status)  return true;
459         
460         status = is.eof() ? LEX_FEOF: LEX_UNDEF;
461         buff[0] = '\0';
462         return false;
463 }
464
465
466 void LyXLex::Pimpl::pushToken(string const & pt)
467 {
468         pushTok = pt;
469 }