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